fix: 退出后不保留登录状态,调试输出改为总开关
This commit is contained in:
+98
-17
@@ -11,6 +11,7 @@ import (
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -120,6 +121,7 @@ func (c *Client) Captcha(ctx context.Context) ([]byte, string, error) {
|
||||
if ctype == "" {
|
||||
ctype = "image/png"
|
||||
}
|
||||
debugf("图形验证码已获取 %d 字节", len(body))
|
||||
return body, ctype, nil
|
||||
}
|
||||
|
||||
@@ -130,6 +132,8 @@ func (c *Client) SendSMS(ctx context.Context, phone, imageCode string) (string,
|
||||
return "", err
|
||||
}
|
||||
c.dropAccountSession()
|
||||
debugf("发送短信 phone=%s 图形验证码=%s", phone, imageCode)
|
||||
c.debugCookies("发送短信前")
|
||||
encPhone, err := c.crypto.encryptLogin(phone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -146,6 +150,7 @@ func (c *Client) SendSMS(ctx context.Context, phone, imageCode string) (string,
|
||||
ResultCode string `json:"resultCode"`
|
||||
}
|
||||
_ = json.Unmarshal(vbody, &captchaRes)
|
||||
debugf("图形验证码校验 phone=%s 图形验证码=%s 原文=%s", phone, imageCode, string(vbody))
|
||||
if captchaRes.ResultCode != "" && captchaRes.ResultCode != "0" {
|
||||
return "", errors.New("图形验证码不正确")
|
||||
}
|
||||
@@ -181,7 +186,10 @@ func (c *Client) SendSMS(ctx context.Context, phone, imageCode string) (string,
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return smsMessage(strings.TrimSpace(string(body)))
|
||||
msg, err := smsMessage(strings.TrimSpace(string(body)))
|
||||
debugf("短信发送结果 phone=%s 原文=%s", phone, string(body))
|
||||
c.debugCookies("短信发送后")
|
||||
return msg, err
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID string) (LoginResult, error) {
|
||||
@@ -191,6 +199,8 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
return LoginResult{}, err
|
||||
}
|
||||
c.dropAccountSession()
|
||||
debugf("短信登录 phone=%s 图形验证码=%s 短信验证码=%s AES密钥=%s", phone, imageCode, smsCode, string(c.crypto.aesKey))
|
||||
c.debugCookies("短信登录前")
|
||||
encPhone, err := c.crypto.encryptLogin(phone)
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
@@ -233,6 +243,7 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
ResultDesc string `json:"resultDesc"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginRes); err != nil {
|
||||
debugf("短信登录结果无法解析 phone=%s 原文=%s", phone, string(body))
|
||||
return LoginResult{}, errors.New("登录结果无法解析")
|
||||
}
|
||||
if loginRes.Code == "" {
|
||||
@@ -242,6 +253,7 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
loginRes.Desc = loginRes.ResultDesc
|
||||
}
|
||||
if loginRes.Code != "0000" || loginRes.Artifact == "" {
|
||||
debugf("短信登录被拒绝 phone=%s code=%s desc=%s 原文=%s", phone, loginRes.Code, loginRes.Desc, string(body))
|
||||
if loginRes.Desc != "" {
|
||||
return LoginResult{}, errors.New(loginRes.Desc)
|
||||
}
|
||||
@@ -254,13 +266,20 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
if typ == "" {
|
||||
typ = "00"
|
||||
}
|
||||
if err := c.exchangeArtifact(ctx, loginRes.Artifact, typ); err != nil {
|
||||
debugf("短信登录已通过 phone=%s type=%s artifact=%s", phone, typ, loginRes.Artifact)
|
||||
if err := c.exchangeArtifact(ctx, loginRes.Artifact, "", typ); err != nil {
|
||||
debugf("交换登录凭证失败 phone=%s %v", phone, err)
|
||||
c.debugCookies("交换凭证失败")
|
||||
return LoginResult{}, err
|
||||
}
|
||||
c.debugCookies("交换凭证后")
|
||||
info, err := c.loginfo(ctx)
|
||||
if err != nil {
|
||||
debugf("读取登录资料失败 phone=%s %s", phone, err.Error())
|
||||
c.debugCookies("读取资料失败")
|
||||
return LoginResult{}, err
|
||||
}
|
||||
debugf("登录资料 phone=%s loginValue=%s safeCode=%s provice=%s province=%s", phone, asString(info["loginValue"]), asString(info["safeCode"]), asString(info["provice"]), asString(info["province"]))
|
||||
prov := asString(info["provice"])
|
||||
if prov == "" {
|
||||
prov = asString(info["province"])
|
||||
@@ -269,9 +288,10 @@ func (c *Client) Login(ctx context.Context, phone, imageCode, smsCode, deviceID
|
||||
}
|
||||
|
||||
type QRStatus struct {
|
||||
Waiting bool
|
||||
Expired bool
|
||||
Result LoginResult
|
||||
Waiting bool
|
||||
Expired bool
|
||||
AssertURL string
|
||||
Result LoginResult
|
||||
}
|
||||
|
||||
func (c *Client) QRCode(ctx context.Context) ([]byte, error) {
|
||||
@@ -297,6 +317,8 @@ func (c *Client) QRCode(ctx context.Context) ([]byte, error) {
|
||||
if c.qrToken == "" {
|
||||
return nil, errors.New("没有拿到扫码凭证")
|
||||
}
|
||||
debugf("扫码凭证 lgToken=%s", c.qrToken)
|
||||
c.debugCookies("二维码")
|
||||
_, _ = c.postForm(ctx, loginOrigin+"/checkBackUrl", url.Values{
|
||||
"backUrl": {backURL},
|
||||
"channelID": {c.channelID},
|
||||
@@ -324,7 +346,7 @@ func (c *Client) PollQR(ctx context.Context) (QRStatus, error) {
|
||||
if status.Expired || status.Waiting || artifact == "" {
|
||||
return status, nil
|
||||
}
|
||||
if err := c.exchangeArtifact(ctx, artifact, ""); err != nil {
|
||||
if err := c.exchangeArtifact(ctx, artifact, status.AssertURL, ""); err != nil {
|
||||
return QRStatus{}, err
|
||||
}
|
||||
info, err := c.loginfo(ctx)
|
||||
@@ -344,8 +366,11 @@ func (c *Client) PollQR(ctx context.Context) (QRStatus, error) {
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (c *Client) exchangeArtifact(ctx context.Context, artifact, typ string) error {
|
||||
art, err := url.Parse(shopOrigin + "/i/v1/auth/getArtifact")
|
||||
func (c *Client) exchangeArtifact(ctx context.Context, artifact, assertURL, typ string) error {
|
||||
if assertURL == "" || !sameSite(shopOrigin, assertURL) {
|
||||
assertURL = shopOrigin + "/i/v1/auth/getArtifact"
|
||||
}
|
||||
art, err := url.Parse(assertURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -362,39 +387,53 @@ func (c *Client) exchangeArtifact(ctx context.Context, artifact, typ string) 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 {
|
||||
if c.channelID != "" {
|
||||
channels = append(channels, c.channelID)
|
||||
}
|
||||
if c.qrChannel != "" && c.qrChannel != c.channelID {
|
||||
channels = append(channels, c.qrChannel)
|
||||
}
|
||||
if len(channels) == 0 {
|
||||
channels = []string{""}
|
||||
}
|
||||
sawWait := false
|
||||
var lastErr error
|
||||
var fallback QRStatus
|
||||
fallbackArtifact := ""
|
||||
for _, channel := range channels {
|
||||
body, err := c.postForm(ctx, loginOrigin+"/chkqr.htm", url.Values{
|
||||
"lgToken": {c.qrToken},
|
||||
"targetChannelID": {channel},
|
||||
"backUrl": {url.QueryEscape(backURL)},
|
||||
})
|
||||
debugf("扫码轮询 channel=%s lgToken=%s err=%v 原文=%s", channel, c.qrToken, err, string(body))
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
status, artifact, err := parseQRPoll(bytes.TrimSpace(body))
|
||||
if err != nil {
|
||||
return QRStatus{}, "", err
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
if artifact != "" {
|
||||
return status, artifact, nil
|
||||
if shopArtifact(status.AssertURL) {
|
||||
return status, artifact, nil
|
||||
}
|
||||
if fallbackArtifact == "" {
|
||||
fallback = status
|
||||
fallbackArtifact = artifact
|
||||
}
|
||||
continue
|
||||
}
|
||||
if status.Expired {
|
||||
continue
|
||||
}
|
||||
sawWait = true
|
||||
}
|
||||
if fallbackArtifact != "" && !sawWait {
|
||||
return fallback, fallbackArtifact, nil
|
||||
}
|
||||
if sawWait {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
}
|
||||
@@ -404,6 +443,17 @@ func (c *Client) pollChannels(ctx context.Context) (QRStatus, string, error) {
|
||||
return QRStatus{Expired: true}, "", nil
|
||||
}
|
||||
|
||||
func shopArtifact(rawURL string) bool {
|
||||
if rawURL == "" {
|
||||
return true
|
||||
}
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return u.Hostname() == "shop.10086.cn"
|
||||
}
|
||||
|
||||
func parseQRPoll(body []byte) (QRStatus, string, error) {
|
||||
if len(body) == 0 {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
@@ -412,6 +462,7 @@ func parseQRPoll(body []byte) (QRStatus, string, error) {
|
||||
ResultCode string `json:"resultCode"`
|
||||
ResultDesc string `json:"resultDesc"`
|
||||
Artifact string `json:"artifact"`
|
||||
AssertURL string `json:"assertUrl"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return QRStatus{Waiting: true}, "", nil
|
||||
@@ -423,7 +474,7 @@ func parseQRPoll(body []byte) (QRStatus, string, error) {
|
||||
if res.Artifact == "" {
|
||||
return QRStatus{}, "", errors.New("登录结果缺少凭证")
|
||||
}
|
||||
return QRStatus{}, res.Artifact, nil
|
||||
return QRStatus{AssertURL: res.AssertURL}, res.Artifact, nil
|
||||
case "8020":
|
||||
return QRStatus{Expired: true}, "", nil
|
||||
default:
|
||||
@@ -537,6 +588,13 @@ func (c *Client) cookieNamed(name string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *Client) SessionAlive(ctx context.Context) bool {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
_, err := c.loginfo(ctx)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (c *Client) Query(ctx context.Context, phone, prov string) (json.RawMessage, json.RawMessage, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
@@ -549,12 +607,15 @@ func (c *Client) Query(ctx context.Context, phone, prov string) (json.RawMessage
|
||||
func (c *Client) queryOnce(ctx context.Context, phone, prov string) (json.RawMessage, json.RawMessage, error) {
|
||||
token, err := c.crypto.phoneToken(phone)
|
||||
if err != nil {
|
||||
debugf("生成号码令牌失败 phone=%s %v", phone, err)
|
||||
return nil, nil, err
|
||||
}
|
||||
pay, err := c.crypto.payPhoneNo(phone)
|
||||
if err != nil {
|
||||
debugf("生成缴费号码失败 phone=%s %v", phone, err)
|
||||
return nil, nil, err
|
||||
}
|
||||
debugf("查询账户 phone=%s prov=%s token=%s pay=%s", phone, prov, token, pay)
|
||||
headers := map[string]string{"provcode": prov, "payphoneno": pay}
|
||||
real, err := c.shopGet(ctx, "/i/v1/fee/real/"+token+"?_="+milli(), headers)
|
||||
if err != nil {
|
||||
@@ -607,9 +668,11 @@ func (c *Client) decodeShop(buf []byte) (json.RawMessage, error) {
|
||||
return nil, errors.New("网站没有返回账户数据")
|
||||
}
|
||||
if wrap.RetCode == "500003" || strings.Contains(wrap.RetMsg, "not login") || strings.Contains(wrap.RetMsg, "未登录") {
|
||||
debugf("个人中心判定未登录 retCode=%s retMsg=%s 原文=%s", wrap.RetCode, wrap.RetMsg, string(buf))
|
||||
return nil, ErrNeedLogin
|
||||
}
|
||||
if wrap.RetCode != "000000" {
|
||||
debugf("个人中心查询失败 retCode=%s retMsg=%s", wrap.RetCode, wrap.RetMsg)
|
||||
if wrap.RetMsg != "" {
|
||||
return nil, errors.New(wrap.RetMsg)
|
||||
}
|
||||
@@ -831,8 +894,17 @@ func (c *Client) do(req *http.Request) ([]byte, string, error) {
|
||||
}
|
||||
}
|
||||
if resp.StatusCode >= 400 && !strings.Contains(resp.Header.Get("Content-Type"), "json") {
|
||||
if debugRequest(req) {
|
||||
debugf("%s %s -> %d 失败", req.Method, req.URL.String(), resp.StatusCode)
|
||||
}
|
||||
return nil, "", fmt.Errorf("网站返回 %d", resp.StatusCode)
|
||||
}
|
||||
if debugRequest(req) {
|
||||
debugf("%s %s -> %d Set-Cookie [%s]", req.Method, req.URL.String(), resp.StatusCode, debugSetCookies(resp))
|
||||
if !strings.Contains(resp.Header.Get("Content-Type"), "image") {
|
||||
debugBody(req.URL.Path, buf)
|
||||
}
|
||||
}
|
||||
return buf, resp.Header.Get("Content-Type"), nil
|
||||
}
|
||||
|
||||
@@ -909,6 +981,7 @@ func (c *Client) Reset() {
|
||||
}
|
||||
c.jar = jar
|
||||
c.http.Jar = jar
|
||||
c.qrToken = ""
|
||||
}
|
||||
|
||||
func dedupeCookies(list []Cookie) []Cookie {
|
||||
@@ -958,6 +1031,14 @@ func smsMessage(body string) (string, error) {
|
||||
}
|
||||
|
||||
func asString(v any) string {
|
||||
s, _ := v.(string)
|
||||
return s
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return t
|
||||
case float64:
|
||||
return strconv.FormatFloat(t, 'f', -1, 64)
|
||||
case json.Number:
|
||||
return t.String()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user