fix: 携程溢出、优化若干

This commit is contained in:
Mmx
2022-01-11 12:30:32 +08:00
parent a240165eed
commit 8b006e77af
12 changed files with 77 additions and 32 deletions

View File

@@ -60,7 +60,7 @@ Config.json说明
"enc": "srun_bx1"
},
"settings": {
"timeout": 1, //检查网络超时时间(秒)
"timeout": 1, //网络请求超时时间(秒)
"interfaces": "",//网卡名称正则注意JSON转义eth0\\.[2-3]
"demo_mode": false, //测试模式,报错更详细,且生成运行日志与错误日志
"guardian": { //守护模式

View File

@@ -12,6 +12,7 @@ import (
// Guardian 守护模式逻辑
func Guardian(output bool) {
util.Log.OutPut = output
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()
@@ -29,7 +30,7 @@ func Guardian(output bool) {
_ = recover()
}()
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")
e := Login(output, true, nil)
if e != nil {
@@ -45,13 +46,14 @@ func Guardian(output bool) {
if e == nil {
var down []srunModels.Eth
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")
down = append(down, eth)
}
}
for _, eth := range down {
util.Log.Println(eth.Name)
e := Login(output, true, eth.Addr)
if e != nil {
util.Log.Println(eth.Name+" login error: ", e)
@@ -63,13 +65,12 @@ func Guardian(output bool) {
c <- false
}()
<-c
time.Sleep(time.Duration(global.Config.Settings.Guardian.Duration) * time.Second)
time.Sleep(GuardianDuration)
}
}
// EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() {
util.Log.OutPut = true
util.Log.Println("[Guardian mode]")
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 {

View File

@@ -13,11 +13,10 @@ func Login(output bool, skipCheck bool, localAddr net.Addr) error {
Demo: global.Config.Settings.DemoMode,
OutPut: output,
CheckNet: !skipCheck,
Timeout: global.Config.Settings.Timeout,
LoginInfo: srunTransfer.LoginInfo{
Form: &global.Config.Form,
Meta: &global.Config.Meta,
},
LocalAddr: localAddr,
Transport: global.Transports(localAddr),
})
}

View File

@@ -6,10 +6,13 @@ import (
"github.com/Mmx233/config"
"log"
"os"
"time"
)
var Config srunModels.Config
var Timeout time.Duration
func init() {
initFlags()
@@ -48,4 +51,7 @@ func init() {
log.Println("读取配置文件失败:\n", e.Error())
os.Exit(1)
}
Timeout = time.Duration(Config.Settings.Timeout) * time.Second
initTransport()
}

37
global/transport.go Normal file
View 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
View File

@@ -4,9 +4,12 @@ go 1.17
require (
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/andybalholm/cascadia v1.3.1 // indirect
github.com/howeyc/fsnotify v0.9.0
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c // indirect
)

4
go.sum
View File

@@ -1,7 +1,7 @@
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/tool v0.3.9 h1:yeb1uCD8gKcx5g62XQZuxAEHaOxILWyR0Uf9VMECoac=
github.com/Mmx233/tool v0.3.9/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
github.com/Mmx233/tool v0.5.7 h1:KM9CrGenqc48cKop50hBujIfiSPHbPfn2JZ2RhN3A+g=
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/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=

View File

@@ -8,6 +8,7 @@ import (
func main() {
util.Log.Demo = global.Config.Settings.DemoMode
util.Log.OutPut = true
defer util.Log.CatchRecover()
if global.Flags.RunningDaemon {
@@ -34,7 +35,6 @@ func main() {
return
}
for _, eth := range interfaces {
util.Log.OutPut = true
util.Log.Println(eth.Name)
if err := controllers.Login(true, false, eth.Addr); err != nil {
util.Log.Println(eth.Name + "运行出错,状态异常")

View File

@@ -2,8 +2,7 @@ package util
import (
"github.com/Mmx233/tool"
"net"
"time"
"net/http"
)
type checker struct{}
@@ -11,12 +10,12 @@ type checker struct{}
var Checker checker
// 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{
Url: "https://www.baidu.com/",
Redirect: false,
Timeout: time.Duration(timeout) * time.Second,
LocalAddr: localAddr,
Transport: transport,
})
if e != nil {
return false

View File

@@ -26,14 +26,17 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
if e != nil {
Log.Println(eth.Name + " 地址获取失败")
}
for _, ip := range addrs {
if strings.Contains(ip.String(), ".") {
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.Println(eth.Name+" ip解析失败", e)
continue
}
result = append(result, srunModels.Eth{
Name: eth.Name,
Addr: func() *net.TCPAddr {
n, _ := net.ResolveTCPAddr("tcp", ip.String())
return n
}(),
Addr: ip,
})
break
}

View File

@@ -16,7 +16,7 @@ func Login(c *srunTransfer.Login) error {
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
if c.CheckNet {
util.Log.Println("Step0: 检查状态…")
if util.Checker.NetOk(c.Timeout, c.LocalAddr) {
if util.Checker.NetOk(c.Transport) {
util.Log.Println("网络 ok")
return nil
}
@@ -27,7 +27,7 @@ func Login(c *srunTransfer.Login) error {
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
Url: G.UrlLoginPage,
Redirect: true,
LocalAddr: c.LocalAddr,
Transport: c.Transport,
}); e != nil {
return e
} else if G.Ip, e = util.GetIp(body); e != nil {
@@ -45,7 +45,7 @@ func Login(c *srunTransfer.Login) error {
"ip": G.Ip,
},
Redirect: true,
LocalAddr: c.LocalAddr,
Transport: c.Transport,
}); e != nil {
return e
} else if G.Token, e = util.GetToken(data); e != nil {
@@ -94,7 +94,7 @@ func Login(c *srunTransfer.Login) error {
"_": time.Now().UnixNano(),
},
Redirect: true,
LocalAddr: c.LocalAddr,
Transport: c.Transport,
}); e != nil {
return e
} else if G.LoginResult, e = util.GetResult(res); e != nil {

View File

@@ -1,6 +1,6 @@
package srunTransfer
import "net"
import "net/http"
type LoginForm struct {
Domain string `json:"domain"`
@@ -28,10 +28,7 @@ type Login struct {
OutPut bool
//登陆前是否检查网络,只在离线时登录
CheckNet bool
//网络检查超时时间
Timeout uint
//登录参数,不可缺省
LoginInfo LoginInfo
//出口地址
LocalAddr net.Addr
Transport *http.Transport
}