refactor:优化结构

This commit is contained in:
Mmx
2021-05-03 14:09:22 +08:00
parent 487c22846a
commit fb562f92c9
4 changed files with 62 additions and 73 deletions

View File

@@ -10,3 +10,17 @@ type Config struct {
Meta LoginMeta `json:"meta"`
Settings Settings `json:"settings"`
}
func (a *Config) Generate() *LoginInfo {
return &LoginInfo{
UrlLoginPage: "http://" + a.From.Domain + "/srun_portal_success",
UrlGetChallengeApi: "http://" + a.From.Domain + "/cgi-bin/get_challenge",
UrlLoginApi: "http://" + a.From.Domain + "/cgi-bin/srun_portal",
UrlCheckApi: "http://" + a.From.Domain + "/cgi-bin/rad_user_info",
Meta: &a.Meta,
Form: &LoginForm{
UserName: a.From.UserName + "@cmcc",
PassWord: a.From.PassWord,
},
}
}

View File

@@ -1,72 +0,0 @@
package Util
import (
"Mmx/Global"
"Mmx/Modles"
"fmt"
"os"
"path/filepath"
)
type config struct {
Path string
}
var Config = config{
Path: "Config.json",
}
func (*config) Generate(Form *Modles.LoginForm, Meta *Modles.LoginMeta) *Modles.LoginInfo {
return &Modles.LoginInfo{
UrlLoginPage: "http://" + Form.Domain + "/srun_portal_success",
UrlGetChallengeApi: "http://" + Form.Domain + "/cgi-bin/get_challenge",
UrlLoginApi: "http://" + Form.Domain + "/cgi-bin/srun_portal",
UrlCheckApi: "http://" + Form.Domain + "/cgi-bin/rad_user_info",
Meta: Meta,
Form: &Modles.LoginForm{
UserName: Form.UserName + "@cmcc",
PassWord: Form.PassWord,
},
}
}
func (a *config) Init() *Modles.LoginInfo {
if t, err := os.Executable(); err != nil {
ErrHandler(err)
} else {
a.Path = filepath.Dir(t) + "/" + a.Path
}
if !File.Exists(a.Path) {
if err := File.Write(a.Path, &Modles.Config{ //默认值
From: Modles.LoginForm{
Domain: "www.msftconnecttest.com",
UserName: "",
PassWord: "",
},
Meta: Modles.LoginMeta{
N: "200",
VType: "1",
Acid: "5",
Enc: "srun_bx1",
},
}); err != nil {
fmt.Println("创建配置文件失败:\n", err.Error())
os.Exit(3)
}
fmt.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
os.Exit(1)
}
var c Modles.Config
if err := File.Read(a.Path, &c); err != nil {
fmt.Println("读取配置文件失败:\n", err.Error())
os.Exit(3)
}
Global.Config = &c
return a.Generate(
&c.From,
&c.Meta,
)
}

47
Util/init.go Normal file
View File

@@ -0,0 +1,47 @@
package Util
import (
"Mmx/Global"
"Mmx/Modles"
"fmt"
"os"
"path/filepath"
)
func init() {
//配置文件初始化
Path := "Config.json"
if t, err := os.Executable(); err != nil {
ErrHandler(err)
} else {
Path = filepath.Dir(t) + "/" + Path
}
if !File.Exists(Path) {
if err := File.Write(Path, &Modles.Config{ //默认值
From: Modles.LoginForm{
Domain: "www.msftconnecttest.com",
UserName: "",
PassWord: "",
},
Meta: Modles.LoginMeta{
N: "200",
VType: "1",
Acid: "5",
Enc: "srun_bx1",
},
}); err != nil {
fmt.Println("创建配置文件失败:\n", err.Error())
os.Exit(3)
}
fmt.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
os.Exit(1)
}
var c Modles.Config
if err := File.Read(Path, &c); err != nil {
fmt.Println("读取配置文件失败:\n", err.Error())
os.Exit(3)
}
Global.Config = &c
}

View File

@@ -11,7 +11,7 @@ import (
func main() {
fmt.Println("Step0: 检查状态…")
G := Util.Config.Init()
G := Global.Config.Generate()
if Global.Config.Settings.QuitIfNetOk && Util.Checker.NetOk(G.UrlCheckApi) {
fmt.Println("网络正常,程序退出")