feat: 移动增加扫码登录并支持换号短信登录
This commit is contained in:
+305
-16
@@ -1,6 +1,7 @@
|
||||
package cmnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -49,6 +50,11 @@ type Client struct {
|
||||
crypto pageCrypto
|
||||
channelID string
|
||||
provinces map[string]string
|
||||
qrToken string
|
||||
qrChannel string
|
||||
hosts []string
|
||||
portal string
|
||||
scanBase string
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
@@ -123,6 +129,7 @@ func (c *Client) SendSMS(ctx context.Context, phone, imageCode string) (string,
|
||||
if err := c.ensureKeys(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
c.dropAccountSession()
|
||||
encPhone, err := c.crypto.encryptLogin(phone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -183,6 +190,7 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
if err := c.ensureKeys(ctx); err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
c.dropAccountSession()
|
||||
encPhone, err := c.crypto.encryptLogin(phone)
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
@@ -217,33 +225,36 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
return LoginResult{}, err
|
||||
}
|
||||
var loginRes struct {
|
||||
Code string `json:"code"`
|
||||
Desc string `json:"desc"`
|
||||
Artifact string `json:"artifact"`
|
||||
Type string `json:"type"`
|
||||
Code string `json:"code"`
|
||||
Desc string `json:"desc"`
|
||||
Artifact string `json:"artifact"`
|
||||
Type string `json:"type"`
|
||||
ResultCode string `json:"resultCode"`
|
||||
ResultDesc string `json:"resultDesc"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginRes); err != nil {
|
||||
return LoginResult{}, errors.New("登录结果无法解析")
|
||||
}
|
||||
if loginRes.Code == "" {
|
||||
loginRes.Code = loginRes.ResultCode
|
||||
}
|
||||
if loginRes.Desc == "" {
|
||||
loginRes.Desc = loginRes.ResultDesc
|
||||
}
|
||||
if loginRes.Code != "0000" || loginRes.Artifact == "" {
|
||||
if loginRes.Desc != "" {
|
||||
return LoginResult{}, errors.New(loginRes.Desc)
|
||||
}
|
||||
if loginRes.Code != "" {
|
||||
return LoginResult{}, fmt.Errorf("登录失败 %s", loginRes.Code)
|
||||
}
|
||||
return LoginResult{}, errors.New("登录失败")
|
||||
}
|
||||
art, err := url.Parse(shopOrigin + "/i/v1/auth/getArtifact")
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
typ := loginRes.Type
|
||||
if typ == "" {
|
||||
typ = "00"
|
||||
}
|
||||
q := art.Query()
|
||||
q.Set("backUrl", backURL)
|
||||
q.Set("artifact", loginRes.Artifact)
|
||||
if loginRes.Type == "" {
|
||||
loginRes.Type = "00"
|
||||
}
|
||||
q.Set("type", loginRes.Type)
|
||||
art.RawQuery = q.Encode()
|
||||
if _, _, err := c.get(ctx, art.String(), c.loginPage()); err != nil {
|
||||
if err := c.exchangeArtifact(ctx, loginRes.Artifact, typ); err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
info, err := c.loginfo(ctx)
|
||||
@@ -257,6 +268,275 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
return LoginResult{Phone: phone, ProvCode: prov, ProvName: c.provinceNameLocked(prov)}, nil
|
||||
}
|
||||
|
||||
type QRStatus struct {
|
||||
Waiting bool
|
||||
Expired bool
|
||||
Result LoginResult
|
||||
}
|
||||
|
||||
func (c *Client) QRCode(ctx context.Context) ([]byte, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if err := c.ensureKeys(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, _, err := c.get(ctx, c.loginPage(), c.loginPage()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.clearLoginCookies()
|
||||
body, ctype, err := c.get(ctx, loginOrigin+"/genqr.htm?timestamp="+milli(), c.loginPage())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !strings.Contains(ctype, "image") && !(len(body) > 4 && body[0] == 0x89) {
|
||||
return nil, errors.New("没有拿到登录二维码")
|
||||
}
|
||||
if c.qrToken == "" {
|
||||
c.qrToken = c.cookieNamed("lgToken")
|
||||
}
|
||||
if c.qrToken == "" {
|
||||
return nil, errors.New("没有拿到扫码凭证")
|
||||
}
|
||||
_, _ = c.postForm(ctx, loginOrigin+"/checkBackUrl", url.Values{
|
||||
"backUrl": {backURL},
|
||||
"channelID": {c.channelID},
|
||||
})
|
||||
_, _ = c.postForm(ctx, loginOrigin+"/checkUidAvailable.action", url.Values{})
|
||||
return c.usableQR(ctx, body), nil
|
||||
}
|
||||
|
||||
func (c *Client) PollQR(ctx context.Context) (QRStatus, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.qrToken == "" {
|
||||
c.qrToken = c.cookieNamed("lgToken")
|
||||
}
|
||||
if c.qrToken == "" {
|
||||
return QRStatus{}, errors.New("请先获取二维码")
|
||||
}
|
||||
if err := c.ensureKeys(ctx); err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
status, artifact, err := c.pollChannels(ctx)
|
||||
if err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
if status.Expired || status.Waiting || artifact == "" {
|
||||
return status, nil
|
||||
}
|
||||
if err := c.exchangeArtifact(ctx, artifact, ""); err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
info, err := c.loginfo(ctx)
|
||||
if err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
phone, err := restorePhone(asString(info["loginValue"]), asString(info["safeCode"]))
|
||||
if err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
prov := asString(info["provice"])
|
||||
if prov == "" {
|
||||
prov = asString(info["province"])
|
||||
}
|
||||
status.Result = LoginResult{Phone: phone, ProvCode: prov, ProvName: c.provinceNameLocked(prov)}
|
||||
c.qrToken = ""
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (c *Client) exchangeArtifact(ctx context.Context, artifact, typ string) error {
|
||||
art, err := url.Parse(shopOrigin + "/i/v1/auth/getArtifact")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
q := art.Query()
|
||||
q.Set("backUrl", backURL)
|
||||
q.Set("artifact", artifact)
|
||||
if typ != "" {
|
||||
q.Set("type", typ)
|
||||
}
|
||||
art.RawQuery = q.Encode()
|
||||
_, _, err = c.get(ctx, art.String(), c.loginPage())
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) pollChannels(ctx context.Context) (QRStatus, string, error) {
|
||||
channels := []string{}
|
||||
if c.qrChannel != "" {
|
||||
channels = append(channels, c.qrChannel)
|
||||
}
|
||||
if c.channelID != "" && c.channelID != c.qrChannel {
|
||||
channels = append(channels, c.channelID)
|
||||
}
|
||||
if len(channels) == 0 {
|
||||
channels = []string{""}
|
||||
}
|
||||
sawWait := false
|
||||
var lastErr error
|
||||
for _, channel := range channels {
|
||||
body, err := c.postForm(ctx, loginOrigin+"/chkqr.htm", url.Values{
|
||||
"lgToken": {c.qrToken},
|
||||
"targetChannelID": {channel},
|
||||
"backUrl": {url.QueryEscape(backURL)},
|
||||
})
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
status, artifact, err := parseQRPoll(bytes.TrimSpace(body))
|
||||
if err != nil {
|
||||
return QRStatus{}, "", err
|
||||
}
|
||||
if artifact != "" {
|
||||
return status, artifact, nil
|
||||
}
|
||||
if status.Expired {
|
||||
continue
|
||||
}
|
||||
sawWait = true
|
||||
}
|
||||
if sawWait {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
}
|
||||
if lastErr != nil {
|
||||
return QRStatus{}, "", lastErr
|
||||
}
|
||||
return QRStatus{Expired: true}, "", nil
|
||||
}
|
||||
|
||||
func parseQRPoll(body []byte) (QRStatus, string, error) {
|
||||
if len(body) == 0 {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
}
|
||||
var res struct {
|
||||
ResultCode string `json:"resultCode"`
|
||||
ResultDesc string `json:"resultDesc"`
|
||||
Artifact string `json:"artifact"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
}
|
||||
switch res.ResultCode {
|
||||
case "":
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
case "0000":
|
||||
if res.Artifact == "" {
|
||||
return QRStatus{}, "", errors.New("登录结果缺少凭证")
|
||||
}
|
||||
return QRStatus{}, res.Artifact, nil
|
||||
case "8020":
|
||||
return QRStatus{Expired: true}, "", nil
|
||||
default:
|
||||
if res.ResultDesc != "" {
|
||||
return QRStatus{}, "", errors.New(res.ResultDesc)
|
||||
}
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
}
|
||||
}
|
||||
|
||||
func restorePhone(masked, code string) (string, error) {
|
||||
masked = strings.TrimSpace(masked)
|
||||
code = strings.TrimSpace(code)
|
||||
if strings.Contains(masked, "******") && len(code) == 6 {
|
||||
phone := strings.Replace(masked, "******", code, 1)
|
||||
if phoneOK(phone) {
|
||||
return phone, nil
|
||||
}
|
||||
}
|
||||
if phoneOK(masked) {
|
||||
return masked, nil
|
||||
}
|
||||
return "", errors.New("没有拿到手机号")
|
||||
}
|
||||
|
||||
func phoneOK(s string) bool {
|
||||
if len(s) != 11 || s[0] != '1' {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Client) dropAccountSession() {
|
||||
hosts := []string{loginOrigin + "/", shopOrigin + "/", "https://10086.cn/", "https://www.10086.cn/"}
|
||||
for _, raw := range hosts {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var drop []*http.Cookie
|
||||
for _, ck := range c.jar.Cookies(u) {
|
||||
if !accountSessionCookie(ck.Name) {
|
||||
continue
|
||||
}
|
||||
path := ck.Path
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
drop = append(drop, &http.Cookie{Name: ck.Name, Path: path, MaxAge: -1})
|
||||
drop = append(drop, &http.Cookie{Name: ck.Name, Domain: "10086.cn", Path: path, MaxAge: -1})
|
||||
}
|
||||
if len(drop) > 0 {
|
||||
c.jar.SetCookies(u, drop)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func accountSessionCookie(name string) bool {
|
||||
switch name {
|
||||
case "cmccssotoken", "is_login", "ssologinprovince":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) clearLoginCookies() {
|
||||
c.qrToken = ""
|
||||
u, err := url.Parse(loginOrigin + "/")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var drop []*http.Cookie
|
||||
for _, ck := range c.jar.Cookies(u) {
|
||||
if !loginHandshakeCookie(ck.Name) {
|
||||
continue
|
||||
}
|
||||
drop = append(drop, &http.Cookie{Name: ck.Name, Path: ck.Path, MaxAge: -1})
|
||||
}
|
||||
if len(drop) > 0 {
|
||||
c.jar.SetCookies(u, drop)
|
||||
}
|
||||
}
|
||||
|
||||
func loginHandshakeCookie(name string) bool {
|
||||
switch name {
|
||||
case "lgToken", "cvToken", "CaptchaCode", "jsessionid-cmcc", "rdmdmd5", "sendflag":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) cookieNamed(name string) string {
|
||||
for _, raw := range []string{loginOrigin + "/", "https://10086.cn/", shopOrigin + "/"} {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, ck := range c.jar.Cookies(u) {
|
||||
if ck.Name == name && ck.Value != "" {
|
||||
return ck.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *Client) Query(ctx context.Context, phone, prov string) (json.RawMessage, json.RawMessage, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
@@ -394,6 +674,7 @@ func (c *Client) fetchShopScript(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.noteLinks(string(page))
|
||||
var cryptoJS, bundle string
|
||||
err = c.readScripts(ctx, string(page), shopOrigin, shopOrigin+"/i/?f=packremainqry", func(src string) bool {
|
||||
if cryptoJS == "" && isCryptoJSLib(src) {
|
||||
@@ -421,6 +702,7 @@ func (c *Client) fetchShopScript(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.noteLinks(bundle)
|
||||
c.provinces = provinces
|
||||
c.channelID = channel
|
||||
c.crypto.cryptoJS = cryptoJS
|
||||
@@ -541,6 +823,13 @@ func (c *Client) do(req *http.Request) ([]byte, string, error) {
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if req.URL != nil && req.URL.Hostname() == "login.10086.cn" {
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "lgToken" && ck.Value != "" {
|
||||
c.qrToken = ck.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
if resp.StatusCode >= 400 && !strings.Contains(resp.Header.Get("Content-Type"), "json") {
|
||||
return nil, "", fmt.Errorf("网站返回 %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user