1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
diff --git a/cmd/tailgrok/main.go b/cmd/tailgrok/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..02fb769e7765d0055f91efa3769e3edeca336b40
--- /dev/null
+++ b/cmd/tailgrok/main.go
@@ -0,0 +1,130 @@
+package main
+
+import (
+ "bytes"
+ "context"
+ "crypto/rand"
+ "encoding/hex"
+ "encoding/json"
+ "flag"
+ "fmt"
+ "net/http"
+ "net/url"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "go.jolheiser.com/tailroute"
+ "golang.org/x/oauth2/clientcredentials"
+)
+
+func main() {
+ fs := flag.NewFlagSet("tailgrok", flag.ExitOnError)
+ if err := fs.Parse(os.Args[1:]); err != nil {
+ panic(err)
+ }
+
+ if fs.NArg() < 1 {
+ panic("tailgrok requires a target URL to proxy")
+ }
+
+ target, err := url.Parse(fs.Arg(0))
+ if err != nil {
+ panic(err)
+ }
+
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ panic(err)
+ }
+ cfgPath := filepath.Join(homeDir, ".config", "tailgrok", "config.json")
+
+ b, err := os.ReadFile(cfgPath)
+ if err != nil {
+ panic(err)
+ }
+ var cfg Config
+ if err := json.Unmarshal(b, &cfg); err != nil {
+ panic(err)
+ }
+
+ clientSecret, err := cfg.ClientSecret()
+ if err != nil {
+ panic(err)
+ }
+
+ oauthConfig := &clientcredentials.Config{
+ ClientID: cfg.ClientID,
+ ClientSecret: clientSecret,
+ TokenURL: "https://api.tailscale.com/api/v2/oauth/token",
+ }
+ client := oauthConfig.Client(context.Background())
+
+ payload, err := json.Marshal(M{
+ "description": "tailgrok",
+ "capabilities": M{
+ "devices": M{
+ "create": M{
+ "reusable": false,
+ "ephemeral": true,
+ "preauthorized": true,
+ "tags": []string{"tag:funnel", "tag:tailgrok"},
+ },
+ },
+ },
+ "expirySeconds": 5,
+ })
+ if err != nil {
+ panic(err)
+ }
+ authTokenReq, err := http.NewRequest(http.MethodPost, "https://api.tailscale.com/api/v2/tailnet/-/keys", bytes.NewReader(payload))
+ if err != nil {
+ panic(err)
+ }
+ resp, err := client.Do(authTokenReq)
+ if err != nil {
+ panic(err)
+ }
+ defer resp.Body.Close()
+ tsKey := struct {
+ Key string `json:"key"`
+ }{}
+ if err := json.NewDecoder(resp.Body).Decode(&tsKey); err != nil {
+ panic(err)
+ }
+ os.Setenv("TS_AUTHKEY", tsKey.Key)
+
+ tmp, err := os.MkdirTemp(os.TempDir(), "tailgrok-*")
+ if err != nil {
+ panic(err)
+ }
+ defer os.RemoveAll(tmp)
+
+ random := make([]byte, 8)
+ if _, err := rand.Read(random); err != nil {
+ panic(err)
+ }
+ id := hex.EncodeToString(random)
+
+ fmt.Printf("https://%s.serval-vibes.ts.net\n", id)
+ r := tailroute.Proxy(target, true)
+ r.Ephemeral = true
+ if err := r.Serve(id, tmp); err != nil {
+ panic(err)
+ }
+}
+
+type M = map[string]any
+
+type Config struct {
+ ClientID string `json:"clientID"`
+ ClientSecretFile string `json:"clientSecretFile"`
+}
+
+func (c Config) ClientSecret() (string, error) {
+ b, err := os.ReadFile(c.ClientSecretFile)
+ if err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(string(b)), nil
+}
|