-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
218 lines (189 loc) · 3.99 KB
/
models.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
package plexus
import (
"encoding/base64"
"encoding/json"
"log/slog"
"net"
"time"
)
// nats topics
const (
DeletePeer = ".deletePeer"
AddRouter = ".addRouter"
AddPeer = ".addPeer"
UpdatePeer = ".updatePeer"
UpdateNetworkPeer = ".updateNetworkPeer"
UpdateListenPorts = ".updateListenPorts"
AddRelay = ".addRelay"
DeleteRelay = ".deleteRelay"
DeleteRouter = ".deleteRouter"
DeleteNetwork = ".deleteNetwork"
JoinNetwork = ".join"
LeaveNetwork = ".leaveNetwork"
LeaveServer = ".leaveServer"
LogLevel = ".loglevel"
Ping = ".ping"
Register = ".register"
Reload = ".reload"
Reset = ".reset"
SetPrivateEndpoint = ".privateEndpoint"
Status = ".status"
Version = ".version"
Checkin = ".checkin"
SendListenPorts = ".listenPorts"
Update = "update."
Networks = "networks."
)
type ErrorResponse struct {
Message string
}
type MessageResponse struct {
Message string
}
type User struct {
Username string `json:"username" form:"username"`
Password string `json:"password" form:"password"`
IsAdmin bool
Updated time.Time
}
type NatsUser struct {
Kind string
Name string
Subscribe []string
Publish []string
}
type Network struct {
Name string `form:"name"`
Net net.IPNet
AddressString string `form:"addressstring"`
Peers []NetworkPeer
}
type NetworkPeer struct {
WGPublicKey string
HostName string
Address net.IPNet
ListenPort int
PublicListenPort int
Endpoint net.IP
PrivateEndpoint net.IP
UsePrivateEndpoint bool
NatsConnected bool
Connectivity float64
IsRelay bool
RelayedPeers []string
IsRelayed bool
IsSubnetRouter bool
Subnet net.IPNet
UseNat bool
UseVirtSubnet bool
VirtSubnet net.IPNet
}
type Key struct {
Name string `form:"name"`
Value string
Usage int `form:"usage"`
Expires time.Time
DispExp string `form:"expires"`
}
type KeyValue struct {
URL string
Seed string
KeyName string
}
type Peer struct {
WGPublicKey string
PubNkey string
Version string
Name string
OS string
Endpoint net.IP
Updated time.Time
NatsConnected bool
}
type ServerRegisterRequest struct {
KeyName string
Peer
}
type JoinRequest struct {
Network string
ListenPort int
PublicListenPort int
Peer
}
type ServerJoinRequest struct {
Network Network
}
type JoinResponse struct {
Message string
Network Network
}
type LevelRequest struct {
Level string
}
type LeaveRequest struct {
Network string
}
type VersionResponse struct {
Server string
Agent string
}
type NetworkUpdate struct {
Action string
Peer NetworkPeer
}
type DeviceUpdate struct {
Action string
Server string
Network Network
}
type RegisterRequest struct {
Token string
}
type ConnectivityData struct {
Network string
Connectivity float64
}
type NetworkResponse struct {
Message string
Networks []Network
}
type CheckinData struct {
ID string
Version string
ListenPort int
PublicListenPort int
Endpoint net.IP
PrivateEndpoints []PrivateEndpoint
Connections []ConnectivityData
}
type PingResponse struct {
Message string
}
type ResetRequest struct {
Network string
}
type ListenPortRequest struct {
Network string
}
type ListenPortResponse struct {
Message string
ListenPort int
PublicListenPort int
}
type PrivateEndpoint struct {
IP string
Network string
}
func DecodeToken(token string) (KeyValue, error) {
kv := KeyValue{}
data, err := base64.StdEncoding.DecodeString(token)
if err != nil {
slog.Error("base64 decode", "error", err)
return kv, err
}
if err := json.Unmarshal(data, &kv); err != nil {
slog.Error("token unmarshal", "error", err)
return kv, err
}
return kv, nil
}