Skip to content

Commit

Permalink
improve https record lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 21, 2024
1 parent 1e906b1 commit e8b0303
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
12 changes: 8 additions & 4 deletions client_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,29 @@ func (c *Client) LookupHTTPS(ctx context.Context, host string) (https []NetHTTPS
value := data[4 : 4+length]
data = data[4+length:]
switch key {
case 1: // alpn
case 1: // ALPN
for len(value) != 0 {
length := int(value[0])
h.ALPN = append(h.ALPN, string(value[1:1+length]))
value = value[1+length:]
}
case 4: // ipv4hint
case 2: // NoDefaultALPN
h.NoDefaultALPN = true
case 3: // Port
h.Port = uint32(value[0])<<8 | uint32(value[1])
case 4: // IPV4Hint
if len(value) != length {
continue
}
for i := 0; i < length; i += 4 {
h.IPv4Hint = append(h.IPv4Hint, netip.AddrFrom4(*(*[4]byte)(value[i : i+4])))
}
case 5: // ech
case 5: // ECH
if len(value) < 2 {
continue
}
h.ECH = append(h.ECH[:0], value...)
case 6: // ipv6hint
case 6: // IPV6Hint
if len(value) != length {
continue
}
Expand Down
1 change: 1 addition & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestClientLookup(t *testing.T) {
{"cloud.phus.lu", TypeANY},
{"cloud.phus.lu", TypeHTTPS},
{"abcde.phus.lu", TypeCNAME},
{"phus.lu", TypeHTTPS},
{"phus.lu", TypeTXT},
{"phus.lu", TypeNS},
{"phus.lu", TypeMX},
Expand Down
18 changes: 14 additions & 4 deletions cmd/fastdig/fastdig.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,29 @@ func cmd(req, resp *fastdns.Message, server string, start, end time.Time) {
value := data[4 : 4+length]
data = data[4+length:]
switch key {
case 1: // alpn
case 1: // ALPN
for len(value) != 0 {
length := int(value[0])
h.ALPN = append(h.ALPN, string(value[1:1+length]))
value = value[1+length:]
}
case 4: // ipv4hint
case 2: // NoDefaultALPN
h.NoDefaultALPN = true
case 3: // Port
h.Port = uint32(value[0])<<8 | uint32(value[1])
case 4: // IPV4Hint
if len(value) != length {
continue
}
for i := 0; i < length; i += 4 {
h.IPv4Hint = append(h.IPv4Hint, netip.AddrFrom4(*(*[4]byte)(value[i : i+4])))
}
case 5: // ech
case 5: // ECH
if len(value) < 2 {
continue
}
h.ECH = append(h.ECH[:0], value...)
case 6: // ipv6hint
case 6: // IPV6Hint
if len(value) != length {
continue
}
Expand All @@ -268,6 +272,12 @@ func cmd(req, resp *fastdns.Message, server string, start, end time.Time) {
if len(h.ALPN) > 0 {
fmt.Fprintf(&sb, "alpn=\"%s\" ", strings.Join(h.ALPN, ","))
}
if h.Port > 0 {
fmt.Fprintf(&sb, "port=%d ", h.Port)
}
if h.NoDefaultALPN {
fmt.Fprintf(&sb, "no-default-alpn=%v ", h.NoDefaultALPN)
}
if len(h.IPv4Hint) > 0 {
fmt.Fprintf(&sb, "ipv4hint=")
for i, ip := range h.IPv4Hint {
Expand Down
10 changes: 6 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,10 @@ func ParseType(s string) (t Type) {
}

type NetHTTPS struct {
ALPN []string
IPv4Hint []netip.Addr
IPv6Hint []netip.Addr
ECH []byte
ALPN []string
NoDefaultALPN bool
Port uint32
IPv4Hint []netip.Addr
IPv6Hint []netip.Addr
ECH []byte
}

0 comments on commit e8b0303

Please sign in to comment.