WebSocket每次返回403错误

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

WebSocket returning 403 every time

问题

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

  1. func WebsocketServer(w http.ResponseWriter, r *http.Request, context *gin.Context) {
  2. fmt.Println("connection received")
  3. var upgrader2 = websocket.Upgrader{
  4. CheckOrigin: func(r *http.Request) bool {
  5. return true
  6. },
  7. EnableCompression: false,
  8. }
  9. db, err := aws.GetDB()
  10. if err != nil {
  11. helpers.Log.Println(err)
  12. return
  13. }
  14. defer db.Close()
  15. c, err := upgrader2.Upgrade(w, r, nil)
  16. if err != nil {
  17. helpers.Log.Println(err)
  18. return
  19. }
  20. }
  1. func PublicRoutes(c *gin.RouterGroup) {
  2. c.GET("/ws", func(c *gin.Context) {
  3. relay.WebsocketServer(c.Writer, c.Request, c)
  4. })
  5. }

我正在使用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

  1. func WebsocketServer(w http.ResponseWriter, r *http.Request, context *gin.Context) {
  2. fmt.Println("connection received")
  3. var upgrader2 = websocket.Upgrader{
  4. CheckOrigin: func(r *http.Request) bool {
  5. return true
  6. },
  7. EnableCompression: false,
  8. }
  9. db, err := aws.GetDB()
  10. if err != nil {
  11. helpers.Log.Println(err)
  12. return
  13. }
  14. defer db.Close()
  15. c, err := upgrader2.Upgrade(w, r, nil)
  16. if err != nil {
  17. helpers.Log.Println(err)
  18. return
  19. }
  20. }
  1. func PublicRoutes(c *gin.RouterGroup) {
  2. c.GET("/ws", func(c *gin.Context) {
  3. relay.WebsocketServer(c.Writer, c.Request, c)
  4. })
  5. }

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出了问题?我通过将配置移到底部来解决了这个问题。我应该在初始问题中放置这段代码。

之前的代码:

  1. router := gin.Default()
  2. config2 := cors.DefaultConfig()
  3. config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
  4. config2.AllowCredentials = true
  5. router.Use(cors.New(config2))
  6. HandleRoutes(router)
  7. public := router.Group("/")
  8. routes.PublicRoutes(public)
  9. router.NoRoute(func(c *gin.Context) {
  10. http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
  11. })
  12. private := router.Group("/")
  13. private.Use(helpers.AuthRequired)
  14. routes.PrivateRoutes(private)
  15. err := router.Run(config.Port)
  16. if err != nil {
  17. log.Println(err)
  18. }

修改后的代码:

  1. router := gin.Default()
  2. HandleRoutes(router)
  3. public := router.Group("/")
  4. routes.PublicRoutes(public)
  5. router.NoRoute(func(c *gin.Context) {
  6. http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
  7. })
  8. private := router.Group("/")
  9. private.Use(helpers.AuthRequired)
  10. routes.PrivateRoutes(private)
  11. config2 := cors.DefaultConfig()
  12. config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
  13. config2.AllowCredentials = true
  14. router.Use(cors.New(config2))
  15. err := router.Run(config.Port)
  16. if err != nil {
  17. log.Println(err)
  18. }
英文:

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

  1. router := gin.Default()
  2. config2 := cors.DefaultConfig()
  3. config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
  4. config2.AllowCredentials = true
  5. router.Use(cors.New(config2))
  6. HandleRoutes(router)
  7. public := router.Group("/")
  8. routes.PublicRoutes(public)
  9. router.NoRoute(func(c *gin.Context) {
  10. http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
  11. })
  12. private := router.Group("/")
  13. private.Use(helpers.AuthRequired)
  14. routes.PrivateRoutes(private)
  15. err := router.Run(config.Port)
  16. if err != nil {
  17. log.Println(err)
  18. }

After

  1. router := gin.Default()
  2. HandleRoutes(router)
  3. public := router.Group("/")
  4. routes.PublicRoutes(public)
  5. router.NoRoute(func(c *gin.Context) {
  6. http.Redirect(c.Writer, c.Request, "/login", http.StatusMovedPermanently)
  7. })
  8. private := router.Group("/")
  9. private.Use(helpers.AuthRequired)
  10. routes.PrivateRoutes(private)
  11. config2 := cors.DefaultConfig()
  12. config2.AllowOrigins = []string{"http://localhost:3000", "https://rblxfortune.vercel.app", "https://bloxbet.click", "http://localhost:80"}
  13. config2.AllowCredentials = true
  14. router.Use(cors.New(config2))
  15. err := router.Run(config.Port)
  16. if err != nil {
  17. log.Println(err)
  18. }
  19. </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:

确定