forked from fly-apps/tailscale-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (133 loc) · 3.91 KB
/
main.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
)
type keyResp struct {
Key string `json:"key"`
}
type devicesResp struct {
Devices []struct {
NodeKey string `json:"nodeKey"`
ID string `json:"id"`
} `json:"devices"`
}
func main() {
fmt.Println("tailscale-router: setting up")
tailnet := "-"
api_key := os.Getenv("TAILSCALE_API_TOKEN")
fmt.Println("tailscale-router: tailnet name", tailnet)
fmt.Println("tailscale-router: api key", api_key)
var jsonData = []byte(`{
"capabilities": {
"devices": {
"create": {
"reusable": true,
"ephemeral": true
}
}
}
}`)
fmt.Println("tailscale-router: creating auth key")
request, err := http.NewRequest("POST", fmt.Sprintf("https://api.tailscale.com/api/v2/tailnet/%s/keys", tailnet), bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
request.SetBasicAuth(api_key, "")
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
fmt.Println("ERROR: Create key")
panic(error)
}
defer response.Body.Close()
var out keyResp
err = json.NewDecoder(response.Body).Decode(&out)
if err != nil {
fmt.Println("ERROR: Decode key resp")
panic(error)
}
key := out.Key
fmt.Println("tailscale-router: key is", key)
fmt.Println("tailscale-router: grepping /etc/hosts to get fly-local-6pn")
output, err := exec.Command("grep", "fly-local-6pn", "/etc/hosts").Output()
if err != nil {
fmt.Println("ERROR: get subnet")
panic(err)
}
fmt.Println("tailscale-router: calculating subnet")
subnet := strings.Join(strings.Split(strings.TrimSuffix(string(output), "\n"), ":")[0:3], ":") + "::/48"
tailscale_binary_path := "/app/tailscale"
if runtime.GOOS == "darwin" {
output, err := exec.Command("bash", "-c", "ps -xo comm | grep MacOS/Tailscale").Output()
if err != nil {
panic(err)
}
tailscale_binary_path = strings.TrimSuffix(string(output), "\n")
}
fmt.Println("tailscale-router: running tailscale up")
upcmd := exec.Command("bash", "-c", fmt.Sprintf("%s up --authkey=%s --hostname=bcit-ltc-subnet-router --advertise-routes=%s", tailscale_binary_path, key, subnet))
err = upcmd.Run()
if err != nil {
panic(err)
}
fmt.Println("tailscale-router: getting PublicKey from tailscale status")
output, err = exec.Command("bash", "-c", fmt.Sprintf("%s status --json | jq -r .Self.PublicKey", tailscale_binary_path)).Output()
if err != nil {
panic(err)
}
nodeKey := strings.TrimSuffix(string(output), "\n")
fmt.Println("tailscale-router: getting all devices")
request, err = http.NewRequest("GET", fmt.Sprintf("https://api.tailscale.com/api/v2/tailnet/%s/devices", tailnet), nil)
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
request.SetBasicAuth(api_key, "")
client = &http.Client{}
response, error = client.Do(request)
if error != nil {
fmt.Println("ERROR: read devices")
panic(error)
}
defer response.Body.Close()
var devicesOut devicesResp
err = json.NewDecoder(response.Body).Decode(&devicesOut)
if err != nil {
fmt.Println("ERROR: Decode key resp")
panic(error)
}
selfID := ""
fmt.Println("tailscale-router: finding our ID")
for _, v := range devicesOut.Devices {
if v.NodeKey == nodeKey {
selfID = v.ID
break
}
}
jsonData = []byte(fmt.Sprintf(`{
"routes": ["%s"]
}`, subnet))
fmt.Println("tailscale-router: configuring routes")
request, err = http.NewRequest("POST", fmt.Sprintf("https://api.tailscale.com/api/v2/device/%s/routes", selfID), bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
request.SetBasicAuth(api_key, "")
client = &http.Client{}
response, error = client.Do(request)
if error != nil {
panic(error)
}
defer response.Body.Close()
fmt.Println("tailscale-router: fully configured")
os.Exit(0)
}