feat: 支持多网卡模式
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Mmx233/BitSrunLoginGo/global"
|
"github.com/Mmx233/BitSrunLoginGo/global"
|
||||||
|
srunModels "github.com/Mmx233/BitSrunLoginGo/models"
|
||||||
"github.com/Mmx233/BitSrunLoginGo/util"
|
"github.com/Mmx233/BitSrunLoginGo/util"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -27,9 +28,10 @@ func Guardian(output bool) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
_ = recover()
|
_ = recover()
|
||||||
}()
|
}()
|
||||||
if !util.Checker.NetOk(global.Config.Settings.Timeout) {
|
if global.Config.Settings.Interfaces == "" { //单网卡
|
||||||
|
if !util.Checker.NetOk(global.Config.Settings.Timeout, nil) {
|
||||||
util.Log.Println("Network down, trying to login")
|
util.Log.Println("Network down, trying to login")
|
||||||
e := Login(output, true)
|
e := Login(output, true, nil)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
util.Log.Println("Error: ", e)
|
util.Log.Println("Error: ", e)
|
||||||
}
|
}
|
||||||
@@ -38,6 +40,26 @@ func Guardian(output bool) {
|
|||||||
util.Log.Println("Network ok")
|
util.Log.Println("Network ok")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else { //多网卡
|
||||||
|
interfaces, e := util.GetInterfaceAddr()
|
||||||
|
if e == nil {
|
||||||
|
var down []srunModels.Eth
|
||||||
|
for _, eth := range interfaces {
|
||||||
|
if !util.Checker.NetOk(global.Config.Settings.Timeout, eth.Addr) {
|
||||||
|
util.Log.Println(eth.Name + " network down")
|
||||||
|
down = append(down, eth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, eth := range down {
|
||||||
|
e := Login(output, true, eth.Addr)
|
||||||
|
if e != nil {
|
||||||
|
util.Log.Println(eth.Name+" login error: ", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c <- false
|
c <- false
|
||||||
}()
|
}()
|
||||||
<-c
|
<-c
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import (
|
|||||||
"github.com/Mmx233/BitSrunLoginGo/global"
|
"github.com/Mmx233/BitSrunLoginGo/global"
|
||||||
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
|
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
|
||||||
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||||
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Login 登录逻辑
|
// Login 登录逻辑
|
||||||
func Login(output bool, skipCheck bool) error {
|
func Login(output bool, skipCheck bool, localAddr net.Addr) error {
|
||||||
return BitSrun.Login(&srunTransfer.Login{
|
return BitSrun.Login(&srunTransfer.Login{
|
||||||
Demo: global.Config.Settings.DemoMode,
|
Demo: global.Config.Settings.DemoMode,
|
||||||
OutPut: output,
|
OutPut: output,
|
||||||
@@ -17,5 +18,6 @@ func Login(output bool, skipCheck bool) error {
|
|||||||
Form: &global.Config.Form,
|
Form: &global.Config.Form,
|
||||||
Meta: &global.Config.Meta,
|
Meta: &global.Config.Meta,
|
||||||
},
|
},
|
||||||
|
LocalAddr: localAddr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.17
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Mmx233/config v0.0.3
|
github.com/Mmx233/config v0.0.3
|
||||||
github.com/Mmx233/tool v0.3.8
|
github.com/Mmx233/tool v0.3.9
|
||||||
github.com/PuerkitoBio/goquery v1.8.0 // indirect
|
github.com/PuerkitoBio/goquery v1.8.0 // indirect
|
||||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||||
github.com/howeyc/fsnotify v0.9.0
|
github.com/howeyc/fsnotify v0.9.0
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -1,7 +1,7 @@
|
|||||||
github.com/Mmx233/config v0.0.3 h1:E744hob5fWD5P7m7q+91Z41R+Y1LDkxIhQXIeV1tel0=
|
github.com/Mmx233/config v0.0.3 h1:E744hob5fWD5P7m7q+91Z41R+Y1LDkxIhQXIeV1tel0=
|
||||||
github.com/Mmx233/config v0.0.3/go.mod h1:3mt+LSHUvD/C6tq1ge/Dmp8xKivFmo7gxSihCPjIAi0=
|
github.com/Mmx233/config v0.0.3/go.mod h1:3mt+LSHUvD/C6tq1ge/Dmp8xKivFmo7gxSihCPjIAi0=
|
||||||
github.com/Mmx233/tool v0.3.8 h1:6CQcxAbK3/bSn6/MacUvXpclhCsgHCa/Dt7+H+ZstTQ=
|
github.com/Mmx233/tool v0.3.9 h1:yeb1uCD8gKcx5g62XQZuxAEHaOxILWyR0Uf9VMECoac=
|
||||||
github.com/Mmx233/tool v0.3.8/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
github.com/Mmx233/tool v0.3.9/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
||||||
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
|
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
|
||||||
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
|
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
|
||||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||||
@@ -9,8 +9,6 @@ github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEq
|
|||||||
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
|
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
|
||||||
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
|
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
|
||||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
|
|
||||||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c h1:WtYZ93XtWSO5KlOMgPZu7hXY9WhMZpprvlm5VwvAl8c=
|
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c h1:WtYZ93XtWSO5KlOMgPZu7hXY9WhMZpprvlm5VwvAl8c=
|
||||||
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|||||||
19
main.go
19
main.go
@@ -15,7 +15,11 @@ func main() {
|
|||||||
} else if global.Config.Settings.Guardian.Enable {
|
} else if global.Config.Settings.Guardian.Enable {
|
||||||
//进入守护模式流程
|
//进入守护模式流程
|
||||||
controllers.EnterGuardian()
|
controllers.EnterGuardian()
|
||||||
} else if err := controllers.Login(true, false); err != nil { //单次登录模式
|
}
|
||||||
|
|
||||||
|
//单次登录模式
|
||||||
|
if global.Config.Settings.Interfaces == "" { //单网卡
|
||||||
|
if err := controllers.Login(true, false, nil); err != nil {
|
||||||
util.Log.Println("运行出错,状态异常")
|
util.Log.Println("运行出错,状态异常")
|
||||||
if global.Config.Settings.DemoMode {
|
if global.Config.Settings.DemoMode {
|
||||||
util.Log.Fatalln(err)
|
util.Log.Fatalln(err)
|
||||||
@@ -24,4 +28,17 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else { //多网卡
|
||||||
|
interfaces, e := util.GetInterfaceAddr()
|
||||||
|
if e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, eth := range interfaces {
|
||||||
|
if err := controllers.Login(true, false, eth.Addr); err != nil {
|
||||||
|
util.Log.Println(eth.Name + "运行出错,状态异常")
|
||||||
|
util.Log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type Guardian struct {
|
|||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
Timeout uint `json:"timeout"`
|
Timeout uint `json:"timeout"`
|
||||||
|
Interfaces string `json:"interfaces"`
|
||||||
DemoMode bool `json:"demo_mode"`
|
DemoMode bool `json:"demo_mode"`
|
||||||
Guardian Guardian
|
Guardian Guardian
|
||||||
Daemon Daemon
|
Daemon Daemon
|
||||||
|
|||||||
8
models/eth.go
Normal file
8
models/eth.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package srunModels
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
|
type Eth struct {
|
||||||
|
Name string
|
||||||
|
Addr net.Addr
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Mmx233/tool"
|
"github.com/Mmx233/tool"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,11 +11,12 @@ type checker struct{}
|
|||||||
var Checker checker
|
var Checker checker
|
||||||
|
|
||||||
// NetOk 网络状况检查
|
// NetOk 网络状况检查
|
||||||
func (checker) NetOk(timeout uint) bool {
|
func (checker) NetOk(timeout uint, localAddr net.Addr) bool {
|
||||||
h, i, e := tool.HTTP.GetReader(&tool.GetRequest{
|
h, i, e := tool.HTTP.GetReader(&tool.GetRequest{
|
||||||
Url: "https://www.baidu.com/",
|
Url: "https://www.baidu.com/",
|
||||||
Redirect: false,
|
Redirect: false,
|
||||||
Timeout: time.Duration(timeout) * time.Second,
|
Timeout: time.Duration(timeout) * time.Second,
|
||||||
|
LocalAddr: localAddr,
|
||||||
})
|
})
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
|
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
|
||||||
if c.CheckNet {
|
if c.CheckNet {
|
||||||
util.Log.Println("Step0: 检查状态…")
|
util.Log.Println("Step0: 检查状态…")
|
||||||
if util.Checker.NetOk(c.Timeout) {
|
if util.Checker.NetOk(c.Timeout, c.LocalAddr) {
|
||||||
util.Log.Println("网络 ok")
|
util.Log.Println("网络 ok")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
|
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
|
||||||
Url: G.UrlLoginPage,
|
Url: G.UrlLoginPage,
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
|
LocalAddr: c.LocalAddr,
|
||||||
}); e != nil {
|
}); e != nil {
|
||||||
return e
|
return e
|
||||||
} else if G.Ip, e = util.GetIp(body); e != nil {
|
} else if G.Ip, e = util.GetIp(body); e != nil {
|
||||||
@@ -44,6 +45,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
"ip": G.Ip,
|
"ip": G.Ip,
|
||||||
},
|
},
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
|
LocalAddr: c.LocalAddr,
|
||||||
}); e != nil {
|
}); e != nil {
|
||||||
return e
|
return e
|
||||||
} else if G.Token, e = util.GetToken(data); e != nil {
|
} else if G.Token, e = util.GetToken(data); e != nil {
|
||||||
@@ -92,6 +94,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
"_": time.Now().UnixNano(),
|
"_": time.Now().UnixNano(),
|
||||||
},
|
},
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
|
LocalAddr: c.LocalAddr,
|
||||||
}); e != nil {
|
}); e != nil {
|
||||||
return e
|
return e
|
||||||
} else if G.LoginResult, e = util.GetResult(res); e != nil {
|
} else if G.LoginResult, e = util.GetResult(res); e != nil {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package srunTransfer
|
package srunTransfer
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
type LoginForm struct {
|
type LoginForm struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
UserName string `json:"username"`
|
UserName string `json:"username"`
|
||||||
@@ -30,4 +32,6 @@ type Login struct {
|
|||||||
Timeout uint
|
Timeout uint
|
||||||
//登录参数,不可缺省
|
//登录参数,不可缺省
|
||||||
LoginInfo LoginInfo
|
LoginInfo LoginInfo
|
||||||
|
//出口地址
|
||||||
|
LocalAddr net.Addr
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user