improve: 使用标准项目结构

This commit is contained in:
Mmx233
2023-03-01 18:58:11 +08:00
parent cb11426bd6
commit 3c63e9ddc3
29 changed files with 178 additions and 183 deletions

39
pkg/srun/XBase64.go Normal file
View File

@@ -0,0 +1,39 @@
package srun
import log "github.com/sirupsen/logrus"
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
pkg/srun/XEncode.go Normal file
View File

@@ -0,0 +1,91 @@
package srun
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)
}

121
pkg/srun/api.go Normal file
View File

@@ -0,0 +1,121 @@
package srun
import (
"encoding/json"
"fmt"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
"time"
)
type Api struct {
inited bool
BaseUrl string
Client *http.Client
Header http.Header
}
func (a *Api) Init(https bool, domain string, client *http.Client, header http.Header) {
if a.inited {
return
}
a.BaseUrl = "http"
if https {
a.BaseUrl += "s"
}
a.BaseUrl = a.BaseUrl + "://" + domain + "/"
a.Client = client
a.inited = true
}
func (a *Api) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
log.Debugln("HTTP GET ", a.BaseUrl+path)
timestamp := fmt.Sprint(time.Now().UnixNano())
callback := "jQuery" + timestamp
if query == nil {
query = make(map[string]interface{}, 2)
}
query["callback"] = callback
query["_"] = timestamp
httpTool := tool.NewHttpTool(a.Client)
req, e := httpTool.GenReq("GET", &tool.DoHttpReq{
Url: a.BaseUrl + path,
Query: query,
})
if e != nil {
log.Debugln(e)
return nil, e
}
for k, v := range a.Header {
req.Header[k] = v
}
resp, e := httpTool.Client.Do(req)
if e != nil {
log.Debugln(e)
return nil, e
}
defer resp.Body.Close()
data, e := io.ReadAll(resp.Body)
if e != nil {
log.Debugln(e)
return nil, e
}
res := string(data)
log.Debugln(res)
res = strings.TrimPrefix(res, callback+"(")
res = strings.TrimSuffix(res, ")")
var r map[string]interface{}
return r, json.Unmarshal([]byte(res), &r)
}
func (a *Api) GetUserInfo() (map[string]interface{}, error) {
return a.request("cgi-bin/rad_user_info", nil)
}
func (a *Api) Login(
Username,
Password,
AcID,
Ip,
Info,
ChkSum,
N,
Type string,
) (map[string]interface{}, error) {
return a.request(
"cgi-bin/srun_portal",
map[string]interface{}{
"action": "login",
"username": Username,
"password": Password,
"ac_id": AcID,
"ip": Ip,
"info": Info,
"chksum": ChkSum,
"n": N,
"type": Type,
"os": "Windows 10",
"name": "windows",
"double_stack": 0,
})
}
func (a *Api) GetChallenge(username, ip string) (map[string]interface{}, error) {
return a.request(
"cgi-bin/get_challenge",
map[string]interface{}{
"username": username,
"ip": ip,
})
}

23
pkg/srun/encode.go Normal file
View File

@@ -0,0 +1,23 @@
package srun
import (
"crypto/md5"
"crypto/sha1"
"fmt"
"io"
)
// Md5 编码
func Md5(content string) string {
w := md5.New()
_, _ = io.WriteString(w, content)
return fmt.Sprintf("%x", w.Sum(nil))
}
// Sha1 编码
func Sha1(content string) string {
h := sha1.New()
h.Write([]byte(content))
bs := h.Sum(nil)
return fmt.Sprintf("%x\n", bs)
}

7
pkg/srun/errors.go Normal file
View File

@@ -0,0 +1,7 @@
package srun
import "errors"
var (
ErrResultCannotFound = errors.New("result cannot found from response")
)

106
pkg/srun/login.go Normal file
View File

@@ -0,0 +1,106 @@
package srun
import (
"encoding/json"
"errors"
log "github.com/sirupsen/logrus"
"strings"
)
func LoginStatus(c *Conf) (online bool, ip string, e error) {
c.initApi()
res, e := c.api.GetUserInfo()
if e != nil {
return false, "", e
}
err, ok := res["error"]
if !ok {
return false, "", ErrResultCannotFound
}
ipInterface, ok := res["client_ip"]
if !ok {
ipInterface, ok = res["online_ip"]
if !ok {
return false, "", ErrResultCannotFound
}
}
// 如果深澜分配的 ip 不是内网 ip说明已经在线且拥有固定 ip
ip = ipInterface.(string)
inet := strings.HasPrefix(ip, "192.168.") || strings.HasPrefix(ip, "10.") || strings.HasPrefix(ip, "172.")
online = err.(string) == "ok" || !inet
return
}
func DoLogin(clientIP string, c *Conf) error {
log.Debugln("正在获取 Token")
if c.LoginInfo.Form.UserType != "" {
c.LoginInfo.Form.UserName += "@" + c.LoginInfo.Form.UserType
}
res, e := c.api.GetChallenge(c.LoginInfo.Form.UserName, clientIP)
if e != nil {
return e
}
token, ok := res["challenge"]
if !ok {
return ErrResultCannotFound
}
tokenStr := token.(string)
log.Debugln("token: ", tokenStr)
log.Debugln("发送登录请求")
info, e := json.Marshal(map[string]string{
"username": c.LoginInfo.Form.UserName,
"password": c.LoginInfo.Form.PassWord,
"ip": clientIP,
"acid": c.LoginInfo.Meta.Acid,
"enc_ver": c.LoginInfo.Meta.Enc,
})
if e != nil {
return e
}
EncryptedInfo := "{SRBX1}" + Base64(XEncode(string(info), tokenStr))
Md5Str := Md5(tokenStr)
EncryptedMd5 := "{MD5}" + Md5Str
EncryptedChkstr := Sha1(
tokenStr + c.LoginInfo.Form.UserName + tokenStr + Md5Str +
tokenStr + c.LoginInfo.Meta.Acid + tokenStr + clientIP +
tokenStr + c.LoginInfo.Meta.N + tokenStr + c.LoginInfo.Meta.Type +
tokenStr + EncryptedInfo,
)
res, e = c.api.Login(
c.LoginInfo.Form.UserName,
EncryptedMd5,
c.LoginInfo.Meta.Acid,
clientIP,
EncryptedInfo,
EncryptedChkstr,
c.LoginInfo.Meta.N,
c.LoginInfo.Meta.Type,
)
if e != nil {
return e
}
var result interface{}
result, ok = res["error"]
if !ok {
return ErrResultCannotFound
}
LoginResult := result.(string)
if LoginResult != "ok" {
return errors.New(LoginResult)
}
return nil
}

40
pkg/srun/models.go Normal file
View File

@@ -0,0 +1,40 @@
package srun
import (
"net/http"
)
type LoginForm struct {
Domain string `json:"domain"`
UserName string `json:"username"`
//运营商类型
UserType string `json:"user_type"`
PassWord string `json:"password"`
}
type LoginMeta struct {
N string `json:"n"`
Type string `json:"type"`
Acid string `json:"acid"`
Enc string `json:"enc"`
}
type LoginInfo struct {
Form *LoginForm
Meta *LoginMeta
}
type Conf struct {
//调用 API 时直接访问 https URL
Https bool
//登录参数,不可缺省
LoginInfo LoginInfo
Client *http.Client
Header http.Header
api Api
}
func (a *Conf) initApi() {
a.api.Init(a.Https, a.LoginInfo.Form.Domain, a.Client, a.Header)
}