feat: add environment variable support
This commit is contained in:
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.23
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/a-h/templ v0.3.833
|
github.com/a-h/templ v0.3.833
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
golang.org/x/crypto v0.32.0
|
golang.org/x/crypto v0.32.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
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 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"tunnel_pls/session"
|
"tunnel_pls/session"
|
||||||
|
"tunnel_pls/utils"
|
||||||
indexView "tunnel_pls/view/index"
|
indexView "tunnel_pls/view/index"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -86,7 +87,7 @@ func Listen() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
|
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
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -126,7 +127,7 @@ func handleRequest(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Host == "localhost" {
|
if r.Host == utils.Getenv("domain") {
|
||||||
writer := &tcpResponseWriter{
|
writer := &tcpResponseWriter{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
header: make(http.Header),
|
header: make(http.Header),
|
||||||
|
|||||||
@ -57,7 +57,7 @@ func (s *Session) handleGlobalRequest() {
|
|||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
binary.Write(buf, binary.BigEndian, uint32(portToBind))
|
binary.Write(buf, binary.BigEndian, uint32(portToBind))
|
||||||
log.Printf("Forwarding approved on port: %d", 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())
|
req.Reply(true, buf.Bytes())
|
||||||
} else {
|
} else {
|
||||||
s.TunnelType = TCP
|
s.TunnelType = TCP
|
||||||
@ -70,7 +70,7 @@ func (s *Session) handleGlobalRequest() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.Listener = listener
|
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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
|
|||||||
@ -1,11 +1,26 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"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 {
|
func GenerateRandomString(length int) string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyz"
|
const charset = "abcdefghijklmnopqrstuvwxyz"
|
||||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(9999))))
|
seededRand := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(9999))))
|
||||||
@ -16,3 +31,27 @@ func GenerateRandomString(length int) string {
|
|||||||
}
|
}
|
||||||
return result.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
|
||||||
|
}
|
||||||
|
|||||||
@ -2,13 +2,12 @@ package indexView
|
|||||||
|
|
||||||
import "tunnel_pls/view/layout"
|
import "tunnel_pls/view/layout"
|
||||||
|
|
||||||
templ Main(title string) {
|
templ Main(title, domain string) {
|
||||||
@layout.Base(title) {
|
@layout.Base(title) {
|
||||||
<div class="min-h-screen bg-gradient-to-b from-gray-900 to-gray-800 text-white">
|
<div class="min-h-screen bg-gradient-to-b from-gray-900 to-gray-800 text-white">
|
||||||
<header class="container mx-auto px-4 py-8">
|
<header class="container mx-auto px-4 py-8">
|
||||||
<nav class="flex justify-between items-center">
|
<nav class="flex justify-between items-center">
|
||||||
<h1 class="text-2xl font-bold">SSH Tunnel</h1>
|
<h1 class="text-2xl font-bold">Tunnel Pls</h1>
|
||||||
<button class="px-4 py-2 border border-white rounded hover:bg-white hover:text-gray-900 transition-colors">Get Started</button>
|
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
<main class="container mx-auto px-4 py-16 text-center">
|
<main class="container mx-auto px-4 py-16 text-center">
|
||||||
@ -25,7 +24,7 @@ templ Main(title string) {
|
|||||||
Example Command
|
Example Command
|
||||||
</h3>
|
</h3>
|
||||||
<code id="sshCommand" class="text-green-400 text-lg block mb-4 bg-gray-900 p-4 rounded">
|
<code id="sshCommand" class="text-green-400 text-lg block mb-4 bg-gray-900 p-4 rounded">
|
||||||
ssh tunnl.live -p 2200 -R 80:localhost:8000
|
ssh {domain} -p 2200 -R 80:localhost:8000
|
||||||
</code>
|
</code>
|
||||||
<button id="copyButton" class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors flex items-center justify-center mx-auto">
|
<button id="copyButton" class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors flex items-center justify-center mx-auto">
|
||||||
Copy Command
|
Copy Command
|
||||||
@ -61,12 +60,12 @@ templ Main(title string) {
|
|||||||
<footer class="container mx-auto px-4 py-8 text-center text-gray-400">
|
<footer class="container mx-auto px-4 py-8 text-center text-gray-400">
|
||||||
<p>© 2025 SSH Tunnel. All rights reserved.</p>
|
<p>© 2025 SSH Tunnel. All rights reserved.</p>
|
||||||
<div class="mt-4 flex justify-center space-x-4">
|
<div class="mt-4 flex justify-center space-x-4">
|
||||||
<a href="https://github.com/yourusername" target="_blank" rel="noopener noreferrer" class="hover:text-white">
|
<a href="https://github.com/fossyy" target="_blank" rel="noopener noreferrer" class="hover:text-white">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24">
|
||||||
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"></path>
|
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"></path>
|
||||||
</svg>
|
</svg>
|
||||||
</a>
|
</a>
|
||||||
<a href="mailto:your.email@example.com" class="hover:text-white">
|
<a href="mailto:bagas@fossy.my.id" class="hover:text-white">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
|
||||||
</svg>
|
</svg>
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user