chore: 重新抽象 srun 通讯模块
This commit is contained in:
21
v1/login.go
21
v1/login.go
@@ -2,31 +2,20 @@ package BitSrun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/Mmx233/BitSrunLoginGo/util"
|
||||
srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Login(c *srunTransfer.Login) error {
|
||||
func Login(c *LoginConf) error {
|
||||
c.initApi()
|
||||
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 + "/"
|
||||
}(),
|
||||
Client: c.Client,
|
||||
}
|
||||
|
||||
var ok bool
|
||||
|
||||
{
|
||||
log.Debugln("正在检查登录状态")
|
||||
|
||||
res, e := api.GetUserInfo()
|
||||
res, e := c.api.GetUserInfo()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -56,7 +45,7 @@ func Login(c *srunTransfer.Login) error {
|
||||
{
|
||||
log.Debugln("正在获取 Token")
|
||||
|
||||
res, e := api.GetChallenge(G.Form.UserName, G.Ip)
|
||||
res, e := c.api.GetChallenge(G.Form.UserName, G.Ip)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -92,7 +81,7 @@ func Login(c *srunTransfer.Login) error {
|
||||
chkstr += G.Token + G.EncryptedInfo
|
||||
G.EncryptedChkstr = util.Sha1(chkstr)
|
||||
|
||||
res, e := api.Login(
|
||||
res, e := c.api.Login(
|
||||
G.Form.UserName,
|
||||
G.EncryptedMd5,
|
||||
G.Meta.Acid,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package srunTransfer
|
||||
package BitSrun
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1/srun"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LoginForm struct {
|
||||
Domain string `json:"domain"`
|
||||
@@ -22,10 +25,16 @@ type LoginInfo struct {
|
||||
Meta *LoginMeta
|
||||
}
|
||||
|
||||
type Login struct {
|
||||
type LoginConf struct {
|
||||
//调用 API 时直接访问 https URL
|
||||
Https bool
|
||||
//登录参数,不可缺省
|
||||
LoginInfo LoginInfo
|
||||
Client *http.Client
|
||||
|
||||
api srun.Api
|
||||
}
|
||||
|
||||
func (a *LoginConf) initApi() {
|
||||
a.api.Init(a.Https, a.LoginInfo.Form.Domain, a.Client)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package BitSrun
|
||||
package srun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -10,12 +10,29 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SrunApi struct {
|
||||
type Api struct {
|
||||
inited bool
|
||||
BaseUrl string
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
func (a *SrunApi) request(path string, query map[string]interface{}) (map[string]interface{}, error) {
|
||||
func (a *Api) Init(https bool, domain string, client *http.Client) {
|
||||
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
|
||||
@@ -41,11 +58,11 @@ func (a *SrunApi) request(path string, query map[string]interface{}) (map[string
|
||||
return r, json.Unmarshal([]byte(res), &r)
|
||||
}
|
||||
|
||||
func (a *SrunApi) GetUserInfo() (map[string]interface{}, error) {
|
||||
func (a *Api) GetUserInfo() (map[string]interface{}, error) {
|
||||
return a.request("cgi-bin/rad_user_info", nil)
|
||||
}
|
||||
|
||||
func (a *SrunApi) Login(
|
||||
func (a *Api) Login(
|
||||
Username,
|
||||
Password,
|
||||
AcID,
|
||||
@@ -73,7 +90,7 @@ func (a *SrunApi) Login(
|
||||
})
|
||||
}
|
||||
|
||||
func (a *SrunApi) GetChallenge(username, ip string) (map[string]interface{}, error) {
|
||||
func (a *Api) GetChallenge(username, ip string) (map[string]interface{}, error) {
|
||||
return a.request(
|
||||
"cgi-bin/get_challenge",
|
||||
map[string]interface{}{
|
||||
Reference in New Issue
Block a user