mirror of
https://github.com/fumiama/go-nd-portal.git
synced 2026-06-13 05:04:26 +08:00
## Pull Request Overview
This PR enhances the login CLI to support a new “dorm area” and replaces the old `-x` flag with two new flags for server host (`-s`) and login type (`-t`).
- Introduces a `LoginType` enum and dynamic domain/AcID mapping in `Portal`.
- Refactors URL construction to use `go-querystring` and JSON-encoded user info.
- Updates tests and removes the hard-coded `-x` logic in `cmd/main.go`.
### Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
<details>
<summary>Show a summary per file</summary>
| File | Description |
| ---------------------- | --------------------------------------------------------------- |
| portal/server_test.go | Updated tests to supply the new `loginType` parameter and use `GetUserInfo`. |
| portal/server.go | Reworked URL builders (`GetChallengeURL`, `GetLoginURL`), JSON `UserInfo`, and dynamic flags. |
| portal/portal.go | Added `LoginType`, `ToDomainAcID`, updated `NewPortal`, `GetChallenge`, and `Login` signatures. |
| cmd/main.go | Removed `-x`, added `-s`/`-t` flags, wired them through to `Portal`. |
| base64/base64.go | Fixed typo in package comment. |
| go.mod | Added `github.com/google/go-querystring` dependency. |
</details>
<details>
<summary>Comments suppressed due to low confidence (4)</summary>
**base64/base64.go:1**
* Typo in the comment: 'talbe' should be 'table'.
```
// Package base64 with customized talbe
```
**portal/portal.go:53**
* New login types (`qsh-dx`, `qshd-dx`, `qshd-cmcc`) were added but there are no unit tests verifying `ToDomainAcID` returns the correct domain and AC ID for each, or that invalid types produce an error. Consider adding tests for each mapping and an invalid case.
```
func (lt LoginType) ToDomainAcID() (string, string, error) {
```
**cmd/main.go:54**
* [nitpick] The flag variables `s` and `t` are not descriptive. Consider renaming them to `server` and `loginType` (or similar) to improve clarity for users and maintainers.
```
s := flag.String("s", portal.PortalServerIPQsh, "login host")
```
**cmd/main.go:106**
* The call to `netip.ParseAddr` requires importing the `net/netip` package, but `netip` is not imported. Add `import "net/netip"` to fix the compile error.
```
_, err := netip.ParseAddr(*s)
```
</details>
---------
Co-authored-by: 源文雨 <41315874+fumiama@users.noreply.github.com>
108 lines
3.4 KiB
Go
108 lines
3.4 KiB
Go
package portal
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/fumiama/go-nd-portal/helper"
|
|
)
|
|
|
|
func TestGetUserInfo(t *testing.T) {
|
|
u, err := NewPortal("2000010101001", "12345678", net.IPv4(1, 2, 3, 4).To4(),"qsh-edu")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := GetUserInfo(u.name, u.domain, u.pswd, u.ip, u.acid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log(info)
|
|
assert.Equal(t, `{"username":"2000010101001@dx-uestc","password":"12345678","ip":"1.2.3.4","acid":"1","enc_ver":"srun_bx1"}`, info)
|
|
}
|
|
|
|
func TestDecodeInfo(t *testing.T) {
|
|
info := `{"username":"2000010101001@dx-uestc","password":"12345678","ip":"1.2.3.4","acid":"1","enc_ver":"srun_bx1"}`
|
|
sc := len(info)
|
|
if sc%4 != 0 {
|
|
sc = (sc/4 + 1) * 4
|
|
}
|
|
userinfo := make([]byte, sc)
|
|
copy(userinfo, info)
|
|
v := make([]uint32, sc/4, sc/4+1)
|
|
for i := 0; i < sc/4; i++ {
|
|
v[i] = binary.LittleEndian.Uint32(userinfo[i*4 : i*4+4])
|
|
}
|
|
v = append(v, uint32(len(info)))
|
|
assert.Equal(t, []uint32{1937056379, 1634628197, 975332717, 808464930, 808529968, 808529969, 1681928496, 1702178168, 576943219, 1634738732, 1870099315, 975332466, 858927394, 926299444, 573317688, 975335529, 841888034, 875442990, 1629629474, 577005923, 573645370, 1852121644, 1702256483, 574235250, 1853190771, 829973087, 32034, 106}, v)
|
|
}
|
|
|
|
func TestDecodeKey(t *testing.T) {
|
|
challenge := "c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5"
|
|
sc := len(challenge)
|
|
k := make([]uint32, sc/4)
|
|
token := helper.StringToBytes(challenge)
|
|
for i := 0; i < sc/4; i++ {
|
|
k[i] = binary.LittleEndian.Uint32(token[i*4 : i*4+4])
|
|
}
|
|
assert.Equal(t, []uint32{842085219, 959525985, 859071540, 959852593, 825713205, 1630681444, 929248099, 959524916, 875651384, 845231969, 842018872, 959997490, 1698181172, 878929252, 842217829, 895824228}, k)
|
|
}
|
|
|
|
func TestEncodeUserInfo(t *testing.T) {
|
|
u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4(),"qsh-edu")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := GetUserInfo(u.name, u.domain, u.pswd, u.ip, u.acid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log(info)
|
|
r := EncodeUserInfo(info, "d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e")
|
|
assert.Equal(t, "CfVnZ9mvKmdgvm/ivovlPibZL6RLAWcx+nBTaYmWH3kmThco+eO4LVsCPFceSmM9PyI0UcMgLE7bmpfY9pr0EWnWdTncXrbW29Aydp+lw6QjxKMgNzgYd7uopiPbIyKpxvJZDHsGw5xh8rMEeq3JXrD2vex27xeI", r)
|
|
}
|
|
|
|
func TestHMd5(t *testing.T) {
|
|
u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4(),"qsh-edu")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Equal(t, "91062ae815fb02d9d15aec834aafffd4", u.PasswordHMd5("d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e"))
|
|
}
|
|
|
|
func TestSha1(t *testing.T) {
|
|
h := sha1.New()
|
|
h.Write([]byte("123456"))
|
|
assert.Equal(t, "7c4a8d09ca3762af61e59520943dc26494f8941b", hex.EncodeToString(h.Sum(nil)))
|
|
}
|
|
|
|
func TestCheckSum(t *testing.T) {
|
|
u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4(),"qsh-edu")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := GetUserInfo(u.name, u.domain, u.pswd, u.ip, u.acid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log(info)
|
|
challenge := "d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e"
|
|
s := u.CheckSum(
|
|
challenge,
|
|
u.name,
|
|
PortalDomainQsh,
|
|
u.PasswordHMd5(challenge),
|
|
u.acid,
|
|
u.ip,
|
|
EncodeUserInfo(
|
|
info,
|
|
challenge,
|
|
),
|
|
)
|
|
assert.Equal(t, "64e8913b6019df98e3b807343b8785856909d745", s)
|
|
}
|