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 != "" {
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
}

View file

@ -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
}

View file

@ -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

View file

@ -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
}