-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (89 loc) · 2.18 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"text/template"
)
type Server struct {
IP string
AllowedIPs []string
Type string
Metadata map[string]string
PublicKey string
PrivateKey string
}
type WireguardConfig struct {
IP string
PrivateKey string
AllowedIPs string
}
type ServerRequest struct {
Type string
Metadata map[string]string
}
func getPublicIP() string {
res, err := http.Get("https://api.ipify.org/")
if err != nil {
return "unknown"
}
ip, err := ioutil.ReadAll(res.Body)
if err != nil {
return "unknown"
}
return string(ip)
}
func main() {
if len(os.Args) != 5 {
log.Fatal("usage: ./central-client [endpoint] [secret] [type] [template config file]")
}
templateString, err := ioutil.ReadFile(os.Args[4])
if err != nil {
log.Fatalf("failed to read config template\n%s", err.Error())
}
wgTemplate, err := template.New("wg").Parse(string(templateString))
if err != nil {
log.Fatalf("failed to parse config template\n%s", err.Error())
}
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
reqData, _ := json.Marshal(ServerRequest{
Type: os.Args[3],
Metadata: map[string]string {
"Hostname": hostname,
"IP": getPublicIP(),
},
})
req, _ := http.NewRequest("POST", os.Args[1], bytes.NewBuffer(reqData))
req.Header.Set("Authorization", "Bearer " + os.Args[2])
res, err := (&http.Client{}).Do(req)
if err != nil {
log.Fatalf("failed to send POST request to create server\n%s", err.Error())
}
resJson, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("failed to read server response\n%s", err.Error())
}
var server Server
err = json.Unmarshal(resJson, &server)
pretty, _ := json.MarshalIndent(server, "", "\t");
_, _ = fmt.Fprintf(os.Stderr, "server info: %s\n", string(pretty))
if err != nil {
log.Fatalf("failed to parse server response\n%s\n%s", resJson, err.Error())
}
err = wgTemplate.Execute(os.Stdout, WireguardConfig{
IP: server.IP,
PrivateKey: server.PrivateKey,
AllowedIPs: strings.Join(server.AllowedIPs, ", "),
})
if err != nil {
log.Fatalf("failed to template config\n%s", err.Error())
}
}