diff --git a/cmd/bitsrun/bitsrun.go b/cmd/bitsrun/bitsrun.go new file mode 100644 index 0000000..d905ffe --- /dev/null +++ b/cmd/bitsrun/bitsrun.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + controllers2 "github.com/Mmx233/BitSrunLoginGo/internal/controllers" + global2 "github.com/Mmx233/BitSrunLoginGo/internal/global" + "github.com/Mmx233/BitSrunLoginGo/tools" + log "github.com/sirupsen/logrus" +) + +func main() { + if global2.Flags.RunningDaemon { + //后台挂起模式中 + controllers2.Guardian() + } else if global2.Config.Settings.Guardian.Enable { + //进入守护模式流程 + controllers2.EnterGuardian() + } else { + //登录流程 + var err error + if global2.Config.Settings.Basic.Interfaces == "" { //单网卡 + if err = controllers2.Login(nil, false); err != nil { + log.Errorln("登录出错: ", err) + if !global2.Config.Settings.Log.DebugLevel { + fmt.Printf("开启调试日志(debug_level)获取详细信息") + } + return + } + } else { //多网卡 + log.Infoln("多网卡模式") + interfaces, _ := tools.GetInterfaceAddr() + for _, eth := range interfaces { + log.Infoln("使用网卡: ", eth.Name) + if err = controllers2.Login(eth.Addr, false); err != nil { + log.Errorf("网卡 %s 登录出错: %v", eth.Name, err) + } + } + } + } +} diff --git a/controllers/login.go b/controllers/login.go deleted file mode 100644 index 4f1497d..0000000 --- a/controllers/login.go +++ /dev/null @@ -1,89 +0,0 @@ -package controllers - -import ( - "github.com/Mmx233/BitSrunLoginGo/dns" - "github.com/Mmx233/BitSrunLoginGo/global" - "github.com/Mmx233/BitSrunLoginGo/util" - BitSrun "github.com/Mmx233/BitSrunLoginGo/v1" - log "github.com/sirupsen/logrus" - "net" - "net/http" -) - -// Login 登录逻辑 -func Login(localAddr net.Addr, debugOutput bool) error { - // 登录状态检查 - - httpClient := util.HttpPackSelect(localAddr).Client - conf := &BitSrun.Conf{ - Https: global.Config.Settings.Basic.Https, - LoginInfo: BitSrun.LoginInfo{ - Form: &global.Config.Form, - Meta: &global.Config.Meta, - }, - Client: httpClient, - } - - var output func(args ...interface{}) - if debugOutput { - output = log.Debugln - } else { - output = log.Infoln - } - - output("正在获取登录状态") - - online, ip, e := BitSrun.LoginStatus(conf) - if e != nil { - return e - } - - if localAddr != nil && global.Config.Settings.Basic.UseDhcpIP { - ip = localAddr.(*net.TCPAddr).IP.String() - } else if global.Flags.ClientIP != "" { - ip = global.Flags.ClientIP - } - - log.Debugln("认证客户端 ip: ", ip) - - // 登录执行 - - if online { - output("已登录~") - - if global.Config.Settings.DDNS.Enable && global.Config.Settings.Guardian.Enable && ipLast != ip { - if ddns(ip, httpClient) == nil { - ipLast = ip - } - } - - return nil - } else { - log.Infoln("检测到用户未登录,开始尝试登录...") - - if e = BitSrun.DoLogin(ip, conf); e != nil { - return e - } - - log.Infoln("登录成功~") - - if global.Config.Settings.DDNS.Enable { - _ = ddns(ip, httpClient) - } - } - - return nil -} - -var ipLast string - -func ddns(ip string, httpClient *http.Client) error { - return dns.Run(&dns.Config{ - Provider: global.Config.Settings.DDNS.Provider, - IP: ip, - Domain: global.Config.Settings.DDNS.Domain, - TTL: global.Config.Settings.DDNS.TTL, - Conf: global.Config.Settings.DDNS.Config, - Http: httpClient, - }) -} diff --git a/controllers/daemon.go b/internal/controllers/daemon.go similarity index 95% rename from controllers/daemon.go rename to internal/controllers/daemon.go index 68223d5..747c853 100644 --- a/controllers/daemon.go +++ b/internal/controllers/daemon.go @@ -2,7 +2,7 @@ package controllers import ( "fmt" - "github.com/Mmx233/BitSrunLoginGo/global" + "github.com/Mmx233/BitSrunLoginGo/internal/global" "github.com/Mmx233/tool" "github.com/howeyc/fsnotify" "os" diff --git a/controllers/guardian.go b/internal/controllers/guardian.go similarity index 90% rename from controllers/guardian.go rename to internal/controllers/guardian.go index 73371d6..551e218 100644 --- a/controllers/guardian.go +++ b/internal/controllers/guardian.go @@ -1,12 +1,12 @@ package controllers import ( + "github.com/Mmx233/BitSrunLoginGo/internal/global" "os" "os/exec" "time" - "github.com/Mmx233/BitSrunLoginGo/global" - "github.com/Mmx233/BitSrunLoginGo/util" + "github.com/Mmx233/BitSrunLoginGo/tools" log "github.com/sirupsen/logrus" ) @@ -34,7 +34,7 @@ func Guardian() { log.Errorln("登录出错: ", e) } } else { //多网卡 - interfaces, e := util.GetInterfaceAddr() + interfaces, e := tools.GetInterfaceAddr() if e == nil { for _, eth := range interfaces { log.Debugf("使用 %s 网口登录 ", eth.Name) diff --git a/internal/controllers/login.go b/internal/controllers/login.go new file mode 100644 index 0000000..183903f --- /dev/null +++ b/internal/controllers/login.go @@ -0,0 +1,89 @@ +package controllers + +import ( + global2 "github.com/Mmx233/BitSrunLoginGo/internal/global" + dns2 "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns" + "github.com/Mmx233/BitSrunLoginGo/pkg/srun" + "github.com/Mmx233/BitSrunLoginGo/tools" + log "github.com/sirupsen/logrus" + "net" + "net/http" +) + +// Login 登录逻辑 +func Login(localAddr net.Addr, debugOutput bool) error { + // 登录状态检查 + + httpClient := tools.HttpPackSelect(localAddr).Client + conf := &srun.Conf{ + Https: global2.Config.Settings.Basic.Https, + LoginInfo: srun.LoginInfo{ + Form: &global2.Config.Form, + Meta: &global2.Config.Meta, + }, + Client: httpClient, + } + + var output func(args ...interface{}) + if debugOutput { + output = log.Debugln + } else { + output = log.Infoln + } + + output("正在获取登录状态") + + online, ip, e := srun.LoginStatus(conf) + if e != nil { + return e + } + + if localAddr != nil && global2.Config.Settings.Basic.UseDhcpIP { + ip = localAddr.(*net.TCPAddr).IP.String() + } else if global2.Flags.ClientIP != "" { + ip = global2.Flags.ClientIP + } + + log.Debugln("认证客户端 ip: ", ip) + + // 登录执行 + + if online { + output("已登录~") + + if global2.Config.Settings.DDNS.Enable && global2.Config.Settings.Guardian.Enable && ipLast != ip { + if ddns(ip, httpClient) == nil { + ipLast = ip + } + } + + return nil + } else { + log.Infoln("检测到用户未登录,开始尝试登录...") + + if e = srun.DoLogin(ip, conf); e != nil { + return e + } + + log.Infoln("登录成功~") + + if global2.Config.Settings.DDNS.Enable { + _ = ddns(ip, httpClient) + } + } + + return nil +} + +var ipLast string + +func ddns(ip string, httpClient *http.Client) error { + return dns2.Run(&dns2.Config{ + Provider: global2.Config.Settings.DDNS.Provider, + IP: ip, + Domain: global2.Config.Settings.DDNS.Domain, + TTL: global2.Config.Settings.DDNS.TTL, + Conf: global2.Config.Settings.DDNS.Config, + Http: httpClient, + }) +} diff --git a/global/config.go b/internal/global/config.go similarity index 72% rename from global/config.go rename to internal/global/config.go index 903f7fa..695b765 100644 --- a/global/config.go +++ b/internal/global/config.go @@ -1,8 +1,8 @@ package global import ( - "github.com/Mmx233/BitSrunLoginGo/models" - "github.com/Mmx233/BitSrunLoginGo/v1" + "github.com/Mmx233/BitSrunLoginGo/internal/global/models" + "github.com/Mmx233/BitSrunLoginGo/pkg/srun" "github.com/Mmx233/tool" log "github.com/sirupsen/logrus" "github.com/spf13/viper" @@ -10,36 +10,36 @@ import ( "time" ) -var Config srunModels.Config +var Config models.Config var Timeout time.Duration func readConfig() { //配置文件默认值 - viper.SetDefault("form", BitSrun.LoginForm{ + viper.SetDefault("form", srun.LoginForm{ Domain: "www.msftconnecttest.com", UserType: "cmcc", }) - viper.SetDefault("meta", BitSrun.LoginMeta{ + viper.SetDefault("meta", srun.LoginMeta{ N: "200", Type: "1", Acid: "5", Enc: "srun_bx1", }) - viper.SetDefault("settings", srunModels.Settings{ - Basic: srunModels.Basic{ + viper.SetDefault("settings", models.Settings{ + Basic: models.Basic{ Timeout: 5, }, - Daemon: srunModels.Daemon{ + Daemon: models.Daemon{ Path: ".BitSrun", }, - Guardian: srunModels.Guardian{ + Guardian: models.Guardian{ Duration: 300, }, - Log: srunModels.Log{ + Log: models.Log{ FilePath: "./", }, - DDNS: srunModels.DDNS{ + DDNS: models.DDNS{ Enable: false, TTL: 600, Domain: "www.example.com", diff --git a/global/flags.go b/internal/global/flags.go similarity index 100% rename from global/flags.go rename to internal/global/flags.go diff --git a/global/init.go b/internal/global/init.go similarity index 100% rename from global/init.go rename to internal/global/init.go diff --git a/global/log.go b/internal/global/log.go similarity index 100% rename from global/log.go rename to internal/global/log.go diff --git a/models/config.go b/internal/global/models/config.go similarity index 86% rename from models/config.go rename to internal/global/models/config.go index 4f617e3..1ea908a 100644 --- a/models/config.go +++ b/internal/global/models/config.go @@ -1,7 +1,7 @@ -package srunModels +package models import ( - "github.com/Mmx233/BitSrunLoginGo/v1" + "github.com/Mmx233/BitSrunLoginGo/pkg/srun" ) type Daemon struct { @@ -46,7 +46,7 @@ type Settings struct { } type Config struct { - Form BitSrun.LoginForm `json:"form" yaml:"form" mapstructure:"form"` - Meta BitSrun.LoginMeta `json:"meta" yaml:"meta" mapstructure:"meta"` - Settings Settings `json:"settings" yaml:"settings" mapstructure:"settings"` + Form srun.LoginForm `json:"form" yaml:"form" mapstructure:"form"` + Meta srun.LoginMeta `json:"meta" yaml:"meta" mapstructure:"meta"` + Settings Settings `json:"settings" yaml:"settings" mapstructure:"settings"` } diff --git a/dns/aliyun/aliyun.go b/internal/pkg/dns/aliyun/aliyun.go similarity index 96% rename from dns/aliyun/aliyun.go rename to internal/pkg/dns/aliyun/aliyun.go index c781534..bbc04dc 100644 --- a/dns/aliyun/aliyun.go +++ b/internal/pkg/dns/aliyun/aliyun.go @@ -7,7 +7,7 @@ import ( "encoding/json" "errors" "fmt" - dnsUtil "github.com/Mmx233/BitSrunLoginGo/dns/util" + dnsUtil2 "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util" "github.com/Mmx233/tool" "math/rand" "net/http" @@ -28,7 +28,7 @@ func New(ttl uint, conf map[string]interface{}, Http *http.Client) (*DnsProvider TTL: ttl, Http: tool.NewHttpTool(Http), } - e := dnsUtil.DecodeConfig(conf, &p) + e := dnsUtil2.DecodeConfig(conf, &p) if e != nil { return nil, e } @@ -161,7 +161,7 @@ func (a DnsProvider) NewRecord(subDomain, rootDomain, ip string) error { } func (a DnsProvider) SetDomainRecord(domain, ip string) error { - subDomain, rootDomain, e := dnsUtil.DecodeDomain(domain) + subDomain, rootDomain, e := dnsUtil2.DecodeDomain(domain) if e != nil { return e } diff --git a/dns/aliyun/models.go b/internal/pkg/dns/aliyun/models.go similarity index 100% rename from dns/aliyun/models.go rename to internal/pkg/dns/aliyun/models.go diff --git a/dns/cloudflare/cloudflare.go b/internal/pkg/dns/cloudflare/cloudflare.go similarity index 96% rename from dns/cloudflare/cloudflare.go rename to internal/pkg/dns/cloudflare/cloudflare.go index f6fe0db..c134cdd 100644 --- a/dns/cloudflare/cloudflare.go +++ b/internal/pkg/dns/cloudflare/cloudflare.go @@ -3,7 +3,7 @@ package cloudflare import ( "context" "errors" - dnsUtil "github.com/Mmx233/BitSrunLoginGo/dns/util" + "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util" "github.com/cloudflare/cloudflare-go" "net/http" ) diff --git a/dns/dnspod/dnspod.go b/internal/pkg/dns/dnspod/dnspod.go similarity index 92% rename from dns/dnspod/dnspod.go rename to internal/pkg/dns/dnspod/dnspod.go index c6ae9ec..b0b0285 100644 --- a/dns/dnspod/dnspod.go +++ b/internal/pkg/dns/dnspod/dnspod.go @@ -1,7 +1,7 @@ package dnspod import ( - dnsUtil "github.com/Mmx233/BitSrunLoginGo/dns/util" + dnsUtil2 "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions" @@ -19,7 +19,7 @@ type DnsProvider struct { func New(ttl uint64, conf map[string]interface{}, Http http.RoundTripper) (*DnsProvider, error) { var p = DnsProvider{TTL: ttl} - e := dnsUtil.DecodeConfig(conf, &p) + e := dnsUtil2.DecodeConfig(conf, &p) if e != nil { return nil, e } @@ -29,7 +29,7 @@ func New(ttl uint64, conf map[string]interface{}, Http http.RoundTripper) (*DnsP } func (a DnsProvider) SetDomainRecord(domain, ip string) error { - subDomain, rootDomain, e := dnsUtil.DecodeDomain(domain) + subDomain, rootDomain, e := dnsUtil2.DecodeDomain(domain) if e != nil { return e } diff --git a/dns/initer.go b/internal/pkg/dns/initer.go similarity index 84% rename from dns/initer.go rename to internal/pkg/dns/initer.go index 2dfb46f..a731277 100644 --- a/dns/initer.go +++ b/internal/pkg/dns/initer.go @@ -3,9 +3,9 @@ package dns import ( "errors" "fmt" - "github.com/Mmx233/BitSrunLoginGo/dns/aliyun" - "github.com/Mmx233/BitSrunLoginGo/dns/cloudflare" - "github.com/Mmx233/BitSrunLoginGo/dns/dnspod" + "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/aliyun" + "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/cloudflare" + "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/dnspod" log "github.com/sirupsen/logrus" ) diff --git a/dns/models.go b/internal/pkg/dns/models.go similarity index 100% rename from dns/models.go rename to internal/pkg/dns/models.go diff --git a/dns/util/config.go b/internal/pkg/dns/util/config.go similarity index 100% rename from dns/util/config.go rename to internal/pkg/dns/util/config.go diff --git a/dns/util/domain.go b/internal/pkg/dns/util/domain.go similarity index 100% rename from dns/util/domain.go rename to internal/pkg/dns/util/domain.go diff --git a/main.go b/main.go deleted file mode 100644 index 66dae9e..0000000 --- a/main.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "fmt" - "github.com/Mmx233/BitSrunLoginGo/controllers" - "github.com/Mmx233/BitSrunLoginGo/global" - "github.com/Mmx233/BitSrunLoginGo/util" - log "github.com/sirupsen/logrus" -) - -func main() { - if global.Flags.RunningDaemon { - //后台挂起模式中 - controllers.Guardian() - } else if global.Config.Settings.Guardian.Enable { - //进入守护模式流程 - controllers.EnterGuardian() - } else { - //登录流程 - var err error - if global.Config.Settings.Basic.Interfaces == "" { //单网卡 - if err = controllers.Login(nil, false); err != nil { - log.Errorln("登录出错: ", err) - if !global.Config.Settings.Log.DebugLevel { - fmt.Printf("开启调试日志(debug_level)获取详细信息") - } - return - } - } else { //多网卡 - log.Infoln("多网卡模式") - interfaces, _ := util.GetInterfaceAddr() - for _, eth := range interfaces { - log.Infoln("使用网卡: ", eth.Name) - if err = controllers.Login(eth.Addr, false); err != nil { - log.Errorf("网卡 %s 登录出错: %v", eth.Name, err) - } - } - } - } -} diff --git a/models/eth.go b/models/eth.go deleted file mode 100644 index 8356280..0000000 --- a/models/eth.go +++ /dev/null @@ -1,8 +0,0 @@ -package srunModels - -import "net" - -type Eth struct { - Name string - Addr net.Addr -} diff --git a/v1/XBase64.go b/pkg/srun/XBase64.go similarity index 98% rename from v1/XBase64.go rename to pkg/srun/XBase64.go index 8f058eb..847fd41 100644 --- a/v1/XBase64.go +++ b/pkg/srun/XBase64.go @@ -1,4 +1,4 @@ -package BitSrun +package srun import log "github.com/sirupsen/logrus" diff --git a/v1/XEncode.go b/pkg/srun/XEncode.go similarity index 99% rename from v1/XEncode.go rename to pkg/srun/XEncode.go index e2a3aaf..18676a2 100644 --- a/v1/XEncode.go +++ b/pkg/srun/XEncode.go @@ -1,4 +1,4 @@ -package BitSrun +package srun import ( "math" diff --git a/v1/srun/steps.go b/pkg/srun/api.go similarity index 100% rename from v1/srun/steps.go rename to pkg/srun/api.go diff --git a/v1/encode.go b/pkg/srun/encode.go similarity index 95% rename from v1/encode.go rename to pkg/srun/encode.go index 6d518c3..11d2eee 100644 --- a/v1/encode.go +++ b/pkg/srun/encode.go @@ -1,4 +1,4 @@ -package BitSrun +package srun import ( "crypto/md5" diff --git a/v1/errors.go b/pkg/srun/errors.go similarity index 85% rename from v1/errors.go rename to pkg/srun/errors.go index 46e76a1..77df90c 100644 --- a/v1/errors.go +++ b/pkg/srun/errors.go @@ -1,4 +1,4 @@ -package BitSrun +package srun import "errors" diff --git a/v1/login.go b/pkg/srun/login.go similarity index 99% rename from v1/login.go rename to pkg/srun/login.go index ec99838..77b9c0f 100644 --- a/v1/login.go +++ b/pkg/srun/login.go @@ -1,4 +1,4 @@ -package BitSrun +package srun import ( "encoding/json" diff --git a/v1/models.go b/pkg/srun/models.go similarity index 90% rename from v1/models.go rename to pkg/srun/models.go index 5fd6d06..5c5c800 100644 --- a/v1/models.go +++ b/pkg/srun/models.go @@ -1,7 +1,6 @@ -package BitSrun +package srun import ( - "github.com/Mmx233/BitSrunLoginGo/v1/srun" "net/http" ) @@ -33,7 +32,7 @@ type Conf struct { Client *http.Client Header http.Header - api srun.Api + api Api } func (a *Conf) initApi() { diff --git a/util/eth.go b/tools/eth.go similarity index 81% rename from util/eth.go rename to tools/eth.go index 0f0bf80..b14d66b 100644 --- a/util/eth.go +++ b/tools/eth.go @@ -1,16 +1,20 @@ -package util +package tools import ( - "github.com/Mmx233/BitSrunLoginGo/global" - srunModels "github.com/Mmx233/BitSrunLoginGo/models" + "github.com/Mmx233/BitSrunLoginGo/internal/global" log "github.com/sirupsen/logrus" "net" "regexp" "strings" ) -func GetInterfaceAddr() ([]srunModels.Eth, error) { - var result []srunModels.Eth +type Eth struct { + Name string + Addr net.Addr +} + +func GetInterfaceAddr() ([]Eth, error) { + var result []Eth interfaces, e := net.Interfaces() if e != nil { @@ -35,7 +39,7 @@ func GetInterfaceAddr() ([]srunModels.Eth, error) { log.Warnln(eth.Name+" ip解析失败:", e) continue } - result = append(result, srunModels.Eth{ + result = append(result, Eth{ Name: eth.Name, Addr: ip, }) diff --git a/util/http.go b/tools/http.go similarity index 93% rename from util/http.go rename to tools/http.go index a5d172a..6d3843d 100644 --- a/util/http.go +++ b/tools/http.go @@ -1,7 +1,7 @@ -package util +package tools import ( - "github.com/Mmx233/BitSrunLoginGo/global" + "github.com/Mmx233/BitSrunLoginGo/internal/global" "github.com/Mmx233/tool" "net" "net/http"