Skip to content

Commit

Permalink
buyer: Treat failed DNS SRV queries as warnings
Browse files Browse the repository at this point in the history
Instead of treating DNS SRV lookups as failures, treat them as warnings,
report them to the user and still try to connect to the original matcher
host.

This is needed because detection of the "no such host" error is fragile
and platform-dependent, so instead of simply failing to connect and
perform the session we just continue trying with the currently
configured host and leave it up to the user to resolve problems related
to the DNS setup.
  • Loading branch information
matheusd committed Dec 12, 2018
1 parent 3a904d4 commit 8bae438
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/buyer/buyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Reporter interface {
reportMatcherStatus(*pbm.StatusResponse)
reportSavedSession(string)
reportSrvRecordFound(record string)
reportSrvLookupError(err error)
reportSplitPublished()
reportRightTicketPublished()
reportWrongTicketPublished(ticket *wire.MsgTx, session *Session)
Expand Down
27 changes: 19 additions & 8 deletions pkg/buyer/internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ func IsSubDomain(root, subdomain string) bool {
// This uses DNS requests to consult applicable SRV records to redirect the
// connection to a specific server.
// Returns the target host, whether the host was changed due to the presence of
// an SRV record or an error
// an SRV record and any errors that happened.
//
// Note that in case of errors, the original matcherHost is returned.
func DetermineMatcherHost(matcherHost string) (string, bool, error) {
host := RemoveHostPort(matcherHost)

Expand All @@ -141,20 +143,29 @@ func DetermineMatcherHost(matcherHost string) (string, bool, error) {
matcherHost = fmt.Sprintf("%s:%d", target, addrs[0].Port)

if !IsSubDomain(host, target) {
return "", false, errors.Errorf("SRV target host %s is not a "+
"subdomain of %s", target, host)
return matcherHost, false, errors.Errorf("SRV target host %s is "+
"not a subdomain of %s", target, host)
}

return matcherHost, true, nil
}

if err != nil {
if strings.LastIndex(err.Error(), "no such host") == -1 {
// not a great way to check for the "no such error" host (target
// host not found)
return "", false, errors.Wrap(err, "error during SRV record lookup")
// These are not a great ways to check for the "no such error" host
// (target host not found) - ie the stakepool did not configure an SRV
// record and we should continue with process.
errstr := err.Error()

if strings.LastIndex(errstr, "no such host") == len(errstr)-12 {
// Matches on linux platforms.
return matcherHost, false, nil
}

if strings.LastIndex(errstr, "name does not exist.") == len(errstr)-20 {
// Matches on windows platforms.
return matcherHost, false, nil
}
}

return matcherHost, false, nil
return matcherHost, false, err
}
7 changes: 1 addition & 6 deletions pkg/buyer/matcher-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ func ConnectToMatcherService(ctx context.Context, matcherHost string,

rep := reporterFromContext(ctx)

// network, err := connectToDecredNode(netCfg)
// if err != nil {
// return nil, errors.Wrapf(err, "error connecting to dcrd")
// }

matcherHost, isSrv, err := intnet.DetermineMatcherHost(matcherHost)
if err != nil {
return nil, errors.Wrapf(err, "error determining target matcher host")
rep.reportSrvLookupError(err)
}
if isSrv {
rep.reportSrvRecordFound(matcherHost)
Expand Down
5 changes: 5 additions & 0 deletions pkg/buyer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (rep *WriterReporter) reportSrvRecordFound(record string) {
fmt.Fprintf(rep.w, "Found SRV record to use host %s\n", record)
}

func (rep *WriterReporter) reportSrvLookupError(err error) {
fmt.Fprintf(rep.w, "SRV lookup error: %s\n", err)
}

func (rep *WriterReporter) reportSplitPublished() {
fmt.Fprintf(rep.w, "Split tx published in the network\n")
}
Expand Down Expand Up @@ -297,6 +301,7 @@ func (rep NullReporter) reportStage(ctx context.Context, stage Stage, session *S
func (rep NullReporter) reportMatcherStatus(status *pb.StatusResponse) {}
func (rep NullReporter) reportSavedSession(string) {}
func (rep NullReporter) reportSrvRecordFound(record string) {}
func (rep NullReporter) reportSrvLookupError(err error) {}
func (rep NullReporter) reportSplitPublished() {}
func (rep NullReporter) reportRightTicketPublished() {}
func (rep NullReporter) reportWrongTicketPublished(ticket *wire.MsgTx, session *Session) {}
Expand Down

0 comments on commit 8bae438

Please sign in to comment.