feat: Refactor reverse proxy to reuse Transport and enable connection pooling (#8772)

This commit is contained in:
CityFun 2025-05-21 17:00:50 +08:00 committed by GitHub
parent e2876ed1c5
commit d2cd5e226a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 30 deletions

39
core/init/proxy/proxy.go Normal file
View file

@ -0,0 +1,39 @@
package proxy
import (
"context"
"net"
"net/http"
"net/http/httputil"
"time"
)
var (
sockPath = "/etc/1panel/agent.sock"
LocalAgentProxy *httputil.ReverseProxy
)
func Init() {
dialUnix := func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("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"))
},
}
}

View file

@ -1,22 +1,18 @@
package router package router
import ( import (
"context" "github.com/1Panel-dev/1Panel/core/init/proxy"
"net"
"net/http" "net/http"
"net/http/httputil"
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/core/app/repo" "github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/cmd/server/res" "github.com/1Panel-dev/1Panel/core/cmd/server/res"
"github.com/1Panel-dev/1Panel/core/constant" "github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global" "github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/utils/security"
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/core/utils/xpack" "github.com/1Panel-dev/1Panel/core/utils/xpack"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -54,35 +50,13 @@ func Proxy() gin.HandlerFunc {
return return
} }
if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0 || currentNode == "127.0.0.1") { if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0) {
sockPath := "/etc/1panel/agent.sock" sockPath := "/etc/1panel/agent.sock"
if _, err := os.Stat(sockPath); err != nil { if _, err := os.Stat(sockPath); err != nil {
helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err) helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err)
return return
} }
dialUnix := func() (conn net.Conn, err error) { proxy.LocalAgentProxy.ServeHTTP(c.Writer, c.Request)
return net.Dial("unix", sockPath)
}
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialUnix()
},
}
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = "unix"
},
Transport: transport,
ModifyResponse: func(response *http.Response) error {
if response.StatusCode == 404 {
security.HandleNotSecurity(c, "")
c.Abort()
}
return nil
},
}
proxy.ServeHTTP(c.Writer, c.Request)
c.Abort() c.Abort()
return return
} }

View file

@ -4,6 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/core/init/proxy"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -45,6 +46,7 @@ func Start() {
InitOthers() InitOthers()
run.Init() run.Init()
proxy.Init()
rootRouter := router.Routers() rootRouter := router.Routers()