feat: 变更解析方式,打包Srun请求包
This commit is contained in:
7
v1/errors.go
Normal file
7
v1/errors.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package BitSrun
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrResultCannotFound = errors.New("result cannot found from response")
|
||||
)
|
||||
122
v1/login.go
122
v1/login.go
@@ -5,8 +5,6 @@ import (
|
||||
"errors"
|
||||
"github.com/Mmx233/BitSrunLoginGo/util"
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
"github.com/Mmx233/tool"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Login(c *srunTransfer.Login) error {
|
||||
@@ -14,54 +12,61 @@ func Login(c *srunTransfer.Login) error {
|
||||
util.Log.WriteFile = c.WriteLog
|
||||
util.Log.OutPut = c.OutPut
|
||||
|
||||
G := util.GenerateLoginInfo(c.Https, c.LoginInfo.Form, c.LoginInfo.Meta)
|
||||
if c.CheckNet {
|
||||
if c.CheckNetUrl != "" {
|
||||
util.Checker.SetUrl(c.CheckNetUrl)
|
||||
}
|
||||
util.Log.Info("Step0: 检查状态…")
|
||||
if util.Checker.NetOk(c.Transport) {
|
||||
util.Log.Info("网络 ok")
|
||||
return nil
|
||||
}
|
||||
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
|
||||
api := SrunApi{
|
||||
BaseUrl: func() string {
|
||||
url := "http"
|
||||
if c.Https {
|
||||
url += "s"
|
||||
}
|
||||
return url + "://" + c.LoginInfo.Form.Domain + "/"
|
||||
}(),
|
||||
Transport: c.Transport,
|
||||
}
|
||||
|
||||
util.Log.Info("Step1: 正在获取客户端ip")
|
||||
var ok bool
|
||||
|
||||
{
|
||||
util.Log.Debug("GET ", G.UrlLoginPage)
|
||||
if _, body, e := tool.HTTP.GetString(&tool.GetRequest{
|
||||
Url: G.UrlLoginPage,
|
||||
Redirect: true,
|
||||
Transport: c.Transport,
|
||||
}); e != nil {
|
||||
return e
|
||||
} else if G.Ip, e = util.GetIp(body); e != nil {
|
||||
util.Log.Info("Step.0: 正在检查状态")
|
||||
res, e := api.GetUserInfo()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
err := res["error"].(string)
|
||||
if err == "ok" {
|
||||
util.Log.Info("--已登录--")
|
||||
return nil
|
||||
}
|
||||
|
||||
util.Log.Info("Step.1: 正在获取客户端ip")
|
||||
var ip interface{}
|
||||
ip, ok = res["client_ip"]
|
||||
if !ok {
|
||||
ip, ok = res["online_ip"]
|
||||
if !ok {
|
||||
return ErrResultCannotFound
|
||||
}
|
||||
}
|
||||
G.Ip = ip.(string)
|
||||
util.Log.Debug("ip: ", G.Ip)
|
||||
}
|
||||
|
||||
util.Log.Info("Step2: 正在获取token")
|
||||
util.Log.Info("Step.2: 正在获取token")
|
||||
{
|
||||
util.Log.Debug("GET ", G.UrlGetChallengeApi)
|
||||
if _, data, e := tool.HTTP.GetString(&tool.GetRequest{
|
||||
Url: G.UrlGetChallengeApi,
|
||||
Query: map[string]interface{}{
|
||||
"callback": "jsonp1583251661367",
|
||||
"username": G.Form.UserName,
|
||||
"ip": G.Ip,
|
||||
},
|
||||
Redirect: true,
|
||||
Transport: c.Transport,
|
||||
}); e != nil {
|
||||
return e
|
||||
} else if G.Token, e = util.GetToken(data); e != nil {
|
||||
res, e := api.GetChallenge(G.Form.UserName, G.Ip)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
var token interface{}
|
||||
token, ok = res["challenge"]
|
||||
if !ok {
|
||||
return ErrResultCannotFound
|
||||
}
|
||||
G.Token = token.(string)
|
||||
util.Log.Debug("token: ", G.Token)
|
||||
}
|
||||
|
||||
util.Log.Info("Step3: 执行登录…")
|
||||
util.Log.Info("Step.3: 执行登录…")
|
||||
{
|
||||
info, e := json.Marshal(map[string]string{
|
||||
"username": G.Form.UserName,
|
||||
@@ -83,36 +88,27 @@ func Login(c *srunTransfer.Login) error {
|
||||
chkstr += G.Token + G.EncryptedInfo
|
||||
G.EncryptedChkstr = util.Sha1(chkstr)
|
||||
|
||||
util.Log.Debug("GET ", G.UrlLoginApi)
|
||||
if _, res, e := tool.HTTP.GetString(&tool.GetRequest{
|
||||
Url: G.UrlLoginApi,
|
||||
Query: map[string]interface{}{
|
||||
"callback": "jQuery112401157665",
|
||||
"action": "login",
|
||||
"username": G.Form.UserName,
|
||||
"password": G.EncryptedMd5,
|
||||
"ac_id": G.Meta.Acid,
|
||||
"ip": G.Ip,
|
||||
"info": G.EncryptedInfo,
|
||||
"chksum": G.EncryptedChkstr,
|
||||
"n": G.Meta.N,
|
||||
"type": G.Meta.Type,
|
||||
"os": "Windows 10",
|
||||
"name": "windows",
|
||||
"double_stack": 0,
|
||||
"_": time.Now().UnixNano(),
|
||||
},
|
||||
Redirect: true,
|
||||
Transport: c.Transport,
|
||||
}); e != nil {
|
||||
res, e := api.Login(
|
||||
G.Form.UserName,
|
||||
G.EncryptedMd5,
|
||||
G.Meta.Acid,
|
||||
G.Ip,
|
||||
G.EncryptedInfo,
|
||||
G.EncryptedChkstr,
|
||||
G.Meta.N,
|
||||
G.Meta.Type,
|
||||
)
|
||||
if e != nil {
|
||||
return e
|
||||
} else if G.LoginResult, e = util.GetResult(res); e != nil {
|
||||
return e
|
||||
} else {
|
||||
util.Log.Info("登录结果: " + G.LoginResult)
|
||||
util.Log.Debug(res)
|
||||
}
|
||||
var result interface{}
|
||||
result, ok = res["error"]
|
||||
if !ok {
|
||||
return ErrResultCannotFound
|
||||
}
|
||||
G.LoginResult = result.(string)
|
||||
|
||||
util.Log.Info("登录结果: " + G.LoginResult)
|
||||
if G.LoginResult != "ok" {
|
||||
return errors.New(G.LoginResult)
|
||||
}
|
||||
|
||||
85
v1/steps.go
Normal file
85
v1/steps.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package BitSrun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Mmx233/BitSrunLoginGo/util"
|
||||
"github.com/Mmx233/tool"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SrunApi struct {
|
||||
BaseUrl string
|
||||
Transport *http.Transport
|
||||
}
|
||||
|
||||
func (a *SrunApi) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
|
||||
util.Log.Debug("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
|
||||
_, res, e := tool.HTTP.GetString(&tool.GetRequest{
|
||||
Url: a.BaseUrl + path,
|
||||
Query: query,
|
||||
Redirect: true,
|
||||
Transport: a.Transport,
|
||||
})
|
||||
if e != nil {
|
||||
util.Log.Debug(e)
|
||||
return nil, e
|
||||
}
|
||||
|
||||
util.Log.Debug(res)
|
||||
res = strings.TrimPrefix(res, callback+"(")
|
||||
res = strings.TrimSuffix(res, ")")
|
||||
|
||||
var r map[string]interface{}
|
||||
return r, json.Unmarshal([]byte(res), &r)
|
||||
}
|
||||
|
||||
func (a *SrunApi) GetUserInfo() (map[string]interface{}, error) {
|
||||
return a.request("cgi-bin/rad_user_info", nil)
|
||||
}
|
||||
|
||||
func (a *SrunApi) 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 *SrunApi) GetChallenge(username, ip string) (map[string]interface{}, error) {
|
||||
return a.request(
|
||||
"cgi-bin/get_challenge",
|
||||
map[string]interface{}{
|
||||
"username": username,
|
||||
"ip": ip,
|
||||
})
|
||||
}
|
||||
@@ -30,9 +30,6 @@ type Login struct {
|
||||
WriteLog bool
|
||||
//控制台日志打印开关
|
||||
OutPut bool
|
||||
//登陆前是否检查网络,只在离线时登录
|
||||
CheckNet bool
|
||||
CheckNetUrl string
|
||||
//登录参数,不可缺省
|
||||
LoginInfo LoginInfo
|
||||
Transport *http.Transport
|
||||
|
||||
Reference in New Issue
Block a user