WebSocket每次返回403错误

huangapple go评论113阅读模式
英文:

WebSocket returning 403 every time

问题

我不确定为什么会出现这个错误,但是当我尝试在浏览器中连接时,它失败并返回403错误。但这并没有道理,因为在我的代码中没有明确告诉它返回403,并且没有使用中间件。我附上了我得到的错误的图片。我还在使用Gorilla websocket和gin,以下是托管它的代码:

func WebsocketServer(w http.ResponseWriter, r *http.Request, context *gin.Context) {
    fmt.Println("connection received")
    var upgrader2 = websocket.Upgrader{
        CheckOrigin: func(r *http.Request) bool {
            return true
        },
        EnableCompression: false,
    }
    db, err := aws.GetDB()
    if err != nil {
        helpers.Log.Println(err)
        return
    }
    defer db.Close()
    c, err := upgrader2.Upgrade(w, r, nil)
    if err != nil {
        helpers.Log.Println(err)
        return
    }
}
func PublicRoutes(c *gin.RouterGroup) {
    c.GET("/ws", func(c *gin.Context) {
        relay.WebsocketServer(c.Writer, c.Request, c)
    })
}

我正在使用AWS来托管这个,这是一个托管问题吗?

[GIN] 2023/05/07 - 22:18:49 | 403 | 18.344µs | 71.121.188.229 | GET "/ws"

英文:

I'm not sure why this error keeps occurring but when I try connecting in my web browser it fails and returns 403, but it doesn't make sense since no where in my code I specifically told it to return 403, and no middleware is involved, I attached a picture of the error im getting. Im also using Gorilla websocket with gin, here is my code that hosts it

func WebsocketServer(w http.ResponseWriter, r *http.Request, context *gin.Context) {
    fmt.Println("connection received")
    var upgrader2 = websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
		EnableCompression: false,
	}
	db, err := aws.GetDB()
	if err != nil {
		helpers.Log.Println(err)
		return
	}
	defer db.Close()
	c, err := upgrader2.Upgrade(w, r, nil)
	if err != nil {
		helpers.Log.Println(err)
		return
	}

}
func PublicRoutes(c *gin.RouterGroup) {
    c.GET("/ws", func(c *gin.Context) {
        relay.WebsocketServer(c.Writer, c.Request, c)
    })
}

Im using aws to host this, is it a hosting issue?

[GIN] 2023/05/07 - 22:18:49 | 403 | 18.344µs | 71.121.188.229 | GET "/ws"

答案1

得分: 1

这个问题不再是一个问题了,似乎是我的CORS出了问题?我通过将配置移到底部来解决了这个问题。我应该在初始问题中放置这段代码。

之前的代码:

router := gin.Default()
config2 := cors.DefaultConfig()
config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
config2.AllowCredentials = true
router.Use(cors.New(config2))

HandleRoutes(router)
public := router.Group("/")
routes.PublicRoutes(public)
router.NoRoute(func(c *gin.Context) {
    http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
})
private := router.Group("/")
private.Use(helpers.AuthRequired)
routes.PrivateRoutes(private)

err := router.Run(config.Port)
if err != nil {
    log.Println(err)
}

修改后的代码:

router := gin.Default()

HandleRoutes(router)
public := router.Group("/")
routes.PublicRoutes(public)
router.NoRoute(func(c *gin.Context) {
    http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
})
private := router.Group("/")
private.Use(helpers.AuthRequired)
routes.PrivateRoutes(private)
config2 := cors.DefaultConfig()
config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
config2.AllowCredentials = true
router.Use(cors.New(config2))
err := router.Run(config.Port)
if err != nil {
    log.Println(err)
}
英文:

This issue is no longer a problem, it seemed to be an issue with my CORS? I fixed it by moving the configuration to the bottom. I should've put this code in my intial question

Before

router := gin.Default()
config2 := cors.DefaultConfig()
	config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
	config2.AllowCredentials = true
	router.Use(cors.New(config2))

	HandleRoutes(router)
	public := router.Group("/")
	routes.PublicRoutes(public)
	router.NoRoute(func(c *gin.Context) {
		http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
	})
	private := router.Group("/")
	private.Use(helpers.AuthRequired)
	routes.PrivateRoutes(private)
	
	err := router.Run(config.Port)
	if err != nil {
		log.Println(err)
	}

After

router := gin.Default()

	HandleRoutes(router)
	public := router.Group("/")
	routes.PublicRoutes(public)
	router.NoRoute(func(c *gin.Context) {
		http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
	})
	private := router.Group("/")
	private.Use(helpers.AuthRequired)
	routes.PrivateRoutes(private)
	config2 := cors.DefaultConfig()
	config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
	config2.AllowCredentials = true
	router.Use(cors.New(config2))
	err := router.Run(config.Port)
	if err != nil {
		log.Println(err)
	}

</details>



huangapple
  • 本文由 发表于 2023年5月8日 06:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76196547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定