Allow to send through random IPv6

This commit is contained in:
风扇滑翔翼 2024-03-21 10:15:07 +00:00 committed by yuhan6665
parent 657c5c8570
commit 70a5fe9a25
4 changed files with 84 additions and 40 deletions

View file

@ -4,6 +4,8 @@ import (
"context"
"errors"
"io"
"math/rand"
gonet "net"
"os"
"github.com/xtls/xray-core/app/proxyman"
@ -269,7 +271,11 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connecti
outbound = new(session.Outbound)
ctx = session.ContextWithOutbound(ctx, outbound)
}
outbound.Gateway = h.senderSettings.Via.AsAddress()
if h.senderSettings.ViaCidr == "" {
outbound.Gateway = h.senderSettings.Via.AsAddress()
} else { //Get a random address.
outbound.Gateway = ParseRandomIPv6(h.senderSettings.Via.AsAddress(), h.senderSettings.ViaCidr)
}
}
}
@ -312,3 +318,17 @@ func (h *Handler) Close() error {
common.Close(h.mux)
return nil
}
// Return random IPv6 in a CIDR block
func ParseRandomIPv6(address net.Address, prefix string) net.Address {
addr := address.IP().String()
_, network, _ := gonet.ParseCIDR(addr + "/" + prefix)
ipv6 := network.IP.To16()
prefixLen, _ := network.Mask.Size()
for i := prefixLen / 8; i < 16; i++ {
ipv6[i] = byte(rand.Intn(256))
}
return net.ParseAddress(gonet.IP(ipv6).String())
}