diff --git a/proxyserver/main.go b/proxyserver/main.go index 05631968..811f6476 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -164,17 +164,19 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac if websocket.IsWebSocketUpgrade(r) { //todo: 建立websocket的代理 - target := url.URL{Scheme: "ws", Host: backend.HttpAddress, Path: r.URL.Path} + target := url.URL{Scheme: "ws", Host: strings.Split(backend.HttpAddress, "//")[1], Path: r.URL.Path} var transfer = func(src, dest *websocket.Conn) { for { mType, msg, err := src.ReadMessage() if err != nil { + log.Println(err) break } err = dest.WriteMessage(mType, msg) if err != nil { + log.Println(err) break } } @@ -183,22 +185,33 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac dest.Close() } - proxyConn, _, err := backend.Dialer.Dial(target.String(), nil) + header := r.Header.Clone() + // log.Println(target.String()) + header.Del("Sec-Websocket-Extensions") + header.Del("Upgrade") + header.Del("Sec-Websocket-Key") + header.Del("Sec-Websocket-Version") + header.Del("Connection") + // header.Del("Origin") + proxyConn, _, err := backend.Dialer.Dial(target.String(), header) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - defer proxyConn.Close() + // defer proxyConn.Close() - upgrader := websocket.Upgrader{} + upgrader := websocket.Upgrader{ + CheckOrigin: func(r *http.Request) bool { return true }, + } conn, err := upgrader.Upgrade(w, r, nil) if err != nil { return } - defer conn.Close() + // defer conn.Close() go transfer(proxyConn, conn) - go transfer(conn, proxyConn) + // go transfer(conn, proxyConn) + return } // 解析目标URL,包含了查询参数