diff --git a/README.md b/README.md index daf75d0..63ada18 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ Config.json说明: "enc": "srun_bx1" }, "settings": { + "timeout": 3, //检查网络超时时间(秒) "demo_mode": false, //测试模式,报错更详细,且生成运行日志与错误日志 - "dns": "1.2.4.8", //检查网络用的DNS地址,建议设为网关分发的内网DNS地址 "guardian": 0, //守护模式,值为网络检查周期(秒),设为0关闭守护模式 "daemon": false, //将守护挂入后台 } diff --git a/controllers/daemon.go b/controllers/daemon.go index a591e8e..f31a8bb 100644 --- a/controllers/daemon.go +++ b/controllers/daemon.go @@ -1,9 +1,9 @@ package controllers import ( - "autoLogin/util" "flag" "fmt" + "github.com/Mmx233/tool" "time" ) @@ -26,11 +26,11 @@ func init() { } func (a *daemon) MarkDaemon() error { - return util.File.Write(a.Path, []byte(a.Mark)) + return tool.File.Write(a.Path, []byte(a.Mark)) } func (a *daemon) CheckDaemon() bool { - if data, err := util.File.Read(a.Path); err != nil { + if data, err := tool.File.Read(a.Path); err != nil { return true } else { return string(data) == a.Mark diff --git a/global/config.go b/global/config.go new file mode 100644 index 0000000..0e4a30d --- /dev/null +++ b/global/config.go @@ -0,0 +1,42 @@ +package global + +import ( + "autoLogin/models" + "github.com/Mmx233/config" + "log" + "os" +) + +var Config *models.Config + +func init() { + //配置文件初始化 + if e := config.Load(config.Options{ + Config: &Config, + Default: &models.Config{ + From: models.LoginForm{ + Domain: "www.msftconnecttest.com", + UserType: "cmcc", + }, + Meta: models.LoginMeta{ + N: "200", + Type: "1", + Acid: "5", + Enc: "srun_bx1", + }, + Settings: models.Settings{ + Timeout: 1, + }, + }, + Path: "Config.json", + FillDefault: true, + Overwrite: true, + }); e != nil { + if config.IsNew(e) { + log.Println("已生成配置文件,请编辑 'Config.json' 然后重试") + os.Exit(0) + } + log.Println("读取配置文件失败:\n", e.Error()) + os.Exit(1) + } +} diff --git a/go.mod b/go.mod index 1827649..a6f150b 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/Mmx233/config v0.0.3 - github.com/Mmx233/tool v0.2.3 + github.com/Mmx233/tool v0.2.4 github.com/PuerkitoBio/goquery v1.7.1 // indirect golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect ) diff --git a/go.sum b/go.sum index 1a4de5d..6a587eb 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Mmx233/config v0.0.3 h1:E744hob5fWD5P7m7q+91Z41R+Y1LDkxIhQXIeV1tel0= github.com/Mmx233/config v0.0.3/go.mod h1:3mt+LSHUvD/C6tq1ge/Dmp8xKivFmo7gxSihCPjIAi0= -github.com/Mmx233/tool v0.2.3 h1:fnjkYrX6Zhc0vcyPF5ihnxkNjXQ8zeAoG1Xm6xo7aBE= -github.com/Mmx233/tool v0.2.3/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0= +github.com/Mmx233/tool v0.2.4 h1:lsq45GmGJGZ7RkOVj6EYucvxRK58/nGSVf8rhgZNov0= +github.com/Mmx233/tool v0.2.4/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0= github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4= github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY= github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE= diff --git a/util/file.go b/util/file.go deleted file mode 100644 index 27b329b..0000000 --- a/util/file.go +++ /dev/null @@ -1,79 +0,0 @@ -package util - -import ( - "bufio" - "encoding/json" - "io/ioutil" - "os" - "path/filepath" -) - -type file struct{} - -var File file - -func (a *file) Exists(path string) bool { - root, err := a.GetRootPath() - if err != nil { - return false - } - _, err = os.Stat(root + path) - return err == nil || os.IsExist(err) -} - -func (a *file) Read(path string) ([]byte, error) { - root, err := a.GetRootPath() - if err != nil { - return nil, err - } - return ioutil.ReadFile(root + path) -} - -func (a *file) ReadJson(path string, receiver interface{}) error { - data, err := a.Read(path) - if err != nil { - return err - } - return json.Unmarshal(data, receiver) -} - -func (a *file) Write(path string, data []byte) error { - root, err := a.GetRootPath() - if err != nil { - return err - } - return ioutil.WriteFile(root+path, data, 700) -} - -func (a *file) WriteJson(path string, receiver interface{}) error { - data, err := json.MarshalIndent(receiver, "", " ") - if err != nil { - return err - } - return a.Write(path, data) -} - -func (*file) GetRootPath() (string, error) { - t, err := os.Executable() - if err != nil { - return "", err - } - return filepath.Dir(t) + "/", nil -} - -func (a *file) Add(path string, c string) error { - root, err := a.GetRootPath() - if err != nil { - return err - } - file, err := os.OpenFile(root+path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700) - defer file.Close() - if err != nil { - return err - } - w := bufio.NewWriter(file) - if _, err = w.WriteString(c + "\n"); err != nil { - return err - } - return w.Flush() -} diff --git a/util/init.go b/util/init.go index 62f68d5..37d43ff 100644 --- a/util/init.go +++ b/util/init.go @@ -1,11 +1,13 @@ package util import ( + "autoLogin/global" "github.com/Mmx233/tool" "time" ) func init() { - //http工具设定 - tool.HTTP.Options.Timeout = 3 * time.Second + //tool设定 + tool.HTTP.Options.Timeout = time.Duration(global.Config.Settings.Timeout) * time.Second + tool.File.Options.ForceRoot = true } diff --git a/util/log.go b/util/log.go index d8794b7..1c52fc1 100644 --- a/util/log.go +++ b/util/log.go @@ -3,6 +3,7 @@ package util import ( "autoLogin/global" "fmt" + "github.com/Mmx233/tool" "log" "os" "reflect" @@ -30,7 +31,7 @@ func (*loG) WriteLog(name string, a ...interface{}) { default: t = fmt.Sprint(v) } - err := File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t) + err := tool.File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700) if err != nil { log.Println("Log error: ", err) }