feat: core 增加 xpack 节点管理

This commit is contained in:
ssongliu
2024-07-25 14:43:41 +08:00
parent cabca70ee5
commit fafa042ee9
36 changed files with 411 additions and 116 deletions
+28 -12
View File
@@ -1,6 +1,11 @@
package middleware
import (
"context"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"github.com/gin-gonic/gin"
@@ -8,20 +13,31 @@ import (
func Proxy() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/v1/auth") ||
strings.HasPrefix(c.Request.URL.Path, "/api/v1/setting") ||
strings.HasPrefix(c.Request.URL.Path, "/api/v1/log") {
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") {
c.Next()
return
} else {
sockPath := "/tmp/agent.sock"
if _, err := os.Stat(sockPath); err != nil {
panic(err)
}
dialUnix := func() (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()
},
}
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = "unix"
},
Transport: transport,
}
proxy.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
//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)
}
}