From fa40a5929d17c854831eb8735eb73081e761f0cd Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 23 Mar 2023 15:37:11 -0400 Subject: [PATCH] added turn ep + nat type to host model --- logic/hosts.go | 5 +++++ models/api_host.go | 3 +++ models/host.go | 16 ++++++++++++++++ mq/handlers.go | 24 +++++++++++++++--------- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/logic/hosts.go b/logic/hosts.go index 19feda1a..df7a0cf1 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -178,6 +178,11 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) if newHost.Name != "" { currHost.Name = newHost.Name } + if len(newHost.NatType) > 0 && newHost.NatType != currHost.NatType { + currHost.NatType = newHost.NatType + logger.Log(0, "updated host nat type", newHost.Name, newHost.NatType) + sendPeerUpdate = true + } return } diff --git a/models/api_host.go b/models/api_host.go index 06369360..b912f36f 100644 --- a/models/api_host.go +++ b/models/api_host.go @@ -33,6 +33,7 @@ type ApiHost struct { RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"` IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"` RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"` + NatType string `json:"nat_type" bson:"nat_type" yaml:"nat_type"` } // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host @@ -112,6 +113,8 @@ func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host { h.IsRelayed = a.IsRelayed h.ProxyEnabled = a.ProxyEnabled h.IsDefault = a.IsDefault + h.NatType = currentHost.NatType + h.TurnEndpoint = currentHost.TurnEndpoint return &h } diff --git a/models/host.go b/models/host.go index 13e44b50..bdaa8e98 100644 --- a/models/host.go +++ b/models/host.go @@ -2,6 +2,7 @@ package models import ( "net" + "net/netip" "github.com/google/uuid" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -22,6 +23,19 @@ var OS_Types = struct { IoT: "iot", } +// NAT_Types - the type of NAT in which a HOST currently resides (simplified) +var NAT_Types = struct { + Public string + Symmetric string + Asymmetric string + Double string +}{ + Public: "public", + Symmetric: "symmetric", + Asymmetric: "asymmetric", + Double: "double", +} + // WIREGUARD_INTERFACE name of wireguard interface const WIREGUARD_INTERFACE = "netmaker" @@ -60,6 +74,8 @@ type Host struct { IsK8S bool `json:"isk8s" yaml:"isk8s"` IsStatic bool `json:"isstatic" yaml:"isstatic"` IsDefault bool `json:"isdefault" yaml:"isdefault"` + NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"` + TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"` } // FormatBool converts a boolean to a [yes|no] string diff --git a/mq/handlers.go b/mq/handlers.go index 51d3d5c5..22c5e62a 100644 --- a/mq/handlers.go +++ b/mq/handlers.go @@ -396,16 +396,22 @@ func handleHostCheckin(h, currentHost *models.Host) bool { for i := range h.Interfaces { h.Interfaces[i].AddressString = h.Interfaces[i].Address.String() } - ifaceDelta := len(h.Interfaces) != len(currentHost.Interfaces) || !h.EndpointIP.Equal(currentHost.EndpointIP) - currentHost.EndpointIP = h.EndpointIP - currentHost.Interfaces = h.Interfaces - currentHost.DefaultInterface = h.DefaultInterface - if err := logic.UpsertHost(currentHost); err != nil { - logger.Log(0, "failed to update host after check-in", h.Name, h.ID.String(), err.Error()) - return false + ifaceDelta := len(h.Interfaces) != len(currentHost.Interfaces) || + !h.EndpointIP.Equal(currentHost.EndpointIP) || + (len(h.NatType) > 0 && h.NatType != currentHost.NatType) || + h.DefaultInterface != currentHost.DefaultInterface + if ifaceDelta { // only save if something changes + currentHost.EndpointIP = h.EndpointIP + currentHost.Interfaces = h.Interfaces + currentHost.DefaultInterface = h.DefaultInterface + currentHost.NatType = h.NatType + if err := logic.UpsertHost(currentHost); err != nil { + logger.Log(0, "failed to update host after check-in", h.Name, h.ID.String(), err.Error()) + return false + } + logger.Log(1, "updated host after check-in", currentHost.Name, currentHost.ID.String()) } - logger.Log(0, "ping processed for host", h.Name, h.ID.String()) + logger.Log(2, "check-in processed for host", h.Name, h.ID.String()) return ifaceDelta - }