Skip to content

Commit

Permalink
add LookupHTTPS method to client
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 10, 2024
1 parent 7b3f0aa commit 1d0279c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
49 changes: 49 additions & 0 deletions client_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,52 @@ func (c *Client) LookupNetIP(ctx context.Context, network, host string) (ips []n

return
}

func (c *Client) LookupHTTPS(ctx context.Context, network, host string) (https []NetHTTPS, err error) {
req, resp := AcquireMessage(), AcquireMessage()
defer ReleaseMessage(resp)
defer ReleaseMessage(req)

req.SetRequestQuestion(host, TypeHTTPS, ClassINET)

err = c.Exchange(req, resp)
if err != nil {
return
}

_ = resp.Walk(func(name []byte, typ Type, class Class, ttl uint32, data []byte) bool {
switch typ {
case TypeHTTPS:
var h NetHTTPS
data = data[3:]
for len(data) != 0 {
key := int(data[0])<<8 | int(data[1])
length := int(data[2])<<8 | int(data[3])
value := data[4 : 4+length]
data = data[4+length:]
switch key {
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
for i := 0; i < length; i += 4 {
h.IPv4Hint = append(h.IPv4Hint, netip.AddrFrom4(*(*[4]byte)(value[i : i+4])))
}
case 5: // ech
h.ECH = value[2:]
case 6: // ipv6hint
for i := 0; i < length; i += 16 {
h.IPv6Hint = append(h.IPv6Hint, netip.AddrFrom16(*(*[16]byte)(value[i : i+16])))
}
}
}
https = append(https, h)
}
return true
})

return
}
14 changes: 14 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ func TestLookupNetIP(t *testing.T) {

t.Logf("client.LookupNetIP(%+v) return ips=%s err=%+v\n", host, ips, err)
}

func TestLookupHTTPS(t *testing.T) {
host := "cloud.phus.lu"

client := &Client{
AddrPort: netip.AddrPortFrom(netip.AddrFrom4([4]byte{8, 8, 8, 8}), 53),
ReadTimeout: 1 * time.Second,
MaxConns: 1000,
}

https, err := client.LookupHTTPS(context.Background(), "ip", host)

t.Logf("client.LookupHTTPS(%+v) return https=%+v err=%+v\n", host, https, err)
}
14 changes: 13 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fastdns

import (
"net/netip"
)

// Rcode denotes a 4bit field that specifies the response
// code for a query.
type Rcode byte
Expand Down Expand Up @@ -102,7 +106,8 @@ func (c Opcode) String() string {

// Flags is an arbitrary 16bit represents QR, Opcode, AA, TC, RD, RA, Z and RCODE.
//
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
//
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Expand Down Expand Up @@ -616,3 +621,10 @@ func ParseType(s string) (t Type) {
}
return
}

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

0 comments on commit 1d0279c

Please sign in to comment.