-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
149 lines (130 loc) · 4.41 KB
/
Copy pathserver.go
File metadata and controls
149 lines (130 loc) · 4.41 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
package model
import (
"fmt"
"github.com/NetSepio/erebrus/util"
"golang.zx2c4.com/wireguard/wgctrl"
// "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// Server structure
/*type Server struct {
Address []string `json:"address"`
ListenPort int64 `json:"listenPort"`
Mtu int64 `json:"mtu"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
Endpoint string `json:"endpoint"`
PersistentKeepalive int64 `json:"persistentKeepalive"`
DNS []string `json:"dns"`
AllowedIPs []string `json:"allowedips"`
PreUp string `json:"preUp"`
PostUp string `json:"postUp"`
PreDown string `json:"preDown"`
PostDown string `json:"postDown"`
UpdatedBy string `json:"updatedBy"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}*/
/*type Status struct {
Version string `json:"version"`
HostName string`json:"hostname"`
Domain string `json:"domain"`
PublicIP string `json:"publicIP"`
gRPCPort string`json:"grpcport"`
PrivateIP string`json:"private"`
HttpPort string`json:"httpport"`
Region string
VPNPort string
}*/
// WireGuardServer supports both Kernel and Userland implementations of WireGuard.
type WireGuardServer struct {
wg *wgctrl.Client
deviceName string
}
// NewServer initializes a Server with a WireGuard client.
func NewServer(wg *wgctrl.Client, deviceName string) (*WireGuardServer, error) {
return &WireGuardServer{wg: wg, deviceName: deviceName}, nil
}
// ListPeers retrieves information about all Peers known to the current
// WireGuard interface, including allowed IP addresses and usage stats,
// optionally with pagination.
// func (wgServer *WireGuardServer) ListPeers(ctx context.Context, req *Client) (*Client, error) {
// if err := validateListPeersRequest(req); err != nil {
// return nil, err
// }
// dev, err := wgServer.wg.Device(s.deviceName)
// if err != nil {
// return nil, fmt.Errorf("could not get WireGuard device: %w", err)
// }
// var peers []*client.Peer
// for _, peer := range dev.Peers {
// peers = append(peers, peer2rpc(peer))
// }
// //TODO(jc): pagination
// return &Client{
// Peers: peers,
// }, nil
// }
// func peer2rpc(peer wgtypes.Peer) *client.Peer {
// var keepAlive string
// if peer.PersistentKeepaliveInterval > 0 {
// keepAlive = peer.PersistentKeepaliveInterval.String()
// }
// var allowedIPs []string
// for _, allowedIP := range peer.AllowedIPs {
// allowedIPs = append(allowedIPs, allowedIP.String())
// }
// return &client.Peer{
// PublicKey: peer.PublicKey.String(),
// HasPresharedKey: peer.PresharedKey != wgtypes.Key{},
// Endpoint: peer.Endpoint.String(),
// PersistentKeepAlive: keepAlive,
// LastHandshake: peer.LastHandshakeTime,
// ReceiveBytes: peer.ReceiveBytes,
// TransmitBytes: peer.TransmitBytes,
// AllowedIPs: allowedIPs,
// ProtocolVersion: peer.ProtocolVersion,
// }
// }
// IsValid check if model is valid
func (a Server) IsValid() []error {
errs := make([]error, 0)
// check if the address empty
if len(a.Address) == 0 {
errs = append(errs, fmt.Errorf("address is required"))
}
// check if the address are valid
for _, address := range a.Address {
if !util.IsValidCidr(address) {
errs = append(errs, fmt.Errorf("address %s is invalid", address))
}
}
// check if the listenPort is valid
if a.ListenPort < 0 || a.ListenPort > 65535 {
errs = append(errs, fmt.Errorf("listenPort %d is invalid", a.ListenPort))
}
// check if the endpoint empty
if a.Endpoint == "" {
errs = append(errs, fmt.Errorf("endpoint is required"))
}
// check if the persistentKeepalive is valid
if a.PersistentKeepalive < 0 {
errs = append(errs, fmt.Errorf("persistentKeepalive %d is invalid", a.PersistentKeepalive))
}
// check if the mtu is valid
if a.Mtu < 0 {
errs = append(errs, fmt.Errorf("MTU %d is invalid", a.PersistentKeepalive))
}
// check if the address are valid
for _, dns := range a.DNS {
if !util.IsValidIP(dns) {
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
}
}
// check if the allowedIPs are valid
for _, allowedIP := range a.AllowedIPs {
if !util.IsValidCidr(allowedIP) {
errs = append(errs, fmt.Errorf("allowedIP %s is invalid", allowedIP))
}
}
return errs
}