set local address binding match prefer ip type

This commit is contained in:
tickstep 2024-04-08 19:36:32 +08:00
parent 54dd4e766b
commit e9ae974017
3 changed files with 17 additions and 5 deletions

View File

@ -210,7 +210,7 @@ func (c *PanConfig) init() error {
// 设置本地网卡地址
if c.LocalAddrs != "" {
ips := ParseLocalAddress(c.LocalAddrs)
ips := ParseLocalAddress(c.LocalAddrs, strings.ToLower(c.PreferIPType))
if len(ips) > 0 {
logger.Verboseln("bind local address list: ", ips)
requester.SetLocalTCPAddrList(ips...)

View File

@ -33,7 +33,7 @@ func (c *PanConfig) SetProxy(proxy string) {
// SetLocalAddrs 设置localAddrs
func (c *PanConfig) SetLocalAddrs(localAddrs string) {
c.LocalAddrs = localAddrs
ips := ParseLocalAddress(localAddrs)
ips := ParseLocalAddress(localAddrs, strings.ToLower(c.PreferIPType))
if len(ips) > 0 {
requester.SetLocalTCPAddrList(ips...)
}

View File

@ -139,7 +139,7 @@ func RandomDeviceId() string {
}
// ParseLocalAddress 解析网络接口配置为对应的本地IP地址
func ParseLocalAddress(localAddrs string) []string {
func ParseLocalAddress(localAddrs string, filterIpType string) []string {
allLocalAddress, _ := nets.GetLocalNetInterfaceAddress()
localAddrNames := strings.Split(localAddrs, ",")
ips := []string{}
@ -151,10 +151,22 @@ func ParseLocalAddress(localAddrs string) []string {
// maybe local interface name
if localAddr := allLocalAddress.GetByName(addr); localAddr != nil {
if localAddr.IPv4 != "" {
ips = append(ips, localAddr.IPv4)
if filterIpType == "any" || filterIpType == "" { // 不限制
ips = append(ips, localAddr.IPv4)
} else {
if filterIpType == "ipv4" {
ips = append(ips, localAddr.IPv4)
}
}
}
if localAddr.IPv6 != "" {
ips = append(ips, localAddr.IPv6)
if filterIpType == "any" || filterIpType == "" { // 不限制
ips = append(ips, localAddr.IPv6)
} else {
if filterIpType == "ipv6" {
ips = append(ips, localAddr.IPv6)
}
}
}
}
} else {