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] 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 efda4a052..365aea5b8 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 c122f62ee..783c1917d 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)