From 4ec7bc6106d56254dc6b93c27e8373824feda012 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 10 Aug 2023 18:30:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxyserver/main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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,包含了查询参数