dnscontrol/providers/hexonet/nameservers.go
Patrick Gaskin 2f83aa9302 Internals: Switch to v2 go.mod, drop GOPATH, and fix Azure Pipelines (#595)
* Switched to v2 go.mod

Also set GO111MODULE=on in build stuff to always use Go modules
even when in GOPATH.

* Ensure go.mod, go.sum, and vendor are up to date

* Attempt to fix Azure pipelines

* Add set -e to properly fail on exit (it didn't seem to be
  propagating properly before).
* Set workingDirectory for GoFmt and GoGen (this might be why it
  fails unlike compile and unitests).

* Another attempt to fix Azure Pipelines

* Use the Go env template for all go-related jobs.

* Completely fixed Azure Pipelines

* Added a display name to GoFmt for consistency.
* Fixed diffs for GoFmt and GoGen.
* Show git status for checks.

* Drop GOPATH for tests

TODO: Do the same for integration tests.

* Drop GOPATH for integration tests

* Show more diffs

* Regenerate provider support matrix

This wasn't done in #590...
2020-01-28 10:42:31 -05:00

100 lines
2.8 KiB
Go

package hexonet
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/StackExchange/dnscontrol/v2/models"
)
var defaultNameservers = []*models.Nameserver{
{Name: "ns1.ispapi.net"},
{Name: "ns2.ispapi.net"},
{Name: "ns3.ispapi.net"},
}
var nsRegex = regexp.MustCompile(`ns([1-3]{1})[0-9]+\.ispapi\.net`)
// GetNameservers gets the nameservers set on a domain.
func (n *HXClient) GetNameservers(domain string) ([]*models.Nameserver, error) {
// This is an interesting edge case. hexonet expects you to SET the nameservers to ns[1-3].ispapi.net,
// but it will internally set it to (ns1xyz|ns2uvw|ns3asd).ispapi.net, where xyz/uvw/asd is a uniqueish number.
// In order to avoid endless loops, we will use the unique nameservers if present, or else the generic ones if not.
nss, err := n.getNameserversRaw(domain)
if err != nil {
return nil, err
}
toUse := []string{
defaultNameservers[0].Name,
defaultNameservers[1].Name,
defaultNameservers[2].Name,
}
for _, ns := range nss {
if matches := nsRegex.FindStringSubmatch(ns); len(matches) == 2 && len(matches[1]) == 1 {
idx := matches[1][0] - '1' // regex ensures proper range
toUse[idx] = matches[0]
}
}
return models.StringsToNameservers(toUse), nil
}
func (n *HXClient) getNameserversRaw(domain string) ([]string, error) {
r := n.client.Request(map[string]string{
"COMMAND": "StatusDomain",
"DOMAIN": domain,
})
code := r.Code()
if code != 200 {
return nil, n.GetHXApiError("Could not get status for domain", domain, r)
}
ns := r.GetColumn("NAMESERVER")
sort.Strings(ns)
return ns, nil
}
// GetRegistrarCorrections gathers corrections that would being n to match dc.
func (n *HXClient) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
nss, err := n.getNameserversRaw(dc.Name)
if err != nil {
return nil, err
}
foundNameservers := strings.Join(nss, ",")
expected := []string{}
for _, ns := range dc.Nameservers {
name := strings.TrimRight(ns.Name, ".")
expected = append(expected, name)
}
sort.Strings(expected)
expectedNameservers := strings.Join(expected, ",")
if foundNameservers != expectedNameservers {
return []*models.Correction{
{
Msg: fmt.Sprintf("Update nameservers %s -> %s", foundNameservers, expectedNameservers),
F: n.updateNameservers(expected, dc.Name),
},
}, nil
}
return nil, nil
}
func (n *HXClient) updateNameservers(ns []string, domain string) func() error {
return func() error {
cmd := map[string]string{
"COMMAND": "ModifyDomain",
"DOMAIN": domain,
}
for idx, ns := range ns {
cmd[fmt.Sprintf("NAMESERVER%d", idx)] = ns
}
response := n.client.Request(cmd)
code := response.Code()
if code != 200 {
return fmt.Errorf(fmt.Sprintf("%d %s", code, response.Description()))
}
return nil
}
}