aboutsummaryrefslogtreecommitdiffstats
path: root/api/handlers/peer.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2025-02-06 15:22:34 +0000
committerLeonardo Bishop <me@leonardobishop.com>2025-02-06 15:22:34 +0000
commit2475f5a8b92ef0dd28e7af5f36d01b25243ed778 (patch)
tree12f8931d241db4159f8d30f7bf2b648709a94166 /api/handlers/peer.go
Initial commit
Diffstat (limited to 'api/handlers/peer.go')
-rw-r--r--api/handlers/peer.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/api/handlers/peer.go b/api/handlers/peer.go
new file mode 100644
index 0000000..51fa047
--- /dev/null
+++ b/api/handlers/peer.go
@@ -0,0 +1,75 @@
+package handlers
+
+import (
+ "fmt"
+ "log/slog"
+ "net/http"
+
+ "github.com/LMBishop/gunnel/pkg/config"
+ "github.com/LMBishop/gunnel/pkg/store"
+ "github.com/LMBishop/gunnel/pkg/wireguard"
+ "github.com/gorilla/mux"
+)
+
+const script = `#!/bin/bash
+
+# Your IP address: %s
+# Private key: %s
+# Unique slug: %s
+
+# Run this script as root to set up your client
+
+set -euo pipefail
+
+sudo ip link delete dev %s 2>/dev/null || true
+sudo ip link add %s type wireguard
+sudo ip addr add %s dev %s
+echo "%s" | sudo tee /tmp/tunnel-private > /dev/null
+sudo wg set %s private-key /tmp/tunnel-private
+sudo wg set %s peer %s allowed-ips %s endpoint %s:%s persistent-keepalive 21
+sudo ip link set up dev %s
+sudo ip route add %s dev %s
+
+echo "http://0.0.0.0:%s is now reachable at http://%s.%s"`
+
+func NewPeer(storeService store.Service, wireguardService wireguard.Service, configService config.Service) func(http.ResponseWriter, *http.Request) {
+ return func(w http.ResponseWriter, r *http.Request) {
+ params := mux.Vars(r)
+ port := params["port"]
+
+ peer, err := wireguardService.NewPeer()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ slug := storeService.GetUnusedSlug()
+
+ ipAddr := peer.IPAddr.String()
+
+ storeService.NewForwardingRule(slug, peer, port)
+
+ iface := configService.Config().WireGuard.InterfaceName
+ wireguardPort := configService.Config().WireGuard.Port
+ hostname := configService.Config().Hostname
+ network := configService.Config().WireGuard.Network
+ publicKey := wireguardService.PublicKey()
+
+ slog.Info("new peer", "peer", peer.PrivateKey)
+
+ fmt.Fprintf(w, script,
+ ipAddr,
+ peer.PrivateKey,
+ slug,
+ iface,
+ iface,
+ ipAddr, iface,
+ peer.PrivateKey,
+ iface,
+ iface, publicKey, network, hostname, wireguardPort,
+ iface,
+ network, iface,
+ port, slug, hostname,
+ )
+ }
+}