mirror of
https://github.com/CyberL1/holesail-proxy.git
synced 2025-02-22 10:09:20 -05:00
init
This commit is contained in:
commit
59e30adaa2
23
cmd/root.go
Normal file
23
cmd/root.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
101
cmd/up.go
Normal file
101
cmd/up.go
Normal file
@ -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
|
||||
}
|
10
go.mod
Normal file
10
go.mod
Normal file
@ -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
|
||||
)
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -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=
|
Loading…
x
Reference in New Issue
Block a user