feat: 支持多网卡模式
This commit is contained in:
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"github.com/Mmx233/tool"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -10,11 +11,12 @@ type checker struct{}
|
||||
var Checker checker
|
||||
|
||||
// NetOk 网络状况检查
|
||||
func (checker) NetOk(timeout uint) bool {
|
||||
func (checker) NetOk(timeout uint, localAddr net.Addr) bool {
|
||||
h, i, e := tool.HTTP.GetReader(&tool.GetRequest{
|
||||
Url: "https://www.baidu.com/",
|
||||
Redirect: false,
|
||||
Timeout: time.Duration(timeout) * time.Second,
|
||||
Url: "https://www.baidu.com/",
|
||||
Redirect: false,
|
||||
Timeout: time.Duration(timeout) * time.Second,
|
||||
LocalAddr: localAddr,
|
||||
})
|
||||
if e != nil {
|
||||
return false
|
||||
|
||||
42
util/eth.go
Normal file
42
util/eth.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/global"
|
||||
srunModels "github.com/Mmx233/BitSrunLoginGo/models"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetInterfaceAddr() ([]srunModels.Eth, error) {
|
||||
var result []srunModels.Eth
|
||||
|
||||
interfaces, e := net.Interfaces()
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
reg, e := regexp.Compile(global.Config.Settings.Interfaces)
|
||||
if e != nil {
|
||||
Log.Println("interfaces设置异常,无法解析")
|
||||
return nil, e
|
||||
}
|
||||
for _, eth := range interfaces {
|
||||
if reg.Match([]byte(eth.Name)) {
|
||||
addrs, e := eth.Addrs()
|
||||
if e != nil {
|
||||
Log.Println(eth.Name + " 地址获取失败")
|
||||
}
|
||||
for _, ip := range addrs {
|
||||
if strings.Contains(ip.String(), ".") {
|
||||
result = append(result, srunModels.Eth{
|
||||
Name: eth.Name,
|
||||
Addr: ip,
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user