diff --git a/.gitignore b/.gitignore index 70048d63a..0bca9eb9c 100644 --- a/.gitignore +++ b/.gitignore @@ -39,10 +39,10 @@ cmd/server/web/index.html frontend/auto-imports.d.ts frontend/components.d.ts frontend/src/xpack -backend/xpack -backend/router/entry_xpack.go -backend/server/init_xpack.go -backend/utils/xpack/xpack_xpack.go +agent/xpack +agent/router/entry_xpack.go +agent/server/init_xpack.go +agent/utils/xpack/xpack_xpack.go .history/ dist/ diff --git a/agent/router/entry_xpack.go b/agent/router/entry_xpack.go deleted file mode 120000 index 4a98d3368..000000000 --- a/agent/router/entry_xpack.go +++ /dev/null @@ -1 +0,0 @@ -/Users/slooop/Documents/mycode/xpack-backend/other/entry_xpack.go \ No newline at end of file diff --git a/agent/server/init_xpack.go b/agent/server/init_xpack.go deleted file mode 120000 index 2ac522410..000000000 --- a/agent/server/init_xpack.go +++ /dev/null @@ -1 +0,0 @@ -/Users/slooop/Documents/mycode/xpack-backend/other/init_xpack.go \ No newline at end of file diff --git a/agent/server/server.go b/agent/server/server.go index 4843d5a11..dcb247f12 100644 --- a/agent/server/server.go +++ b/agent/server/server.go @@ -5,7 +5,6 @@ import ( "net/http" "github.com/1Panel-dev/1Panel/agent/cron" - "github.com/1Panel-dev/1Panel/agent/global" "github.com/1Panel-dev/1Panel/agent/i18n" "github.com/1Panel-dev/1Panel/agent/init/app" "github.com/1Panel-dev/1Panel/agent/init/business" @@ -37,19 +36,24 @@ func Start() { rootRouter := router.Routers() server := &http.Server{ - Addr: "0.0.0.0:9998", Handler: rootRouter, } - ln, err := net.Listen("tcp4", "0.0.0.0:9998") - if err != nil { - panic(err) - } - type tcpKeepAliveListener struct { - *net.TCPListener - } + //ln, err := net.Listen("tcp4", "0.0.0.0:9998") + //if err != nil { + // panic(err) + //} + //type tcpKeepAliveListener struct { + // *net.TCPListener + //} + // + //global.LOG.Info("listen at http://0.0.0.0:9998") + //if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil { + // panic(err) + //} - global.LOG.Info("listen at http://0.0.0.0:9998") - if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil { + listener, err := net.Listen("unix", "/tmp/agent.sock") + if err != nil { panic(err) } + server.Serve(listener) } diff --git a/agent/utils/xpack/xpack_xpack.go b/agent/utils/xpack/xpack_xpack.go deleted file mode 120000 index aec4042e5..000000000 --- a/agent/utils/xpack/xpack_xpack.go +++ /dev/null @@ -1 +0,0 @@ -/Users/slooop/Documents/mycode/xpack-backend/other/xpack_xpack.go \ No newline at end of file diff --git a/core/init/router/router.go b/core/init/router/router.go index 56ffcd4f7..843ef6abc 100644 --- a/core/init/router/router.go +++ b/core/init/router/router.go @@ -1,9 +1,8 @@ package router import ( + "context" "fmt" - "net/http" - "github.com/1Panel-dev/1Panel/cmd/server/docs" "github.com/1Panel-dev/1Panel/cmd/server/web" "github.com/1Panel-dev/1Panel/core/global" @@ -14,6 +13,11 @@ import ( "github.com/gin-gonic/gin" swaggerfiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + "net" + "net/http" + "net/http/httputil" + "os" + "strings" ) var ( @@ -44,13 +48,6 @@ func Routers() *gin.Engine { Router.Use(middleware.DemoHandle()) } - Router.NoRoute(func(c *gin.Context) { - c.Writer.WriteHeader(http.StatusOK) - _, _ = c.Writer.Write(web.IndexByte) - c.Writer.Header().Add("Accept", "text/html") - c.Writer.Flush() - }) - Router.Use(i18n.UseI18n()) Router.Use(middleware.Proxy()) @@ -73,5 +70,41 @@ func Routers() *gin.Engine { router.InitRouter(PrivateGroup) } + // 使用 unix 代理 + sockPath := "/tmp/agent.sock" + if _, err := os.Stat(sockPath); err != nil { + panic(err) + } + dialUnix := func(proto, addr string) (conn net.Conn, err error) { + return net.Dial("unix", sockPath) + } + transport := &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialUnix(network, addr) + }, + } + proxy := &httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.URL.Scheme = "http" + req.URL.Host = "unix" + }, + Transport: transport, + } + Router.Use(func(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, "/api") { + proxy.ServeHTTP(c.Writer, c.Request) + c.Abort() + return + } + c.Next() + }) + + Router.NoRoute(func(c *gin.Context) { + c.Writer.WriteHeader(http.StatusOK) + _, _ = c.Writer.Write(web.IndexByte) + c.Writer.Header().Add("Accept", "text/html") + c.Writer.Flush() + }) + return Router } diff --git a/core/middleware/proxy.go b/core/middleware/proxy.go index 0c06e5e08..17eb417ca 100644 --- a/core/middleware/proxy.go +++ b/core/middleware/proxy.go @@ -1,9 +1,6 @@ package middleware import ( - "fmt" - "net/http/httputil" - "net/url" "strings" "github.com/gin-gonic/gin" @@ -17,14 +14,14 @@ func Proxy() gin.HandlerFunc { c.Next() return } - target, err := url.Parse("http://127.0.0.1:9998") - if err != nil { - fmt.Printf("Failed to parse target URL: %v", err) - } - proxy := httputil.NewSingleHostReverseProxy(target) - c.Request.Host = target.Host - c.Request.URL.Scheme = target.Scheme - c.Request.URL.Host = target.Host - proxy.ServeHTTP(c.Writer, c.Request) + //target, err := url.Parse("http://127.0.0.1:9998") + //if err != nil { + // fmt.Printf("Failed to parse target URL: %v", err) + //} + //proxy := httputil.NewSingleHostReverseProxy(target) + //c.Request.Host = target.Host + //c.Request.URL.Scheme = target.Scheme + //c.Request.URL.Host = target.Host + //proxy.ServeHTTP(c.Writer, c.Request) } } diff --git a/core/router/entry.go b/core/router/entry.go index 49ed25fd1..da8fc47ad 100644 --- a/core/router/entry.go +++ b/core/router/entry.go @@ -1,5 +1,3 @@ -//go:build !xpack - package router func RouterGroups() []CommonRouter {