Skip to content

Commit ff964f4

Browse files
Improve SRV name parsing
1 parent 1f8d80b commit ff964f4

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

dns.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func createDNSReplyFromRequest(
154154
continue
155155
}
156156

157-
target := removeServiceAndPort(answerName)
157+
target := removeServiceAndProtocol(answerName)
158158

159159
reply.Answer = append(reply.Answer, &dns.SRV{
160160
Hdr: rrHeader(target, dns.TypeSRV, config.TTL),
@@ -487,14 +487,14 @@ func delegateToDNSServer(dnsServer string, timeout time.Duration) delegateQuesti
487487
}
488488
}
489489

490-
func removeServiceAndPort(host string) string {
491-
var parts []string
490+
func removeServiceAndProtocol(host string) string {
491+
parts := strings.Split(host, ".")
492492

493-
for _, part := range strings.Split(host, ".") {
493+
for i, part := range parts {
494494
if !strings.HasPrefix(part, "_") {
495-
parts = append(parts, part)
495+
return strings.Join(parts[i:], ".")
496496
}
497497
}
498498

499-
return strings.Join(parts, ".")
499+
return ""
500500
}

dns_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,12 @@ func testSRVQuery(tb testing.TB, matcher string, port uint16) {
451451
tb.Fatalf("port is %d instead of 389", srvRecord.Port)
452452
}
453453

454-
if srvRecord.Hdr.Name != removeServiceAndPort(serviceName) {
455-
tb.Fatalf("reply name is %q instead of %q", srvRecord.Hdr.Name, removeServiceAndPort(serviceName))
454+
if srvRecord.Hdr.Name != removeServiceAndProtocol(serviceName) {
455+
tb.Fatalf("reply name is %q instead of %q", srvRecord.Hdr.Name, removeServiceAndProtocol(serviceName))
456456
}
457457

458-
if srvRecord.Target != removeServiceAndPort(serviceName) {
459-
tb.Fatalf("target name is %q instead of %q", srvRecord.Target, removeServiceAndPort(serviceName))
458+
if srvRecord.Target != removeServiceAndProtocol(serviceName) {
459+
tb.Fatalf("target name is %q instead of %q", srvRecord.Target, removeServiceAndProtocol(serviceName))
460460
}
461461

462462
if len(reply.Extra) != 2 {
@@ -470,8 +470,8 @@ func testSRVQuery(tb testing.TB, matcher string, port uint16) {
470470
case *dns.A:
471471
checkedA = true
472472

473-
if e.Hdr.Name != removeServiceAndPort(serviceName) {
474-
tb.Fatalf("A extra record name is %s instead of %s", e.Hdr.Name, removeServiceAndPort(serviceName))
473+
if e.Hdr.Name != removeServiceAndProtocol(serviceName) {
474+
tb.Fatalf("A extra record name is %s instead of %s", e.Hdr.Name, removeServiceAndProtocol(serviceName))
475475
}
476476

477477
if !e.A.Equal(relayIPv4) {
@@ -480,8 +480,8 @@ func testSRVQuery(tb testing.TB, matcher string, port uint16) {
480480
case *dns.AAAA:
481481
checkedAAAA = true
482482

483-
if e.Hdr.Name != removeServiceAndPort(serviceName) {
484-
tb.Fatalf("AAAA extra record name is %s instead of %s", e.Hdr.Name, removeServiceAndPort(serviceName))
483+
if e.Hdr.Name != removeServiceAndProtocol(serviceName) {
484+
tb.Fatalf("AAAA extra record name is %s instead of %s", e.Hdr.Name, removeServiceAndProtocol(serviceName))
485485
}
486486

487487
if !e.AAAA.Equal(relayIPv6) {

filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func shouldRespondToNameResolutionQuery(config *Config, host string, queryType u
2424

2525
if queryType == dns.TypeSRV {
2626
hostWithService = host
27-
host = removeServiceAndPort(host)
27+
host = removeServiceAndProtocol(host)
2828
}
2929

3030
if config.spoofingTemporarilyDisabled {

0 commit comments

Comments
 (0)