forked from Mrs4s/go-cqhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
180 lines (169 loc) · 4.61 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"bytes"
"os"
"strings"
"time"
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
"github.com/Mrs4s/MiraiGo/client"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tuotoo/qrcode"
"github.com/Mrs4s/go-cqhttp/global"
)
var console = bufio.NewReader(os.Stdin)
var readLine = func() (str string) {
str, _ = console.ReadString('\n')
str = strings.TrimSpace(str)
return
}
var readLineTimeout = func(t time.Duration, de string) (str string) {
r := make(chan string)
go func() {
select {
case r <- readLine():
case <-time.After(t):
}
}()
str = de
select {
case str = <-r:
case <-time.After(t):
}
return
}
var cli *client.QQClient
// ErrSMSRequestError SMS请求出错
var ErrSMSRequestError = errors.New("sms request error")
func commonLogin() error {
res, err := cli.Login()
if err != nil {
return err
}
return loginResponseProcessor(res)
}
func qrcodeLogin() error {
rsp, err := cli.FetchQRCode()
if err != nil {
return err
}
fi, err := qrcode.Decode(bytes.NewReader(rsp.ImageData))
if err != nil {
return err
}
_ = os.WriteFile("qrcode.png", rsp.ImageData, 0o644)
defer func() { _ = os.Remove("qrcode.png") }()
if cli.Uin != 0 {
log.Infof("请使用账号 %v 登录手机QQ扫描二维码 (qrcode.png) : ", cli.Uin)
} else {
log.Infof("请使用手机QQ扫描二维码 (qrcode.png) : ")
}
time.Sleep(time.Second)
qrcodeTerminal.New().Get(fi.Content).Print()
s, err := cli.QueryQRCodeStatus(rsp.Sig)
if err != nil {
return err
}
prevState := s.State
for {
time.Sleep(time.Second)
s, _ = cli.QueryQRCodeStatus(rsp.Sig)
if s == nil {
continue
}
if prevState == s.State {
continue
}
prevState = s.State
switch s.State {
case client.QRCodeCanceled:
log.Fatalf("扫码被用户取消.")
case client.QRCodeTimeout:
log.Fatalf("二维码过期")
case client.QRCodeWaitingForConfirm:
log.Infof("扫码成功, 请在手机端确认登录.")
case client.QRCodeConfirmed:
res, err := cli.QRCodeLogin(s.LoginInfo)
if err != nil {
return err
}
return loginResponseProcessor(res)
case client.QRCodeImageFetch, client.QRCodeWaitingForScan:
// ignore
}
}
}
func loginResponseProcessor(res *client.LoginResponse) error {
var err error
for {
if err != nil {
return err
}
if res.Success {
return nil
}
var text string
switch res.Error {
case client.SliderNeededError:
log.Warnf("登录需要滑条验证码, 请使用手机QQ扫描二维码以继续登录.")
cli.Disconnect()
cli.Release()
cli = client.NewClientEmpty()
return qrcodeLogin()
case client.NeedCaptcha:
log.Warnf("登录需要验证码.")
_ = os.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
log.Warnf("请输入验证码 (captcha.jpg): (Enter 提交)")
text = readLine()
global.DelFile("captcha.jpg")
res, err = cli.SubmitCaptcha(text, res.CaptchaSign)
continue
case client.SMSNeededError:
log.Warnf("账号已开启设备锁, 按 Enter 向手机 %v 发送短信验证码.", res.SMSPhone)
readLine()
if !cli.RequestSMS() {
log.Warnf("发送验证码失败,可能是请求过于频繁.")
return errors.WithStack(ErrSMSRequestError)
}
log.Warn("请输入短信验证码: (Enter 提交)")
text = readLine()
res, err = cli.SubmitSMS(text)
continue
case client.SMSOrVerifyNeededError:
log.Warnf("账号已开启设备锁,请选择验证方式:")
log.Warnf("1. 向手机 %v 发送短信验证码", res.SMSPhone)
log.Warnf("2. 使用手机QQ扫码验证.")
log.Warn("请输入(1 - 2) (将在10秒后自动选择2):")
text = readLineTimeout(time.Second*10, "2")
if strings.Contains(text, "1") {
if !cli.RequestSMS() {
log.Warnf("发送验证码失败,可能是请求过于频繁.")
return errors.WithStack(ErrSMSRequestError)
}
log.Warn("请输入短信验证码: (Enter 提交)")
text = readLine()
res, err = cli.SubmitSMS(text)
continue
}
fallthrough
case client.UnsafeDeviceError:
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证后重启Bot.", res.VerifyUrl)
log.Infof("按 Enter 或等待 5s 后继续....")
readLineTimeout(time.Second*5, "")
os.Exit(0)
case client.OtherLoginError, client.UnknownLoginError, client.TooManySMSRequestError:
msg := res.ErrorMessage
if strings.Contains(msg, "版本") {
msg = "密码错误或账号被冻结"
}
if strings.Contains(msg, "冻结") {
log.Fatalf("账号被冻结")
}
log.Warnf("登录失败: %v", msg)
log.Infof("按 Enter 或等待 5s 后继续....")
readLineTimeout(time.Second*5, "")
os.Exit(0)
}
}
}