diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..8f7aba5 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,59 @@ +name: 稳定版 +on: + push: + tags: + - v* + +env: + GITHUB_TOKEN: ${{ github.token }} + +jobs: + my-job: + name: Build go-nd-portal on Push Tag 🚀 + runs-on: ubuntu-latest + steps: + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.19 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Cache Go + id: cache + uses: actions/cache@v2 + with: + # A list of files, directories, and wildcard patterns to cache and restore + path: ~/go/pkg/mod + key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }} + + - name: Tidy Go modules + run: go mod tidy + + - name: Build linux-x64 + run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/go-nd-portal-linux-x64 + - name: Build linux-x86 + run: CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-s -w" -o artifacts/go-nd-portal-linux-x86 + - name: Build windows-x64 + run: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/go-nd-portal-x64.exe + - name: Build windows-x86 + run: CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="-s -w" -o artifacts/go-nd-portal-x86.exe + - name: Build arm64 + run: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags="-s -w" -o artifacts/go-nd-portal-linux-arm64 + - name: Build armv6 + run: CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -o artifacts/go-nd-portal-linux-armv6 + - name: Build darwin amd64 + run: CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/go-nd-portal-darwin-amd64 + - name: Build darwin arm64 + run: CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o artifacts/go-nd-portal-darwin-arm64 + + - name: Upload binaries to release + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: artifacts/go-nd-portal-* + tag: ${{ github.ref }} + overwrite: true + file_glob: true diff --git a/README.md b/README.md new file mode 100644 index 0000000..81ee981 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# go-nd-portal +Go 你电 portal + +## 安装 + +你可以下载 [已经编译好的二进制文件](https://github.com/fumiama/go-nd-portal/releases). + +从源码安装: +```bash +$ go install github.com/fumiama/go-nd-portal@latest +``` + +## 使用方法 + +```bash +./go-nd-portal -n 20xxxxxxxxxxx -p password +``` +默认值: + * `ip`: 本机公网出口,可自定义 diff --git a/base64.go b/base64.go deleted file mode 100644 index 5556cbc..0000000 --- a/base64.go +++ /dev/null @@ -1,9 +0,0 @@ -package gondportal - -import "encoding/base64" - -const ( - PortalBase64Table = "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA" -) - -var Base64Encoding = base64.NewEncoding(PortalBase64Table) diff --git a/base64/base64.go b/base64/base64.go new file mode 100644 index 0000000..fce99b8 --- /dev/null +++ b/base64/base64.go @@ -0,0 +1,9 @@ +package base64 + +import b64 "encoding/base64" + +const ( + PortalBase64Table = "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA" +) + +var Base64Encoding = b64.NewEncoding(PortalBase64Table) diff --git a/base64_test.go b/base64/base64_test.go similarity index 63% rename from base64_test.go rename to base64/base64_test.go index c55003a..8b586b9 100644 --- a/base64_test.go +++ b/base64/base64_test.go @@ -1,7 +1,7 @@ -package gondportal +package base64 import ( - "encoding/base64" + b64 "encoding/base64" "strings" "testing" @@ -10,6 +10,6 @@ import ( func TestBase64(t *testing.T) { buf := strings.Builder{} - base64.NewEncoder(Base64Encoding, &buf).Write([]byte("123456")) + b64.NewEncoder(Base64Encoding, &buf).Write([]byte("123456")) assert.Equal(t, "9F2z0JHI", buf.String()) } diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..59c25b1 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,116 @@ +package cmd + +import ( + "flag" + "fmt" + "net" + "net/netip" + "os" + "runtime" + "time" + + "golang.org/x/term" + + "github.com/sirupsen/logrus" + + "github.com/fumiama/go-nd-portal/helper" + "github.com/fumiama/go-nd-portal/portal" +) + +func outip() (net.IP, error) { + conn, err := net.Dial("udp", "8.8.8.8:53") + if err != nil { + return nil, err + } + _ = conn.Close() + return conn.LocalAddr().(*net.UDPAddr).IP.To4(), nil +} + +func line() int { + _, _, fileLine, ok := runtime.Caller(1) + if ok { + return fileLine + } + return -1 +} + +const query = "query" + +func Main() { + ip, err := outip() + ipf := "" + if err != nil { + ipf = query + } else { + ipf = ip.String() + } + ips := flag.String("ip", ipf, "public IP") + n := flag.String("n", query, "username") + p := flag.String("p", query, "password") + 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") + flag.Parse() + if *h { + fmt.Println("Usage:") + flag.PrintDefaults() + os.Exit(0) + } + if *d { + logrus.SetLevel(logrus.DebugLevel) + } else if *w { + logrus.SetLevel(logrus.WarnLevel) + } + if *ips == query { + fmt.Printf("ip: ") + _, err = fmt.Scanln(ips) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + } + if *ips != ip.String() { + ipaddr, err := netip.ParseAddr(*ips) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + a4 := ipaddr.As4() + copy(ip, a4[:]) + } + if *n == query { + fmt.Printf("username: ") + _, err = fmt.Scanln(n) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + } + if *p == query { + fmt.Printf("password: ") + data, err := term.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + *p = helper.BytesToString(data) + fmt.Println() + } + portal, err := portal.NewPortal(*n, *p, ip) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + challenge, err := portal.GetChallenge() + 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) + if err != nil { + logrus.Errorln(err) + os.Exit(line()) + } + logrus.Infoln("success") +} diff --git a/go.mod b/go.mod index ba9f2b3..5b1a612 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,16 @@ go 1.19 require ( github.com/FloatTech/floatbox v0.0.0-20221121151328-cac4af114b44 + github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.7.0 + golang.org/x/term v0.2.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.2.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sirupsen/logrus v1.9.0 // indirect - golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect + golang.org/x/sys v0.2.0 // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 195899d..8d69b33 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,11 @@ github.com/FloatTech/floatbox v0.0.0-20221121151328-cac4af114b44/go.mod h1:DUd62 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= @@ -14,9 +19,12 @@ github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc= -golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helper.go b/helper/helper.go similarity index 95% rename from helper.go rename to helper/helper.go index ede8c79..46a474b 100644 --- a/helper.go +++ b/helper/helper.go @@ -1,4 +1,4 @@ -package gondportal +package helper import ( "reflect" diff --git a/main.go b/main.go new file mode 100644 index 0000000..6e136d0 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/fumiama/go-nd-portal/cmd" + +func main() { + cmd.Main() +} diff --git a/portal.go b/portal/portal.go similarity index 59% rename from portal.go rename to portal/portal.go index b6fae0d..8fc14a4 100644 --- a/portal.go +++ b/portal/portal.go @@ -1,4 +1,4 @@ -package gondportal +package portal import ( "crypto/hmac" @@ -8,9 +8,13 @@ import ( "errors" "fmt" "net" + "net/url" "time" "github.com/FloatTech/floatbox/web" + "github.com/sirupsen/logrus" + + "github.com/fumiama/go-nd-portal/helper" ) var ( @@ -27,8 +31,7 @@ type Portal struct { type rsp struct { Challenge string `json:"challenge"` - Ecode int `json:"ecode"` - Msg string `json:"error_msg"` + Error string `json:"error"` } func NewPortal(name, password string, ipv4 net.IP) (*Portal, error) { @@ -43,14 +46,13 @@ func NewPortal(name, password string, ipv4 net.IP) (*Portal, error) { } func (p *Portal) GetChallenge() (string, error) { - data, err := web.RequestDataWith( - web.NewDefaultClient(), - fmt.Sprintf(PortalGetChallenge, "gondportal", p.nam, p.ip, time.Now().UnixMilli()), - "GET", "", PortalHeaderUA, - ) + u := fmt.Sprintf(PortalGetChallenge, "gondportal", url.QueryEscape(p.nam), p.ip, time.Now().UnixMilli()) + logrus.Debugln("GET", u) + data, err := web.RequestDataWith(web.NewDefaultClient(), u, "GET", "", PortalHeaderUA) if err != nil { return "", err } + logrus.Debugln("get challenge resp:", helper.BytesToString(data)) if len(data) < 12 { return "", ErrUnexpectedChallengeResponse } @@ -59,30 +61,30 @@ func (p *Portal) GetChallenge() (string, error) { if err != nil { return "", err } - if r.Ecode != 0 { - return "", errors.New(r.Msg) + if r.Error != "ok" { + return "", errors.New(r.Error) } + logrus.Debugln("get challenge:", r.Challenge) return r.Challenge, nil } func (p *Portal) PasswordHMd5(challenge string) string { var buf [16]byte - h := hmac.New(md5.New, StringToBytes(challenge)) - _, _ = h.Write(StringToBytes(p.pwd)) + h := hmac.New(md5.New, helper.StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(p.pwd)) return hex.EncodeToString(h.Sum(buf[:0])) } func (p *Portal) Login(challenge string) error { info := EncodeUserInfo(p.String(), challenge) hmd5 := p.PasswordHMd5(challenge) - data, err := web.RequestDataWith( - web.NewDefaultClient(), - fmt.Sprintf(PortalLogin, "gondportal", p.nam, hmd5, p.ip, p.CheckSum(challenge, hmd5, info), info, time.Now().UnixMilli()), - "GET", "", PortalHeaderUA, - ) + u := fmt.Sprintf(PortalLogin, "gondportal", url.QueryEscape(p.nam), hmd5, p.ip, p.CheckSum(challenge, hmd5, info), url.QueryEscape(info), time.Now().UnixMilli()) + logrus.Debugln("GET", u) + data, err := web.RequestDataWith(web.NewDefaultClient(), u, "GET", "", PortalHeaderUA) if err != nil { return err } + logrus.Debugln("get login resp:", helper.BytesToString(data)) if len(data) < 12 { return ErrUnexpectedLoginResponse } @@ -91,8 +93,9 @@ func (p *Portal) Login(challenge string) error { if err != nil { return err } - if r.Ecode != 0 { - return errors.New(r.Msg) + logrus.Debugln("login rsp:", &r) + if r.Error != "ok" { + return errors.New(r.Error) } return nil } diff --git a/server.go b/portal/server.go similarity index 57% rename from server.go rename to portal/server.go index 0995a28..57b83db 100644 --- a/server.go +++ b/portal/server.go @@ -1,24 +1,27 @@ -package gondportal +package portal import ( "crypto/sha1" "encoding/binary" "encoding/hex" + + "github.com/fumiama/go-nd-portal/base64" + "github.com/fumiama/go-nd-portal/helper" ) const ( PortalServerIP = "10.253.0.237" - PortalGetChallenge = "http://" + PortalServerIP + "/cgi-bin/get_challenge?callback=%s&username=%s@dx-uestc&ip=%v&_=%d" - PortalLogin = "http://" + PortalServerIP + "/cgi-bin/srun_portal?callback=%s&action=login&username=%s@dx-uestc&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" + 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" ) const ( - PortalHeaderAccept = "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01" - PortalHeaderUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.56" + PortalHeaderUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.56" ) const ( - PortalUserInfo = `{"username":"%s@dx-uestc","password":"%s","ip":"%v","acid":"1","enc_ver":"srun_bx1"}` + PortalUserInfo = `{"username":"%s` + PortalDomain + `","password":"%s","ip":"%v","acid":"1","enc_ver":"srun_bx1"}` ) func EncodeUserInfo(info, challenge string) string { @@ -41,7 +44,7 @@ func EncodeUserInfo(info, challenge string) string { sc = 16 } k := make([]uint32, sc/4) - token := StringToBytes(challenge) + token := helper.StringToBytes(challenge) for i := 0; i < sc/4; i++ { k[i] = binary.LittleEndian.Uint32(token[i*4 : i*4+4]) } @@ -70,27 +73,27 @@ func EncodeUserInfo(info, challenge string) string { for i := 0; i < len(v); i++ { binary.LittleEndian.PutUint32(lv[i*4:i*4+4], v[i]) } - return Base64Encoding.EncodeToString(lv) + return base64.Base64Encoding.EncodeToString(lv) } func (p *Portal) CheckSum(challenge, hmd5, info string) string { var buf [20]byte h := sha1.New() - _, _ = h.Write(StringToBytes(challenge)) - _, _ = h.Write(StringToBytes(p.nam)) - _, _ = h.Write([]byte("@dx-uestc")) - _, _ = h.Write(StringToBytes(challenge)) - _, _ = h.Write(StringToBytes(hmd5)) - _, _ = h.Write(StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(p.nam)) + _, _ = h.Write([]byte(PortalDomain)) + _, _ = h.Write(helper.StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(hmd5)) + _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write([]byte("1")) // ac_id - _, _ = h.Write(StringToBytes(challenge)) - _, _ = h.Write(StringToBytes(p.ip.String())) - _, _ = h.Write(StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(p.ip.String())) + _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write([]byte("200")) // n - _, _ = h.Write(StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write([]byte("1")) // type - _, _ = h.Write(StringToBytes(challenge)) + _, _ = h.Write(helper.StringToBytes(challenge)) _, _ = h.Write([]byte("{SRBX1}")) - _, _ = h.Write(StringToBytes(info)) + _, _ = h.Write(helper.StringToBytes(info)) return hex.EncodeToString(h.Sum(buf[:0])) } diff --git a/server_test.go b/portal/server_test.go similarity index 63% rename from server_test.go rename to portal/server_test.go index 3821498..2f195a9 100644 --- a/server_test.go +++ b/portal/server_test.go @@ -1,8 +1,6 @@ -package gondportal +package portal import ( - "crypto/hmac" - "crypto/md5" "crypto/sha1" "encoding/binary" "encoding/hex" @@ -10,6 +8,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/fumiama/go-nd-portal/helper" ) func TestDecodeInfo(t *testing.T) { @@ -32,7 +32,7 @@ func TestDecodeKey(t *testing.T) { challenge := "c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5" sc := len(challenge) k := make([]uint32, sc/4) - token := StringToBytes(challenge) + token := helper.StringToBytes(challenge) for i := 0; i < sc/4; i++ { k[i] = binary.LittleEndian.Uint32(token[i*4 : i*4+4]) } @@ -40,19 +40,21 @@ func TestDecodeKey(t *testing.T) { } func TestEncodeUserInfo(t *testing.T) { - u, err := NewPortal("2000010101001", "12345678", net.IPv4(1, 2, 3, 4).To4()) + u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4()) if err != nil { t.Fatal(err) } t.Log(u.String()) - r := EncodeUserInfo(u.String(), "c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5") - assert.Equal(t, "LMDd8Hmfuq32k+whLiNtcuRwEVxEswfsm4rKEoAoGnFeDlMijgeXC6mtK3nTlrNmjwoEmRyLsWePyrFzDd/EI7EfgKh2gF3c9dGmUrlFO9cy6PFqBDShWsGaAuatVgZLhKBOACTShgxGraRJBoA9WS==", r) + r := EncodeUserInfo(u.String(), "d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e") + assert.Equal(t, "CfVnZ9mvKmdgvm/ivovlPibZL6RLAWcx+nBTaYmWH3kmThco+eO4LVsCPFceSmM9PyI0UcMgLE7bmpfY9pr0EWnWdTncXrbW29Aydp+lw6QjxKMgNzgYd7uopiPbIyKpxvJZDHsGw5xh8rMEeq3JXrD2vex27xeI", r) } func TestHMd5(t *testing.T) { - h := hmac.New(md5.New, []byte("c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5")) - h.Write([]byte("1234567890")) - assert.Equal(t, "69ff50d80e734878259dbee3322591a7", hex.EncodeToString(h.Sum(nil))) + u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4()) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "91062ae815fb02d9d15aec834aafffd4", u.PasswordHMd5("d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e")) } func TestSha1(t *testing.T) { @@ -62,18 +64,19 @@ func TestSha1(t *testing.T) { } func TestCheckSum(t *testing.T) { - u, err := NewPortal("2000010101001", "1234567890", net.IPv4(1, 2, 3, 4).To4()) + u, err := NewPortal("2001010101001", "1234567890", net.IPv4(113, 54, 148, 243).To4()) if err != nil { t.Fatal(err) } t.Log(u.String()) + challenge := "d26466d4036507dadb17e87e23358126e0210cb289d19151f59bcfcefdcf345e" s := u.CheckSum( - "c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5", - "69ff50d80e734878259dbee3322591a7", + challenge, + u.PasswordHMd5(challenge), EncodeUserInfo( u.String(), - "c312a4194d4310695b71d92ac3c740198a14a7a280022f89408edec4e932d1e5", + challenge, ), ) - assert.Equal(t, "3785bd1e1fa71a2b26470b5faa64aad9130ae418", s) + assert.Equal(t, "64e8913b6019df98e3b807343b8785856909d745", s) }