Skip to content

Commit

Permalink
improve: log fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Sep 25, 2024
1 parent d9d09bf commit a3e28d3
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 66 deletions.
8 changes: 6 additions & 2 deletions cmd/bitsrun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/Mmx233/BitSrunLoginGo/internal/config"
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
"github.com/Mmx233/BitSrunLoginGo/internal/controllers"
)

Expand All @@ -13,9 +14,12 @@ func main() {

if config.Settings.Guardian.Enable {
//进入守护模式
controllers.Guardian()
controllers.Guardian(logger.WithField(keys.LogComponent, "guard"))
} else {
//执行单次流程
_ = controllers.Login()
_ = controllers.Login(controllers.LoginConf{
Logger: logger.WithField(keys.LogComponent, "login"),
IsOnlineDetectLogDebugLevel: false,
})
}
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/Mmx233/BitSrunLoginGo
go 1.23.1

require (
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240924144324-7b288b13d4c6
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240925131851-fca176afd6a4
github.com/Mmx233/tool v0.7.8
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/cloudflare/cloudflare-go v0.104.0
github.com/cloudflare/cloudflare-go v0.105.0
github.com/sirupsen/logrus v1.9.3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1009
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1009
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1010
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1010
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240924144324-7b288b13d4c6 h1:sfvujwTP5u1qTiJTGzwzGc7/2xdT5qywXyeEImteytE=
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240924144324-7b288b13d4c6/go.mod h1:lSwJMEvGYQoEyQzvEFI88x+c2PHzp4/1ut2CrzJ3Q2o=
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240925131851-fca176afd6a4 h1:Jdz0jXMJmvm7am4SNmEvNzuLNJxijI1bwi1zqXs3fE0=
github.com/Mmx233/BackoffCli/backoff v0.0.0-20240925131851-fca176afd6a4/go.mod h1:lSwJMEvGYQoEyQzvEFI88x+c2PHzp4/1ut2CrzJ3Q2o=
github.com/Mmx233/tool v0.7.8 h1:SNl1AHhvF3owGDMFGRxWCqFukvySDqAv9IJe8ubWeWg=
github.com/Mmx233/tool v0.7.8/go.mod h1:tEQN6qc2s/sOJKcxr06SktzZ2fCuwdOs0fK01UTfe4Q=
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/cloudflare/cloudflare-go v0.104.0 h1:R/lB0dZupaZbOgibAH/BRrkFbZ6Acn/WsKg2iX2xXuY=
github.com/cloudflare/cloudflare-go v0.104.0/go.mod h1:pfUQ4PIG4ISI0/Mmc21Bp86UnFU0ktmPf3iTgbSL+cM=
github.com/cloudflare/cloudflare-go v0.105.0 h1:yu2IatITLZ4dw7/byzRrlE5DfUvtub0k9CHZ5zBlj90=
github.com/cloudflare/cloudflare-go v0.105.0/go.mod h1:pfUQ4PIG4ISI0/Mmc21Bp86UnFU0ktmPf3iTgbSL+cM=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -32,10 +32,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1009 h1:nlftpuG6a8AOnvzYikn1i4vsZLgL20RMXphyVVS5GTQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1009/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1009 h1:a0wClE2xM+ltINDr46gQU0LkdJsxPo4m6xXJUJJ9pCE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1009/go.mod h1:tItxq45pTgKkiNh5xHoKF0lsPuUA948lxPdb4s/fWz8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1010 h1:HwhKTg+VcQd5M9AyaEZaqKmpKuWxbf5kni23g1KRmOk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1010/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1010 h1:tjRPlgu12YmtpMnaCU/3SbU+xorvsFmY2giuOsCXCrM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1010/go.mod h1:QwZGGCWSUxjtrrbseqSSIMMsAj6Iyubyq3BpgigmXTY=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
3 changes: 2 additions & 1 deletion internal/config/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/Mmx233/BackoffCli/backoff"
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
"time"
)

Expand All @@ -10,7 +11,7 @@ var BackoffConfig backoff.Conf
func initBackoff() {
setting := Settings.Backoff
BackoffConfig = backoff.Conf{
Logger: Logger,
Logger: Logger.WithField(keys.LogComponent, "backoff"),
DisableRecovery: true,
InitialDuration: time.Duration(setting.InitialDuration) * time.Second,
MaxDuration: time.Duration(setting.MaxDuration) * time.Second,
Expand Down
14 changes: 8 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/Mmx233/BitSrunLoginGo/internal/config/flags"
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
Expand All @@ -24,33 +25,34 @@ var (
)

func init() {
logger := log.New().WithField(keys.LogComponent, "init")
reader := newReaderFromPath(flags.Path)

// 生成配置文件
exist, err := tool.File.Exists(flags.Path)
if err != nil {
log.Fatalln("[init] 读取配置文件失败:", err)
logger.Fatalln("读取配置文件失败:", err)
} else if !exist {
var data []byte
data, err = reader.Marshal(&defaultConfig)
if err != nil {
log.Fatalln("[init] 生成配置文件失败:", err)
logger.Fatalln("生成配置文件失败:", err)
}
if err = os.WriteFile(flags.Path, data, 0600); err != nil {
log.Fatalln("[init] 写入配置文件失败:", err)
logger.Fatalln("写入配置文件失败:", err)
}
log.Infoln("[init] 已生成配置文件,请编辑 '" + flags.Path + "' 然后重试")
logger.Infoln("已生成配置文件,请编辑 '" + flags.Path + "' 然后重试")
os.Exit(0)
}

// 读取配置文件
data, err := os.ReadFile(flags.Path)
if err != nil {
log.Fatalln("[init] 读取配置失败:", err)
logger.Fatalln("读取配置失败:", err)
}
var fileConf ConfFromFile
if err = reader.Unmarshal(data, &fileConf); err != nil {
log.Fatalln("[init] 解析配置失败:", err)
logger.Fatalln("解析配置失败:", err)
}
Form = &fileConf.Form
Meta = &fileConf.Meta
Expand Down
6 changes: 6 additions & 0 deletions internal/config/keys/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package keys

const (
LogComponent = "comp"
LogLoginModule = "mod"
)
1 change: 0 additions & 1 deletion internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func initLog() {

//设置输出格式
Logger.SetFormatter(&nested.Formatter{
HideKeys: true,
NoColors: Settings.Log.WriteFile,
TimestampFormat: "2006-01-02 15:04:05",
})
Expand Down
14 changes: 7 additions & 7 deletions internal/controllers/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package controllers

import (
"github.com/Mmx233/BitSrunLoginGo/internal/config"
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
log "github.com/sirupsen/logrus"
"time"
)

// Guardian 守护模式逻辑
func Guardian() {
logger := config.Logger

logger.Infoln("[守护模式]")

func Guardian(logger log.FieldLogger) {
GuardianDuration := time.Duration(config.Settings.Guardian.Duration) * time.Second

for {
_ = Login()
_ = Login(LoginConf{
Logger: logger.WithField(keys.LogComponent, "login"),
IsOnlineDetectLogDebugLevel: true,
})
time.Sleep(GuardianDuration)
}
}
80 changes: 52 additions & 28 deletions internal/controllers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/Mmx233/BackoffCli/backoff"
"github.com/Mmx233/BitSrunLoginGo/internal/config"
"github.com/Mmx233/BitSrunLoginGo/internal/config/flags"
"github.com/Mmx233/BitSrunLoginGo/internal/config/keys"
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns"
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/http_client"
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
"github.com/Mmx233/BitSrunLoginGo/tools"
log "github.com/sirupsen/logrus"
"net/http"
"sync"
"time"
Expand All @@ -18,10 +20,18 @@ import (
var ipLast string
var debugTip sync.Once

func Login() error {
logger := config.Logger
type LoginConf struct {
Logger log.FieldLogger
IsOnlineDetectLogDebugLevel bool
}

func Login(conf LoginConf) error {
logger := conf.Logger
if config.Settings.Basic.Interfaces == "" { //单网卡
err := LoginSingle(nil, true)
err := LoginSingle(LoginSingleConf{
LoginConf: conf,
Eth: nil,
})
if err != nil {
logger.Errorln("登录出错: ", err)
debugTip.Do(func() {
Expand All @@ -32,13 +42,13 @@ func Login() error {
}
return err
} else { //多网卡
return LoginInterfaces()
return LoginInterfaces(conf)
}
}

func ddns(ip string, httpClient *http.Client) error {
func ddns(logger log.FieldLogger, ip string, httpClient *http.Client) error {
return dns.Run(&dns.Config{
Logger: config.Logger,
Logger: logger.WithField(keys.LogLoginModule, "ddns"),
Provider: config.Settings.DDNS.Provider,
IP: ip,
Domain: config.Settings.DDNS.Domain,
Expand All @@ -48,8 +58,8 @@ func ddns(ip string, httpClient *http.Client) error {
})
}

func LoginInterfaces() error {
logger := config.Logger
func LoginInterfaces(conf LoginConf) error {
logger := conf.Logger
interfaces, err := tools.GetInterfaceAddr(logger, config.Settings.Basic.Interfaces)
if err != nil {
return err
Expand All @@ -58,8 +68,11 @@ func LoginInterfaces() error {
var errCount int
for i, eth := range interfaces {
logger.Infoln("使用网卡: ", eth.Name)
if err := LoginSingle(&eth, false); err != nil {
config.Logger.Errorf("网卡 %s 登录出错: %v", eth.Name, err)
if err := LoginSingle(LoginSingleConf{
LoginConf: conf,
Eth: &eth,
}); err != nil {
logger.Errorf("网卡 %s 登录出错: %v", eth.Name, err)
errCount++
}
if i != len(interfaces)-1 {
Expand All @@ -72,21 +85,26 @@ func LoginInterfaces() error {
return nil
}

func LoginSingle(eth *tools.Eth, debugOutput bool) error {
type LoginSingleConf struct {
LoginConf
Eth *tools.Eth
}

func LoginSingle(conf LoginSingleConf) error {
if config.Settings.Backoff.Enable {
return backoff.NewInstance(func(ctx context.Context) error {
return doLogin(eth, debugOutput)
return doLogin(conf)
}, config.BackoffConfig).Run(context.TODO())
} else {
return doLogin(eth, debugOutput)
return doLogin(conf)
}
}

func doLogin(eth *tools.Eth, debugOutput bool) error {
logger := config.Logger
func doLogin(conf LoginSingleConf) error {
logger := conf.Logger

// 登录配置初始化
httpClient := http_client.HttpPackSelect(eth).Client
httpClient := http_client.HttpPackSelect(conf.Eth).Client
srunClient := srun.New(&srun.Conf{
Logger: logger,
Https: config.Settings.Basic.Https,
Expand All @@ -103,6 +121,8 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
// Reality 与 Acid
var acidOnReality bool
if config.Settings.Reality.Enable {
logger := logger.WithField(keys.LogLoginModule, "reality")

logger.Debugln("开始 Reality 流程")
acid, _, err := srunDetector.Reality(config.Settings.Reality.Addr, flags.AutoAcid)
if err != nil {
Expand All @@ -114,13 +134,15 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
}
}
if !acidOnReality && flags.AutoAcid {
logger.Debugln("开始嗅探 acid")
logger := logger.WithField(keys.LogLoginModule, "acid")

logger.Debugln("开始嗅探")
acid, err := srunDetector.DetectAcid()
if err != nil {
if errors.Is(err, srun.ErrAcidCannotFound) {
logger.Warnln("找不到 acid,使用配置 acid")
} else {
logger.Warnf("嗅探 acid 失败,使用配置 acid: %v", err)
logger.Warnf("嗅探失败,使用配置 acid: %v", err)
}
} else {
logger.Debugf("使用嗅探 acid: %s", acid)
Expand All @@ -129,13 +151,15 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
}

if flags.AutoEnc {
logger.Debugln("开始嗅探 enc")
logger := logger.WithField(keys.LogLoginModule, "enc")

logger.Debugln("开始嗅探")
enc, err := srunDetector.DetectEnc()
if err != nil {
if errors.Is(err, srun.ErrEnvCannotFound) {
logger.Warnln("找不到 enc,使用配置 enc")
} else {
logger.Warnf("嗅探 enc 失败,使用配置 enc: %v", err)
logger.Warnf("嗅探失败,使用配置 enc: %v", err)
}
} else {
logger.Debugf("使用嗅探 enc: %s", enc)
Expand All @@ -144,14 +168,14 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
}

// 选择输出函数
var output func(args ...interface{})
if debugOutput {
output = logger.Debugln
var _Println func(args ...interface{})
if conf.IsOnlineDetectLogDebugLevel {
_Println = logger.Debugln
} else {
output = logger.Infoln
_Println = logger.Infoln
}

output("正在获取登录状态")
_Println("正在获取登录状态")

online, ip, err := srunClient.LoginStatus()
if err != nil {
Expand All @@ -170,10 +194,10 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
// 登录执行

if online {
output("已登录~")
_Println("已登录~")

if config.Settings.DDNS.Enable && config.Settings.Guardian.Enable && ipLast != ip {
if ddns(ip, httpClient) == nil {
if ddns(logger, ip, httpClient) == nil {
ipLast = ip
}
}
Expand All @@ -189,7 +213,7 @@ func doLogin(eth *tools.Eth, debugOutput bool) error {
logger.Infoln("登录成功~")

if config.Settings.DDNS.Enable {
_ = ddns(ip, httpClient)
_ = ddns(logger, ip, httpClient)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dns/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ type Config struct {
TTL uint
Conf config.DdnsProviderConfigSum
Http *http.Client
Logger *log.Logger
Logger log.FieldLogger
}
Loading

0 comments on commit a3e28d3

Please sign in to comment.