mirror of
https://github.com/fumiama/go-nd-portal.git
synced 2026-07-02 00:50:25 +08:00
feat: auto select server IP when not specified (#4)
This commit is contained in:
@@ -30,7 +30,8 @@ var (
|
||||
type Portal struct {
|
||||
name string
|
||||
pswd string
|
||||
ip net.IP
|
||||
cip net.IP
|
||||
sip string
|
||||
domain string
|
||||
acid string
|
||||
}
|
||||
@@ -49,6 +50,21 @@ const (
|
||||
LoginTypeQshDormCMCC LoginType = "qshd-cmcc"
|
||||
)
|
||||
|
||||
// GetDefaultPortalServerIP returns default PortalServerIP by LoginType
|
||||
func (lt LoginType) GetDefaultPortalServerIP() (string, error) {
|
||||
var sIP string
|
||||
switch lt {
|
||||
case LoginTypeQshEdu, LoginTypeQshDX:
|
||||
sIP = PortalServerIPQsh
|
||||
case LoginTypeQshDormDX, LoginTypeQshDormCMCC:
|
||||
sIP = PortalServerIPQshDorm
|
||||
default:
|
||||
return "", ErrIllegalLoginType
|
||||
}
|
||||
|
||||
return sIP, nil
|
||||
}
|
||||
|
||||
// ToDomainAcID converts LoginType to domain and acid
|
||||
func (lt LoginType) ToDomainAcID() (string, string, error) {
|
||||
var domain, acid string
|
||||
@@ -80,8 +96,8 @@ type rsp struct {
|
||||
}
|
||||
|
||||
// NewPortal creates a new Portal instance
|
||||
func NewPortal(name, password string, ipv4 net.IP, loginType LoginType) (*Portal, error) {
|
||||
if len(ipv4) != 4 {
|
||||
func NewPortal(name, password, sIP string, cIP net.IP, loginType LoginType) (*Portal, error) {
|
||||
if len(cIP) != 4 {
|
||||
return nil, ErrIllegalIPv4
|
||||
}
|
||||
|
||||
@@ -89,28 +105,35 @@ func NewPortal(name, password string, ipv4 net.IP, loginType LoginType) (*Portal
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logrus.Debugf("portal domain: %s, ac_id: %s", domain, acid)
|
||||
logrus.Debugf("login type: %s, portal domain: %s, ac_id: %s", loginType, domain, acid)
|
||||
|
||||
if sIP == "" {
|
||||
sIP, err = loginType.GetDefaultPortalServerIP()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
logrus.Debugf("server addr: %s", sIP)
|
||||
|
||||
return &Portal{
|
||||
name: name,
|
||||
pswd: password,
|
||||
ip: ipv4,
|
||||
cip: cIP,
|
||||
sip: sIP,
|
||||
domain: domain,
|
||||
acid: acid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetChallenge gets token for encryption from server
|
||||
// input:
|
||||
// server IP
|
||||
func (p *Portal) GetChallenge(sIP string) (string, error) {
|
||||
func (p *Portal) GetChallenge() (string, error) {
|
||||
// Note: no need to do URL encoding here
|
||||
u, err := GetChallengeURL(
|
||||
sIP,
|
||||
p.sip,
|
||||
"gondportal",
|
||||
p.name,
|
||||
p.domain,
|
||||
p.ip,
|
||||
p.cip,
|
||||
time.Now().UnixMilli(),
|
||||
)
|
||||
|
||||
@@ -148,10 +171,9 @@ func (p *Portal) PasswordHMd5(challenge string) string {
|
||||
|
||||
// Login sends login request to server
|
||||
// input:
|
||||
// server IP
|
||||
// challenge
|
||||
func (p *Portal) Login(sIP, challenge string) error {
|
||||
userInfo, err := GetUserInfo(p.name, p.domain, p.pswd, p.ip, p.acid)
|
||||
func (p *Portal) Login(challenge string) error {
|
||||
userInfo, err := GetUserInfo(p.name, p.domain, p.pswd, p.cip, p.acid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -159,14 +181,14 @@ func (p *Portal) Login(sIP, challenge string) error {
|
||||
hmd5 := p.PasswordHMd5(challenge)
|
||||
// Note: no need to do URL encoding here
|
||||
u, err := GetLoginURL(
|
||||
sIP,
|
||||
p.sip,
|
||||
"gondportal",
|
||||
p.name,
|
||||
p.domain,
|
||||
hmd5,
|
||||
p.acid,
|
||||
p.ip,
|
||||
p.CheckSum(challenge, p.name, p.domain, hmd5, p.acid, p.ip, info),
|
||||
p.cip,
|
||||
p.CheckSum(challenge, p.name, p.domain, hmd5, p.acid, p.cip, info),
|
||||
info,
|
||||
time.Now().UnixMilli(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user