From 3a00bcb5b3de167110518715b4add7e521173e68 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Thu, 4 Dec 2025 13:19:51 +0800 Subject: [PATCH 1/2] feat: Enhance proxy initialization and error handling * Add a timeout to the dialer for Unix socket connections * Improve error response by including the error message in the "Bad Gateway" response --- core/init/proxy/proxy.go | 7 +++++-- core/init/router/proxy.go | 6 ------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/core/init/proxy/proxy.go b/core/init/proxy/proxy.go index efda4a052065..365aea5b8b0e 100644 --- a/core/init/proxy/proxy.go +++ b/core/init/proxy/proxy.go @@ -15,8 +15,11 @@ var ( ) func Init() { + dialer := &net.Dialer{ + Timeout: 5 * time.Second, + } dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) { - return net.Dial("unix", sockPath) + return dialer.DialContext(ctx, "unix", sockPath) } transport := &http.Transport{ DialContext: dialUnix, @@ -33,7 +36,7 @@ func Init() { Transport: transport, ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) { rw.WriteHeader(http.StatusBadGateway) - rw.Write([]byte("Bad Gateway")) + _, _ = rw.Write([]byte("Bad Gateway: " + err.Error())) }, } } diff --git a/core/init/router/proxy.go b/core/init/router/proxy.go index c122f62ee7ec..783c1917dcfe 100644 --- a/core/init/router/proxy.go +++ b/core/init/router/proxy.go @@ -3,7 +3,6 @@ package router import ( "net/http" "net/url" - "os" "strconv" "strings" @@ -52,11 +51,6 @@ func Proxy() gin.HandlerFunc { } if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0) { - sockPath := "/etc/1panel/agent.sock" - if _, err := os.Stat(sockPath); err != nil { - helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err) - return - } defer func() { if err := recover(); err != nil && err != http.ErrAbortHandler { global.LOG.Debug(err) From a47261ffd0dce391ffdb94c92cba893f87f9f587 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Thu, 4 Dec 2025 13:27:27 +0800 Subject: [PATCH 2/2] refactor: Change sockPath variable to constant in proxy initialization * Update sockPath to a constant SockPath for improved clarity and consistency * Ensure the new constant is used in the dialer function for Unix socket connections --- core/init/proxy/proxy.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/init/proxy/proxy.go b/core/init/proxy/proxy.go index 365aea5b8b0e..7cbb9526c410 100644 --- a/core/init/proxy/proxy.go +++ b/core/init/proxy/proxy.go @@ -8,9 +8,9 @@ import ( "time" ) -var ( - sockPath = "/etc/1panel/agent.sock" +const SockPath = "/etc/1panel/agent.sock" +var ( LocalAgentProxy *httputil.ReverseProxy ) @@ -19,7 +19,7 @@ func Init() { Timeout: 5 * time.Second, } dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) { - return dialer.DialContext(ctx, "unix", sockPath) + return dialer.DialContext(ctx, "unix", SockPath) } transport := &http.Transport{ DialContext: dialUnix,