diff --git a/controllers/guardian.go b/controllers/guardian.go index ff7805c..8d794d5 100644 --- a/controllers/guardian.go +++ b/controllers/guardian.go @@ -1,12 +1,13 @@ package controllers import ( - "github.com/Mmx233/BitSrunLoginGo/global" - "github.com/Mmx233/BitSrunLoginGo/util" - log "github.com/sirupsen/logrus" "os" "os/exec" "time" + + "github.com/Mmx233/BitSrunLoginGo/global" + "github.com/Mmx233/BitSrunLoginGo/util" + log "github.com/sirupsen/logrus" ) // Guardian 守护模式逻辑 @@ -30,13 +31,13 @@ func Guardian() { if global.Config.Settings.Basic.Interfaces == "" { //单网卡 e := Login(nil) if e != nil { - log.Errorln("登陆失败: ", e) + log.Errorln("错误: ", e) } } else { //多网卡 interfaces, e := util.GetInterfaceAddr() if e == nil { for _, eth := range interfaces { - log.Infoln(eth.Name) + log.Debugln("登录使用的网口: " + eth.Name) e = Login(eth.Addr) if e != nil { log.Errorln("网口 ", eth.Name+" 登录失败: ", e) @@ -54,12 +55,12 @@ func Guardian() { // EnterGuardian 守护模式入口,控制是否进入daemon func EnterGuardian() { - log.Infoln("[Guardian mode]") + log.Infoln("[以守护模式启动]") 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 { log.Fatalln("启动守护失败: ", err) } - log.Infoln("[Daemon mode entered]") + log.Infoln("[进入后台进程模式]") return } Guardian() diff --git a/global/log.go b/global/log.go index 98a9151..5cbb8fc 100644 --- a/global/log.go +++ b/global/log.go @@ -1,11 +1,13 @@ package global import ( - log "github.com/sirupsen/logrus" "io" "os" "strings" "time" + + nested "github.com/antonfisher/nested-logrus-formatter" + log "github.com/sirupsen/logrus" ) func initLog() { @@ -27,7 +29,7 @@ func initLog() { Config.Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log" } - f, e := os.OpenFile(Config.Settings.Log.FilePath+Config.Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700) + f, e := os.OpenFile(Config.Settings.Log.FilePath+Config.Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if e != nil { log.Fatalln(e) } @@ -35,5 +37,10 @@ func initLog() { //设置双重输出 mw := io.MultiWriter(os.Stdout, f) log.SetOutput(mw) + log.SetFormatter(&nested.Formatter{ + HideKeys: true, + NoColors: Config.Settings.Log.WriteFile, + TimestampFormat: "2006-01-02 15:04:05", + }) } } diff --git a/go.mod b/go.mod index 362aac1..2f57a66 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( github.com/Mmx233/tool v0.6.5 + github.com/antonfisher/nested-logrus-formatter v1.3.1 github.com/howeyc/fsnotify v0.9.0 github.com/sirupsen/logrus v1.9.0 github.com/spf13/viper v1.13.0 @@ -21,7 +22,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.1 // indirect - golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect + golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index e144a4b..9f93420 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Mmx233/tool v0.6.5 h1:ewVu25vax7nCXaeFURfVgDrhjy4yqhKiZrrmfwcJH64= github.com/Mmx233/tool v0.6.5/go.mod h1:gg+b9qiYcD0DjTCk7PrNd7/ESbLJcSFn2P6pyjcD+B8= +github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= +github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -141,8 +143,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw= -github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI= github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -162,8 +162,6 @@ github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmq github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ= -github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI= github.com/spf13/viper v1.13.0 h1:BWSJ/M+f+3nmdz9bxB+bWX28kkALN2ok11D0rSo8EJU= github.com/spf13/viper v1.13.0/go.mod h1:Icm2xNL3/8uyh/wFuB1jI7TiTNKp8632Nwegu+zgdYw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -173,11 +171,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= -github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs= -github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo= github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -316,10 +311,8 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= -golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go index 46e1280..7b70e81 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ func main() { log.Debugln("多网卡模式") interfaces, _ := util.GetInterfaceAddr() for _, eth := range interfaces { - log.Infoln("网卡: ", eth.Name) + log.Debugln("使用网卡: ", eth.Name) if err = controllers.Login(eth.Addr); err != nil { log.Errorln("运行出错,状态异常: ", err) } diff --git a/models/config.go b/models/config.go index 3a0b18b..21fc50c 100644 --- a/models/config.go +++ b/models/config.go @@ -1,6 +1,6 @@ package srunModels -import "github.com/Mmx233/BitSrunLoginGo/v1/transfer" +import srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer" type Daemon struct { Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"` diff --git a/v1/login.go b/v1/login.go index 3ad1b7e..7244af9 100644 --- a/v1/login.go +++ b/v1/login.go @@ -2,9 +2,9 @@ package BitSrun import ( "encoding/json" - "errors" + "github.com/Mmx233/BitSrunLoginGo/util" - "github.com/Mmx233/BitSrunLoginGo/v1/transfer" + srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer" log "github.com/sirupsen/logrus" ) @@ -24,18 +24,19 @@ func Login(c *srunTransfer.Login) error { var ok bool { - log.Infoln("Step.0: 正在检查状态") + log.Debugln("正在检查登录状态") res, e := api.GetUserInfo() if e != nil { return e } err := res["error"].(string) if err == "ok" { - log.Infoln("--已登录--") + log.Debugln("用户已登录~") return nil } + log.Infoln("检测到用户未登录,开始尝试登录...") - log.Infoln("Step.1: 正在获取客户端ip") + log.Debugln("正在获取客户端 IP") var ip interface{} ip, ok = res["client_ip"] if !ok { @@ -48,7 +49,7 @@ func Login(c *srunTransfer.Login) error { log.Debugln("ip: ", G.Ip) } - log.Infoln("Step.2: 正在获取token") + log.Debugln("正在获取 Token") { res, e := api.GetChallenge(G.Form.UserName, G.Ip) if e != nil { @@ -63,7 +64,7 @@ func Login(c *srunTransfer.Login) error { log.Debugln("token: ", G.Token) } - log.Infoln("Step.3: 执行登录…") + log.Debugln("发送登录请求") { info, e := json.Marshal(map[string]string{ "username": G.Form.UserName, @@ -105,9 +106,11 @@ func Login(c *srunTransfer.Login) error { } G.LoginResult = result.(string) - log.Infoln("登录结果: " + G.LoginResult) - if G.LoginResult != "ok" { - return errors.New(G.LoginResult) + if G.LoginResult == "ok" { + log.Infoln("已成功登录~") + } else { + log.Errorln("登陆失败: ", G.LoginResult) + return nil } }