Skip to content

Commit

Permalink
Add ECS source netmark option
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 10, 2024
1 parent 9fb3613 commit f52ce53
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions extension_edns0_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

type edns0SubnetTransportWrapper struct {
Transport
clientSubnet netip.Addr
clientSubnet netip.Prefix
}

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) {
func SetClientSubnet(message *dns.Msg, clientSubnet netip.Prefix, override bool) {
var (
optRecord *dns.OPT
subnetOption *dns.EDNS0_SUBNET
Expand Down Expand Up @@ -52,11 +52,11 @@ findExists:
optRecord.Option = append(optRecord.Option, subnetOption)
}
subnetOption.Code = dns.EDNS0SUBNET
if clientSubnet.Is4() {
if clientSubnet.Addr().Is4() {
subnetOption.Family = 1
} else {
subnetOption.Family = 2
}
subnetOption.SourceNetmask = uint8(clientSubnet.BitLen())
subnetOption.Address = clientSubnet.AsSlice()
subnetOption.SourceNetmask = uint8(clientSubnet.Bits())
subnetOption.Address = clientSubnet.Addr().AsSlice()
}
6 changes: 3 additions & 3 deletions extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func transportNameFromContext(ctx context.Context) (string, bool) {

type clientSubnetKey struct{}

func ContextWithClientSubnet(ctx context.Context, clientSubnet netip.Addr) context.Context {
func ContextWithClientSubnet(ctx context.Context, clientSubnet netip.Prefix) context.Context {
return context.WithValue(ctx, clientSubnetKey{}, clientSubnet)
}

func ClientSubnetFromContext(ctx context.Context) (netip.Addr, bool) {
clientSubnet, ok := ctx.Value(clientSubnetKey{}).(netip.Addr)
func ClientSubnetFromContext(ctx context.Context) (netip.Prefix, bool) {
clientSubnet, ok := ctx.Value(clientSubnetKey{}).(netip.Prefix)
return clientSubnet, ok
}
2 changes: 1 addition & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type TransportOptions struct {
Name string
Dialer N.Dialer
Address string
ClientSubnet netip.Addr
ClientSubnet netip.Prefix
}

var transports map[string]TransportConstructor
Expand Down
2 changes: 1 addition & 1 deletion transport_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type myTransportAdapter struct {
cancel context.CancelFunc
dialer N.Dialer
serverAddr M.Socksaddr
clientAddr netip.Addr
clientAddr netip.Prefix
handler myTransportHandler
access sync.Mutex
conn *dnsConnection
Expand Down

0 comments on commit f52ce53

Please sign in to comment.