From ca72da0c06243ed6a31c939b1acb737aff706005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 6 Dec 2022 12:56:12 +0800 Subject: [PATCH] support dx login --- README.md | 5 +++-- cmd/main.go | 19 ++++++++++++++----- portal/portal.go | 8 ++++---- portal/server.go | 18 +++++++++++------- portal/server_test.go | 1 + 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a2c852b..d9ebf4e 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,11 @@ $ go install github.com/fumiama/go-nd-portal@latest > 也可不带参数运行,会在启动时询问参数 ```bash -./go-nd-portal -n 20xxxxxxxxxxx -p password +./go-nd-portal -n 20xxxxxxxxxxx -p password [-x] ``` 默认值: - * `ip`: 本机公网出口,可自定义 + * `-ip`: 本机公网出口,可自定义 + * `-x`: 是否使用电信登陆(否) ## 效果 diff --git a/cmd/main.go b/cmd/main.go index 59c25b1..892a7b0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,7 +7,6 @@ import ( "net/netip" "os" "runtime" - "time" "golang.org/x/term" @@ -50,6 +49,7 @@ func Main() { h := flag.Bool("h", false, "display this help") w := flag.Bool("w", false, "only display warn-or-higher-level log") d := flag.Bool("d", false, "display debug-level log") + x := flag.Bool("x", false, "do dx login") flag.Parse() if *h { fmt.Println("Usage:") @@ -96,18 +96,27 @@ func Main() { *p = helper.BytesToString(data) fmt.Println() } - portal, err := portal.NewPortal(*n, *p, ip) + ptl, err := portal.NewPortal(*n, *p, ip) if err != nil { logrus.Errorln(err) os.Exit(line()) } - challenge, err := portal.GetChallenge() + u := portal.PortalGetChallenge + if *x { + u = portal.PortalGetChallengeDX + } + challenge, err := ptl.GetChallenge(u) if err != nil { logrus.Errorln(err) os.Exit(line()) } - time.Sleep(time.Second + time.Duration(time.Now().Unix()%1000)*time.Millisecond) - err = portal.Login(challenge) + u = portal.PortalLogin + dm := portal.PortalDomain + if *x { + u = portal.PortalLoginDX + dm = portal.PortalDomainDX + } + err = ptl.Login(u, dm, challenge) if err != nil { logrus.Errorln(err) os.Exit(line()) diff --git a/portal/portal.go b/portal/portal.go index aacc4de..906f058 100644 --- a/portal/portal.go +++ b/portal/portal.go @@ -44,8 +44,8 @@ func NewPortal(name, password string, ipv4 net.IP) (*Portal, error) { }, nil } -func (p *Portal) GetChallenge() (string, error) { - u := fmt.Sprintf(PortalGetChallenge, "gondportal", url.QueryEscape(p.nam), p.ip, time.Now().UnixMilli()) +func (p *Portal) GetChallenge(u string) (string, error) { + u = fmt.Sprintf(u, "gondportal", url.QueryEscape(p.nam), p.ip, time.Now().UnixMilli()) logrus.Debugln("GET", u) data, err := requestDataWith(u, "GET", PortalHeaderUA) if err != nil { @@ -74,10 +74,10 @@ func (p *Portal) PasswordHMd5(challenge string) string { return hex.EncodeToString(h.Sum(buf[:0])) } -func (p *Portal) Login(challenge string) error { +func (p *Portal) Login(u, domain, challenge string) error { info := EncodeUserInfo(p.String(), challenge) hmd5 := p.PasswordHMd5(challenge) - u := fmt.Sprintf(PortalLogin, "gondportal", url.QueryEscape(p.nam), hmd5, p.ip, p.CheckSum(challenge, hmd5, info), url.QueryEscape(info), time.Now().UnixMilli()) + u = fmt.Sprintf(u, "gondportal", url.QueryEscape(p.nam), hmd5, p.ip, p.CheckSum(domain, challenge, hmd5, info), url.QueryEscape(info), time.Now().UnixMilli()) logrus.Debugln("GET", u) data, err := requestDataWith(u, "GET", PortalHeaderUA) if err != nil { diff --git a/portal/server.go b/portal/server.go index 57b83db..63bb814 100644 --- a/portal/server.go +++ b/portal/server.go @@ -10,10 +10,13 @@ import ( ) const ( - PortalServerIP = "10.253.0.237" - PortalDomain = "@dx-uestc" - PortalGetChallenge = "http://" + PortalServerIP + "/cgi-bin/get_challenge?callback=%s&username=%s" + PortalDomain + "&ip=%v&_=%d" - PortalLogin = "http://" + PortalServerIP + "/cgi-bin/srun_portal?callback=%s&action=login&username=%s" + PortalDomain + "&password={MD5}%s&ac_id=1&ip=%v&chksum=%s&info={SRBX1}%s&n=200&type=1&os=Windows+10&name=Windows&double_stack=0&_=%d" + PortalServerIP = "10.253.0.237" + PortalDomain = "@dx-uestc" + PortalDomainDX = "@dx" + PortalGetChallenge = "http://" + PortalServerIP + "/cgi-bin/get_challenge?callback=%s&username=%s" + PortalDomain + "&ip=%v&_=%d" + PortalGetChallengeDX = "http://" + PortalServerIP + "/cgi-bin/get_challenge?callback=%s&username=%s" + PortalDomainDX + "&ip=%v&_=%d" + PortalLogin = "http://" + PortalServerIP + "/cgi-bin/srun_portal?callback=%s&action=login&username=%s" + PortalDomain + "&password={MD5}%s&ac_id=1&ip=%v&chksum=%s&info={SRBX1}%s&n=200&type=1&os=Windows+10&name=Windows&double_stack=0&_=%d" + PortalLoginDX = "http://" + PortalServerIP + "/cgi-bin/srun_portal?callback=%s&action=login&username=%s" + PortalDomainDX + "&password={MD5}%s&ac_id=1&ip=%v&chksum=%s&info={SRBX1}%s&n=200&type=1&os=Windows+10&name=Windows&double_stack=0&_=%d" ) const ( @@ -21,7 +24,8 @@ const ( ) const ( - PortalUserInfo = `{"username":"%s` + PortalDomain + `","password":"%s","ip":"%v","acid":"1","enc_ver":"srun_bx1"}` + PortalUserInfo = `{"username":"%s` + PortalDomain + `","password":"%s","ip":"%v","acid":"1","enc_ver":"srun_bx1"}` + PortalUserInfoDX = `{"username":"%s` + PortalDomainDX + `","password":"%s","ip":"%v","acid":"1","enc_ver":"srun_bx1"}` ) func EncodeUserInfo(info, challenge string) string { @@ -76,12 +80,12 @@ func EncodeUserInfo(info, challenge string) string { return base64.Base64Encoding.EncodeToString(lv) } -func (p *Portal) CheckSum(challenge, hmd5, info string) string { +func (p *Portal) CheckSum(domain, challenge, hmd5, info string) string { var buf [20]byte h := sha1.New() _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write(helper.StringToBytes(p.nam)) - _, _ = h.Write([]byte(PortalDomain)) + _, _ = h.Write([]byte(domain)) _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write(helper.StringToBytes(hmd5)) _, _ = h.Write(helper.StringToBytes(challenge)) diff --git a/portal/server_test.go b/portal/server_test.go index 2f195a9..b0567b3 100644 --- a/portal/server_test.go +++ b/portal/server_test.go @@ -71,6 +71,7 @@ func TestCheckSum(t *testing.T) { t.Log(u.String()) challenge := "d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e" s := u.CheckSum( + PortalDomain, challenge, u.PasswordHMd5(challenge), EncodeUserInfo(