Browse Source

created conversion functions for hosts

0xdcarns 2 years ago
parent
commit
e0d3d17921
2 changed files with 100 additions and 13 deletions
  1. 7 7
      logic/gateway.go
  2. 93 6
      models/api_host.go

+ 7 - 7
logic/gateway.go

@@ -46,7 +46,7 @@ func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, erro
 	}
 	node.IsEgressGateway = true
 	node.EgressGatewayRanges = gateway.Ranges
-	node.EgressGatewayNatEnabled = gateway.NatEnabled
+	node.EgressGatewayNatEnabled = gateway.NatEnabled == "yes"
 	node.EgressGatewayRequest = gateway // store entire request for use when preserving the egress gateway
 	postUpCmd := ""
 	postDownCmd := ""
@@ -335,7 +335,7 @@ func firewallNFTCommandsCreateIngress(networkInterface string) (string, string)
 }
 
 // firewallNFTCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the nftables firewall.
-func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface string, gatewayranges []string, egressNatEnabled string, ipv4, ipv6 bool) (string, string) {
+func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface string, gatewayranges []string, egressNatEnabled bool, ipv4, ipv6 bool) (string, string) {
 	// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
 	postUp := ""
 	postDown := ""
@@ -351,7 +351,7 @@ func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface s
 
 		postDown += "nft flush table filter ; "
 
-		if egressNatEnabled == "yes" {
+		if egressNatEnabled {
 			postUp += "nft add table nat ; "
 			postUp += "nft add chain nat postrouting ; "
 			postUp += "nft add rule ip nat postrouting oifname " + gatewayInterface + " counter masquerade ; "
@@ -368,7 +368,7 @@ func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface s
 
 		postDown += "nft flush table ip6 filter ; "
 
-		if egressNatEnabled == "yes" {
+		if egressNatEnabled {
 			postUp += "nft add table ip6 nat ; "
 			postUp += "nft 'add chain ip6 nat prerouting { type nat hook prerouting priority 0 ;}' ; "
 			postUp += "nft 'add chain ip6 nat postrouting { type nat hook postrouting priority 0 ;}' ; "
@@ -411,7 +411,7 @@ func firewallIPTablesCommandsCreateIngress(networkInterface string, ipv4, ipv6 b
 }
 
 // firewallIPTablesCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the iptables firewall.
-func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled string, ipv4, ipv6 bool) (string, string) {
+func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled bool, ipv4, ipv6 bool) (string, string) {
 	// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
 	postUp := ""
 	postDown := ""
@@ -421,7 +421,7 @@ func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterf
 		postDown += "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
 		postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
 
-		if egressNatEnabled == "yes" {
+		if egressNatEnabled {
 			postUp += "iptables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
 			postDown += "iptables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
 		}
@@ -432,7 +432,7 @@ func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterf
 		postDown += "ip6tables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
 		postDown += "ip6tables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
 
-		if egressNatEnabled == "yes" {
+		if egressNatEnabled {
 			postUp += "ip6tables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
 			postDown += "ip6tables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
 		}

+ 93 - 6
models/api_host.go

@@ -1,18 +1,17 @@
 package models
 
-// APIHost - the host struct for API usage
-type APIHost struct {
+import "net"
+
+// 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"`
-	IPForwarding    bool     `json:"ipforwarding"`
-	DaemonInstalled bool     `json:"daemoninstalled"`
-	HostPass        string   `json:"hostpass"`
 	Name            string   `json:"name"`
 	OS              string   `json:"os"`
-	Interface       string   `json:"interface"`
 	Debug           bool     `json:"debug"`
+	IsStatic        bool     `json:"isstatic"`
 	ListenPort      int      `json:"listenport"`
 	LocalAddress    string   `json:"localaddress"`
 	LocalRange      string   `json:"localrange"`
@@ -20,8 +19,96 @@ type APIHost struct {
 	ProxyListenPort int      `json:"proxy_listen_port"`
 	MTU             int      `json:"mtu" yaml:"mtu"`
 	Interfaces      []Iface  `json:"interfaces" yaml:"interfaces"`
+	EndpointIP      string   `json:"endpointip" yaml:"endpointip"`
 	PublicKey       string   `json:"publickey"`
 	MacAddress      string   `json:"macaddress"`
 	InternetGateway string   `json:"internetgateway"`
 	Nodes           []string `json:"nodes"`
+	ProxyEnabled    bool     `json:"proxy_enabled" yaml:"proxy_enabled"`
+}
+
+// Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
+func (h *Host) ConvertNMHostToAPI() *ApiHost {
+	a := ApiHost{}
+	a.Debug = h.Debug
+	a.EndpointIP = h.EndpointIP.String()
+	a.FirewallInUse = h.FirewallInUse
+	a.ID = h.ID.String()
+	a.Interfaces = h.Interfaces
+	a.InternetGateway = h.InternetGateway.String()
+	if isEmptyAddr(a.InternetGateway) {
+		a.InternetGateway = ""
+	}
+	a.IsStatic = h.IsStatic
+	a.ListenPort = h.ListenPort
+	a.LocalAddress = h.LocalAddress.String()
+	if isEmptyAddr(a.LocalAddress) {
+		a.LocalAddress = ""
+	}
+	a.LocalListenPort = h.LocalListenPort
+	a.LocalRange = h.LocalRange.String()
+	if isEmptyAddr(a.LocalRange) {
+		a.LocalRange = ""
+	}
+	a.MTU = h.MTU
+	a.MacAddress = h.MacAddress.String()
+	a.Name = h.Name
+	a.OS = h.OS
+	a.Nodes = h.Nodes
+	a.ProxyEnabled = h.ProxyEnabled
+	a.ProxyListenPort = h.ProxyListenPort
+	a.PublicKey = h.PublicKey.String()
+	a.Verbosity = h.Verbosity
+	a.Version = h.Version
+
+	return &a
+}
+
+// APIHost.ConvertAPIHostToNMHost - convert's a given apihost struct to
+// a Host struct
+func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
+	h := Host{}
+	h.ID = currentHost.ID
+	h.HostPass = currentHost.HostPass
+	h.DaemonInstalled = currentHost.DaemonInstalled
+	h.EndpointIP = net.ParseIP(a.EndpointIP)
+	h.Debug = a.Debug
+	h.FirewallInUse = a.FirewallInUse
+	h.IPForwarding = currentHost.IPForwarding
+	h.Interface = currentHost.Interface
+	h.Interfaces = currentHost.Interfaces
+	h.InternetGateway = currentHost.InternetGateway
+	h.IsDocker = currentHost.IsDocker
+	h.IsK8S = currentHost.IsK8S
+	h.IsStatic = a.IsStatic
+	h.ListenPort = a.ListenPort
+	h.LocalListenPort = currentHost.ListenPort
+	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
+	if len(a.LocalAddress) > 0 {
+		_, localAddr, err := net.ParseCIDR(a.LocalAddress)
+		if err == nil {
+			h.LocalAddress = *localAddr
+		}
+	} else if !isEmptyAddr(currentHost.LocalAddress.String()) {
+		h.LocalAddress = currentHost.LocalAddress
+	}
+	if len(a.LocalRange) > 0 {
+		_, localRange, err := net.ParseCIDR(a.LocalRange)
+		if err == nil {
+			h.LocalRange = *localRange
+		}
+	} else if !isEmptyAddr(currentHost.LocalRange.String()) {
+		h.LocalRange = currentHost.LocalRange
+	}
+	h.ProxyEnabled = a.ProxyEnabled
+
+	return &h
 }