mirror of
https://github.com/gravitl/netmaker.git
synced 2026-10-12 02:15:06 +00:00
205 lines
8.0 KiB
Go
205 lines
8.0 KiB
Go
package models
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gravitl/netmaker/schema"
|
|
)
|
|
|
|
// ApiHost - the host struct for API usage
|
|
type ApiHost struct {
|
|
ID string `json:"id"`
|
|
Verbosity int `json:"verbosity"`
|
|
FirewallInUse string `json:"firewallinuse"`
|
|
Version string `json:"version"`
|
|
Name string `json:"name"`
|
|
OS string `json:"os"`
|
|
OSFamily string `json:"os_family" yaml:"os_family"`
|
|
OSVersion string `json:"os_version" yaml:"os_version"`
|
|
KernelVersion string `json:"kernel_version" yaml:"kernel_version"`
|
|
Debug bool `json:"debug"`
|
|
IsStaticPort bool `json:"isstaticport"`
|
|
IsStatic bool `json:"isstatic"`
|
|
ListenPort int `json:"listenport"`
|
|
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
|
|
TcpProxyEnabled bool `json:"tcp_proxy_enabled"`
|
|
TcpProxyListenPort int `json:"tcp_proxy_listen_port"`
|
|
TcpProxyTLSMode string `json:"tcp_proxy_tls_mode"`
|
|
TcpProxyListenAddr string `json:"tcp_proxy_listen_addr,omitempty"`
|
|
TcpProxyPublicHostname string `json:"tcp_proxy_public_hostname,omitempty"`
|
|
TcpProxyCertFingerprint string `json:"tcp_proxy_cert_fingerprint,omitempty"`
|
|
MTU int `json:"mtu" yaml:"mtu"`
|
|
Interfaces []ApiIface `json:"interfaces" yaml:"interfaces"`
|
|
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
|
|
EndpointIP string `json:"endpointip" yaml:"endpointip"`
|
|
EndpointIPv6 string `json:"endpointipv6" yaml:"endpointipv6"`
|
|
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"`
|
|
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
|
|
DNS string `json:"dns" yaml:"dns"`
|
|
EnableFlowLogs bool `json:"enable_flow_logs" yaml:"enable_flow_logs"`
|
|
ManageSSH bool `json:"manage_ssh" yaml:"manage_ssh"`
|
|
Location string `json:"location"`
|
|
CountryCode string `json:"country_code"`
|
|
// Device-matching identifiers reported by netclient; read-only via the API.
|
|
EntraDeviceID string `json:"entra_device_id" yaml:"entra_device_id"`
|
|
SerialNumber string `json:"serial_number" yaml:"serial_number"`
|
|
HardwareUUID string `json:"hardware_uuid" yaml:"hardware_uuid"`
|
|
}
|
|
|
|
// ApiIface - the interface struct for API usage
|
|
// The original Iface struct contains a net.Address, which does not get marshalled correctly
|
|
type ApiIface struct {
|
|
Name string `json:"name"`
|
|
AddressString string `json:"addressString"`
|
|
}
|
|
|
|
// NewApiHostFromSchemaHost - converts a Netmaker host to an API editable host
|
|
func NewApiHostFromSchemaHost(h *schema.Host) *ApiHost {
|
|
a := ApiHost{}
|
|
a.Debug = h.Debug
|
|
a.EndpointIP = h.EndpointIP.String()
|
|
if a.EndpointIP == "<nil>" {
|
|
a.EndpointIP = ""
|
|
}
|
|
a.EndpointIPv6 = h.EndpointIPv6.String()
|
|
if a.EndpointIPv6 == "<nil>" {
|
|
a.EndpointIPv6 = ""
|
|
}
|
|
a.FirewallInUse = h.FirewallInUse
|
|
a.ID = h.ID.String()
|
|
a.Interfaces = make([]ApiIface, len(h.Interfaces))
|
|
for i := range a.Interfaces {
|
|
a.Interfaces[i] = ApiIface{
|
|
Name: h.Interfaces[i].Name,
|
|
AddressString: h.Interfaces[i].Address.String(),
|
|
}
|
|
}
|
|
a.DefaultInterface = h.DefaultInterface
|
|
a.IsStaticPort = h.IsStaticPort
|
|
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.OSFamily = h.OSFamily
|
|
a.KernelVersion = h.KernelVersion
|
|
a.Nodes = h.Nodes
|
|
a.WgPublicListenPort = h.WgPublicListenPort
|
|
a.TcpProxyEnabled = h.TcpProxyEnabled
|
|
a.TcpProxyListenPort = h.TcpProxyListenPort
|
|
a.TcpProxyTLSMode = h.TcpProxyTLSMode
|
|
a.TcpProxyListenAddr = h.TcpProxyListenAddr
|
|
a.TcpProxyPublicHostname = h.TcpProxyPublicHostname
|
|
a.TcpProxyCertFingerprint = h.TcpProxyCertFingerprint
|
|
a.PublicKey = h.PublicKey.String()
|
|
a.Verbosity = h.Verbosity
|
|
a.Version = h.Version
|
|
a.IsDefault = h.IsDefault
|
|
a.NatType = h.NatType
|
|
a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
|
|
a.AutoUpdate = h.AutoUpdate
|
|
a.DNS = h.DNS
|
|
a.EnableFlowLogs = h.EnableFlowLogs
|
|
a.ManageSSH = h.ManageSSH
|
|
a.Location = h.Location
|
|
a.CountryCode = h.CountryCode
|
|
a.EntraDeviceID = h.EntraDeviceID
|
|
a.SerialNumber = h.SerialNumber
|
|
a.HardwareUUID = h.HardwareUUID
|
|
return &a
|
|
}
|
|
|
|
// APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
|
|
// a Host struct
|
|
func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *schema.Host) *schema.Host {
|
|
h := schema.Host{}
|
|
h.ID = currentHost.ID
|
|
h.TenantID = currentHost.TenantID
|
|
h.HostPass = currentHost.HostPass
|
|
h.DaemonInstalled = currentHost.DaemonInstalled
|
|
if len(a.EndpointIP) == 0 || strings.Contains(a.EndpointIP, "nil") {
|
|
h.EndpointIP = currentHost.EndpointIP
|
|
} else {
|
|
h.EndpointIP = net.ParseIP(a.EndpointIP)
|
|
}
|
|
if len(a.EndpointIPv6) == 0 || strings.Contains(a.EndpointIPv6, "nil") {
|
|
h.EndpointIPv6 = currentHost.EndpointIPv6
|
|
} else {
|
|
h.EndpointIPv6 = net.ParseIP(a.EndpointIPv6)
|
|
}
|
|
h.Debug = a.Debug
|
|
h.FirewallInUse = a.FirewallInUse
|
|
h.IPForwarding = currentHost.IPForwarding
|
|
h.Interface = currentHost.Interface
|
|
h.Interfaces = currentHost.Interfaces
|
|
h.DefaultInterface = currentHost.DefaultInterface
|
|
h.IsDocker = currentHost.IsDocker
|
|
h.IsK8S = currentHost.IsK8S
|
|
h.IsStaticPort = a.IsStaticPort
|
|
h.IsStatic = a.IsStatic
|
|
h.ListenPort = a.ListenPort
|
|
// Public listen port is client/STUN-owned. On admin updates:
|
|
// - static port → public port tracks ListenPort
|
|
// - listen port changed or just switched to dynamic → clear so peers fall
|
|
// back to ListenPort until netclient reports a fresh STUN mapping
|
|
// - otherwise preserve the last known STUN port
|
|
if a.IsStaticPort {
|
|
h.WgPublicListenPort = a.ListenPort
|
|
} else if a.ListenPort != currentHost.ListenPort || currentHost.IsStaticPort {
|
|
h.WgPublicListenPort = 0
|
|
} else {
|
|
h.WgPublicListenPort = currentHost.WgPublicListenPort
|
|
}
|
|
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
|
|
h.OSFamily = currentHost.OSFamily
|
|
h.KernelVersion = currentHost.KernelVersion
|
|
h.IsDefault = a.IsDefault
|
|
h.NatType = currentHost.NatType
|
|
h.TurnEndpoint = currentHost.TurnEndpoint
|
|
h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
|
|
h.AutoUpdate = a.AutoUpdate
|
|
h.DNS = strings.ToLower(a.DNS)
|
|
h.EnableFlowLogs = a.EnableFlowLogs
|
|
h.ManageSSH = a.ManageSSH
|
|
h.TcpProxyEnabled = a.TcpProxyEnabled
|
|
h.TcpProxyListenPort = a.TcpProxyListenPort
|
|
if mode, err := schema.NormaliseTcpProxyTLSMode(a.TcpProxyTLSMode); err == nil {
|
|
h.TcpProxyTLSMode = mode
|
|
} else {
|
|
h.TcpProxyTLSMode = currentHost.TcpProxyTLSMode
|
|
}
|
|
h.TcpProxyListenAddr = a.TcpProxyListenAddr
|
|
if hn, err := schema.NormaliseTcpProxyPublicHostname(a.TcpProxyPublicHostname); err == nil {
|
|
h.TcpProxyPublicHostname = hn
|
|
} else {
|
|
h.TcpProxyPublicHostname = currentHost.TcpProxyPublicHostname
|
|
}
|
|
if h.TcpProxyTLSMode == schema.TcpProxyTLSModeProxy {
|
|
h.TcpProxyCertFingerprint = ""
|
|
} else {
|
|
h.TcpProxyCertFingerprint = currentHost.TcpProxyCertFingerprint
|
|
}
|
|
h.Location = currentHost.Location
|
|
h.CountryCode = currentHost.CountryCode
|
|
h.EntraDeviceID = currentHost.EntraDeviceID
|
|
h.SerialNumber = currentHost.SerialNumber
|
|
h.HardwareUUID = currentHost.HardwareUUID
|
|
return &h
|
|
}
|