Files
BitSrunLoginGo/Util/util.go
Mmx 39bbc30d9c fix:增加config属性
1、能自定义是否在启动前检查网络
2、增加demo模式
2021-03-17 17:59:31 +08:00

61 lines
1.2 KiB
Go

package Util
import (
"Mmx/Global"
"crypto/md5"
"crypto/sha1"
"errors"
"fmt"
"io"
"os"
"regexp"
)
func Search(reg string, content string) (string, error) {
r := regexp.MustCompile(reg)
if r == nil {
return "", errors.New("解析正则表达式失败")
}
if s := r.FindStringSubmatch(content); len(s) < 2 {
return "", errors.New("无匹配")
} else {
return s[1], nil
}
}
func GetIp(body string) (string, error) {
return Search("id=\"user_ip\" value=\"(.*?)\"", body)
}
func GetToken(body string) (string, error) {
return Search("\"challenge\":\"(.*?)\"", body)
}
func GetResult(body string) (string, error) {
return Search("\"error\":\"(.+?)\"", body)
}
func Md5(content string) string {
w := md5.New()
_, _ = io.WriteString(w, content) //将str写入到w中
return fmt.Sprintf("%x", w.Sum(nil)) //w.Sum(nil)将w的hash转成[]byte格式
}
func Sha1(content string) string {
h := sha1.New()
h.Write([]byte(content))
bs := h.Sum(nil)
return fmt.Sprintf("%x\n", bs)
}
func ErrHandler(err error) {
if err != nil {
fmt.Println("Error occurred")
if Global.Config.Settings.DemoMode {
panic(err)
}
fmt.Println(err)
os.Exit(3)
}
}