fix: 携程溢出、优化若干
This commit is contained in:
@@ -60,7 +60,7 @@ Config.json说明:
|
|||||||
"enc": "srun_bx1"
|
"enc": "srun_bx1"
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"timeout": 1, //检查网络超时时间(秒)
|
"timeout": 1, //网络请求超时时间(秒)
|
||||||
"interfaces": "",//网卡名称正则(注意JSON转义),如:eth0\\.[2-3]
|
"interfaces": "",//网卡名称正则(注意JSON转义),如:eth0\\.[2-3]
|
||||||
"demo_mode": false, //测试模式,报错更详细,且生成运行日志与错误日志
|
"demo_mode": false, //测试模式,报错更详细,且生成运行日志与错误日志
|
||||||
"guardian": { //守护模式
|
"guardian": { //守护模式
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
// Guardian 守护模式逻辑
|
// Guardian 守护模式逻辑
|
||||||
func Guardian(output bool) {
|
func Guardian(output bool) {
|
||||||
util.Log.OutPut = output
|
util.Log.OutPut = output
|
||||||
|
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
|
||||||
|
|
||||||
if global.Config.Settings.Daemon.Enable {
|
if global.Config.Settings.Daemon.Enable {
|
||||||
go Daemon.DaemonChan()
|
go Daemon.DaemonChan()
|
||||||
@@ -29,7 +30,7 @@ func Guardian(output bool) {
|
|||||||
_ = recover()
|
_ = recover()
|
||||||
}()
|
}()
|
||||||
if global.Config.Settings.Interfaces == "" { //单网卡
|
if global.Config.Settings.Interfaces == "" { //单网卡
|
||||||
if !util.Checker.NetOk(global.Config.Settings.Timeout, nil) {
|
if !util.Checker.NetOk(global.Transports(nil)) {
|
||||||
util.Log.Println("Network down, trying to login")
|
util.Log.Println("Network down, trying to login")
|
||||||
e := Login(output, true, nil)
|
e := Login(output, true, nil)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@@ -45,13 +46,14 @@ func Guardian(output bool) {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
var down []srunModels.Eth
|
var down []srunModels.Eth
|
||||||
for _, eth := range interfaces {
|
for _, eth := range interfaces {
|
||||||
if !util.Checker.NetOk(global.Config.Settings.Timeout, eth.Addr) {
|
if !util.Checker.NetOk(global.Transports(eth.Addr)) {
|
||||||
util.Log.Println(eth.Name + " network down")
|
util.Log.Println(eth.Name + " network down")
|
||||||
down = append(down, eth)
|
down = append(down, eth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, eth := range down {
|
for _, eth := range down {
|
||||||
|
util.Log.Println(eth.Name)
|
||||||
e := Login(output, true, eth.Addr)
|
e := Login(output, true, eth.Addr)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
util.Log.Println(eth.Name+" login error: ", e)
|
util.Log.Println(eth.Name+" login error: ", e)
|
||||||
@@ -63,13 +65,12 @@ func Guardian(output bool) {
|
|||||||
c <- false
|
c <- false
|
||||||
}()
|
}()
|
||||||
<-c
|
<-c
|
||||||
time.Sleep(time.Duration(global.Config.Settings.Guardian.Duration) * time.Second)
|
time.Sleep(GuardianDuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnterGuardian 守护模式入口,控制是否进入daemon
|
// EnterGuardian 守护模式入口,控制是否进入daemon
|
||||||
func EnterGuardian() {
|
func EnterGuardian() {
|
||||||
util.Log.OutPut = true
|
|
||||||
util.Log.Println("[Guardian mode]")
|
util.Log.Println("[Guardian mode]")
|
||||||
if global.Config.Settings.Daemon.Enable || global.Flags.Daemon {
|
if global.Config.Settings.Daemon.Enable || global.Flags.Daemon {
|
||||||
if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil {
|
if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil {
|
||||||
|
|||||||
@@ -13,11 +13,10 @@ func Login(output bool, skipCheck bool, localAddr net.Addr) error {
|
|||||||
Demo: global.Config.Settings.DemoMode,
|
Demo: global.Config.Settings.DemoMode,
|
||||||
OutPut: output,
|
OutPut: output,
|
||||||
CheckNet: !skipCheck,
|
CheckNet: !skipCheck,
|
||||||
Timeout: global.Config.Settings.Timeout,
|
|
||||||
LoginInfo: srunTransfer.LoginInfo{
|
LoginInfo: srunTransfer.LoginInfo{
|
||||||
Form: &global.Config.Form,
|
Form: &global.Config.Form,
|
||||||
Meta: &global.Config.Meta,
|
Meta: &global.Config.Meta,
|
||||||
},
|
},
|
||||||
LocalAddr: localAddr,
|
Transport: global.Transports(localAddr),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,13 @@ import (
|
|||||||
"github.com/Mmx233/config"
|
"github.com/Mmx233/config"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config srunModels.Config
|
var Config srunModels.Config
|
||||||
|
|
||||||
|
var Timeout time.Duration
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initFlags()
|
initFlags()
|
||||||
|
|
||||||
@@ -48,4 +51,7 @@ func init() {
|
|||||||
log.Println("读取配置文件失败:\n", e.Error())
|
log.Println("读取配置文件失败:\n", e.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Timeout = time.Duration(Config.Settings.Timeout) * time.Second
|
||||||
|
initTransport()
|
||||||
}
|
}
|
||||||
|
|||||||
37
global/transport.go
Normal file
37
global/transport.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Mmx233/tool"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var transport *http.Transport
|
||||||
|
|
||||||
|
var transports map[net.Addr]*http.Transport
|
||||||
|
|
||||||
|
func Transports(addr net.Addr) *http.Transport {
|
||||||
|
if transport != nil {
|
||||||
|
return transport
|
||||||
|
}
|
||||||
|
if transport, ok := transports[addr]; ok {
|
||||||
|
return transport
|
||||||
|
} else {
|
||||||
|
transport = tool.HTTP.GenTransport(&tool.GenTransport{
|
||||||
|
Timeout: Timeout,
|
||||||
|
LocalAddr: addr,
|
||||||
|
})
|
||||||
|
transports[addr] = transport
|
||||||
|
return transport
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initTransport() {
|
||||||
|
if Config.Settings.Interfaces == "" {
|
||||||
|
transport = tool.HTTP.GenTransport(&tool.GenTransport{
|
||||||
|
Timeout: Timeout,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
transports = make(map[net.Addr]*http.Transport, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
7
go.mod
7
go.mod
@@ -4,9 +4,12 @@ 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.9
|
github.com/Mmx233/tool v0.5.7
|
||||||
|
github.com/howeyc/fsnotify v0.9.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
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
|
|
||||||
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c // indirect
|
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
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.9 h1:yeb1uCD8gKcx5g62XQZuxAEHaOxILWyR0Uf9VMECoac=
|
github.com/Mmx233/tool v0.5.7 h1:KM9CrGenqc48cKop50hBujIfiSPHbPfn2JZ2RhN3A+g=
|
||||||
github.com/Mmx233/tool v0.3.9/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
github.com/Mmx233/tool v0.5.7/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=
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
util.Log.Demo = global.Config.Settings.DemoMode
|
util.Log.Demo = global.Config.Settings.DemoMode
|
||||||
|
util.Log.OutPut = true
|
||||||
defer util.Log.CatchRecover()
|
defer util.Log.CatchRecover()
|
||||||
|
|
||||||
if global.Flags.RunningDaemon {
|
if global.Flags.RunningDaemon {
|
||||||
@@ -34,7 +35,6 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, eth := range interfaces {
|
for _, eth := range interfaces {
|
||||||
util.Log.OutPut = true
|
|
||||||
util.Log.Println(eth.Name)
|
util.Log.Println(eth.Name)
|
||||||
if err := controllers.Login(true, false, eth.Addr); err != nil {
|
if err := controllers.Login(true, false, eth.Addr); err != nil {
|
||||||
util.Log.Println(eth.Name + "运行出错,状态异常")
|
util.Log.Println(eth.Name + "运行出错,状态异常")
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Mmx233/tool"
|
"github.com/Mmx233/tool"
|
||||||
"net"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type checker struct{}
|
type checker struct{}
|
||||||
@@ -11,12 +10,12 @@ type checker struct{}
|
|||||||
var Checker checker
|
var Checker checker
|
||||||
|
|
||||||
// NetOk 网络状况检查
|
// NetOk 网络状况检查
|
||||||
func (checker) NetOk(timeout uint, localAddr net.Addr) bool {
|
func (a *checker) NetOk(transport *http.Transport) 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,
|
Transport: transport,
|
||||||
LocalAddr: localAddr,
|
|
||||||
})
|
})
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return false
|
return false
|
||||||
|
|||||||
15
util/eth.go
15
util/eth.go
@@ -26,14 +26,17 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
|
|||||||
if e != nil {
|
if e != nil {
|
||||||
Log.Println(eth.Name + " 地址获取失败")
|
Log.Println(eth.Name + " 地址获取失败")
|
||||||
}
|
}
|
||||||
for _, ip := range addrs {
|
for _, addr := range addrs {
|
||||||
if strings.Contains(ip.String(), ".") {
|
if strings.Contains(addr.String(), ".") {
|
||||||
|
var ip *net.TCPAddr
|
||||||
|
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
|
||||||
|
if e != nil {
|
||||||
|
Log.Println(eth.Name+" ip解析失败:", e)
|
||||||
|
continue
|
||||||
|
}
|
||||||
result = append(result, srunModels.Eth{
|
result = append(result, srunModels.Eth{
|
||||||
Name: eth.Name,
|
Name: eth.Name,
|
||||||
Addr: func() *net.TCPAddr {
|
Addr: ip,
|
||||||
n, _ := net.ResolveTCPAddr("tcp", ip.String())
|
|
||||||
return n
|
|
||||||
}(),
|
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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, c.LocalAddr) {
|
if util.Checker.NetOk(c.Transport) {
|
||||||
util.Log.Println("网络 ok")
|
util.Log.Println("网络 ok")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -27,7 +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,
|
Transport: c.Transport,
|
||||||
}); 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 {
|
||||||
@@ -45,7 +45,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
"ip": G.Ip,
|
"ip": G.Ip,
|
||||||
},
|
},
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
LocalAddr: c.LocalAddr,
|
Transport: c.Transport,
|
||||||
}); 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 {
|
||||||
@@ -94,7 +94,7 @@ func Login(c *srunTransfer.Login) error {
|
|||||||
"_": time.Now().UnixNano(),
|
"_": time.Now().UnixNano(),
|
||||||
},
|
},
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
LocalAddr: c.LocalAddr,
|
Transport: c.Transport,
|
||||||
}); 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,6 +1,6 @@
|
|||||||
package srunTransfer
|
package srunTransfer
|
||||||
|
|
||||||
import "net"
|
import "net/http"
|
||||||
|
|
||||||
type LoginForm struct {
|
type LoginForm struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
@@ -28,10 +28,7 @@ type Login struct {
|
|||||||
OutPut bool
|
OutPut bool
|
||||||
//登陆前是否检查网络,只在离线时登录
|
//登陆前是否检查网络,只在离线时登录
|
||||||
CheckNet bool
|
CheckNet bool
|
||||||
//网络检查超时时间
|
|
||||||
Timeout uint
|
|
||||||
//登录参数,不可缺省
|
//登录参数,不可缺省
|
||||||
LoginInfo LoginInfo
|
LoginInfo LoginInfo
|
||||||
//出口地址
|
Transport *http.Transport
|
||||||
LocalAddr net.Addr
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user