2024-07-23 02:11:43 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-07-25 06:43:41 +00:00
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"os"
|
2024-07-23 02:11:43 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Proxy() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-07-25 06:43:41 +00:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") {
|
2024-07-23 02:11:43 +00:00
|
|
|
c.Next()
|
|
|
|
return
|
2024-07-25 06:43:41 +00:00
|
|
|
} 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()
|
2024-07-23 02:11:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|