- ssh tunnl.live -p 2200 -R 80:localhost:8000
+ ssh {domain} -p 2200 -R 80:localhost:8000
diff --git a/go.mod b/go.mod index f5f9109..d00f01c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23 require ( github.com/a-h/templ v0.3.833 + github.com/joho/godotenv v1.5.1 golang.org/x/crypto v0.32.0 ) diff --git a/go.sum b/go.sum index e8f33cb..aadc42e 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU= github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= diff --git a/http/http.go b/http/http.go index af58673..4ba6c0b 100644 --- a/http/http.go +++ b/http/http.go @@ -10,6 +10,7 @@ import ( "os" "strings" "tunnel_pls/session" + "tunnel_pls/utils" indexView "tunnel_pls/view/index" ) @@ -86,7 +87,7 @@ func Listen() { } router.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) { - indexView.Main("Main Page").Render(r.Context(), w) + indexView.Main("Main Page", utils.Getenv("domain")).Render(r.Context(), w) return }) @@ -126,7 +127,7 @@ func handleRequest(conn net.Conn) { return } - if r.Host == "localhost" { + if r.Host == utils.Getenv("domain") { writer := &tcpResponseWriter{ conn: conn, header: make(http.Header), diff --git a/session/handler.go b/session/handler.go index e77ca34..395f7a5 100644 --- a/session/handler.go +++ b/session/handler.go @@ -57,7 +57,7 @@ func (s *Session) handleGlobalRequest() { buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, uint32(portToBind)) log.Printf("Forwarding approved on port: %d", portToBind) - s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to http://%s.tunnl.live \r\n", slug))) + s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to http://%s.%s \r\n", slug, utils.Getenv("domain")))) req.Reply(true, buf.Bytes()) } else { s.TunnelType = TCP @@ -70,7 +70,7 @@ func (s *Session) handleGlobalRequest() { continue } s.Listener = listener - s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to tunnl.live:%d \r\n", portToBind))) + s.ConnChannels[0].Write([]byte(fmt.Sprintf("Forwarding your traffic to %s:%d \r\n", utils.Getenv("domain"), portToBind))) go func() { for { conn, err := listener.Accept() diff --git a/utils/utils.go b/utils/utils.go index da9a779..a8c3d37 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,11 +1,26 @@ package utils import ( + "github.com/joho/godotenv" + "log" "math/rand" + "os" "strings" + "sync" "time" ) +type Env struct { + value map[string]string + mu sync.Mutex +} + +var env *Env + +func init() { + env = &Env{value: map[string]string{}} +} + func GenerateRandomString(length int) string { const charset = "abcdefghijklmnopqrstuvwxyz" seededRand := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(9999)))) @@ -16,3 +31,27 @@ func GenerateRandomString(length int) string { } return result.String() } + +func Getenv(key string) string { + env.mu.Lock() + defer env.mu.Unlock() + if val, ok := env.value[key]; ok { + return val + } + + if os.Getenv("HOSTNAME") == "" { + err := godotenv.Load(".env") + if err != nil { + log.Fatalf("Error loading .env file: %s", err) + } + } + + val := os.Getenv(key) + env.value[key] = val + + if val == "" { + panic("Asking for env: " + key + " but got nothing, please set your environment first") + } + + return val +} diff --git a/view/index/index.templ b/view/index/index.templ index 61f4c06..a36b1ed 100644 --- a/view/index/index.templ +++ b/view/index/index.templ @@ -2,13 +2,12 @@ package indexView import "tunnel_pls/view/layout" -templ Main(title string) { +templ Main(title, domain string) { @layout.Base(title) {
- ssh tunnl.live -p 2200 -R 80:localhost:8000
+ ssh {domain} -p 2200 -R 80:localhost:8000