added turn ep + nat type to host model

This commit is contained in:
0xdcarns 2023-03-23 15:37:11 -04:00
parent b1b4fb821e
commit fa40a5929d
4 changed files with 39 additions and 9 deletions

View file

@ -178,6 +178,11 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
if newHost.Name != "" { if newHost.Name != "" {
currHost.Name = 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 return
} }

View file

@ -33,6 +33,7 @@ type ApiHost struct {
RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"` RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"` IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"` 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 // 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.IsRelayed = a.IsRelayed
h.ProxyEnabled = a.ProxyEnabled h.ProxyEnabled = a.ProxyEnabled
h.IsDefault = a.IsDefault h.IsDefault = a.IsDefault
h.NatType = currentHost.NatType
h.TurnEndpoint = currentHost.TurnEndpoint
return &h return &h
} }

View file

@ -2,6 +2,7 @@ package models
import ( import (
"net" "net"
"net/netip"
"github.com/google/uuid" "github.com/google/uuid"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
@ -22,6 +23,19 @@ var OS_Types = struct {
IoT: "iot", 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 // WIREGUARD_INTERFACE name of wireguard interface
const WIREGUARD_INTERFACE = "netmaker" const WIREGUARD_INTERFACE = "netmaker"
@ -60,6 +74,8 @@ type Host struct {
IsK8S bool `json:"isk8s" yaml:"isk8s"` IsK8S bool `json:"isk8s" yaml:"isk8s"`
IsStatic bool `json:"isstatic" yaml:"isstatic"` IsStatic bool `json:"isstatic" yaml:"isstatic"`
IsDefault bool `json:"isdefault" yaml:"isdefault"` 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 // FormatBool converts a boolean to a [yes|no] string

View file

@ -396,16 +396,22 @@ func handleHostCheckin(h, currentHost *models.Host) bool {
for i := range h.Interfaces { for i := range h.Interfaces {
h.Interfaces[i].AddressString = h.Interfaces[i].Address.String() h.Interfaces[i].AddressString = h.Interfaces[i].Address.String()
} }
ifaceDelta := len(h.Interfaces) != len(currentHost.Interfaces) || !h.EndpointIP.Equal(currentHost.EndpointIP) ifaceDelta := len(h.Interfaces) != len(currentHost.Interfaces) ||
currentHost.EndpointIP = h.EndpointIP !h.EndpointIP.Equal(currentHost.EndpointIP) ||
currentHost.Interfaces = h.Interfaces (len(h.NatType) > 0 && h.NatType != currentHost.NatType) ||
currentHost.DefaultInterface = h.DefaultInterface h.DefaultInterface != currentHost.DefaultInterface
if err := logic.UpsertHost(currentHost); err != nil { if ifaceDelta { // only save if something changes
logger.Log(0, "failed to update host after check-in", h.Name, h.ID.String(), err.Error()) currentHost.EndpointIP = h.EndpointIP
return false 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 return ifaceDelta
} }