Initial commit
This commit is contained in:
72
captcha/recognize.go
Normal file
72
captcha/recognize.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"jwts/client"
|
||||
"jwts/config"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
// 保存验证码图片
|
||||
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
||||
fmt.Println("[2] 获取验证码...")
|
||||
|
||||
req, err := http.NewRequest("GET", config.BaseURL+"/captchaImage", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header = client.GetHeaders()
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := os.WriteFile("captcha.png", body, 0644); err != nil {
|
||||
fmt.Printf(" 警告: 无法保存验证码图片: %v\n", err)
|
||||
} else {
|
||||
fmt.Println(" 验证码已保存到: captcha.png")
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// 识别验证码 - 使用固定验证码用于测试
|
||||
func Recognize(imagePath string) (string, error) {
|
||||
fmt.Println("[3] 使用固定验证码...")
|
||||
// 固定验证码用于测试
|
||||
return "....", nil
|
||||
}
|
||||
|
||||
// 获取并识别验证码
|
||||
func GetAndRecognize(client *http.Client) (string, error) {
|
||||
_, err := SaveCaptchaImage(client)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
captchaCode, err := Recognize("captcha.png")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Printf(" 识别结果: %s\n", captchaCode)
|
||||
return captchaCode, nil
|
||||
}
|
||||
|
||||
// 用于解析HTML(登录页面)
|
||||
func ParseHTML(html string) (*goquery.Document, error) {
|
||||
return goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
}
|
||||
Reference in New Issue
Block a user