improve: 使用标准项目结构

This commit is contained in:
Mmx233
2023-03-01 18:58:11 +08:00
parent cb11426bd6
commit 3c63e9ddc3
29 changed files with 178 additions and 183 deletions

61
tools/eth.go Normal file
View File

@@ -0,0 +1,61 @@
package tools
import (
"github.com/Mmx233/BitSrunLoginGo/internal/global"
log "github.com/sirupsen/logrus"
"net"
"regexp"
"strings"
)
type Eth struct {
Name string
Addr net.Addr
}
func GetInterfaceAddr() ([]Eth, error) {
var result []Eth
interfaces, e := net.Interfaces()
if e != nil {
return nil, e
}
reg, e := regexp.Compile(global.Config.Settings.Basic.Interfaces)
if e != nil {
log.Fatalln("interfaces设置异常无法解析: ", e)
}
for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) {
addrs, e := eth.Addrs()
if e != nil {
log.Warnln(eth.Name+" 网卡地址获取失败: ", e)
continue
}
for _, addr := range addrs {
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
}
result = append(result, Eth{
Name: eth.Name,
Addr: ip,
})
break
}
}
} else {
log.Debugf("网卡 %s 不匹配", eth.Name)
}
}
log.Debugln("有效匹配网卡:", result)
if len(result) == 0 {
log.Warnln("没有扫描到有效匹配网卡")
}
return result, nil
}