chore: 重新抽象 srun 通讯模块

This commit is contained in:
Mmx233
2022-10-21 13:20:31 +08:00
parent 43e377283c
commit 09acd88312
8 changed files with 56 additions and 38 deletions

View File

@@ -4,15 +4,14 @@ import (
"github.com/Mmx233/BitSrunLoginGo/global" "github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/BitSrunLoginGo/util" "github.com/Mmx233/BitSrunLoginGo/util"
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1" BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
"net" "net"
) )
// Login 登录逻辑 // Login 登录逻辑
func Login(localAddr net.Addr) error { func Login(localAddr net.Addr) error {
return BitSrun.Login(&srunTransfer.Login{ return BitSrun.Login(&BitSrun.LoginConf{
Https: global.Config.Settings.Basic.Https, Https: global.Config.Settings.Basic.Https,
LoginInfo: srunTransfer.LoginInfo{ LoginInfo: BitSrun.LoginInfo{
Form: &global.Config.Form, Form: &global.Config.Form,
Meta: &global.Config.Meta, Meta: &global.Config.Meta,
}, },

View File

@@ -2,7 +2,7 @@ package global
import ( import (
"github.com/Mmx233/BitSrunLoginGo/models" "github.com/Mmx233/BitSrunLoginGo/models"
"github.com/Mmx233/BitSrunLoginGo/v1/transfer" "github.com/Mmx233/BitSrunLoginGo/v1"
"github.com/Mmx233/tool" "github.com/Mmx233/tool"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -16,11 +16,11 @@ var Timeout time.Duration
func readConfig() { func readConfig() {
//配置文件默认值 //配置文件默认值
viper.SetDefault("form", srunTransfer.LoginForm{ viper.SetDefault("form", BitSrun.LoginForm{
Domain: "www.msftconnecttest.com", Domain: "www.msftconnecttest.com",
UserType: "cmcc", UserType: "cmcc",
}) })
viper.SetDefault("meta", srunTransfer.LoginMeta{ viper.SetDefault("meta", BitSrun.LoginMeta{
N: "200", N: "200",
Type: "1", Type: "1",
Acid: "5", Acid: "5",

View File

@@ -1,6 +1,8 @@
package srunModels package srunModels
import srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer" import (
srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1"
)
type Daemon struct { type Daemon struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"` Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`

View File

@@ -1,6 +1,8 @@
package srunModels package srunModels
import "github.com/Mmx233/BitSrunLoginGo/v1/transfer" import (
"github.com/Mmx233/BitSrunLoginGo/v1"
)
type LoginInfo struct { type LoginInfo struct {
Ip string Ip string
@@ -11,6 +13,6 @@ type LoginInfo struct {
EncryptedChkstr string EncryptedChkstr string
LoginResult string LoginResult string
Form *srunTransfer.LoginForm Form *BitSrun.LoginForm
Meta *srunTransfer.LoginMeta Meta *BitSrun.LoginMeta
} }

View File

@@ -2,13 +2,13 @@ package util
import ( import (
"github.com/Mmx233/BitSrunLoginGo/models" "github.com/Mmx233/BitSrunLoginGo/models"
"github.com/Mmx233/BitSrunLoginGo/v1/transfer" "github.com/Mmx233/BitSrunLoginGo/v1"
) )
func GenerateLoginInfo(Form *srunTransfer.LoginForm, Meta *srunTransfer.LoginMeta) *srunModels.LoginInfo { func GenerateLoginInfo(Form *BitSrun.LoginForm, Meta *BitSrun.LoginMeta) *srunModels.LoginInfo {
return &srunModels.LoginInfo{ return &srunModels.LoginInfo{
Meta: Meta, Meta: Meta,
Form: &srunTransfer.LoginForm{ Form: &BitSrun.LoginForm{
UserName: func() string { UserName: func() string {
if Form.UserType == "" { if Form.UserType == "" {
return Form.UserName return Form.UserName

View File

@@ -2,31 +2,20 @@ package BitSrun
import ( import (
"encoding/json" "encoding/json"
"github.com/Mmx233/BitSrunLoginGo/util" "github.com/Mmx233/BitSrunLoginGo/util"
srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer"
log "github.com/sirupsen/logrus" 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) 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 var ok bool
{ {
log.Debugln("正在检查登录状态") log.Debugln("正在检查登录状态")
res, e := api.GetUserInfo() res, e := c.api.GetUserInfo()
if e != nil { if e != nil {
return e return e
} }
@@ -56,7 +45,7 @@ func Login(c *srunTransfer.Login) error {
{ {
log.Debugln("正在获取 Token") 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 { if e != nil {
return e return e
} }
@@ -92,7 +81,7 @@ func Login(c *srunTransfer.Login) error {
chkstr += G.Token + G.EncryptedInfo chkstr += G.Token + G.EncryptedInfo
G.EncryptedChkstr = util.Sha1(chkstr) G.EncryptedChkstr = util.Sha1(chkstr)
res, e := api.Login( res, e := c.api.Login(
G.Form.UserName, G.Form.UserName,
G.EncryptedMd5, G.EncryptedMd5,
G.Meta.Acid, G.Meta.Acid,

View File

@@ -1,6 +1,9 @@
package srunTransfer package BitSrun
import "net/http" import (
"github.com/Mmx233/BitSrunLoginGo/v1/srun"
"net/http"
)
type LoginForm struct { type LoginForm struct {
Domain string `json:"domain"` Domain string `json:"domain"`
@@ -22,10 +25,16 @@ type LoginInfo struct {
Meta *LoginMeta Meta *LoginMeta
} }
type Login struct { type LoginConf struct {
//调用 API 时直接访问 https URL //调用 API 时直接访问 https URL
Https bool Https bool
//登录参数,不可缺省 //登录参数,不可缺省
LoginInfo LoginInfo LoginInfo LoginInfo
Client *http.Client Client *http.Client
api srun.Api
}
func (a *LoginConf) initApi() {
a.api.Init(a.Https, a.LoginInfo.Form.Domain, a.Client)
} }

View File

@@ -1,4 +1,4 @@
package BitSrun package srun
import ( import (
"encoding/json" "encoding/json"
@@ -10,12 +10,29 @@ import (
"time" "time"
) )
type SrunApi struct { type Api struct {
inited bool
BaseUrl string BaseUrl string
Client *http.Client 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) log.Debugln("HTTP GET ", a.BaseUrl+path)
timestamp := fmt.Sprint(time.Now().UnixNano()) timestamp := fmt.Sprint(time.Now().UnixNano())
callback := "jQuery" + timestamp 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) 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) return a.request("cgi-bin/rad_user_info", nil)
} }
func (a *SrunApi) Login( func (a *Api) Login(
Username, Username,
Password, Password,
AcID, 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( return a.request(
"cgi-bin/get_challenge", "cgi-bin/get_challenge",
map[string]interface{}{ map[string]interface{}{