feat: 重构配置层,添加 json 支持

This commit is contained in:
Mmx233
2023-08-28 22:03:03 +08:00
parent 2a098e00dd
commit 6f283a93f5
19 changed files with 318 additions and 272 deletions

69
internal/config/config.go Normal file
View File

@@ -0,0 +1,69 @@
package config
import (
"github.com/Mmx233/BitSrunLoginGo/internal/config/flags"
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"os"
"time"
)
type ConfFromFile struct {
Form srun.LoginForm `json:"form" yaml:"form"`
Meta srun.LoginMeta `json:"meta" yaml:"meta"`
Settings SettingsConf `json:"settings" yaml:"settings"`
}
var (
Form *srun.LoginForm
Meta *srun.LoginMeta
Settings *SettingsConf
Timeout time.Duration
)
func init() {
reader := newReaderFromPath(flags.Path)
// 生成配置文件
exist, err := tool.File.Exists(flags.Path)
if err != nil {
log.Fatalln("[init] 读取配置文件失败:", err)
} else if !exist {
var data []byte
data, err = reader.Marshal(&defaultConfig)
if err != nil {
log.Fatalln("[init] 生成配置文件失败:", err)
}
if err = os.WriteFile(flags.Path, data, 0600); err != nil {
log.Fatalln("[init] 写入配置文件失败:", err)
}
log.Infoln("[init] 已生成配置文件,请编辑 '" + flags.Path + "' 然后重试")
os.Exit(0)
}
// 读取配置文件
data, err := os.ReadFile(flags.Path)
if err != nil {
log.Fatalln("[init] 读取配置失败:", err)
}
var fileConf ConfFromFile
if err = reader.Unmarshal(data, &fileConf); err != nil {
log.Fatalln("[init] 解析配置失败:", err)
}
Form = &fileConf.Form
Meta = &fileConf.Meta
Settings = &fileConf.Settings
Timeout = time.Duration(Settings.Basic.Timeout) * time.Second
// flag 配置覆写
if flags.Debug {
Settings.Log.DebugLevel = true
}
if flags.Acid != "" {
Meta.Acid = flags.Acid
}
initLog()
}

View File

@@ -0,0 +1,39 @@
package config
import (
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
)
var defaultConfig = ConfFromFile{
Form: srun.LoginForm{
Domain: "www.msftconnecttest.com",
UserType: "cmcc",
},
Meta: srun.LoginMeta{
N: "200",
Type: "1",
Acid: "5",
Enc: "srun_bx1",
},
Settings: SettingsConf{
Basic: BasicConf{
Timeout: 5,
},
Guardian: GuardianConf{
Duration: 300,
},
Log: LogConf{
FilePath: "./",
},
DDNS: DdnsConf{
Enable: false,
TTL: 600,
Domain: "www.example.com",
Provider: "cloudflare",
Config: map[string]interface{}{
"zone": "",
"token": "",
},
},
},
}

View File

@@ -0,0 +1,26 @@
package flags
import (
"flag"
)
var (
// Path 配置文件路径
Path string
Interface string
Debug bool
AutoAcid bool
Acid string
)
func init() {
flag.StringVar(&Path, "config", "Config.yaml", "config path")
flag.StringVar(&Interface, "interface", "", "specify the eth name")
flag.BoolVar(&Debug, "debug", false, "enable debug mode")
flag.BoolVar(&AutoAcid, "auto-acid", false, "auto detect acid")
flag.StringVar(&Acid, "acid", "", "specify acid value")
flag.Parse()
}

48
internal/config/log.go Normal file
View File

@@ -0,0 +1,48 @@
package config
import (
"io"
"os"
"strings"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
log "github.com/sirupsen/logrus"
)
func initLog() {
if Settings.Log.DebugLevel {
log.SetLevel(log.DebugLevel)
}
if Settings.Log.WriteFile {
//日志路径初始化与处理
if !strings.HasSuffix(Settings.Log.FilePath, "/") {
Settings.Log.FilePath += "/"
}
e := os.MkdirAll(Settings.Log.FilePath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
if Settings.Log.FileName == "" {
Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log"
}
f, e := os.OpenFile(Settings.Log.FilePath+Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
//设置输出格式
log.SetFormatter(&nested.Formatter{
HideKeys: true,
NoColors: Settings.Log.WriteFile,
TimestampFormat: "2006-01-02 15:04:05",
})
}
}

37
internal/config/models.go Normal file
View File

@@ -0,0 +1,37 @@
package config
type (
GuardianConf struct {
Enable bool `json:"enable" yaml:"enable"`
Duration uint `json:"duration" yaml:"duration"`
}
BasicConf struct {
Https bool `json:"https" yaml:"https"`
SkipCertVerify bool `json:"skip_cert_verify" yaml:"skip_cert_verify"`
Timeout uint `json:"timeout" yaml:"timeout"`
Interfaces string `json:"interfaces" yaml:"interfaces"`
}
LogConf struct {
DebugLevel bool `json:"debug_level" yaml:"debug_level"`
WriteFile bool `json:"write_file" yaml:"write_file"`
FilePath string `json:"file_path" yaml:"log_path"`
FileName string `json:"file_name" yaml:"log_name"`
}
DdnsConf struct {
Enable bool `json:"enable" yaml:"enable"`
TTL uint `json:"ttl" yaml:"ttl"`
Domain string `json:"domain" yaml:"domain"`
Provider string `json:"provider" yaml:"provider"`
Config map[string]interface{} `json:"config" json:"config" yaml:"config"`
}
)
type SettingsConf struct {
Basic BasicConf `json:"basic" yaml:"basic"`
Guardian GuardianConf `json:"guardian" yaml:"guardian"`
Log LogConf `json:"log" yaml:"log"`
DDNS DdnsConf `json:"ddns" yaml:"ddns"`
}

45
internal/config/reader.go Normal file
View File

@@ -0,0 +1,45 @@
package config
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"path"
)
func newReaderFromPath(pathname string) Reader {
switch path.Ext(pathname) {
case ".json":
return Json{}
case ".yaml":
return Yaml{}
default:
log.Warnf("未知配置类型,使用 yaml 进行解析")
return Yaml{}
}
}
type Reader interface {
Marshal(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
}
type Json struct {
}
func (Json) Marshal(v any) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
func (Json) Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
type Yaml struct {
}
func (Yaml) Marshal(v any) ([]byte, error) {
return yaml.Marshal(v)
}
func (Yaml) Unmarshal(data []byte, v any) error {
return yaml.Unmarshal(data, v)
}