feat: 支持使用 flag 绑定网卡

This commit is contained in:
Mmx233
2023-06-01 17:58:08 +08:00
parent 2f5651d063
commit d26634f6c7
3 changed files with 49 additions and 20 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/Mmx233/BitSrunLoginGo/internal/global" "github.com/Mmx233/BitSrunLoginGo/internal/global"
"github.com/Mmx233/BitSrunLoginGo/tools" "github.com/Mmx233/BitSrunLoginGo/tools"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net"
) )
func main() { func main() {
@@ -19,7 +20,21 @@ func main() {
//登录流程 //登录流程
var err error var err error
if global.Config.Settings.Basic.Interfaces == "" { //单网卡 if global.Config.Settings.Basic.Interfaces == "" { //单网卡
if err = controllers.Login(nil, false); err != nil { var eth *tools.Eth
if global.Flags.Interface != "" {
netEth, e := net.InterfaceByName(global.Flags.Interface)
if e != nil {
log.Warnf("获取指定网卡 %s 失败,使用默认网卡: %v", global.Flags.Interface, e)
} else {
eth, e = tools.ConvertInterface(*netEth)
if e != nil {
log.Warnf("获取指定网卡 %s ip 地址失败,使用默认网卡: %v", global.Flags.Interface, e)
} else if eth == nil {
log.Warnf("指定网卡 %s 无可用 ip 地址,使用默认网卡", global.Flags.Interface)
}
}
}
if err = controllers.Login(eth, false); err != nil {
log.Errorln("登录出错: ", err) log.Errorln("登录出错: ", err)
if !global.Config.Settings.Log.DebugLevel { if !global.Config.Settings.Log.DebugLevel {
fmt.Printf("开启调试日志debug_level获取详细信息") fmt.Printf("开启调试日志debug_level获取详细信息")
@@ -28,7 +43,7 @@ func main() {
} }
} else { //多网卡 } else { //多网卡
log.Infoln("多网卡模式") log.Infoln("多网卡模式")
interfaces, _ := tools.GetInterfaceAddr() interfaces, _ := tools.GetInterfaceAddr(global.Config.Settings.Basic.Interfaces)
for _, eth := range interfaces { for _, eth := range interfaces {
log.Infoln("使用网卡: ", eth.Name) log.Infoln("使用网卡: ", eth.Name)
if err = controllers.Login(&eth, false); err != nil { if err = controllers.Login(&eth, false); err != nil {

View File

@@ -11,10 +11,13 @@ var Flags struct {
RunningDaemon bool RunningDaemon bool
//强制daemon //强制daemon
Daemon bool Daemon bool
Interface string
} }
func initFlags() { func initFlags() {
flag.StringVar(&Flags.Path, "config", "Config.yaml", "config path") flag.StringVar(&Flags.Path, "config", "Config.yaml", "config path")
flag.StringVar(&Flags.Interface, "interface", "", "specify the eth name")
flag.BoolVar(&Flags.RunningDaemon, "running-daemon", false, "") flag.BoolVar(&Flags.RunningDaemon, "running-daemon", false, "")
flag.BoolVar(&Flags.Daemon, "daemon", false, "") flag.BoolVar(&Flags.Daemon, "daemon", false, "")
flag.Parse() flag.Parse()

View File

@@ -1,7 +1,6 @@
package tools package tools
import ( import (
"github.com/Mmx233/BitSrunLoginGo/internal/global"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net" "net"
"regexp" "regexp"
@@ -13,38 +12,50 @@ type Eth struct {
Addr net.Addr Addr net.Addr
} }
func GetInterfaceAddr() ([]Eth, error) { // ConvertInterface 当没有 ipv4 地址时 eth 可能为 nil
func ConvertInterface(eth net.Interface) (*Eth, error) {
addresses, e := eth.Addrs()
if e != nil {
return nil, e
}
for _, addr := range addresses {
if strings.Contains(addr.String(), ".") {
var ip *net.TCPAddr
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
log.Warnln(eth.Name+" ip解析失败", e)
continue
}
return &Eth{
Name: eth.Name,
Addr: ip,
}, nil
}
}
return nil, nil
}
func GetInterfaceAddr(regexpStr string) ([]Eth, error) {
var result []Eth var result []Eth
interfaces, e := net.Interfaces() interfaces, e := net.Interfaces()
if e != nil { if e != nil {
return nil, e return nil, e
} }
reg, e := regexp.Compile(global.Config.Settings.Basic.Interfaces) reg, e := regexp.Compile(regexpStr)
if e != nil { if e != nil {
log.Fatalln("interfaces设置异常无法解析: ", e) log.Fatalln("interfaces设置异常无法解析: ", e)
} }
for _, eth := range interfaces { for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) { if reg.Match([]byte(eth.Name)) {
addrs, e := eth.Addrs() cEth, e := ConvertInterface(eth)
if e != nil { if e != nil {
log.Warnln(eth.Name+" 网卡地址获取失败: ", e) log.Warnln(eth.Name+" 网卡地址获取失败: ", e)
continue continue
} }
for _, addr := range addrs {
if strings.Contains(addr.String(), ".") { if cEth != nil {
var ip *net.TCPAddr result = append(result, *cEth)
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
log.Warnln(eth.Name+" ip解析失败", e)
continue
}
result = append(result, Eth{
Name: eth.Name,
Addr: ip,
})
break
}
} }
} else { } else {
log.Debugf("网卡 %s 不匹配", eth.Name) log.Debugf("网卡 %s 不匹配", eth.Name)