netmaker/models/network.go

99 lines
5 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package models
import (
2021-05-01 11:07:25 +08:00
"time"
2021-03-26 00:17:52 +08:00
)
2021-10-09 03:07:12 +08:00
// Network Struct - contains info for a given unique network
2021-03-26 00:17:52 +08:00
//At some point, need to replace all instances of Name with something else like Identifier
type Network struct {
2021-10-09 03:07:12 +08:00
AddressRange string `json:"addressrange" bson:"addressrange" validate:"required,cidr"`
AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"regexp=^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$"`
NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=15"`
DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
AllowManualSignUp string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
IsLocal string `json:"islocal" bson:"islocal" validate:"checkyesorno"`
IsDualStack string `json:"isdualstack" bson:"isdualstack" validate:"checkyesorno"`
IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
2022-02-21 22:45:42 +08:00
IsPointToSite string `json:"ispointtosite" bson:"ispointtosite" validate:"checkyesorno"`
2021-10-09 03:07:12 +08:00
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
2021-05-03 19:13:12 +08:00
}
2021-07-24 06:24:34 +08:00
2021-10-09 03:07:12 +08:00
// SaveData - sensitive fields of a network that should be kept the same
2021-07-24 06:24:34 +08:00
type SaveData struct { // put sensitive fields here
NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
}
2021-10-09 03:07:12 +08:00
// Network.SetNodesLastModified - sets nodes last modified on network, depricated
2021-05-01 11:07:25 +08:00
func (network *Network) SetNodesLastModified() {
network.NodesLastModified = time.Now().Unix()
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// Network.SetNetworkLastModified - sets network last modified time
2021-05-01 11:07:25 +08:00
func (network *Network) SetNetworkLastModified() {
network.NetworkLastModified = time.Now().Unix()
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// Network.SetDefaults - sets default values for a network struct
2021-05-01 11:07:25 +08:00
func (network *Network) SetDefaults() {
2021-07-24 06:24:34 +08:00
if network.DefaultUDPHolePunch == "" {
2022-02-19 08:37:12 +08:00
network.DefaultUDPHolePunch = "no"
2021-07-24 06:24:34 +08:00
}
if network.IsLocal == "" {
network.IsLocal = "no"
}
2022-02-21 22:45:42 +08:00
if network.IsPointToSite == "" {
network.IsPointToSite = "no"
2021-05-01 11:07:25 +08:00
}
if network.DefaultInterface == "" {
if len(network.NetID) < 13 {
network.DefaultInterface = "nm-" + network.NetID
} else {
network.DefaultInterface = network.NetID
}
2021-05-01 11:07:25 +08:00
}
if network.DefaultListenPort == 0 {
network.DefaultListenPort = 51821
}
2021-07-22 06:55:19 +08:00
if network.NodeLimit == 0 {
network.NodeLimit = 999999999
}
2021-05-01 11:07:25 +08:00
if network.DefaultKeepalive == 0 {
network.DefaultKeepalive = 20
}
2021-07-24 06:24:34 +08:00
if network.AllowManualSignUp == "" {
network.AllowManualSignUp = "no"
}
if network.IsDualStack == "" {
network.IsDualStack = "no"
2021-05-01 11:07:25 +08:00
}
2021-07-24 06:24:34 +08:00
if network.IsDualStack == "yes" {
2021-05-26 00:48:04 +08:00
network.IsIPv6 = "yes"
network.IsIPv4 = "yes"
2021-07-24 06:24:34 +08:00
} else {
2021-07-22 06:55:19 +08:00
network.IsIPv6 = "no"
network.IsIPv4 = "yes"
2021-05-26 00:48:04 +08:00
}
2021-07-24 06:24:34 +08:00
2021-08-31 03:58:23 +08:00
if network.DefaultMTU == 0 {
network.DefaultMTU = 1280
2021-07-25 04:13:24 +08:00
}
if network.DefaultACL == "" {
network.DefaultACL = "yes"
}
2021-07-25 04:13:24 +08:00
}