-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add support for the client-subnet option
- Loading branch information
1 parent
27e217b
commit 6a377c9
Showing
14 changed files
with
222 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dns | ||
|
||
import ( | ||
"context" | ||
"net/netip" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
type edns0SubnetTransportWrapper struct { | ||
Transport | ||
clientSubnet netip.Addr | ||
} | ||
|
||
func (t *edns0SubnetTransportWrapper) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) { | ||
SetClientSubnet(message, t.clientSubnet, false) | ||
return t.Transport.Exchange(ctx, message) | ||
} | ||
|
||
func SetClientSubnet(message *dns.Msg, clientSubnet netip.Addr, override bool) { | ||
var subnetOption *dns.EDNS0_SUBNET | ||
findExists: | ||
for _, record := range message.Extra { | ||
if optRecord, isOPTRecord := record.(*dns.OPT); isOPTRecord { | ||
for _, option := range optRecord.Option { | ||
var isEDNS0Subnet bool | ||
subnetOption, isEDNS0Subnet = option.(*dns.EDNS0_SUBNET) | ||
if isEDNS0Subnet { | ||
if !override { | ||
return | ||
} | ||
break findExists | ||
} | ||
} | ||
} | ||
} | ||
if subnetOption == nil { | ||
subnetOption = new(dns.EDNS0_SUBNET) | ||
message.Extra = append(message.Extra, &dns.OPT{ | ||
Hdr: dns.RR_Header{ | ||
Name: ".", | ||
Rrtype: dns.TypeOPT, | ||
}, | ||
Option: []dns.EDNS0{subnetOption}, | ||
}) | ||
} | ||
subnetOption.Code = dns.EDNS0SUBNET | ||
if clientSubnet.Is4() { | ||
subnetOption.Family = 1 | ||
} else { | ||
subnetOption.Family = 2 | ||
} | ||
subnetOption.SourceNetmask = uint8(clientSubnet.BitLen()) | ||
subnetOption.Address = clientSubnet.AsSlice() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dns | ||
|
||
import ( | ||
"context" | ||
"net/netip" | ||
) | ||
|
||
type disableCacheKey struct{} | ||
|
||
func ContextWithDisableCache(ctx context.Context, val bool) context.Context { | ||
return context.WithValue(ctx, (*disableCacheKey)(nil), val) | ||
} | ||
|
||
func DisableCacheFromContext(ctx context.Context) bool { | ||
val := ctx.Value((*disableCacheKey)(nil)) | ||
if val == nil { | ||
return false | ||
} | ||
return val.(bool) | ||
} | ||
|
||
type rewriteTTLKey struct{} | ||
|
||
func ContextWithRewriteTTL(ctx context.Context, val uint32) context.Context { | ||
return context.WithValue(ctx, (*rewriteTTLKey)(nil), val) | ||
} | ||
|
||
func RewriteTTLFromContext(ctx context.Context) (uint32, bool) { | ||
val := ctx.Value((*rewriteTTLKey)(nil)) | ||
if val == nil { | ||
return 0, false | ||
} | ||
return val.(uint32), true | ||
} | ||
|
||
type transportKey struct{} | ||
|
||
func contextWithTransportName(ctx context.Context, transportName string) context.Context { | ||
return context.WithValue(ctx, transportKey{}, transportName) | ||
} | ||
|
||
func transportNameFromContext(ctx context.Context) (string, bool) { | ||
value, loaded := ctx.Value(transportKey{}).(string) | ||
return value, loaded | ||
} | ||
|
||
type clientSubnetKey struct{} | ||
|
||
func ContextWithClientSubnet(ctx context.Context, clientSubnet netip.Addr) context.Context { | ||
return context.WithValue(ctx, clientSubnetKey{}, clientSubnet) | ||
} | ||
|
||
func ClientSubnetFromContext(ctx context.Context) (netip.Addr, bool) { | ||
clientSubnet, ok := ctx.Value(clientSubnetKey{}).(netip.Addr) | ||
return clientSubnet, ok | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.