NET-186: Wg public listen port (#2344)

* fetch public listen of wg if present

* check if wg pub listen port has been changed on host update

* wg public port to host api model for visibility

* rm comment
This commit is contained in:
Abhishek K
2023-05-31 08:21:02 +05:30
committed by GitHub
parent 5271a2084b
commit 47edf65b1f
4 changed files with 83 additions and 63 deletions

View File

@@ -173,6 +173,10 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
currHost.ListenPort = newHost.ListenPort currHost.ListenPort = newHost.ListenPort
sendPeerUpdate = true sendPeerUpdate = true
} }
if newHost.WgPublicListenPort != 0 && currHost.WgPublicListenPort != newHost.WgPublicListenPort {
currHost.WgPublicListenPort = newHost.WgPublicListenPort
sendPeerUpdate = true
}
if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort { if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
currHost.ProxyListenPort = newHost.ProxyListenPort currHost.ProxyListenPort = newHost.ProxyListenPort
sendPeerUpdate = true sendPeerUpdate = true

View File

@@ -220,11 +220,12 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
} }
peerConfig.Endpoint = &net.UDPAddr{ peerConfig.Endpoint = &net.UDPAddr{
IP: peerHost.EndpointIP, IP: peerHost.EndpointIP,
Port: peerHost.ListenPort, Port: getPeerWgListenPort(peerHost),
} }
if uselocal { if uselocal {
peerConfig.Endpoint.IP = peer.LocalAddress.IP peerConfig.Endpoint.IP = peer.LocalAddress.IP
peerConfig.Endpoint.Port = peerHost.ListenPort
} }
allowedips := GetAllowedIPs(&node, &peer, nil) allowedips := GetAllowedIPs(&node, &peer, nil)
if peer.IsIngressGateway { if peer.IsIngressGateway {
@@ -425,9 +426,21 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
return hostPeerUpdate, nil return hostPeerUpdate, nil
} }
// getPeerWgListenPort - fetches the wg listen port for the host
func getPeerWgListenPort(host *models.Host) int {
peerPort := host.ListenPort
if host.WgPublicListenPort != 0 {
peerPort = host.WgPublicListenPort
}
return peerPort
}
// GetPeerListenPort - given a host, retrieve it's appropriate listening port // GetPeerListenPort - given a host, retrieve it's appropriate listening port
func GetPeerListenPort(host *models.Host) int { func GetPeerListenPort(host *models.Host) int {
peerPort := host.ListenPort peerPort := host.ListenPort
if host.WgPublicListenPort != 0 {
peerPort = host.WgPublicListenPort
}
if host.ProxyEnabled { if host.ProxyEnabled {
if host.PublicListenPort != 0 { if host.PublicListenPort != 0 {
peerPort = host.PublicListenPort peerPort = host.PublicListenPort

View File

@@ -7,32 +7,33 @@ import (
// ApiHost - the host struct for API usage // ApiHost - the host struct for API usage
type ApiHost struct { type ApiHost struct {
ID string `json:"id"` ID string `json:"id"`
Verbosity int `json:"verbosity"` Verbosity int `json:"verbosity"`
FirewallInUse string `json:"firewallinuse"` FirewallInUse string `json:"firewallinuse"`
Version string `json:"version"` Version string `json:"version"`
Name string `json:"name"` Name string `json:"name"`
OS string `json:"os"` OS string `json:"os"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
IsStatic bool `json:"isstatic"` IsStatic bool `json:"isstatic"`
ListenPort int `json:"listenport"` ListenPort int `json:"listenport"`
LocalListenPort int `json:"locallistenport"` LocalListenPort int `json:"locallistenport"`
ProxyListenPort int `json:"proxy_listen_port"` ProxyListenPort int `json:"proxy_listen_port"`
PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"` PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"` WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"` MTU int `json:"mtu" yaml:"mtu"`
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"` Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
EndpointIP string `json:"endpointip" yaml:"endpointip"` DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
PublicKey string `json:"publickey"` EndpointIP string `json:"endpointip" yaml:"endpointip"`
MacAddress string `json:"macaddress"` PublicKey string `json:"publickey"`
InternetGateway string `json:"internetgateway"` MacAddress string `json:"macaddress"`
Nodes []string `json:"nodes"` InternetGateway string `json:"internetgateway"`
ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"` Nodes []string `json:"nodes"`
IsDefault bool `json:"isdefault" yaml:"isdefault"` ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"` IsDefault bool `json:"isdefault" yaml:"isdefault"`
RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"` IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"` RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"` IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
} }
// Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host // Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
@@ -60,6 +61,7 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
a.Nodes = h.Nodes a.Nodes = h.Nodes
a.ProxyEnabled = h.ProxyEnabled a.ProxyEnabled = h.ProxyEnabled
a.PublicListenPort = h.PublicListenPort a.PublicListenPort = h.PublicListenPort
a.WgPublicListenPort = h.WgPublicListenPort
a.ProxyListenPort = h.ProxyListenPort a.ProxyListenPort = h.ProxyListenPort
a.PublicKey = h.PublicKey.String() a.PublicKey = h.PublicKey.String()
a.Verbosity = h.Verbosity a.Verbosity = h.Verbosity

View File

@@ -41,42 +41,43 @@ const WIREGUARD_INTERFACE = "netmaker"
// Host - represents a host on the network // Host - represents a host on the network
type Host struct { type Host struct {
ID uuid.UUID `json:"id" yaml:"id"` ID uuid.UUID `json:"id" yaml:"id"`
Verbosity int `json:"verbosity" yaml:"verbosity"` Verbosity int `json:"verbosity" yaml:"verbosity"`
FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"` FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
Version string `json:"version" yaml:"version"` Version string `json:"version" yaml:"version"`
IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"` IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"` DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"` AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
HostPass string `json:"hostpass" yaml:"hostpass"` HostPass string `json:"hostpass" yaml:"hostpass"`
Name string `json:"name" yaml:"name"` Name string `json:"name" yaml:"name"`
OS string `json:"os" yaml:"os"` OS string `json:"os" yaml:"os"`
Interface string `json:"interface" yaml:"interface"` Interface string `json:"interface" yaml:"interface"`
Debug bool `json:"debug" yaml:"debug"` Debug bool `json:"debug" yaml:"debug"`
ListenPort int `json:"listenport" yaml:"listenport"` ListenPort int `json:"listenport" yaml:"listenport"`
PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"` PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
ProxyListenPort int `json:"proxy_listen_port" yaml:"proxy_listen_port"` WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"` ProxyListenPort int `json:"proxy_listen_port" yaml:"proxy_listen_port"`
PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"` MTU int `json:"mtu" yaml:"mtu"`
MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"` PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"` MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"` TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
Nodes []string `json:"nodes" yaml:"nodes"` InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
IsRelayed bool `json:"isrelayed" yaml:"isrelayed"` Nodes []string `json:"nodes" yaml:"nodes"`
RelayedBy string `json:"relayed_by" yaml:"relayed_by"` IsRelayed bool `json:"isrelayed" yaml:"isrelayed"`
IsRelay bool `json:"isrelay" yaml:"isrelay"` RelayedBy string `json:"relayed_by" yaml:"relayed_by"`
RelayedHosts []string `json:"relay_hosts" yaml:"relay_hosts"` IsRelay bool `json:"isrelay" yaml:"isrelay"`
Interfaces []Iface `json:"interfaces" yaml:"interfaces"` RelayedHosts []string `json:"relay_hosts" yaml:"relay_hosts"`
DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"` Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
EndpointIP net.IP `json:"endpointip" yaml:"endpointip"` DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"` EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"` ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
IsDocker bool `json:"isdocker" yaml:"isdocker"` ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"`
IsK8S bool `json:"isk8s" yaml:"isk8s"` IsDocker bool `json:"isdocker" yaml:"isdocker"`
IsStatic bool `json:"isstatic" yaml:"isstatic"` IsK8S bool `json:"isk8s" yaml:"isk8s"`
IsDefault bool `json:"isdefault" yaml:"isdefault"` IsStatic bool `json:"isstatic" yaml:"isstatic"`
NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"` IsDefault bool `json:"isdefault" yaml:"isdefault"`
TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"` 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