From f5c8389b1c07da85ee3ff6e4707a06248e270638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E7=94=B0=E5=BC=98?= <1240092443@qq.com> Date: Wed, 15 Jan 2025 11:17:48 +0800 Subject: [PATCH] faet:localproxy --- frontend/src/components/setting/FrpcEdit.vue | 23 +- .../src/components/setting/LocalProxy.vue | 544 +++++++++++------- frontend/src/stores/proxy.ts | 2 +- godo/model/local_proxy.go | 11 +- godo/proxy/local.go | 56 +- 5 files changed, 378 insertions(+), 258 deletions(-) diff --git a/frontend/src/components/setting/FrpcEdit.vue b/frontend/src/components/setting/FrpcEdit.vue index 0f347c9..f1bb987 100644 --- a/frontend/src/components/setting/FrpcEdit.vue +++ b/frontend/src/components/setting/FrpcEdit.vue @@ -1,13 +1,13 @@ diff --git a/frontend/src/components/setting/LocalProxy.vue b/frontend/src/components/setting/LocalProxy.vue index d65eaf9..5c44c82 100644 --- a/frontend/src/components/setting/LocalProxy.vue +++ b/frontend/src/components/setting/LocalProxy.vue @@ -1,229 +1,329 @@ - - - - - - - - - - - - - {{scope.row.status ? '启用' : '禁用'}} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 确认 - - - - - - - \ No newline at end of file + + + + + + + + + + + + + {{ scope.row.status ? "启用" : "禁用" }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 确认 + + + + + + + diff --git a/frontend/src/stores/proxy.ts b/frontend/src/stores/proxy.ts index 783093e..8eedf14 100644 --- a/frontend/src/stores/proxy.ts +++ b/frontend/src/stores/proxy.ts @@ -186,7 +186,7 @@ export const useProxyStore = defineStore('proxyStore', () => { return await fetch(`${apiUrl}/proxy/frpc/getconfig`).then(res => res.json()) } const setConfig = async (config: any) => { - return await fetch(`${apiUrl}/frpc/setconfig`, { + return await fetch(`${apiUrl}/proxy/frpc/setconfig`, { method: "POST", headers: { "Content-Type": "application/json" diff --git a/godo/model/local_proxy.go b/godo/model/local_proxy.go index 78c4613..7599500 100644 --- a/godo/model/local_proxy.go +++ b/godo/model/local_proxy.go @@ -2,11 +2,12 @@ package model type LocalProxy struct { BaseModel - Port uint `json:"port"` - ProxyType string `json:"proxyType"` - Domain string `json:"domain"` - Path string `json:"path"` - Status bool `json:"status"` + Port uint `json:"port"` // 本地端口 + ProxyType string `json:"proxyType"` // 代理类型 + Domain string `json:"domain"` // 代理域名 + Path string `json:"path"` // 代理路径 + Status bool `json:"status"` // 状态 + ListenPort uint `json:"listenPort"` // 代理端口 } func (*LocalProxy) TableName() string { diff --git a/godo/proxy/local.go b/godo/proxy/local.go index 8eb695f..df16b65 100644 --- a/godo/proxy/local.go +++ b/godo/proxy/local.go @@ -116,10 +116,11 @@ func UpdateLocalProxyHandler(w http.ResponseWriter, r *http.Request) { return } updata := map[string]interface{}{ - "port": lp.Port, - "proxy_type": lp.ProxyType, - "domain": lp.Domain, - "status": lp.Status, + "port": lp.Port, + "proxy_type": lp.ProxyType, + "domain": lp.Domain, + "status": lp.Status, + "listen_port": lp.ListenPort, // path } @@ -158,6 +159,8 @@ func DeleteLocalProxyHandler(w http.ResponseWriter, r *http.Request) { libs.SuccessMsg(w, nil, "delete proxy success") } + +// HandlerSetProxyStatus 设置代理状态 func HandlerSetProxyStatus(w http.ResponseWriter, r *http.Request) { idStr := r.URL.Query().Get("id") if idStr == "" { @@ -270,37 +273,52 @@ func stopProxy(id uint) { // HTTP 代理处理函数 func httpProxyHandler(proxy model.LocalProxy) { - remote, err := url.Parse(proxy.Domain) + // 如果 ListenPort 没有传递,默认为 80 + if proxy.ListenPort == 0 { + proxy.ListenPort = 80 + } + + fmt.Printf("Initializing HTTP proxy for ID %d on domain %s and listen port %d\n", proxy.ID, proxy.Domain, proxy.ListenPort) + + // 使用 proxy.Port 作为本地目标端口 + remote, err := url.Parse(fmt.Sprintf("http://localhost:%d", proxy.Port)) if err != nil { fmt.Printf("Failed to parse remote URL for port %d: %v\n", proxy.Port, err) return } + fmt.Printf("Parsed remote URL: %s\n", remote.String()) - if remote.Scheme == "" { - fmt.Printf("Remote URL for port %d does not contain a scheme (http/https): %s\n", proxy.Port, proxy.Domain) - return + reverseProxy := httputil.NewSingleHostReverseProxy(remote) + reverseProxy.Director = func(req *http.Request) { + req.URL.Scheme = remote.Scheme + req.URL.Host = remote.Host + req.Host = remote.Host + req.URL.Path = proxy.Path + req.URL.Path // 使用代理路径 + fmt.Printf("Proxying request to: %s\n", req.URL.String()) } - reverseProxy := httputil.NewSingleHostReverseProxy(remote) + reverseProxy.ModifyResponse = func(resp *http.Response) error { + fmt.Printf("Received response with status: %s\n", resp.Status) + return nil + } - // 设置请求头 - // reverseProxy.Director = func(req *http.Request) { - // req.Header.Add("X-Forwarded-For", req.RemoteAddr) - // req.Header.Add("X-Real-IP", req.RemoteAddr) - // req.Host = remote.Host - // } + reverseProxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) { + fmt.Printf("Error during proxying: %v\n", err) + http.Error(rw, "Proxy error", http.StatusBadGateway) + } - // 启动 HTTP 服务器并监听指定端口 + // 监听配置中指定的域名和端口 server := &http.Server{ - Addr: fmt.Sprintf(":%d", proxy.Port), + Addr: fmt.Sprintf(":%d", proxy.ListenPort), Handler: reverseProxy, } proxyServers.Store(proxy.ID, ProxyServer{Type: "http", Server: server}) - fmt.Printf("Starting HTTP proxy on LocalHost port %d and forwarding to %s\n", proxy.Port, proxy.Domain) if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - fmt.Printf("Failed to start HTTP proxy on port %d: %v\n", proxy.Port, err) + fmt.Printf("Failed to start HTTP proxy on domain %s and listen port %d: %v\n", proxy.Domain, proxy.ListenPort, err) + } else { + fmt.Printf("HTTP proxy on domain %s and listen port %d started successfully\n", proxy.Domain, proxy.ListenPort) } }