improve: 使用标准项目结构

This commit is contained in:
Mmx233
2023-03-01 18:58:11 +08:00
parent cb11426bd6
commit 3c63e9ddc3
29 changed files with 178 additions and 183 deletions

40
cmd/bitsrun/bitsrun.go Normal file
View File

@@ -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)
}
}
}
}
}

View File

@@ -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,
})
}

View File

@@ -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"

View File

@@ -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)

View File

@@ -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,
})
}

View File

@@ -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",

View File

@@ -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"`
}

View File

@@ -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
}

View File

@@ -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"
)

View File

@@ -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
}

View File

@@ -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"
)

40
main.go
View File

@@ -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)
}
}
}
}
}

View File

@@ -1,8 +0,0 @@
package srunModels
import "net"
type Eth struct {
Name string
Addr net.Addr
}

View File

@@ -1,4 +1,4 @@
package BitSrun
package srun
import log "github.com/sirupsen/logrus"

View File

@@ -1,4 +1,4 @@
package BitSrun
package srun
import (
"math"

View File

@@ -1,4 +1,4 @@
package BitSrun
package srun
import (
"crypto/md5"

View File

@@ -1,4 +1,4 @@
package BitSrun
package srun
import "errors"

View File

@@ -1,4 +1,4 @@
package BitSrun
package srun
import (
"encoding/json"

View File

@@ -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() {

View File

@@ -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,
})

View File

@@ -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"