1Panel/core/init/proxy/proxy.go
KOMATA 781155b029
perf: remove useless stat checking logic (#11178)
* 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

* 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
2025-12-04 18:07:47 +08:00

42 lines
922 B
Go

package proxy
import (
"context"
"net"
"net/http"
"net/http/httputil"
"time"
)
const SockPath = "/etc/1panel/agent.sock"
var (
LocalAgentProxy *httputil.ReverseProxy
)
func Init() {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
}
dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, "unix", SockPath)
}
transport := &http.Transport{
DialContext: dialUnix,
ForceAttemptHTTP2: false,
MaxIdleConns: 50,
MaxIdleConnsPerHost: 50,
IdleConnTimeout: 30 * time.Second,
}
LocalAgentProxy = &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = "unix"
},
Transport: transport,
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
rw.WriteHeader(http.StatusBadGateway)
_, _ = rw.Write([]byte("Bad Gateway: " + err.Error()))
},
}
}