feat: 添加更多debug日志、checker url可控化

This commit is contained in:
Mmx233
2022-03-02 23:06:29 +08:00
parent fc94672038
commit 53f813c230
8 changed files with 41 additions and 16 deletions

View File

@@ -13,6 +13,7 @@ import (
func Guardian(output bool) {
util.Log.OutPut = output
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
util.Checker.SetUrl(global.Config.Settings.Basic.NetCheckUrl)
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()

View File

@@ -15,6 +15,7 @@ func Login(output bool, skipCheck bool, localAddr net.Addr) error {
WriteLog: global.Config.Settings.Debug.WriteLog,
OutPut: output,
CheckNet: !skipCheck,
CheckNetUrl: global.Config.Settings.Basic.NetCheckUrl,
LoginInfo: srunTransfer.LoginInfo{
Form: &global.Config.Form,
Meta: &global.Config.Meta,

View File

@@ -29,6 +29,7 @@ func readConfig() error {
viper.SetDefault("settings", srunModels.Settings{
Basic: srunModels.Basic{
Timeout: 5,
NetCheckUrl: "https://www.baidu.com/",
},
Daemon: srunModels.Daemon{
Path: ".autoLogin",
@@ -45,21 +46,21 @@ func readConfig() error {
if !tool.File.Exists(Flags.Path) {
e := viper.WriteConfigAs(Flags.Path)
if e != nil {
log.Println("生成配置文件失败:", e)
log.Println("[init] 生成配置文件失败:", e)
return e
}
log.Println("已生成配置文件,请编辑 '" + Flags.Path + "' 然后重试")
log.Println("[init] 已生成配置文件,请编辑 '" + Flags.Path + "' 然后重试")
os.Exit(0)
}
//读取配置文件
viper.SetConfigFile(Flags.Path)
if e := viper.ReadInConfig(); e != nil {
log.Println("读取配置文件失败:", e)
log.Println("[init] 读取配置文件失败:", e)
return e
}
if e := viper.Unmarshal(&Config); e != nil {
log.Println("解析配置文件失败:", e)
log.Println("[init] 解析配置文件失败:", e)
return e
}
@@ -74,6 +75,7 @@ func init() {
os.Exit(1)
}
//初始化常变量
Timeout = time.Duration(Config.Settings.Basic.Timeout) * time.Second
initTransport()
}

View File

@@ -18,6 +18,7 @@ type Basic struct {
Timeout uint `json:"timeout" yaml:"timeout" mapstructure:"timeout"`
Interfaces string `json:"interfaces" yaml:"interfaces" mapstructure:"interfaces"`
SkipNetCheck bool `json:"skip_net_check" yaml:"skip_net_check" mapstructure:"skip_net_check"`
NetCheckUrl string `json:"net_check_url" yaml:"net_check_url" mapstructure:"net_check_url"`
}
type Settings struct {

View File

@@ -5,14 +5,28 @@ import (
"net/http"
)
type checker struct{}
type checker struct {
url string
set bool
}
var Checker checker
var Checker = checker{
url: "https://www.baidu.com/",
}
func (a *checker) SetUrl(url string) {
if a.set {
return
}
a.url = url
a.set = true
}
// NetOk 网络状况检查
func (a *checker) NetOk(transport *http.Transport) bool {
Log.Debug("GET ", a.url)
res, e := tool.HTTP.GetReader(&tool.GetRequest{
Url: "https://www.baidu.com/",
Url: a.url,
Redirect: false,
Transport: transport,
})

View File

@@ -26,7 +26,7 @@ func (c *loG) Init(debug, logFile, outPut bool, path string) error {
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
//日志路径初始化与处理
if c.DebugMode {
if c.DebugMode && c.WriteFile {
if !strings.HasSuffix(path, "/") {
path += "/"
}

View File

@@ -16,6 +16,9 @@ func Login(c *srunTransfer.Login) error {
G := util.GenerateLoginInfo(c.Https, c.LoginInfo.Form, c.LoginInfo.Meta)
if c.CheckNet {
if c.CheckNetUrl != "" {
util.Checker.SetUrl(c.CheckNetUrl)
}
util.Log.Info("Step0: 检查状态…")
if util.Checker.NetOk(c.Transport) {
util.Log.Info("网络 ok")
@@ -35,9 +38,10 @@ func Login(c *srunTransfer.Login) error {
} else if G.Ip, e = util.GetIp(body); e != nil {
return e
}
util.Log.Debug("ip: ", G.Ip)
}
util.Log.Info("Step2: 正在获取Token")
util.Log.Info("Step2: 正在获取token")
{
util.Log.Debug("GET ", G.UrlGetChallengeApi)
if _, data, e := tool.HTTP.GetString(&tool.GetRequest{
@@ -54,6 +58,7 @@ func Login(c *srunTransfer.Login) error {
} else if G.Token, e = util.GetToken(data); e != nil {
return e
}
util.Log.Debug("token: ", G.Token)
}
util.Log.Info("Step3: 执行登录…")

View File

@@ -32,6 +32,7 @@ type Login struct {
OutPut bool
//登陆前是否检查网络,只在离线时登录
CheckNet bool
CheckNetUrl string
//登录参数,不可缺省
LoginInfo LoginInfo
Transport *http.Transport