Skip to content

Commit

Permalink
Answer nodes per name
Browse files Browse the repository at this point in the history
  • Loading branch information
WGOS committed Feb 5, 2024
1 parent b6015c2 commit 8bbe0f7
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var client swarm.Client
var logflag bool
var rateLimit int64
var nodes []swarm.SwarmNode
var nameToNodeMap map[string]*swarm.SwarmNode
var nameToNodeMap map[string][]swarm.SwarmNode
var mutex = &sync.Mutex{}
var swarmDomains arrayFlags
var returnWorkers bool
Expand Down Expand Up @@ -143,9 +143,27 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
w.WriteMsg(m)
}

func replaceLast(s, search, replace string) string {
i := strings.LastIndex(s, search)
if i == -1 {
return s
}
return s[:i] + replace + s[i+len(search):]
}

func answerForNodes(domain string) []dns.RR {
mutex.Lock()
var rrs []dns.RR
var nodes = nodes

var swarmDomain = *matchingDomain(domain)
var normDomain = strings.ToLower(domain)

if swarmDomain != normDomain {
var subDomain = replaceLast(normDomain, "."+swarmDomain, "")
nodes = nameToNodeMap[subDomain]
}

for _, node := range nodes {
if node.IsManager || returnWorkers {
rr := new(dns.A)
Expand Down Expand Up @@ -182,17 +200,19 @@ func refreshNodes() {
}

func mapNodesToNames(nodes []swarm.SwarmNode) {
var newMap = make(map[string]*swarm.SwarmNode)
var newMap = make(map[string][]swarm.SwarmNode)

for _, node := range nodes {
if len(node.DnsNames) <= 0 {
continue
}

for _, name := range node.DnsNames {
newMap[name] = &node
newMap[name] = append(newMap[name], node)

logger.Printf("Mapped: %v to %v\n", name, node.Hostname)
logger.Printf("Mapped: [%v] to [%v]\n", name, node.Hostname)
}
}

nameToNodeMap = newMap
}

0 comments on commit 8bbe0f7

Please sign in to comment.