-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
322 lines (281 loc) · 10.5 KB
/
server.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ServerRequest represents a request to create or update a server
type ServerRequest struct {
Name string `json:"name"`
Network string `json:"network"`
NetworkWg string `json:"network_wg"`
NetworkMode string `json:"network_mode"`
NetworkStart *string `json:"network_start,omitempty"`
NetworkEnd *string `json:"network_end,omitempty"`
RestrictRoutes bool `json:"restrict_routes"`
Wg bool `json:"wg"`
Ipv6 bool `json:"ipv6"`
Ipv6Firewall bool `json:"ipv6_firewall"`
DynamicFirewall bool `json:"dynamic_firewall"`
DeviceAuth bool `json:"device_auth"`
BindAddress *string `json:"bind_address,omitempty"`
Protocol string `json:"protocol"`
Port int `json:"port"`
PortWg int `json:"port_wg"`
DhParamBits int `json:"dh_param_bits"`
Groups []string `json:"groups"`
MultiDevice bool `json:"multi_device"`
DnsServers []string `json:"dns_servers"`
SearchDomain string `json:"search_domain"`
InterClient bool `json:"inter_client"`
PingInterval int `json:"ping_interval"`
PingTimeout int `json:"ping_timeout"`
LinkPingInterval int `json:"link_ping_interval"`
LinkPingTimeout int `json:"link_ping_timeout"`
InactiveTimeout *int `json:"inactive_timeout,omitempty"`
SessionTimeout *int `json:"session_timeout,omitempty"`
AllowedDevices *string `json:"allowed_devices,omitempty"`
MaxClients int `json:"max_clients"`
MaxDevices int `json:"max_devices"`
ReplicaCount int `json:"replica_count"`
Vxlan bool `json:"vxlan"`
DnsMapping bool `json:"dns_mapping"`
RouteDns bool `json:"route_dns"`
Debug bool `json:"debug"`
SsoAuth bool `json:"sso_auth"`
OtpAuth bool `json:"otp_auth"`
LzoCompression bool `json:"lzo_compression"`
Cipher string `json:"cipher"`
Hash string `json:"hash"`
BlockOutsideDns bool `json:"block_outside_dns"`
JumboFrames bool `json:"jumbo_frames"`
PreConnectMsg string `json:"pre_connect_msg"`
Policy string `json:"policy"`
MssFix interface{} `json:"mss_fix"`
Multihome bool `json:"multihome"`
}
// ServerResponse represents a server response
type ServerResponse struct {
ID string `json:"id"`
Status string `json:"status"`
Uptime uint `json:"uptime"`
UsersOnline int `json:"users_online"`
DevicesOnline int `json:"devices_online"`
UserCount int `json:"user_count"`
Name string `json:"name"` // Starting here is common to `ServerRequest` Struct
Network string `json:"network"`
NetworkWg string `json:"network_wg"`
NetworkMode string `json:"network_mode"`
NetworkStart string `json:"network_start"`
NetworkEnd string `json:"network_end"`
RestrictRoutes bool `json:"restrict_routes"`
Wg bool `json:"wg"`
Ipv6 bool `json:"ipv6"`
Ipv6Firewall bool `json:"ipv6_firewall"`
DynamicFirewall bool `json:"dynamic_firewall"`
DeviceAuth bool `json:"device_auth"`
BindAddress string `json:"bind_address"`
Protocol string `json:"protocol"`
Port int `json:"port"`
PortWg int `json:"port_wg"`
DhParamBits int `json:"dh_param_bits"`
Groups []string `json:"groups"`
MultiDevice bool `json:"multi_device"`
DnsServers []string `json:"dns_servers"`
SearchDomain string `json:"search_domain"`
InterClient bool `json:"inter_client"`
PingInterval int `json:"ping_interval"`
PingTimeout int `json:"ping_timeout"`
LinkPingInterval int `json:"link_ping_interval"`
LinkPingTimeout int `json:"link_ping_timeout"`
InactiveTimeout int `json:"inactive_timeout"`
SessionTimeout int `json:"session_timeout"`
AllowedDevices string `json:"allowed_devices"`
MaxClients int `json:"max_clients"`
MaxDevices int `json:"max_devices"`
ReplicaCount int `json:"replica_count"`
Vxlan bool `json:"vxlan"`
DnsMapping bool `json:"dns_mapping"`
RouteDns bool `json:"route_dns"`
Debug bool `json:"debug"`
SsoAuth bool `json:"sso_auth"`
OtpAuth bool `json:"otp_auth"`
LzoCompression bool `json:"lzo_compression"`
Cipher string `json:"cipher"`
Hash string `json:"hash"`
BlockOutsideDns bool `json:"block_outside_dns"`
JumboFrames bool `json:"jumbo_frames"`
PreConnectMsg string `json:"pre_connect_msg"`
Policy string `json:"policy"`
MssFix interface{} `json:"mss_fix"`
Multihome bool `json:"multihome"`
}
// ServerGet retrieves a server or servers
func (c *Client) ServerGet(ctx context.Context, srvId ...string) ([]ServerResponse, error) {
// Construct the API path
path := "/server"
// Handle optional srvId argument
if len(srvId) > 0 {
path = fmt.Sprintf("%s/%s", path, srvId[0]) // Use the first element if srvId is provided
}
// Send an authenticated GET request to retrieve the server(s)
response, err := c.AuthRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerCreate creates a new server
func (c *Client) ServerCreate(ctx context.Context, newServer ServerRequest) ([]ServerResponse, error) {
// Marshal the ServerRequest into JSON data
serverData, err := json.Marshal(newServer)
if err != nil {
return nil, fmt.Errorf("failed to marshal server data: %w", err)
}
// Construct the API path
path := "/server"
// Send an authenticated POST request to create a new server
response, err := c.AuthRequest(ctx, http.MethodPost, path, serverData)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerUpdate updates an existing server
func (c *Client) ServerUpdate(ctx context.Context, srvId string, newServer ServerRequest) ([]ServerResponse, error) {
// Marshal the ServerRequest into JSON data
serverData, err := json.Marshal(newServer)
if err != nil {
return nil, fmt.Errorf("failed to marshal server data: %w", err)
}
// Construct the API path
path := fmt.Sprintf("/server/%s", srvId)
// Send an authenticated PUT request to update the server
response, err := c.AuthRequest(ctx, http.MethodPut, path, serverData)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerDelete deletes an existing server
func (c *Client) ServerDelete(ctx context.Context, srvId string) ([]ServerResponse, error) {
// Construct the API path
path := fmt.Sprintf("/server/%s", srvId)
// Send an authenticated DELETE request to delete the server
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerStop stops an existing server
func (c *Client) ServerStop(ctx context.Context, srvId string) ([]ServerResponse, error) {
// Construct the API path
path := fmt.Sprintf("/server/%s/operation/stop", srvId)
// Send an authenticated PUT request to stop the server
response, err := c.AuthRequest(ctx, http.MethodPut, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerStart starts an existing server
func (c *Client) ServerStart(ctx context.Context, srvId string) ([]ServerResponse, error) {
// Construct the API path
path := fmt.Sprintf("/server/%s/operation/start", srvId)
// Send an authenticated PUT request to stop the server
response, err := c.AuthRequest(ctx, http.MethodPut, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}
// ServerRestart restarts an existing server
func (c *Client) ServerRestart(ctx context.Context, srvId string) ([]ServerResponse, error) {
// Construct the API path
path := fmt.Sprintf("/server/%s/operation/restart", srvId)
// Send an authenticated PUT request to stop the server
response, err := c.AuthRequest(ctx, http.MethodPut, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerResponse
var servers []ServerResponse
if err := handleUnmarshal(body, &servers); err != nil {
return nil, err
}
// Return the slice of servers
return servers, nil
}