support address name for local address binding #402

This commit is contained in:
tickstep 2024-03-26 19:09:58 +08:00
parent 670b5c5dad
commit d1a577810c
4 changed files with 102 additions and 6 deletions

View File

@ -198,10 +198,6 @@ func (c *PanConfig) init() error {
if c.Proxy != "" { if c.Proxy != "" {
requester.SetGlobalProxy(c.Proxy) requester.SetGlobalProxy(c.Proxy)
} }
// 设置本地网卡地址
if c.LocalAddrs != "" {
requester.SetLocalTCPAddrList(strings.Split(c.LocalAddrs, ",")...)
}
// 设置域名解析策略 IPv4 or IPv6 // 设置域名解析策略 IPv4 or IPv6
t := requester.IPAny t := requester.IPAny
@ -212,6 +208,15 @@ func (c *PanConfig) init() error {
} }
requester.SetPreferIPType(t) requester.SetPreferIPType(t)
// 设置本地网卡地址
if c.LocalAddrs != "" {
ips := ParseLocalAddress(c.LocalAddrs)
if len(ips) > 0 {
logger.Verboseln("bind local address list: ", ips)
requester.SetLocalTCPAddrList(ips...)
}
}
return nil return nil
} }

View File

@ -33,7 +33,10 @@ func (c *PanConfig) SetProxy(proxy string) {
// SetLocalAddrs 设置localAddrs // SetLocalAddrs 设置localAddrs
func (c *PanConfig) SetLocalAddrs(localAddrs string) { func (c *PanConfig) SetLocalAddrs(localAddrs string) {
c.LocalAddrs = localAddrs c.LocalAddrs = localAddrs
requester.SetLocalTCPAddrList(strings.Split(localAddrs, ",")...) ips := ParseLocalAddress(localAddrs)
if len(ips) > 0 {
requester.SetLocalTCPAddrList(ips...)
}
} }
func (c *PanConfig) SetPreferIPType(ipType string) { func (c *PanConfig) SetPreferIPType(ipType string) {
@ -112,7 +115,7 @@ func (c *PanConfig) PrintTable() {
[]string{"max_upload_rate", showMaxRate(c.MaxUploadRate), "", "限制单个文件最大上传速度, 0代表不限制"}, []string{"max_upload_rate", showMaxRate(c.MaxUploadRate), "", "限制单个文件最大上传速度, 0代表不限制"},
[]string{"savedir", c.SaveDir, "", "下载文件的储存目录"}, []string{"savedir", c.SaveDir, "", "下载文件的储存目录"},
[]string{"proxy", c.Proxy, "", "设置代理, 支持 http/socks5 代理,例如: http://127.0.0.1:8888 或者 socks5://127.0.0.1:8889"}, []string{"proxy", c.Proxy, "", "设置代理, 支持 http/socks5 代理,例如: http://127.0.0.1:8888 或者 socks5://127.0.0.1:8889"},
[]string{"local_addrs", c.LocalAddrs, "", "设置本地网卡地址, 多个地址用逗号隔开,例如: 127.0.0.1,192.168.100.126"}, []string{"local_addrs", c.LocalAddrs, "", "绑定本地网卡地址, 多个地址用逗号隔开,支持网口名称,例如: 127.0.0.1,192.168.100.126,en0,eth0"},
[]string{"ip_type", c.PreferIPType, "ipv4-优先IPv4ipv6-优先IPv6", "设置域名解析IP优先类型。修改后需要重启应用生效"}, []string{"ip_type", c.PreferIPType, "ipv4-优先IPv4ipv6-优先IPv6", "设置域名解析IP优先类型。修改后需要重启应用生效"},
[]string{"file_record_config", fileRecorderLabel, "1-开启2-禁用", "设置是否开启上传、下载、同步文件的结果记录开启后会把结果记录到CSV文件方便后期查看"}, []string{"file_record_config", fileRecorderLabel, "1-开启2-禁用", "设置是否开启上传、下载、同步文件的结果记录开启后会把结果记录到CSV文件方便后期查看"},
[]string{"device_id", c.DeviceId, "", "客户端ID用于标识登录客户端阿里单个账号最多允许10个客户端同时在线。修改后需要重启应用生效"}, []string{"device_id", c.DeviceId, "", "客户端ID用于标识登录客户端阿里单个账号最多允许10个客户端同时在线。修改后需要重启应用生效"},

View File

@ -17,11 +17,13 @@ import (
"encoding/hex" "encoding/hex"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
"github.com/tickstep/aliyunpan/cmder/cmdtable" "github.com/tickstep/aliyunpan/cmder/cmdtable"
"github.com/tickstep/aliyunpan/library/nets"
"github.com/tickstep/library-go/converter" "github.com/tickstep/library-go/converter"
"github.com/tickstep/library-go/crypto" "github.com/tickstep/library-go/crypto"
"github.com/tickstep/library-go/ids" "github.com/tickstep/library-go/ids"
"github.com/tickstep/library-go/logger" "github.com/tickstep/library-go/logger"
"math/rand" "math/rand"
"net"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -135,3 +137,30 @@ func RandomDeviceId() string {
} }
return str.String() return str.String()
} }
// ParseLocalAddress 解析网络接口配置为对应的本地IP地址
func ParseLocalAddress(localAddrs string) []string {
allLocalAddress, _ := nets.GetLocalNetInterfaceAddress()
localAddrNames := strings.Split(localAddrs, ",")
ips := []string{}
for _, addr := range localAddrNames {
if addr == "" {
continue
}
if net.ParseIP(addr) == nil {
// maybe local interface name
if localAddr := allLocalAddress.GetByName(addr); localAddr != nil {
if localAddr.IPv4 != "" {
ips = append(ips, localAddr.IPv4)
}
if localAddr.IPv6 != "" {
ips = append(ips, localAddr.IPv6)
}
}
} else {
// ip
ips = append(ips, addr)
}
}
return ips
}

59
library/nets/util.go Normal file
View File

@ -0,0 +1,59 @@
package nets
import (
"net"
"strings"
)
type NetInterfaceInfoList []*NetInterfaceInfo
type NetInterfaceInfo struct {
Name string `json:"name"`
Mac string `json:"mac"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
}
func (n *NetInterfaceInfoList) GetByName(name string) *NetInterfaceInfo {
for _, v := range *n {
if v.Name == name {
return v
}
}
return nil
}
// GetLocalNetInterfaceAddress 获取本地接口地址信息
func GetLocalNetInterfaceAddress() (NetInterfaceInfoList, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
netList := NetInterfaceInfoList{}
for _, inter := range interfaces {
netInfo := &NetInterfaceInfo{
Name: inter.Name,
Mac: inter.HardwareAddr.String(),
IPv4: "",
IPv6: "",
}
addrs, err2 := inter.Addrs()
if err2 != nil {
continue
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok {
if ipnet.IP.To4() != nil { // ipv4
if ipnet.IP.String() != "" {
netInfo.IPv4 = ipnet.IP.String()
}
} else if ipnet.IP.To16() != nil && !ipnet.IP.IsLoopback() { // ipv6
if ipnet.IP.String() != "" && !strings.HasPrefix(ipnet.IP.String(), "fe80") { // 去掉本地IPv6地址
netInfo.IPv6 = ipnet.IP.String()
}
}
}
}
netList = append(netList, netInfo)
}
return netList, nil
}