Procházet zdrojové kódy

add service connector api

abhishek9686 před 5 měsíci
rodič
revize
197cbda93e
3 změnil soubory, kde provedl 20 přidání a 2 odebrání
  1. 16 1
      logic/egress.go
  2. 3 0
      models/api_host.go
  3. 1 1
      models/host.go

+ 16 - 1
logic/egress.go

@@ -4,6 +4,7 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"maps"
 	"math/big"
 	"net"
 	"slices"
@@ -56,14 +57,28 @@ func ToIPNet(ipaddr net.IP) *net.IPNet {
 }
 
 func MapExternalServicesToHostNodes(h *models.Host) {
+	if h.EgressServices == nil {
+		return
+	}
 	ranges := []string{}
 	rangesWithMetric := []models.EgressRangeMetric{}
+	// fetch namservers for internal dns
+	hostEgressServices := maps.Clone(h.EgressServices)
+	for _, nsI := range h.NameServers {
+		nsIP := net.ParseIP(nsI)
+		if nsIP.IsPrivate() {
+			hostEgressServices["DNS"] = append(hostEgressServices["DNS"], models.EgressIPNat{
+				EgressIP: nsIP,
+			})
+		}
+	}
+
 	for _, nodeID := range h.Nodes {
 		node, err := GetNodeByID(nodeID)
 		if err != nil {
 			continue
 		}
-		for i, egressServiceIPs := range h.EgressServices {
+		for i, egressServiceIPs := range hostEgressServices {
 			if len(egressServiceIPs) == 0 {
 				continue
 			}

+ 3 - 0
models/api_host.go

@@ -32,6 +32,7 @@ type ApiHost struct {
 	PersistentKeepalive int                      `json:"persistentkeepalive"   yaml:"persistentkeepalive"`
 	AutoUpdate          bool                     `json:"autoupdate"              yaml:"autoupdate"`
 	EgressServices      map[string][]EgressIPNat `json:"egress_services"`
+	NameServers         []string                 `json:"name_servers"`
 }
 
 // ApiIface - the interface struct for API usage
@@ -80,6 +81,7 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
 	a.PersistentKeepalive = int(h.PersistentKeepalive.Seconds())
 	a.AutoUpdate = h.AutoUpdate
 	a.EgressServices = h.EgressServices
+	a.NameServers = h.NameServers
 	return &a
 }
 
@@ -126,5 +128,6 @@ func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
 	h.PersistentKeepalive = time.Duration(a.PersistentKeepalive) * time.Second
 	h.AutoUpdate = a.AutoUpdate
 	h.EgressServices = a.EgressServices
+	h.NameServers = currentHost.NameServers
 	return &h
 }

+ 1 - 1
models/host.go

@@ -73,7 +73,7 @@ type Host struct {
 	TurnEndpoint        *netip.AddrPort          `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
 	PersistentKeepalive time.Duration            `json:"persistentkeepalive" swaggertype:"primitive,integer" format:"int64" yaml:"persistentkeepalive"`
 	EgressServices      map[string][]EgressIPNat `json:"egress_services"`
-	NameServers         []string                 `json:"name_servers" yaml:"name_servers"`
+	NameServers         []string                 `json:"name_servers"`
 }
 
 type EgressIPNat struct {