Skip to content

Commit

Permalink
fix: checkorigin toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
NNcrawler committed Sep 23, 2021
1 parent e008c25 commit b381101
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/reference/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Number of goroutine spawn to Ping clients.

### `SERVER_WEBSOCKET_CHECK_ORIGIN`

Toggle CORS check function. Keep this to true, currently false will lead to rejection on every request.
Toggle CORS check function. Set `true` to check each request origin. Set `false` to disable check origin and allow every request. Check origin function check against `Origin` header.

* Type: `Optional`
* Default value: `true`
Expand Down
14 changes: 9 additions & 5 deletions websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func CreateServer() (*Server, chan EventsBatch) {
pingChannel := make(chan connection, config.ServerWs.ServerMaxConn)
user := NewUserStore(config.ServerWs.ServerMaxConn)
wsHandler := &Handler{
websocketUpgrader: getWebSocketUpgrader(config.ServerWs.ReadBufferSize, config.ServerWs.WriteBufferSize, config.ServerWs.CheckOrigin),
websocketUpgrader: newWebSocketUpgrader(config.ServerWs.ReadBufferSize, config.ServerWs.WriteBufferSize, config.ServerWs.CheckOrigin),
bufferChannel: bufferChannel,
user: user,
PongWaitInterval: config.ServerWs.PongWaitInterval,
Expand Down Expand Up @@ -101,13 +101,17 @@ func Router(h *Handler) http.Handler {
return router
}

func getWebSocketUpgrader(readBufferSize int, writeBufferSize int, checkOrigin bool) websocket.Upgrader {
func newWebSocketUpgrader(readBufferSize int, writeBufferSize int, checkOrigin bool) websocket.Upgrader {
var checkOriginFunc func(r *http.Request) bool
if checkOrigin == false {
checkOriginFunc = func(r *http.Request) bool {
return true
}
}
ug := websocket.Upgrader{
ReadBufferSize: readBufferSize,
WriteBufferSize: writeBufferSize,
CheckOrigin: func(r *http.Request) bool {
return checkOrigin
},
CheckOrigin: checkOriginFunc,
}
return ug
}

0 comments on commit b381101

Please sign in to comment.