chore: 重新抽象 srun 通讯模块
This commit is contained in:
@@ -4,15 +4,14 @@ import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/global"
|
||||
"github.com/Mmx233/BitSrunLoginGo/util"
|
||||
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
"net"
|
||||
)
|
||||
|
||||
// Login 登录逻辑
|
||||
func Login(localAddr net.Addr) error {
|
||||
return BitSrun.Login(&srunTransfer.Login{
|
||||
return BitSrun.Login(&BitSrun.LoginConf{
|
||||
Https: global.Config.Settings.Basic.Https,
|
||||
LoginInfo: srunTransfer.LoginInfo{
|
||||
LoginInfo: BitSrun.LoginInfo{
|
||||
Form: &global.Config.Form,
|
||||
Meta: &global.Config.Meta,
|
||||
},
|
||||
|
||||
@@ -2,7 +2,7 @@ package global
|
||||
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/models"
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1"
|
||||
"github.com/Mmx233/tool"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
@@ -16,11 +16,11 @@ var Timeout time.Duration
|
||||
|
||||
func readConfig() {
|
||||
//配置文件默认值
|
||||
viper.SetDefault("form", srunTransfer.LoginForm{
|
||||
viper.SetDefault("form", BitSrun.LoginForm{
|
||||
Domain: "www.msftconnecttest.com",
|
||||
UserType: "cmcc",
|
||||
})
|
||||
viper.SetDefault("meta", srunTransfer.LoginMeta{
|
||||
viper.SetDefault("meta", BitSrun.LoginMeta{
|
||||
N: "200",
|
||||
Type: "1",
|
||||
Acid: "5",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package srunModels
|
||||
|
||||
import srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
import (
|
||||
srunTransfer "github.com/Mmx233/BitSrunLoginGo/v1"
|
||||
)
|
||||
|
||||
type Daemon struct {
|
||||
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package srunModels
|
||||
|
||||
import "github.com/Mmx233/BitSrunLoginGo/v1/transfer"
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/v1"
|
||||
)
|
||||
|
||||
type LoginInfo struct {
|
||||
Ip string
|
||||
@@ -11,6 +13,6 @@ type LoginInfo struct {
|
||||
EncryptedChkstr string
|
||||
LoginResult string
|
||||
|
||||
Form *srunTransfer.LoginForm
|
||||
Meta *srunTransfer.LoginMeta
|
||||
Form *BitSrun.LoginForm
|
||||
Meta *BitSrun.LoginMeta
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package util
|
||||
|
||||
import (
|
||||
"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{
|
||||
Meta: Meta,
|
||||
Form: &srunTransfer.LoginForm{
|
||||
Form: &BitSrun.LoginForm{
|
||||
UserName: func() string {
|
||||
if Form.UserType == "" {
|
||||
return Form.UserName
|
||||
|
||||
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