-
Notifications
You must be signed in to change notification settings - Fork 12
/
vpn_net.go
64 lines (51 loc) · 1.01 KB
/
vpn_net.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
package shared
import (
"net"
"github.com/apparentlymart/go-cidr/cidr"
)
type VPNNet struct {
ip net.IP
ipNet *net.IPNet
str string
}
func (r *VPNNet) GetRawIP() net.IP {
return r.ip
}
func (r *VPNNet) GetServerIP() net.IP {
ip, _ := r.GetIPAt(1)
return ip
}
func (r *VPNNet) GetIPAt(idx int) (net.IP, error) {
ip, err := cidr.Host(r.ipNet, idx)
return ip, err
}
func (r *VPNNet) GetNetmask() string {
return IPNetGetNetMask(r.ipNet)
}
func (r *VPNNet) GetSubnet() *net.IPNet {
return r.ipNet
}
func (r *VPNNet) GetRaw() string {
return r.str
}
func (r *VPNNet) GetSize() int {
subnetOnes, _ := r.ipNet.Mask.Size()
return subnetOnes
}
func (r *VPNNet) GetClientSlots() uint64 {
return cidr.AddressCount(r.ipNet) - 3
}
func (r *VPNNet) Equals(other *VPNNet) bool {
return r.str == other.str
}
func ParseVPNNet(rNetStr string) (*VPNNet, error) {
ip, ipNet, err := net.ParseCIDR(rNetStr)
if err != nil {
return nil, err
}
return &VPNNet{
ip: ip,
ipNet: ipNet,
str: rNetStr,
}, nil
}