mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 20:13:50 +08:00
refa: use ticker for fetchCaptcha()
This commit is contained in:
parent
93074ef4af
commit
bf5562b6dd
@ -221,19 +221,31 @@ func loginResponseProcessor(res *client.LoginResponse) error {
|
||||
func getTicket(u string) (str string) {
|
||||
id := utils.RandomString(8)
|
||||
log.Warnf("请前往该地址验证 -> %v <- 或输入手动抓取的 ticket:(Enter 提交)", strings.ReplaceAll(u, "https://ssl.captcha.qq.com/template/wireless_mqq_captcha.html?", fmt.Sprintf("https://captcha.go-cqhttp.org/captcha?id=%v&", id)))
|
||||
r := make(chan string)
|
||||
manual := make(chan string)
|
||||
go func() {
|
||||
str = readLine()
|
||||
manual <- str
|
||||
}()
|
||||
ticker := time.NewTicker(time.Second)
|
||||
for count := 120; count > 0; count-- {
|
||||
select {
|
||||
case r <- readLine():
|
||||
case r <- sliderCaptchaProcessor(id):
|
||||
}
|
||||
str = <-r
|
||||
case <-ticker.C:
|
||||
str = fetchCaptcha(id)
|
||||
if str != "" {
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
case <-manual:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
ticker.Stop()
|
||||
log.Warnf("验证超时")
|
||||
return ""
|
||||
}
|
||||
|
||||
func sliderCaptchaProcessor(id string) string {
|
||||
start := time.Now()
|
||||
for time.Since(start).Minutes() < 2 {
|
||||
time.Sleep(time.Second)
|
||||
func fetchCaptcha(id string) string {
|
||||
data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id)
|
||||
if err != nil {
|
||||
log.Warnf("获取 Ticket 时出现错误: %v", err)
|
||||
@ -243,7 +255,5 @@ func sliderCaptchaProcessor(id string) string {
|
||||
if g.Get("ticket").Exists() {
|
||||
return g.Get("ticket").String()
|
||||
}
|
||||
}
|
||||
log.Warnf("验证超时")
|
||||
return ""
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user