feat: 改用 logrus 输出日志

This commit is contained in:
Mmx233
2022-08-14 20:47:23 +08:00
parent 51e06a1596
commit df87d75aca
14 changed files with 107 additions and 181 deletions

View File

@@ -3,44 +3,43 @@ package controllers
import (
"github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/BitSrunLoginGo/util"
log "github.com/sirupsen/logrus"
"os"
"os/exec"
"time"
)
// Guardian 守护模式逻辑
func Guardian(output bool) {
util.Log.OutPut = output
func Guardian() {
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()
if e := Daemon.MarkDaemon(); e != nil {
util.Log.Warn("写入daemon标记文件失败: ", e)
log.Warnln("写入daemon标记文件失败: ", e)
}
}
var c = make(chan bool)
for {
util.Log.OutPut = output
go func() {
defer func() {
_ = recover()
}()
if global.Config.Settings.Basic.Interfaces == "" { //单网卡
e := Login(output, nil)
e := Login(nil)
if e != nil {
util.Log.Warn("登陆失败: ", e)
log.Errorln("登陆失败: ", e)
}
} else { //多网卡
interfaces, e := util.GetInterfaceAddr()
if e == nil {
for _, eth := range interfaces {
util.Log.Info(eth.Name)
e := Login(output, eth.Addr)
log.Infoln(eth.Name)
e = Login(eth.Addr)
if e != nil {
util.Log.Warn("网口 ", eth.Name+" 登录失败: ", e)
log.Errorln("网口 ", eth.Name+" 登录失败: ", e)
}
}
}
@@ -55,13 +54,13 @@ func Guardian(output bool) {
// EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() {
util.Log.Info("[Guardian mode]")
log.Infoln("[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 {
util.Log.Fatal("启动守护失败: ", err)
log.Fatalln("启动守护失败: ", err)
}
util.Log.Info("[Daemon mode entered]")
log.Infoln("[Daemon mode entered]")
return
}
Guardian(true)
Guardian()
}

View File

@@ -9,12 +9,9 @@ import (
)
// Login 登录逻辑
func Login(output bool, localAddr net.Addr) error {
func Login(localAddr net.Addr) error {
return BitSrun.Login(&srunTransfer.Login{
Https: global.Config.Settings.Basic.Https,
Debug: global.Config.Settings.Debug.Enable,
WriteLog: global.Config.Settings.Debug.WriteLog,
OutPut: output,
Https: global.Config.Settings.Basic.Https,
LoginInfo: srunTransfer.LoginInfo{
Form: &global.Config.Form,
Meta: &global.Config.Meta,

View File

@@ -65,15 +65,3 @@ func readConfig() error {
return nil
}
func init() {
initFlags()
//配置文件初始化
if readConfig() != nil {
os.Exit(1)
}
//初始化常变量
Timeout = time.Duration(Config.Settings.Basic.Timeout) * time.Second
}

21
global/init.go Normal file
View File

@@ -0,0 +1,21 @@
package global
import (
"os"
"time"
)
func init() {
initFlags()
//配置文件初始化
if readConfig() != nil {
os.Exit(1)
}
//初始化常变量
Timeout = time.Duration(Config.Settings.Basic.Timeout) * time.Second
//初始化日志配置
initLog()
}

34
global/log.go Normal file
View File

@@ -0,0 +1,34 @@
package global
import (
log "github.com/sirupsen/logrus"
"io"
"os"
"strings"
)
func initLog() {
if Config.Settings.Debug.Enable {
log.SetLevel(log.DebugLevel)
if Config.Settings.Debug.WriteLog {
//日志路径初始化与处理
if !strings.HasSuffix(Config.Settings.Debug.LogPath, "/") {
Config.Settings.Debug.LogPath += "/"
}
e := os.MkdirAll(Config.Settings.Debug.LogPath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
f, e := os.OpenFile(Config.Settings.Debug.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
}
}
}

1
go.mod
View File

@@ -5,6 +5,7 @@ go 1.19
require (
github.com/Mmx233/tool v0.6.5
github.com/howeyc/fsnotify v0.9.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.12.0
)

33
go.sum
View File

@@ -57,6 +57,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -98,6 +99,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -128,19 +130,17 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
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.0-beta.8 h1:dy81yyLYJDwMTifq24Oi/IslOslRrDSb3jwDggjz3Z0=
github.com/pelletier/go-toml/v2 v2.0.0-beta.8/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
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/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -149,20 +149,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
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.11.0 h1:7OX/1FS6n7jHD1zGrZTM7WtY13ZELRyosK4k93oPr44=
github.com/spf13/viper v1.11.0/go.mod h1:djo0X/bA5+tYVoCn+C7cAYJGcVn/qYLFTG8gdUsX7Zk=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -170,11 +167,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
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/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -312,10 +306,7 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 h1:wEZYwx+kK+KlZ0hpvP2Ls1Xr4+RWnlzGFwPP0aiDjIU=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/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/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
@@ -473,18 +464,12 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

25
main.go
View File

@@ -4,38 +4,29 @@ import (
"github.com/Mmx233/BitSrunLoginGo/controllers"
"github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/BitSrunLoginGo/util"
"log"
log "github.com/sirupsen/logrus"
)
func main() {
if e := util.Log.Init(
global.Config.Settings.Debug.Enable,
global.Config.Settings.Debug.WriteLog,
true,
global.Config.Settings.Debug.LogPath,
); e != nil {
log.Fatalln("初始化日志失败: ", e)
}
if global.Flags.RunningDaemon {
//后台挂起模式中
controllers.Guardian(false)
controllers.Guardian()
} else if global.Config.Settings.Guardian.Enable {
//进入守护模式流程
controllers.EnterGuardian()
} else {
//单次登录模式
if global.Config.Settings.Basic.Interfaces == "" { //单网卡
if err := controllers.Login(true, nil); err != nil {
util.Log.Fatal("运行出错,状态异常: ", err)
if err := controllers.Login(nil); err != nil {
log.Fatalln("运行出错,状态异常: ", err)
}
} else { //多网卡
util.Log.Debug("多网卡模式")
log.Debugln("多网卡模式")
interfaces, _ := util.GetInterfaceAddr()
for _, eth := range interfaces {
util.Log.Info("网卡: ", eth.Name)
if err := controllers.Login(true, eth.Addr); err != nil {
util.Log.Warn("运行出错,状态异常: ", err)
log.Infoln("网卡: ", eth.Name)
if err := controllers.Login(eth.Addr); err != nil {
log.Errorln("运行出错,状态异常: ", err)
}
}
}

View File

@@ -1,9 +1,11 @@
package util
import log "github.com/sirupsen/logrus"
func getbyte(a byte) int {
x := int(a)
if x > 255 {
Log.Fatal("INVALID_CHARACTER_ERR: DOM Exception 5")
log.Fatalln("INVALID_CHARACTER_ERR: DOM Exception 5")
}
return x
}

View File

@@ -3,6 +3,7 @@ package util
import (
"github.com/Mmx233/BitSrunLoginGo/global"
srunModels "github.com/Mmx233/BitSrunLoginGo/models"
log "github.com/sirupsen/logrus"
"net"
"regexp"
"strings"
@@ -17,13 +18,13 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
}
reg, e := regexp.Compile(global.Config.Settings.Basic.Interfaces)
if e != nil {
Log.Fatal("interfaces设置异常无法解析: ", e)
log.Fatalln("interfaces设置异常无法解析: ", e)
}
for _, eth := range interfaces {
if reg.Match([]byte(eth.Name)) {
addrs, e := eth.Addrs()
if e != nil {
Log.Warn(eth.Name+" 网卡地址获取失败: ", e)
log.Warnln(eth.Name+" 网卡地址获取失败: ", e)
continue
}
for _, addr := range addrs {
@@ -31,7 +32,7 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) {
var ip *net.TCPAddr
ip, e = net.ResolveTCPAddr("tcp", strings.Split(addr.String(), "/")[0]+":0")
if e != nil {
Log.Warn(eth.Name+" ip解析失败", e)
log.Warnln(eth.Name+" ip解析失败", e)
continue
}
result = append(result, srunModels.Eth{

View File

@@ -1,84 +0,0 @@
package util
import (
"fmt"
"github.com/Mmx233/tool"
"log"
"os"
"strings"
"time"
)
type loG struct {
timeStamp string
WriteFile bool
Path string
OutPut bool
DebugMode bool
}
var Log loG
func (c *loG) Init(debug, logFile, outPut bool, path string) error {
c.DebugMode = debug
c.WriteFile = logFile
c.OutPut = outPut
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
//日志路径初始化与处理
if c.DebugMode && c.WriteFile {
if !strings.HasSuffix(path, "/") {
path += "/"
}
c.Path = path
return os.MkdirAll(path, os.ModePerm)
}
return nil
}
func (c *loG) time() string {
return time.Now().Format("2006/01/02 15:04:05")
}
func (c *loG) WriteLog(name string, a ...interface{}) {
err := tool.File.Add(c.Path+name, c.time()+" "+fmt.Sprint(a...), 700)
if err != nil && c.OutPut {
log.Println(err)
}
}
func (c *loG) print(name string, fatal bool, a ...interface{}) {
a = append([]interface{}{"[" + name + "] "}, a...)
if c.DebugMode && c.WriteFile {
c.WriteLog("Login-"+c.timeStamp+".log", a...)
}
if c.OutPut {
if fatal {
if c.DebugMode {
log.Panicln(a...)
} else {
log.Fatalln(a...)
}
} else {
log.Println(a...)
}
}
}
func (c *loG) Debug(a ...interface{}) {
if c.DebugMode {
c.print("DEBUG", false, a...)
}
}
func (c *loG) Info(a ...interface{}) {
c.print("INFO", false, a...)
}
func (c *loG) Warn(a ...interface{}) {
c.print("WARN", false, a...)
}
func (c *loG) Fatal(a ...interface{}) {
c.print("FATAL", true, a...)
}

View File

@@ -5,13 +5,10 @@ import (
"errors"
"github.com/Mmx233/BitSrunLoginGo/util"
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
log "github.com/sirupsen/logrus"
)
func Login(c *srunTransfer.Login) error {
util.Log.DebugMode = c.Debug
util.Log.WriteFile = c.WriteLog
util.Log.OutPut = c.OutPut
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
api := SrunApi{
BaseUrl: func() string {
@@ -27,18 +24,18 @@ func Login(c *srunTransfer.Login) error {
var ok bool
{
util.Log.Info("Step.0: 正在检查状态")
log.Infoln("Step.0: 正在检查状态")
res, e := api.GetUserInfo()
if e != nil {
return e
}
err := res["error"].(string)
if err == "ok" {
util.Log.Info("--已登录--")
log.Infoln("--已登录--")
return nil
}
util.Log.Info("Step.1: 正在获取客户端ip")
log.Infoln("Step.1: 正在获取客户端ip")
var ip interface{}
ip, ok = res["client_ip"]
if !ok {
@@ -48,10 +45,10 @@ func Login(c *srunTransfer.Login) error {
}
}
G.Ip = ip.(string)
util.Log.Debug("ip: ", G.Ip)
log.Debugln("ip: ", G.Ip)
}
util.Log.Info("Step.2: 正在获取token")
log.Infoln("Step.2: 正在获取token")
{
res, e := api.GetChallenge(G.Form.UserName, G.Ip)
if e != nil {
@@ -63,10 +60,10 @@ func Login(c *srunTransfer.Login) error {
return ErrResultCannotFound
}
G.Token = token.(string)
util.Log.Debug("token: ", G.Token)
log.Debugln("token: ", G.Token)
}
util.Log.Info("Step.3: 执行登录…")
log.Infoln("Step.3: 执行登录…")
{
info, e := json.Marshal(map[string]string{
"username": G.Form.UserName,
@@ -108,7 +105,7 @@ func Login(c *srunTransfer.Login) error {
}
G.LoginResult = result.(string)
util.Log.Info("登录结果: " + G.LoginResult)
log.Infoln("登录结果: " + G.LoginResult)
if G.LoginResult != "ok" {
return errors.New(G.LoginResult)
}

View File

@@ -3,8 +3,8 @@ package BitSrun
import (
"encoding/json"
"fmt"
"github.com/Mmx233/BitSrunLoginGo/util"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
"time"
@@ -16,7 +16,7 @@ type SrunApi struct {
}
func (a *SrunApi) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
util.Log.Debug("HTTP GET ", a.BaseUrl+path)
log.Debugln("HTTP GET ", a.BaseUrl+path)
timestamp := fmt.Sprint(time.Now().UnixNano())
callback := "jQuery" + timestamp
if query == nil {
@@ -29,11 +29,11 @@ func (a *SrunApi) request(path string, query map[string]interface{}) (map[string
Query: query,
})
if e != nil {
util.Log.Debug(e)
log.Debugln(e)
return nil, e
}
util.Log.Debug(res)
log.Debugln(res)
res = strings.TrimPrefix(res, callback+"(")
res = strings.TrimSuffix(res, ")")

View File

@@ -24,12 +24,6 @@ type LoginInfo struct {
type Login struct {
//调用API时直接访问https URL
Https bool
//Debug模式
Debug bool
//输出日志文件
WriteLog bool
//控制台日志打印开关
OutPut bool
//登录参数,不可缺省
LoginInfo LoginInfo
Client *http.Client