Browse Source

feat: 本地 agent 使用 unix 通信

pull/5993/head
zhengkunwang223 4 months ago
parent
commit
cabca70ee5
  1. 8
      .gitignore
  2. 1
      agent/router/entry_xpack.go
  3. 1
      agent/server/init_xpack.go
  4. 26
      agent/server/server.go
  5. 1
      agent/utils/xpack/xpack_xpack.go
  6. 51
      core/init/router/router.go
  7. 21
      core/middleware/proxy.go
  8. 2
      core/router/entry.go

8
.gitignore vendored

@ -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/

1
agent/router/entry_xpack.go

@ -1 +0,0 @@
/Users/slooop/Documents/mycode/xpack-backend/other/entry_xpack.go

1
agent/server/init_xpack.go

@ -1 +0,0 @@
/Users/slooop/Documents/mycode/xpack-backend/other/init_xpack.go

26
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)
}

1
agent/utils/xpack/xpack_xpack.go

@ -1 +0,0 @@
/Users/slooop/Documents/mycode/xpack-backend/other/xpack_xpack.go

51
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
}

21
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)
}
}

2
core/router/entry.go

@ -1,5 +1,3 @@
//go:build !xpack
package router
func RouterGroups() []CommonRouter {

Loading…
Cancel
Save