Skip to content

Commit

Permalink
Improve set client subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Mar 16, 2024
1 parent f5e8dd5 commit 29c23c4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions extension_edns0_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ func (t *edns0SubnetTransportWrapper) Exchange(ctx context.Context, message *dns
}

func SetClientSubnet(message *dns.Msg, clientSubnet netip.Addr, override bool) {
var subnetOption *dns.EDNS0_SUBNET
var (
optRecord *dns.OPT
subnetOption *dns.EDNS0_SUBNET
)
findExists:
for _, record := range message.Extra {
if optRecord, isOPTRecord := record.(*dns.OPT); isOPTRecord {
var isOPTRecord bool
if optRecord, isOPTRecord = record.(*dns.OPT); isOPTRecord {
for _, option := range optRecord.Option {
var isEDNS0Subnet bool
subnetOption, isEDNS0Subnet = option.(*dns.EDNS0_SUBNET)
Expand All @@ -34,15 +38,18 @@ findExists:
}
}
}
if subnetOption == nil {
subnetOption = new(dns.EDNS0_SUBNET)
message.Extra = append(message.Extra, &dns.OPT{
if optRecord == nil {
optRecord = &dns.OPT{
Hdr: dns.RR_Header{
Name: ".",
Rrtype: dns.TypeOPT,
},
Option: []dns.EDNS0{subnetOption},
})
}
message.Extra = append(message.Extra, optRecord)
}
if subnetOption == nil {
subnetOption = new(dns.EDNS0_SUBNET)
optRecord.Option = append(optRecord.Option, subnetOption)
}
subnetOption.Code = dns.EDNS0SUBNET
if clientSubnet.Is4() {
Expand Down

0 comments on commit 29c23c4

Please sign in to comment.