Update dialer.go

This commit is contained in:
RPRX 2024-04-26 03:08:54 +00:00 committed by GitHub
parent 003ffdd537
commit 1ce11cff0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package websocket
import (
"bytes"
"context"
_ "embed"
"encoding/base64"
@ -28,20 +29,21 @@ var conns chan *websocket.Conn
func init() {
addr := platform.NewEnvFlag(platform.BrowserDialerAddress).GetValue(func() string { return "" })
if addr != "" {
csrfToken := uuid.New()
token := uuid.New()
csrfToken := token.String()
webpage = bytes.ReplaceAll(webpage, []byte("csrfToken"), []byte(csrfToken))
conns = make(chan *websocket.Conn, 256)
go http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/websocket" && r.URL.Query().Get("csrf") == csrfToken.String() {
if conn, err := upgrader.Upgrade(w, r, nil); err == nil {
conns <- conn
} else {
newError("Browser dialer http upgrade unexpected error").AtError().WriteToLog()
if r.URL.Path == "/websocket" {
if r.URL.Query().Get("token") == csrfToken {
if conn, err := upgrader.Upgrade(w, r, nil); err == nil {
conns <- conn
} else {
newError("Browser dialer http upgrade unexpected error").AtError().WriteToLog()
}
}
} else {
w.Write(webpage)
w.Write([]byte("<script>\ncsrfToken = \""))
w.Write([]byte(csrfToken.String()))
w.Write([]byte("\";\n</script>"))
}
}))
}