Skip to content

Commit

Permalink
Support dns round robin.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbae1212 committed Jun 19, 2019
1 parent 2214a0c commit a848bca
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"math/rand"
"net"
"strconv"
"strings"
Expand All @@ -15,6 +16,10 @@ import (
"github.com/miekg/dns"
)

const (
dnsRR = "rr"
)

type Server interface {
Start()
}
Expand Down Expand Up @@ -63,6 +68,20 @@ func (s *server) Lookup(search string) ([]*Record, error) {
if ix, err := strconv.Atoi(prefix); err == nil {
num = ix
}

// if a suffix means round-robin, must be responsibility to return shuffle result
if suffix == dnsRR {
records, err := s.store.Lookup(strings.Join(seps[0:(len(seps)-1)], "."))
if err != nil {
return nil, err
}
rand.Seed(time.Now().UnixNano())
dummy := make([]*Record, len(records))
copy(dummy, records)
rand.Shuffle(len(dummy), func(i, j int) { dummy[i], dummy[j] = dummy[j], dummy[i] })
return dummy, nil
}

if suffix == "aws" {
checkVendor = AWS
} else if suffix == "gcp" {
Expand Down Expand Up @@ -271,4 +290,4 @@ func checkConfig(config *CommonConfig) (*CommonConfig, error) {
}
}
return config, nil
}
}

0 comments on commit a848bca

Please sign in to comment.