2022-12-22 00:56:29 +08:00
|
|
|
package models
|
|
|
|
|
2023-02-11 03:10:00 +08:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
2023-10-06 14:09:19 +08:00
|
|
|
"time"
|
2023-02-11 03:10:00 +08:00
|
|
|
)
|
2022-12-22 05:42:24 +08:00
|
|
|
|
|
|
|
// ApiHost - the host struct for API usage
|
|
|
|
type ApiHost struct {
|
2023-10-06 14:09:19 +08:00
|
|
|
ID string `json:"id"`
|
|
|
|
Verbosity int `json:"verbosity"`
|
|
|
|
FirewallInUse string `json:"firewallinuse"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
OS string `json:"os"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
IsStatic bool `json:"isstatic"`
|
|
|
|
ListenPort int `json:"listenport"`
|
|
|
|
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
|
|
|
|
MTU int `json:"mtu" yaml:"mtu"`
|
|
|
|
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
|
|
|
|
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
|
|
|
|
EndpointIP string `json:"endpointip" yaml:"endpointip"`
|
|
|
|
PublicKey string `json:"publickey"`
|
|
|
|
MacAddress string `json:"macaddress"`
|
|
|
|
Nodes []string `json:"nodes"`
|
|
|
|
IsDefault bool `json:"isdefault" yaml:"isdefault"`
|
|
|
|
NatType string `json:"nat_type" yaml:"nat_type"`
|
|
|
|
PersistentKeepalive int `json:"persistentkeepalive" yaml:"persistentkeepalive"`
|
2023-10-19 12:59:36 +08:00
|
|
|
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
|
2022-12-22 05:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
|
|
|
|
func (h *Host) ConvertNMHostToAPI() *ApiHost {
|
|
|
|
a := ApiHost{}
|
|
|
|
a.Debug = h.Debug
|
|
|
|
a.EndpointIP = h.EndpointIP.String()
|
|
|
|
a.FirewallInUse = h.FirewallInUse
|
|
|
|
a.ID = h.ID.String()
|
|
|
|
a.Interfaces = h.Interfaces
|
2023-01-13 17:58:00 +08:00
|
|
|
for i := range a.Interfaces {
|
|
|
|
a.Interfaces[i].AddressString = a.Interfaces[i].Address.String()
|
|
|
|
}
|
2023-01-18 03:23:13 +08:00
|
|
|
a.DefaultInterface = h.DefaultInterface
|
2022-12-22 05:42:24 +08:00
|
|
|
a.IsStatic = h.IsStatic
|
|
|
|
a.ListenPort = h.ListenPort
|
|
|
|
a.MTU = h.MTU
|
|
|
|
a.MacAddress = h.MacAddress.String()
|
|
|
|
a.Name = h.Name
|
|
|
|
a.OS = h.OS
|
|
|
|
a.Nodes = h.Nodes
|
2023-05-31 10:51:02 +08:00
|
|
|
a.WgPublicListenPort = h.WgPublicListenPort
|
2022-12-22 05:42:24 +08:00
|
|
|
a.PublicKey = h.PublicKey.String()
|
|
|
|
a.Verbosity = h.Verbosity
|
|
|
|
a.Version = h.Version
|
2022-12-23 02:39:29 +08:00
|
|
|
a.IsDefault = h.IsDefault
|
2023-06-28 22:53:11 +08:00
|
|
|
a.NatType = h.NatType
|
2023-10-06 14:09:19 +08:00
|
|
|
a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
|
2023-10-19 12:59:36 +08:00
|
|
|
a.AutoUpdate = h.AutoUpdate
|
2022-12-22 05:42:24 +08:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
|
|
|
|
// a Host struct
|
|
|
|
func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
|
|
|
|
h := Host{}
|
|
|
|
h.ID = currentHost.ID
|
|
|
|
h.HostPass = currentHost.HostPass
|
|
|
|
h.DaemonInstalled = currentHost.DaemonInstalled
|
2023-02-11 03:10:00 +08:00
|
|
|
if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
|
|
|
|
h.EndpointIP = currentHost.EndpointIP
|
|
|
|
} else {
|
|
|
|
h.EndpointIP = net.ParseIP(a.EndpointIP)
|
|
|
|
}
|
2022-12-22 05:42:24 +08:00
|
|
|
h.Debug = a.Debug
|
|
|
|
h.FirewallInUse = a.FirewallInUse
|
|
|
|
h.IPForwarding = currentHost.IPForwarding
|
|
|
|
h.Interface = currentHost.Interface
|
|
|
|
h.Interfaces = currentHost.Interfaces
|
2023-01-18 03:23:13 +08:00
|
|
|
h.DefaultInterface = currentHost.DefaultInterface
|
2022-12-22 05:42:24 +08:00
|
|
|
h.IsDocker = currentHost.IsDocker
|
|
|
|
h.IsK8S = currentHost.IsK8S
|
|
|
|
h.IsStatic = a.IsStatic
|
|
|
|
h.ListenPort = a.ListenPort
|
|
|
|
h.MTU = a.MTU
|
|
|
|
h.MacAddress = currentHost.MacAddress
|
|
|
|
h.PublicKey = currentHost.PublicKey
|
|
|
|
h.Name = a.Name
|
|
|
|
h.Version = currentHost.Version
|
|
|
|
h.Verbosity = a.Verbosity
|
|
|
|
h.Nodes = currentHost.Nodes
|
|
|
|
h.TrafficKeyPublic = currentHost.TrafficKeyPublic
|
|
|
|
h.OS = currentHost.OS
|
2022-12-23 02:39:29 +08:00
|
|
|
h.IsDefault = a.IsDefault
|
2023-03-24 03:37:11 +08:00
|
|
|
h.NatType = currentHost.NatType
|
|
|
|
h.TurnEndpoint = currentHost.TurnEndpoint
|
2023-10-06 14:09:19 +08:00
|
|
|
h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
|
2023-10-19 12:59:36 +08:00
|
|
|
h.AutoUpdate = a.AutoUpdate
|
2022-12-22 05:42:24 +08:00
|
|
|
return &h
|
2022-12-22 00:56:29 +08:00
|
|
|
}
|