style:包名小写
This commit is contained in:
37
util/XBase64.go
Normal file
37
util/XBase64.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package util
|
||||
|
||||
func getbyte(a byte) int {
|
||||
x := int(a)
|
||||
if x > 255 {
|
||||
Log.Fatalln("INVALID_CHARACTER_ERR: DOM Exception 5")
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func Base64(s []byte) string {
|
||||
const ALPHA = "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA"
|
||||
const PADCHAR = "="
|
||||
i := 0
|
||||
b10 := 0
|
||||
var x []byte
|
||||
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)])
|
||||
}
|
||||
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)
|
||||
}
|
||||
91
util/XEncode.go
Normal file
91
util/XEncode.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func ordat(msg string, idx int) uint32 {
|
||||
if len(msg) > idx {
|
||||
return uint32([]byte(msg)[idx])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func sensCode(content string, key bool) []uint32 {
|
||||
l := len(content)
|
||||
pwd := make([]uint32, 0)
|
||||
for i := 0; i < l; i += 4 {
|
||||
pwd = append(
|
||||
pwd,
|
||||
ordat(content, i)|ordat(content, i+1)<<8|ordat(content, i+2)<<16|ordat(content, i+3)<<24,
|
||||
)
|
||||
}
|
||||
if key {
|
||||
pwd = append(pwd, uint32(l))
|
||||
}
|
||||
return pwd
|
||||
}
|
||||
|
||||
func lenCode(msg []uint32, key bool) []byte {
|
||||
l := uint32(len(msg))
|
||||
ll := (l - 1) << 2
|
||||
if key {
|
||||
m := msg[l-1]
|
||||
if m < ll-3 || m > ll {
|
||||
return nil
|
||||
}
|
||||
ll = m
|
||||
}
|
||||
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))
|
||||
}
|
||||
if key {
|
||||
return t[0:ll]
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func XEncode(content string, key string) []byte {
|
||||
if content == "" {
|
||||
return nil
|
||||
}
|
||||
pwd := sensCode(content, true)
|
||||
pwdk := sensCode(key, false)
|
||||
if len(pwdk) < 4 {
|
||||
for i := 0; i < (4 - len(pwdk)); i++ {
|
||||
pwdk = append(pwdk, 0)
|
||||
}
|
||||
}
|
||||
var n = uint32(len(pwd) - 1)
|
||||
z := pwd[n]
|
||||
y := pwd[0]
|
||||
var c uint32 = 0x86014019 | 0x183639A0
|
||||
var m uint32 = 0
|
||||
var e uint32 = 0
|
||||
var p uint32 = 0
|
||||
q := math.Floor(6 + 52/(float64(n)+1))
|
||||
var d uint32 = 0
|
||||
for 0 < q {
|
||||
d = d + c&(0x8CE0D9BF|0x731F2640)
|
||||
e = d >> 2 & 3
|
||||
p = 0
|
||||
for p < n {
|
||||
y = pwd[p+1]
|
||||
m = z>>5 ^ y<<2
|
||||
m = m + ((y>>3 ^ z<<4) ^ (d ^ y))
|
||||
m = m + (pwdk[(p&3)^e] ^ z)
|
||||
pwd[p] = pwd[p] + m&(0xEFB8D130|0x10472ECF)
|
||||
z = pwd[p]
|
||||
p = p + 1
|
||||
}
|
||||
y = pwd[0]
|
||||
m = z>>5 ^ y<<2
|
||||
m = m + ((y>>3 ^ z<<4) ^ (d ^ y))
|
||||
m = m + (pwdk[(p&3)^e] ^ z)
|
||||
pwd[n] = pwd[n] + m&(0xBB390742|0x44C6F8BD)
|
||||
z = pwd[n]
|
||||
q = q - 1
|
||||
}
|
||||
return lenCode(pwd, false)
|
||||
}
|
||||
23
util/checker.go
Normal file
23
util/checker.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
type checker struct{}
|
||||
|
||||
var Checker checker
|
||||
|
||||
func (checker) NetOk() bool {
|
||||
r := &net.Resolver{ //指定DNS,防止本地DNS缓存影响
|
||||
PreferGo: true,
|
||||
Dial: NetDailEr(),
|
||||
}
|
||||
if ip, err := r.LookupIP(context.Background(), "ip4", "www.msftconnecttest.com"); err != nil { //通过DNS确认是否在线
|
||||
return false
|
||||
} else if len(ip) == 0 || ip[0].String() != "13.107.4.52" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
69
util/file.go
Normal file
69
util/file.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type file struct{}
|
||||
|
||||
var File file
|
||||
|
||||
func (a *file) Exists(path string) bool {
|
||||
_, err := os.Stat(a.GetRootPath() + path)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *file) Read(path string) ([]byte, error) {
|
||||
return ioutil.ReadFile(a.GetRootPath() + 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 {
|
||||
return ioutil.WriteFile(a.GetRootPath()+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 {
|
||||
t, err := os.Executable()
|
||||
if err != nil {
|
||||
ErrHandler(err)
|
||||
}
|
||||
return filepath.Dir(t) + "/"
|
||||
}
|
||||
|
||||
func (a *file) Add(path string, c string) error {
|
||||
file, err := os.OpenFile(a.GetRootPath()+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()
|
||||
}
|
||||
34
util/init.go
Normal file
34
util/init.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"Mmx/Modles"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//配置文件初始化
|
||||
Path := "Config.json"
|
||||
var c modles.Config
|
||||
if !File.Exists(Path) {
|
||||
if err := File.WriteJson(
|
||||
Path,
|
||||
c.FillDefault(),
|
||||
); err != nil {
|
||||
log.Println("创建配置文件失败:\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err := File.ReadJson(Path, &c); err != nil {
|
||||
log.Println("读取配置文件失败:\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_ = File.WriteJson(Path, c.FillDefault())
|
||||
|
||||
global.Config = &c
|
||||
}
|
||||
53
util/log.go
Normal file
53
util/log.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type loG struct {
|
||||
timeStamp string
|
||||
}
|
||||
|
||||
var Log loG
|
||||
|
||||
func (*loG) WriteLog(name string, a ...interface{}) {
|
||||
for _, v := range a {
|
||||
var t string
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.String:
|
||||
t = v.(string)
|
||||
case reflect.Interface:
|
||||
t = v.(error).Error()
|
||||
default:
|
||||
t = fmt.Sprint(v)
|
||||
}
|
||||
err := File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t)
|
||||
if err != nil {
|
||||
log.Println("Log error: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *loG) genTimeStamp() {
|
||||
if c.timeStamp == "" {
|
||||
c.timeStamp = time.Now().Format("2006.01.02-15.04.05")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *loG) Println(a ...interface{}) {
|
||||
c.genTimeStamp()
|
||||
if global.Config.Settings.DemoMode {
|
||||
c.WriteLog("Login-"+c.timeStamp+".log", a...)
|
||||
}
|
||||
log.Println(a...)
|
||||
}
|
||||
|
||||
func (c *loG) Fatalln(a ...interface{}) {
|
||||
c.genTimeStamp()
|
||||
c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
|
||||
log.Fatalln(a...)
|
||||
}
|
||||
71
util/util.go
Normal file
71
util/util.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
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)
|
||||
return fmt.Sprintf("%x", w.Sum(nil))
|
||||
}
|
||||
|
||||
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 {
|
||||
Log.Println("运行出错,状态异常")
|
||||
if global.Config.Settings.DemoMode {
|
||||
Log.Fatalln(err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func NetDailEr() func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
d := net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
return d.DialContext(ctx, "udp", global.Config.Settings.Dns+":53")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user