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

View File

@@ -5,7 +5,7 @@ import (
"os"
)
func getbyte(a byte)int{
func getbyte(a byte) int {
x := int(a)
if x > 255 {
fmt.Println("INVALID_CHARACTER_ERR: DOM Exception 5")
@@ -14,30 +14,30 @@ func getbyte(a byte)int{
return x
}
func Base64(s []byte)string{
func Base64(s []byte) string {
const ALPHA = "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA"
const PADCHAR = "="
i:=0
b10:=0
i := 0
b10 := 0
var x []byte
imax:=len(s)-len(s)%3
if len(s)==0{
imax := len(s) - len(s)%3
if len(s) == 0 {
return ""
}
for i:=0;i<imax;i+=3{
b10=(getbyte(s[i])<<16)|(getbyte(s[i+1]) << 8)|(getbyte(s[i+2]))
x=append(x,ALPHA[(b10>>18)])
x=append(x,ALPHA[((b10 >> 12) & 63)])
x=append(x,ALPHA[((b10 >> 6) & 63)])
x=append(x,ALPHA[(b10 & 63)])
for i := 0; i < imax; i += 3 {
b10 = (getbyte(s[i]) << 16) | (getbyte(s[i+1]) << 8) | (getbyte(s[i+2]))
x = append(x, ALPHA[(b10>>18)])
x = append(x, ALPHA[((b10>>12)&63)])
x = append(x, ALPHA[((b10>>6)&63)])
x = append(x, ALPHA[(b10&63)])
}
i=imax
if len(s)-imax==1{
b10=getbyte(s[i])<<16
x=append(x,ALPHA[(b10 >> 18)],ALPHA[((b10 >> 12) & 63)],PADCHAR[0],PADCHAR[0])
}else if len(s)-imax ==2{
b10=(getbyte(s[i])<<16)|(getbyte(s[i+1])<<8)
x=append(x,ALPHA[(b10 >> 18)],ALPHA[((b10 >> 12) & 63)],ALPHA[((b10 >> 6) & 63)],PADCHAR[0])
i = imax
if len(s)-imax == 1 {
b10 = getbyte(s[i]) << 16
x = append(x, ALPHA[(b10>>18)], ALPHA[((b10>>12)&63)], PADCHAR[0], PADCHAR[0])
} else if len(s)-imax == 2 {
b10 = (getbyte(s[i]) << 16) | (getbyte(s[i+1]) << 8)
x = append(x, ALPHA[(b10>>18)], ALPHA[((b10>>12)&63)], ALPHA[((b10>>6)&63)], PADCHAR[0])
}
return string(x)
}
}

View File

@@ -38,7 +38,7 @@ func lenCode(msg []uint32, key bool) []byte {
}
var t []byte
for i := range msg {
t = append(t, byte(msg[i]&0xff),byte(msg[i]>>8&0xff),byte(msg[i]>>16&0xff),byte(msg[i]>>24&0xff))
t = append(t, byte(msg[i]&0xff), byte(msg[i]>>8&0xff), byte(msg[i]>>16&0xff), byte(msg[i]>>24&0xff))
}
if key {
return t[0:ll]

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)
}

View File

@@ -26,11 +26,11 @@ func GetIp(body string) (string, error) {
}
func GetToken(body string) (string, error) {
return Search("\"challenge\":\"(.*?)\"",body)
return Search("\"challenge\":\"(.*?)\"", body)
}
func GetResult(body string)(string,error){
return Search("\"error\":\"(.+?)\"",body)
func GetResult(body string) (string, error) {
return Search("\"error\":\"(.+?)\"", body)
}
func Md5(content string) string {
@@ -39,9 +39,9 @@ func Md5(content string) string {
return fmt.Sprintf("%x", w.Sum(nil)) //w.Sum(nil)将w的hash转成[]byte格式
}
func Sha1(content string)string{
func Sha1(content string) string {
h := sha1.New()
h.Write([]byte(content))
bs := h.Sum(nil)
return fmt.Sprintf("%x\n", bs)
}
}