diff --git a/internal/config/pan_config.go b/internal/config/pan_config.go index b8c15c6..df31549 100644 --- a/internal/config/pan_config.go +++ b/internal/config/pan_config.go @@ -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...) diff --git a/internal/config/pan_config_export.go b/internal/config/pan_config_export.go index 68747ac..4545f83 100644 --- a/internal/config/pan_config_export.go +++ b/internal/config/pan_config_export.go @@ -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...) } diff --git a/internal/config/utils.go b/internal/config/utils.go index e01dc73..e98da1e 100644 --- a/internal/config/utils.go +++ b/internal/config/utils.go @@ -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 {