feature:支持使用配置文件,增加网络检查

1、从Config.json读取配置
2、网络正常时跳过登录
This commit is contained in:
Mmx
2021-03-16 22:51:54 +08:00
parent a273e2673c
commit 56dfeaf4cf
11 changed files with 220 additions and 99 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
.idea/ .idea/
Mmx
Config.json

6
Modles/config.go Normal file
View File

@@ -0,0 +1,6 @@
package Modles
type Config struct {
From LoginForm
Meta LoginMeta
}

31
Modles/login.go Normal file
View 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
}

View File

@@ -1,4 +1,5 @@
# BitSrunLoginGo # BitSrunLoginGo
NCU移动深澜校园网登录脚本Go语言版 NCU移动深澜校园网登录脚本Go语言版
在main函数中填入账号密码和登录地址ip即可 在main函数中填入账号密码和登录地址ip即可

20
Util/checker.go Normal file
View 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
View 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
View 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
View File

@@ -5,63 +5,23 @@ import (
"Mmx/Util" "Mmx/Util"
"encoding/json" "encoding/json"
"fmt" "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) { func ErrHandler(err error) {
if err != nil { if err != nil {
defer os.Exit(3) fmt.Println("Error occurred")
fmt.Println("Error")
panic(err) panic(err)
} }
} }
func main() { func main() {
G := Generate( if Util.Checker.NetOk() {
"", //登录地址域名或ip fmt.Println("There's no need to login")
"", //账号 return
"", //密码 }
)
G := Util.Config.Init()
fmt.Println("Step1: Get local ip returned from srun server.") fmt.Println("Step1: Get local ip returned from srun server.")
{ {
body, err := Request.Get(G.UrlLoginPage, nil) body, err := Request.Get(G.UrlLoginPage, nil)
@@ -86,8 +46,8 @@ func main() {
"username": G.Form.UserName, "username": G.Form.UserName,
"password": G.Form.PassWord, "password": G.Form.PassWord,
"ip": G.Ip, "ip": G.Ip,
"acid": G.Acid, "acid": G.Meta.Acid,
"enc_ver": G.Enc, "enc_ver": G.Meta.Enc,
}) })
ErrHandler(err) ErrHandler(err)
G.EncryptedInfo = "{SRBX1}" + Util.Base64(Util.XEncode(string(info), G.Token)) G.EncryptedInfo = "{SRBX1}" + Util.Base64(Util.XEncode(string(info), G.Token))
@@ -97,10 +57,10 @@ func main() {
var chkstr string var chkstr string
chkstr = G.Token + G.Form.UserName chkstr = G.Token + G.Form.UserName
chkstr += G.Token + G.Md5 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.Ip
chkstr += G.Token + G.N chkstr += G.Token + G.Meta.N
chkstr += G.Token + G.VType chkstr += G.Token + G.Meta.VType
chkstr += G.Token + G.EncryptedInfo chkstr += G.Token + G.EncryptedInfo
G.EncryptedChkstr = Util.Sha1(chkstr) G.EncryptedChkstr = Util.Sha1(chkstr)
@@ -109,12 +69,12 @@ func main() {
"action": "login", "action": "login",
"username": G.Form.UserName, "username": G.Form.UserName,
"password": G.EncryptedMd5, "password": G.EncryptedMd5,
"ac_id": G.Acid, "ac_id": G.Meta.Acid,
"ip": G.Ip, "ip": G.Ip,
"info": G.EncryptedInfo, "info": G.EncryptedInfo,
"chksum": G.EncryptedChkstr, "chksum": G.EncryptedChkstr,
"n": G.N, "n": G.Meta.N,
"type": G.VType, "type": G.Meta.VType,
"os": "Windows 10", "os": "Windows 10",
"name": "windows", "name": "windows",
"double_stack": "0", "double_stack": "0",