-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring.go
More file actions
179 lines (151 loc) · 3.63 KB
/
monitoring.go
File metadata and controls
179 lines (151 loc) · 3.63 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type Meta struct {
Monitored int64 `json:"monitored"`
WireguardConfig string `json:"wireguard_config"`
}
type State struct {
State int `json:"state"`
Meta Meta `json:"meta"`
}
type RequestData struct {
State State `json:"state"`
}
func getConfig(path string) (string, error) {
wgConfig, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read wireguard config: %w", err)
}
body := make(map[string]any)
if err := json.Unmarshal(wgConfig, &body); err != nil {
return "", err
}
clients, ok := body["clients"].(map[string]any)
if !ok || len(clients) == 0 {
return "", fmt.Errorf("no clients")
}
server, ok := body["server"].(map[string]any)
if !ok {
return "", fmt.Errorf("no server")
}
var client map[string]any
for key := range clients {
client = clients[key].(map[string]any)
break
}
wgPortNum, err := strconv.Atoi(wgPort)
if err != nil {
return "", fmt.Errorf("invalid port")
}
clientConfig := WireGuardConfig{
ClientPrivateKey: client["privateKey"].(string),
ClientAddress: client["address"].(string),
ClientListenPort: wgPortNum,
ClientDNS: "1.1.1.1",
ServerPublicKey: server["publicKey"].(string),
ServerPresharedKey: client["preSharedKey"].(string),
ServerAllowedIPs: []string{"0.0.0.0/0"},
ServerEndpoint: wgHost + ":" + wgPort,
}
return clientConfig.CreateConfig(), nil
}
func sendConfig(path string) error {
now := time.Now().UTC().Unix()
configString, err := getConfig(path)
if err != nil {
return err
}
data := RequestData{
State: State{
State: 3,
Meta: Meta{
Monitored: now,
WireguardConfig: configString,
},
},
}
return sendMonitoringRequest(data)
}
func sendMonitoring(path string) {
for {
created, err := ensureClient(path)
if err != nil {
log.Println("Error ensuring client from monitoring. Error: " + err.Error())
continue
}
if created {
restartChan <- struct{}{}
}
if err := sendConfig(path); err != nil {
log.Println("Error sending monitoring data:", err)
}
time.Sleep(157 * time.Second)
}
}
func sendMonitoringRequest(data RequestData) error {
jsonData, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
req, err := http.NewRequest("POST", "https://"+stateUrl, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+instanceToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("send config back error. Server returned: %s", string(body))
}
return nil
}
type WireGuardConfig struct {
ClientPrivateKey string
ClientAddress string
ClientListenPort int
ClientDNS string
ServerPublicKey string
ServerPresharedKey string
ServerAllowedIPs []string
ServerEndpoint string
}
func (wgConfig WireGuardConfig) CreateConfig() string {
allowedIPs := strings.Join(wgConfig.ServerAllowedIPs, ",")
return fmt.Sprintf(`[Interface]
PrivateKey = %s
Address = %s
ListenPort = %d
DNS = %s
[Peer]
PublicKey = %s
PresharedKey = %s
AllowedIPs = %s
Endpoint = %s
`,
wgConfig.ClientPrivateKey,
wgConfig.ClientAddress,
wgConfig.ClientListenPort,
wgConfig.ClientDNS,
wgConfig.ServerPublicKey,
wgConfig.ServerPresharedKey,
allowedIPs,
wgConfig.ServerEndpoint,
)
}