diff options
| author | Shulhan <m.shulhan@gmail.com> | 2024-09-15 21:47:31 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2024-09-15 21:47:31 +0700 |
| commit | 1b6d5089f95e7d3ae6c058108b5be04fce2a3598 (patch) | |
| tree | 4d2a4574458753d3000b7266cc47140d2a14397f | |
| parent | 548712d7fd6d52022c4f8600889bc134cf6d4ad3 (diff) | |
| download | golang-id-tour-1b6d5089f95e7d3ae6c058108b5be04fce2a3598.tar.xz | |
all: tambah flag untuk mengatur Origin dari koneksi WebSocket
Misalnya, jika httpListen di set ke "127.0.0.1:10201" dan servis berjalan
dibelakang sebuah proksi dengan domain "tour.golang-id.local", koneksi
ke WebSocket akan ditolak, karena pada saat handshake origin dari request
dicocokan dengan host dan port dari httpListen.
| -rw-r--r-- | local.go | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -36,8 +36,14 @@ const ( ) var ( - httpListen = flag.String("http", "127.0.0.1:3999", "host:port to listen on") - openBrowser = flag.Bool("openbrowser", false, "open browser automatically") + httpListen = flag.String("http", "127.0.0.1:3999", "host:port to listen on") + openBrowser = flag.Bool("openbrowser", false, "open browser automatically") + websocketOrigin = flag.String(`ws-origin`, ``, + `Set origin for handling WebSocket connection. +For service that are running behind proxy (on different IP and port than +httpListen) and/or with domain name you should set this option for WebSocket +to works. +Example value: "https://tour.golang-id.local".`) ) var ( @@ -127,7 +133,16 @@ func main() { http.HandleFunc("/", rootHandler) http.HandleFunc("/lesson/", lessonHandler) - origin := &url.URL{Scheme: "http", Host: host + ":" + port} + var origin *url.URL + + if *websocketOrigin != `` { + origin, err = url.Parse(*websocketOrigin) + if err != nil { + log.Fatalf(`invalid ws-origin: %s`, err) + } + } else { + origin = &url.URL{Scheme: "http", Host: host + ":" + port} + } http.Handle(socketPath, socket.NewHandler(origin)) registerStatic(root) |
