1
0
mirror of https://github.com/fumiama/go-nd-portal.git synced 2026-06-19 17:16:15 +08:00

chore(lint): 改进代码样式 (#2)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2025-05-18 21:04:57 +09:00
committed by GitHub
parent 0fb9dda436
commit 7b844523eb
3 changed files with 105 additions and 109 deletions

View File

@@ -101,7 +101,7 @@ func Main() {
}
logrus.Debugf("server addr: %s, login type: %s", *s, *t)
if *s != portal.PortalServerIPQsh {
// just validate IP here,
// just validate IP here,
// dont convert to net.IP because we need only its string later
_, err := netip.ParseAddr(*s)
if err != nil {
@@ -109,8 +109,8 @@ func Main() {
os.Exit(line())
}
}
// n : username
// p: password
// n : username
// p: password
// ip : public ip
// *t : login type
ptl, err := portal.NewPortal(*n, *p, ip, portal.LoginType(*t))
@@ -125,7 +125,7 @@ func Main() {
logrus.Errorln(err)
os.Exit(line())
}
// input:
// input:
// server IP
// challenge
err = ptl.Login(*s, challenge)

View File

@@ -17,22 +17,22 @@ import (
var (
// ErrIllegalIPv4 is returned when an invalid IPv4 address is provided
ErrIllegalIPv4 = errors.New("illegal ipv4")
ErrIllegalIPv4 = errors.New("illegal ipv4")
// ErrIllegalLoginType is returned when an invalid login type is provided
ErrIllegalLoginType = errors.New("illegal login type")
ErrIllegalLoginType = errors.New("illegal login type")
// ErrUnexpectedChallengeResponse is returned when challenge is shorter than expected
ErrUnexpectedChallengeResponse = errors.New("unexpected challenge response")
// ErrUnexpectedLoginResponse is returned when login resp is shorter than expected
ErrUnexpectedLoginResponse = errors.New("unexpected login response")
ErrUnexpectedLoginResponse = errors.New("unexpected login response")
)
// Portal struct for login config
type Portal struct {
name string
pswd string
ip net.IP
domain string
acid string
name string
pswd string
ip net.IP
domain string
acid string
}
// LoginType defines known login types
@@ -92,11 +92,11 @@ func NewPortal(name, password string, ipv4 net.IP, loginType LoginType) (*Portal
logrus.Debugf("portal domain: %s, ac_id: %s", domain, acid)
return &Portal{
name: name,
pswd: password,
ip: ipv4,
name: name,
pswd: password,
ip: ipv4,
domain: domain,
acid: acid,
acid: acid,
}, nil
}
@@ -106,14 +106,14 @@ func NewPortal(name, password string, ipv4 net.IP, loginType LoginType) (*Portal
func (p *Portal) GetChallenge(sIP string) (string, error) {
// Note: no need to do URL encoding here
u, err := GetChallengeURL(
sIP,
"gondportal",
p.name,
p.domain,
p.ip,
sIP,
"gondportal",
p.name,
p.domain,
p.ip,
time.Now().UnixMilli(),
)
if err != nil {
return "", err
}
@@ -147,7 +147,7 @@ func (p *Portal) PasswordHMd5(challenge string) string {
}
// Login sends login request to server
// input:
// input:
// server IP
// challenge
func (p *Portal) Login(sIP, challenge string) error {
@@ -159,15 +159,15 @@ func (p *Portal) Login(sIP, challenge string) error {
hmd5 := p.PasswordHMd5(challenge)
// Note: no need to do URL encoding here
u, err := GetLoginURL(
sIP,
"gondportal",
p.name,
p.domain,
hmd5,
p.acid,
p.ip,
p.CheckSum(challenge, p.name, p.domain, hmd5, p.acid, p.ip, info),
info,
sIP,
"gondportal",
p.name,
p.domain,
hmd5,
p.acid,
p.ip,
p.CheckSum(challenge, p.name, p.domain, hmd5, p.acid, p.ip, info),
info,
time.Now().UnixMilli(),
)

View File

@@ -17,37 +17,37 @@ import (
const (
// PortalServerIPQsh default Server IP String in Qsh work area
PortalServerIPQsh = "10.253.0.237"
PortalServerIPQsh = "10.253.0.237"
// PortalServerIPQshDorm default Server IP String in Qsh new dorm area
PortalServerIPQshDorm = "10.253.0.235"
PortalServerIPQshDorm = "10.253.0.235"
// PortalDomainQsh PortalDomain for qsh-edu login type
PortalDomainQsh = "@dx-uestc"
PortalDomainQsh = "@dx-uestc"
// PortalDomainQshDX PortalDomain for qsh-dx, qshd-dx login types
PortalDomainQshDX = "@dx"
PortalDomainQshDX = "@dx"
// PortalDomainQshCMCC PortalDomain for qshd-cmcc login type
PortalDomainQshCMCC = "@cmcc"
PortalDomainQshCMCC = "@cmcc"
// PortalGetChallenge GetChallenge URL
PortalGetChallenge = "http://%v/cgi-bin/get_challenge?%s"
// 1.server IP
// 2.callback
// 3.username 4.PortalDomain
PortalGetChallenge = "http://%v/cgi-bin/get_challenge?%s"
// 1.server IP
// 2.callback
// 3.username 4.PortalDomain
// 5.client IP
// 6.timestamp
// PortalGetChallenge = "http://%v/cgi-bin/get_challenge?callback=%s&username=%s%s&ip=%v&_=%d"
// AcIDQsh ACID for Qsh work area
AcIDQsh = "1"
AcIDQsh = "1"
// AcIDQshDorm ACID for Qsh new dorm area
AcIDQshDorm = "3"
AcIDQshDorm = "3"
// PortalCGI Auth CGI URL
PortalCGI = "http://%v/cgi-bin/srun_portal?%s"
PortalCGI = "http://%v/cgi-bin/srun_portal?%s"
// qsh LoginURL key-value order
// 1.server IP
// 2.callback
// 3.username 4.PortalDomain
// 1.server IP
// 2.callback
// 3.username 4.PortalDomain
// 5.encrypted password
// 6.ac_id: determined by login area
// 7.client IP
@@ -59,43 +59,42 @@ const (
// GetChallengeReq struct for GetChallenge URL query
type GetChallengeReq struct {
Callback string `url:"callback"`
Username string `url:"username"`
IP string `url:"ip"`
Timestamp int64 `url:"_"`
Callback string `url:"callback"`
Username string `url:"username"`
IP string `url:"ip"`
Timestamp int64 `url:"_"`
}
// GetPortalReq struct for Portal Auth CGI URL query
type GetPortalReq struct {
Callback string `url:"callback"`
Action string `url:"action"`
Username string `url:"username"`
EncryptedPassword string `url:"password"`
AcID string `url:"ac_id"`
IP string `url:"ip"`
Checksum string `url:"chksum"`
EncodedUserInfo string `url:"info"`
ConstantN string `url:"n"`
ConstantType string `url:"type"`
OS string `url:"os"`
Platform string `url:"name"`
DoubleStack string `url:"double_stack"`
Timestamp int64 `url:"_"`
Callback string `url:"callback"`
Action string `url:"action"`
Username string `url:"username"`
EncryptedPassword string `url:"password"`
AcID string `url:"ac_id"`
IP string `url:"ip"`
Checksum string `url:"chksum"`
EncodedUserInfo string `url:"info"`
ConstantN string `url:"n"`
ConstantType string `url:"type"`
OS string `url:"os"`
Platform string `url:"name"`
DoubleStack string `url:"double_stack"`
Timestamp int64 `url:"_"`
}
// GetChallengeURL generates the URL for getchallenge req
func GetChallengeURL(
sIP,
callback,
callback,
username, domain string,
cIP net.IP,
cIP net.IP,
timestamp int64) (string, error) {
v, err := query.Values(&GetChallengeReq{
Callback: callback,
Username: username + domain,
IP: cIP.String(),
Timestamp: timestamp,
Callback: callback,
Username: username + domain,
IP: cIP.String(),
Timestamp: timestamp,
})
if err != nil {
return "", err
@@ -107,30 +106,29 @@ func GetChallengeURL(
// GetLoginURL generates the URL for login req
func GetLoginURL(
sIP,
callback,
username, domain,
callback,
username, domain,
md5Password,
acid string,
cIP net.IP,
chksum,
info string,
info string,
timestamp int64) (string, error) {
v, err := query.Values(&GetPortalReq{
Callback: callback,
Action: "login",
Username: username + domain,
EncryptedPassword: "{MD5}" + md5Password,
AcID: acid,
IP: cIP.String(),
Checksum: chksum,
EncodedUserInfo: "{SRBX1}" + info,
ConstantN: "200",
ConstantType: "1",
OS: "Windows 10",
Platform: "Windows",
DoubleStack: "0",
Timestamp: timestamp,
Callback: callback,
Action: "login",
Username: username + domain,
EncryptedPassword: "{MD5}" + md5Password,
AcID: acid,
IP: cIP.String(),
Checksum: chksum,
EncodedUserInfo: "{SRBX1}" + info,
ConstantN: "200",
ConstantType: "1",
OS: "Windows 10",
Platform: "Windows",
DoubleStack: "0",
Timestamp: timestamp,
})
if err != nil {
return "", err
@@ -155,24 +153,23 @@ type UserInfo struct {
// GetUserInfo serializes UserInfo JSON to string
func GetUserInfo(
username,
domain,
password string,
cIP net.IP,
username,
domain,
password string,
cIP net.IP,
acid string) (string, error) {
var b strings.Builder
var b strings.Builder
err := json.NewEncoder(&b).Encode(&UserInfo{
Username: username + domain,
Password: password,
IP: cIP.String(),
AcID: acid,
EncVer: "srun_bx1",
Username: username + domain,
Password: password,
IP: cIP.String(),
AcID: acid,
EncVer: "srun_bx1",
})
if err != nil {
return "", err
}
// Note: in case of unexpected error
// we have to remove "\n" at the tail to match actual JSON format
return strings.TrimSpace(b.String()), nil
@@ -233,14 +230,13 @@ func EncodeUserInfo(info, challenge string) string {
// CheckSum calculates chksum parameter for login
func (p *Portal) CheckSum(
challenge,
username,
domain,
hmd5,
acid string,
cIP net.IP,
challenge,
username,
domain,
hmd5,
acid string,
cIP net.IP,
info string) string {
var buf [20]byte
h := sha1.New()
_, _ = h.Write(helper.StringToBytes(challenge))