From 59e30adaa224c7e76271fbba22299d943e7bea45 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Fri, 14 Feb 2025 20:55:26 +0100 Subject: [PATCH] init --- cmd/root.go | 23 ++++++++++++ cmd/up.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 10 ++++++ go.sum | 10 ++++++ main.go | 7 ++++ 5 files changed, 151 insertions(+) create mode 100644 cmd/root.go create mode 100644 cmd/up.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..dc26c4c --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "holesail-proxy", + Short: "A simple proxy for holesail connectors", + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/cmd/up.go b/cmd/up.go new file mode 100644 index 0000000..f3b8ab4 --- /dev/null +++ b/cmd/up.go @@ -0,0 +1,101 @@ +package cmd + +import ( + "fmt" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "os/exec" + "strconv" + "strings" + "time" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(upCmd) +} + +var upCmd = &cobra.Command{ + Use: "up", + Short: "Start holesail proxy", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + cmd.Println("Starting proxy") + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if len(strings.Split(r.Host, ".")) == 1 { + w.Write([]byte("No holesail connector provided")) + return + } + + connector := strings.Split(r.Host, ".")[0] + + findProcess := exec.Command("pgrep", "-f", "/holesail/index.js "+connector) + + out, _ := findProcess.Output() + splitted := strings.Split(string(out), "\n") + + var isRunning bool + var freePort int + + for _, pid := range splitted { + getDirectory := exec.Command("readlink", fmt.Sprintf("/proc/%s/cwd", string(pid))) + out, _ = getDirectory.Output() + + wd, _ := os.Getwd() + + if strings.TrimSpace(string(out)) == wd { + isRunning = true + + getCommand := exec.Command("cat", fmt.Sprintf("/proc/%s/cmdline", string(pid))) + out, _ = getCommand.Output() + + outSplitted := strings.Split(string(out), "/") + + outWithoutBloat := outSplitted[len(outSplitted)-1] + outWithoutBloat = strings.Split(outWithoutBloat, "--port")[1] + + fmt.Print(outWithoutBloat) + freePort, _ = strconv.Atoi(outWithoutBloat) + } + } + + if !isRunning { + freePort, _ = getFreePort() + + cmd := exec.Command("holesail", connector, "--port", strconv.Itoa(freePort)) + cmd.Start() + + // Wait for the connection to be made + for { + _, err := http.Get("http://localhost:" + strconv.Itoa(freePort)) + if err == nil { + break + } + time.Sleep(500 * time.Millisecond) + } + } + + appUrl, _ := url.Parse("http://localhost:" + strconv.Itoa(freePort)) + httputil.NewSingleHostReverseProxy(appUrl).ServeHTTP(w, r) + }) + + http.ListenAndServe(":80", nil) + }, +} + +func getFreePort() (port int, err error) { + var a *net.TCPAddr + if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil { + var l *net.TCPListener + if l, err = net.ListenTCP("tcp", a); err == nil { + defer l.Close() + return l.Addr().(*net.TCPAddr).Port, nil + } + } + return +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7859964 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module holesail-proxy + +go 1.23.5 + +require github.com/spf13/cobra v1.8.1 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..912390a --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..abaf575 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "holesail-proxy/cmd" + +func main() { + cmd.Execute() +}