feature:支持使用配置文件,增加网络检查
1、从Config.json读取配置 2、网络正常时跳过登录
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
.idea/
|
||||
Mmx
|
||||
Config.json
|
||||
6
Modles/config.go
Normal file
6
Modles/config.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package Modles
|
||||
|
||||
type Config struct {
|
||||
From LoginForm
|
||||
Meta LoginMeta
|
||||
}
|
||||
31
Modles/login.go
Normal file
31
Modles/login.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package Modles
|
||||
|
||||
type LoginForm struct {
|
||||
Domain string `json:"domain"`
|
||||
UserName string `json:"username"`
|
||||
PassWord string `json:"password"`
|
||||
}
|
||||
|
||||
type LoginMeta struct {
|
||||
N string `json:"n"`
|
||||
VType string `json:"v_type"`
|
||||
Acid string `json:"acid"`
|
||||
Enc string `json:"enc"`
|
||||
}
|
||||
|
||||
type LoginInfo struct {
|
||||
UrlLoginPage string
|
||||
UrlGetChallengeApi string
|
||||
UrlLoginApi string
|
||||
|
||||
Ip string
|
||||
Token string
|
||||
EncryptedInfo string
|
||||
Md5 string
|
||||
EncryptedMd5 string
|
||||
EncryptedChkstr string
|
||||
LoginResult string
|
||||
|
||||
Form *LoginForm
|
||||
Meta *LoginMeta
|
||||
}
|
||||
20
Util/checker.go
Normal file
20
Util/checker.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type checker struct{}
|
||||
|
||||
var Checker checker
|
||||
|
||||
func (checker) NetOk() bool {
|
||||
if ip, err := net.LookupIP("www.msftconnecttest.com"); err != nil {
|
||||
fmt.Println("a")
|
||||
return false
|
||||
} else if len(ip) == 0 || ip[0].String() != "13.107.4.52" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
62
Util/config.go
Normal file
62
Util/config.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"Mmx/Modles"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
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",
|
||||
Meta: Meta,
|
||||
Form: &Modles.LoginForm{
|
||||
UserName: Form.UserName + "@cmcc",
|
||||
PassWord: Form.PassWord,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *config) Init() *Modles.LoginInfo {
|
||||
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("Create 'Config.json' error:\n", err.Error())
|
||||
os.Exit(3)
|
||||
}
|
||||
fmt.Println("Please edit 'Config.json' and try again.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var c Modles.Config
|
||||
if err := File.Read(a.Path, &c); err != nil {
|
||||
fmt.Println("Read config failed:\n", err.Error())
|
||||
os.Exit(3)
|
||||
}
|
||||
|
||||
return a.Generate(
|
||||
&c.From,
|
||||
&c.Meta,
|
||||
)
|
||||
}
|
||||
39
Util/file.go
Normal file
39
Util/file.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
}
|
||||
|
||||
var File file
|
||||
|
||||
func (*file) Exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (*file) Read(path string, receiver interface{}) error {
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(file, receiver)
|
||||
}
|
||||
|
||||
func (*file) Write(path string, receiver interface{}) error {
|
||||
data, err := json.MarshalIndent(receiver, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(path, data, 777)
|
||||
}
|
||||
72
main.go
72
main.go
@@ -5,63 +5,23 @@ import (
|
||||
"Mmx/Util"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type LoginForm struct {
|
||||
UserName string
|
||||
PassWord string
|
||||
}
|
||||
|
||||
type LoginInfo struct {
|
||||
UrlLoginPage string
|
||||
UrlGetChallengeApi string
|
||||
UrlLoginApi string
|
||||
N string
|
||||
VType string
|
||||
Acid string
|
||||
Enc string
|
||||
|
||||
Ip string
|
||||
Token string
|
||||
EncryptedInfo string
|
||||
Md5 string
|
||||
EncryptedMd5 string
|
||||
EncryptedChkstr string
|
||||
LoginResult string
|
||||
Form *LoginForm
|
||||
}
|
||||
|
||||
func Generate(Domain string, Username string, Password string) *LoginInfo {
|
||||
return &LoginInfo{
|
||||
UrlLoginPage: "http://" + Domain + "/srun_portal_success?ac_id=5&theme=basic1",
|
||||
UrlGetChallengeApi: "http://" + Domain + "/cgi-bin/get_challenge",
|
||||
UrlLoginApi: "http://" + Domain + "/cgi-bin/srun_portal",
|
||||
N: "200",
|
||||
VType: "1",
|
||||
Acid: "5",
|
||||
Enc: "srun_bx1",
|
||||
Form: &LoginForm{
|
||||
UserName: Username + "@cmcc",
|
||||
PassWord: Password,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ErrHandler(err error) {
|
||||
if err != nil {
|
||||
defer os.Exit(3)
|
||||
fmt.Println("Error")
|
||||
fmt.Println("Error occurred")
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
G := Generate(
|
||||
"", //登录地址域名或ip
|
||||
"", //账号
|
||||
"", //密码
|
||||
)
|
||||
if Util.Checker.NetOk() {
|
||||
fmt.Println("There's no need to login")
|
||||
return
|
||||
}
|
||||
|
||||
G := Util.Config.Init()
|
||||
|
||||
fmt.Println("Step1: Get local ip returned from srun server.")
|
||||
{
|
||||
body, err := Request.Get(G.UrlLoginPage, nil)
|
||||
@@ -86,8 +46,8 @@ func main() {
|
||||
"username": G.Form.UserName,
|
||||
"password": G.Form.PassWord,
|
||||
"ip": G.Ip,
|
||||
"acid": G.Acid,
|
||||
"enc_ver": G.Enc,
|
||||
"acid": G.Meta.Acid,
|
||||
"enc_ver": G.Meta.Enc,
|
||||
})
|
||||
ErrHandler(err)
|
||||
G.EncryptedInfo = "{SRBX1}" + Util.Base64(Util.XEncode(string(info), G.Token))
|
||||
@@ -97,10 +57,10 @@ func main() {
|
||||
var chkstr string
|
||||
chkstr = G.Token + G.Form.UserName
|
||||
chkstr += G.Token + G.Md5
|
||||
chkstr += G.Token + G.Acid
|
||||
chkstr += G.Token + G.Meta.Acid
|
||||
chkstr += G.Token + G.Ip
|
||||
chkstr += G.Token + G.N
|
||||
chkstr += G.Token + G.VType
|
||||
chkstr += G.Token + G.Meta.N
|
||||
chkstr += G.Token + G.Meta.VType
|
||||
chkstr += G.Token + G.EncryptedInfo
|
||||
G.EncryptedChkstr = Util.Sha1(chkstr)
|
||||
|
||||
@@ -109,12 +69,12 @@ func main() {
|
||||
"action": "login",
|
||||
"username": G.Form.UserName,
|
||||
"password": G.EncryptedMd5,
|
||||
"ac_id": G.Acid,
|
||||
"ac_id": G.Meta.Acid,
|
||||
"ip": G.Ip,
|
||||
"info": G.EncryptedInfo,
|
||||
"chksum": G.EncryptedChkstr,
|
||||
"n": G.N,
|
||||
"type": G.VType,
|
||||
"n": G.Meta.N,
|
||||
"type": G.Meta.VType,
|
||||
"os": "Windows 10",
|
||||
"name": "windows",
|
||||
"double_stack": "0",
|
||||
|
||||
Reference in New Issue
Block a user