From b9aa0e6d103e172feaa0a322d1e7d8041c290ece Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 10 Aug 2023 17:31:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxyserver/main.go | 66 ++++----------------------------------------- 1 file changed, 5 insertions(+), 61 deletions(-) diff --git a/proxyserver/main.go b/proxyserver/main.go index da313c35..066a16d1 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" "io" - "io/fs" + "io/ioutil" "log" "net" "net/http" @@ -15,7 +15,6 @@ import ( "sync" "time" - "github.com/gorilla/websocket" "gopkg.in/yaml.v2" ) @@ -116,8 +115,6 @@ type Backend struct { HttpAddress string Client *http.Client Handler http.HandlerFunc - - Dialer *websocket.Dialer } func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Backend { @@ -145,29 +142,14 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac }, } - dialer := &websocket.Dialer{ - Proxy: http.ProxyFromEnvironment, - NetDial: func(network, addr string) (net.Conn, error) { - return net.Dial(network, addr) - }, - } - // 创建后端服务对象,包含地址和客户端 backend := &Backend{ HttpAddress: httpAddress, Client: client, - Dialer: dialer, } // 创建处理请求的函数 handleRequest := func(w http.ResponseWriter, r *http.Request) { - - if websocket.IsWebSocketUpgrade(r) { - // Handle websocket connections - handleWebSocketProxy(w, r, backend) - return - } - // 解析目标URL,包含了查询参数 targetURL, err := url.Parse(httpAddress + r.URL.String()) if err != nil { @@ -244,7 +226,7 @@ type Result struct { // GetZeroInfo 遍历指定目录,并解析相关信息 func GetZeroInfo(rootDir string) (results []*Result) { - entries, err := os.ReadDir(rootDir) + entries, err := ioutil.ReadDir(rootDir) if err != nil { log.Fatal(err) } @@ -265,7 +247,7 @@ func GetZeroInfo(rootDir string) (results []*Result) { } // findFoldersAndExtractInfo 查找目录并提取信息 -func findFoldersAndExtractInfo(rootDir string, entry fs.DirEntry) (*Result, error) { +func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) { var result *Result folderName := entry.Name() @@ -295,7 +277,7 @@ func findFoldersAndExtractInfo(rootDir string, entry fs.DirEntry) (*Result, erro configPath := filepath.Join(path, "etc", folderName+".yaml") routesPath := filepath.Join(path, "internal", "handler", "routes.go") - configContent, err := os.ReadFile(configPath) + configContent, err := ioutil.ReadFile(configPath) if err != nil { return err } @@ -307,7 +289,7 @@ func findFoldersAndExtractInfo(rootDir string, entry fs.DirEntry) (*Result, erro } // 读取路由文件 - routesContent, err := os.ReadFile(routesPath) + routesContent, err := ioutil.ReadFile(routesPath) if err != nil { return err } @@ -352,41 +334,3 @@ func extractPrefixRouteValues(content string) map[string]bool { return prefixPath } - -func handleWebSocketProxy(w http.ResponseWriter, r *http.Request, backend *Backend) { - target := url.URL{Scheme: "ws", Host: backend.HttpAddress, Path: r.URL.Path} - - proxyConn, _, err := backend.Dialer.DialContext(r.Context(), target.String(), nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer proxyConn.Close() - - upgrader := websocket.Upgrader{} - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - return - } - defer conn.Close() - - go transfer(proxyConn, conn) - go transfer(conn, proxyConn) -} - -func transfer(src, dest *websocket.Conn) { - for { - messageType, data, err := src.ReadMessage() - if err != nil { - break - } - - err = dest.WriteMessage(messageType, data) - if err != nil { - break - } - } - - src.Close() - dest.Close() -} From 42193df5052fa2ab927a2b3adfc514e11922a134 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 10 Aug 2023 17:41:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95websocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxyserver/main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/proxyserver/main.go b/proxyserver/main.go index 066a16d1..05631968 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -15,6 +15,7 @@ import ( "sync" "time" + "github.com/gorilla/websocket" "gopkg.in/yaml.v2" ) @@ -115,6 +116,7 @@ type Backend struct { HttpAddress string Client *http.Client Handler http.HandlerFunc + Dialer *websocket.Dialer } func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Backend { @@ -142,14 +144,63 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac }, } + dialer := &websocket.Dialer{ + Proxy: http.ProxyFromEnvironment, + NetDial: func(network, addr string) (net.Conn, error) { + return net.Dial(network, addr) + }, + } + // 创建后端服务对象,包含地址和客户端 backend := &Backend{ HttpAddress: httpAddress, Client: client, + Dialer: dialer, } // 创建处理请求的函数 handleRequest := func(w http.ResponseWriter, r *http.Request) { + + if websocket.IsWebSocketUpgrade(r) { + //todo: 建立websocket的代理 + + target := url.URL{Scheme: "ws", Host: backend.HttpAddress, Path: r.URL.Path} + + var transfer = func(src, dest *websocket.Conn) { + for { + mType, msg, err := src.ReadMessage() + if err != nil { + break + } + + err = dest.WriteMessage(mType, msg) + if err != nil { + break + } + } + + src.Close() + dest.Close() + } + + proxyConn, _, err := backend.Dialer.Dial(target.String(), nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer proxyConn.Close() + + upgrader := websocket.Upgrader{} + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + defer conn.Close() + + go transfer(proxyConn, conn) + go transfer(conn, proxyConn) + } + // 解析目标URL,包含了查询参数 targetURL, err := url.Parse(httpAddress + r.URL.String()) if err != nil {