mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-24 02:59:16 +08:00
feat: 本地 agent 使用 unix 通信
This commit is contained in:
parent
8d28b7db94
commit
cabca70ee5
8
.gitignore
vendored
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 +0,0 @@
|
||||
/Users/slooop/Documents/mycode/xpack-backend/other/entry_xpack.go
|
@ -1 +0,0 @@
|
||||
/Users/slooop/Documents/mycode/xpack-backend/other/init_xpack.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")
|
||||
//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)
|
||||
//}
|
||||
|
||||
listener, err := net.Listen("unix", "/tmp/agent.sock")
|
||||
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)
|
||||
}
|
||||
server.Serve(listener)
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
/Users/slooop/Documents/mycode/xpack-backend/other/xpack_xpack.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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
//go:build !xpack
|
||||
|
||||
package router
|
||||
|
||||
func RouterGroups() []CommonRouter {
|
||||
|
Loading…
Reference in New Issue
Block a user