From 3b28223851eed488b61aab34aed01260643dff0d Mon Sep 17 00:00:00 2001 From: raller1028 Date: Fri, 19 May 2023 12:03:13 +0100 Subject: [PATCH] Add zerotier route --- go.mod | 2 +- main.go | 1 + route/v1.go | 2 +- route/v1/zerotier.go | 73 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 route/v1/zerotier.go diff --git a/go.mod b/go.mod index 6b1a91a..0b32cb5 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( go.uber.org/goleak v1.2.1 go.uber.org/zap v1.24.0 golang.org/x/crypto v0.8.0 + golang.org/x/net v0.9.0 golang.org/x/oauth2 v0.6.0 golang.org/x/sync v0.1.0 golang.org/x/sys v0.7.0 @@ -123,7 +124,6 @@ require ( golang.org/x/arch v0.3.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/image v0.6.0 // indirect - golang.org/x/net v0.9.0 // indirect golang.org/x/text v0.9.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/main.go b/main.go index 27a1303..3a8d816 100644 --- a/main.go +++ b/main.go @@ -150,6 +150,7 @@ func main() { "/v1/cloud", "/v1/recover", "/v1/other", + "/v1/zt", route.V2APIPath, route.V2DocPath, route.V3FilePath, diff --git a/route/v1.go b/route/v1.go index b00e9a6..0889c39 100644 --- a/route/v1.go +++ b/route/v1.go @@ -40,7 +40,7 @@ func InitV1Router() *gin.Engine { }) r.GET("/v1/recover/:type", v1.GetRecoverStorage) v1Group := r.Group("/v1") - + r.GET("/v1/zt/*url", v1.AddZerotierToken) v1Group.Use(jwt.ExceptLocalhost(func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(config.CommonInfo.RuntimePath) })) { diff --git a/route/v1/zerotier.go b/route/v1/zerotier.go new file mode 100644 index 0000000..96e2946 --- /dev/null +++ b/route/v1/zerotier.go @@ -0,0 +1,73 @@ +package v1 + +import ( + "fmt" + "io/ioutil" + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +func AddZerotierToken(c *gin.Context) { + // Read the port number from the file + w := c.Writer + r := c.Request + port, err := ioutil.ReadFile("/var/lib/zerotier-one/zerotier-one.port") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Get the request path and remove "/zt" + path := strings.TrimPrefix(r.URL.Path, "/v1/zt") + fmt.Println(path) + + // Build the target URL + targetURL := fmt.Sprintf("http://localhost:%s%s", strings.TrimSpace(string(port)), path) + + // Create a new request + req, err := http.NewRequest(r.Method, targetURL, r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Add the X-ZT1-AUTH header + authToken, err := ioutil.ReadFile("/var/lib/zerotier-one/authtoken.secret") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + req.Header.Set("X-ZT1-AUTH", strings.TrimSpace(string(authToken))) + + copyHeaders(req.Header, r.Header) + + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + copyHeaders(w.Header(), resp.Header) + + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Return the response to the client + w.WriteHeader(resp.StatusCode) + w.Write(respBody) +} + +func copyHeaders(destination, source http.Header) { + for key, values := range source { + for _, value := range values { + destination.Add(key, value) + } + } +}