|
@@ -5,9 +5,11 @@ import (
|
|
|
"bytes"
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
+ "fmt"
|
|
|
"log"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
"qfw/util"
|
|
|
"spiderutil"
|
|
|
"time"
|
|
@@ -39,6 +41,7 @@ func main1() {
|
|
|
}
|
|
|
|
|
|
func (cs *CodeService) GetCodeByImgPath(ctx context.Context, req *proto.GetCodebyImgPathReq) (*proto.GetCodebyImgPathResp, error) {
|
|
|
+ defer util.Catch()
|
|
|
//根据图片地址解析出code
|
|
|
util.Debug("Img Path:", req.Imgpath)
|
|
|
code, headers, cookies := getCode(req.Imgpath, req.Stype, req.Head, req.Cookie)
|
|
@@ -54,6 +57,7 @@ func (cs *CodeService) GetCodeByImgPath(ctx context.Context, req *proto.GetCodeb
|
|
|
func getCode(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
|
|
|
// log.Println("cookie---", cookie)
|
|
|
// log.Println("stype---", stype)
|
|
|
+ defer util.Catch()
|
|
|
client := req.C().SetTimeout(spiderutil.Config.TimeOut * time.Second)
|
|
|
headers := map[string]string{}
|
|
|
if head != "" {
|
|
@@ -63,8 +67,8 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
if cookie != "" {
|
|
|
json.Unmarshal([]byte(cookie), &cookies)
|
|
|
}
|
|
|
- // log.Println("headers:", headers)
|
|
|
- // log.Println("cookies:", cookies)
|
|
|
+ //log.Println("headers:", headers)
|
|
|
+ //log.Println("cookies:", cookies)
|
|
|
for times := 1; times <= 3; times++ { //重试三次
|
|
|
request := client.R()
|
|
|
if len(headers) > 0 {
|
|
@@ -73,12 +77,15 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
if len(cookies) > 0 {
|
|
|
request.SetCookies(cookies...)
|
|
|
}
|
|
|
- //下载验证码
|
|
|
+ //下载验证码图片
|
|
|
getCodeResp, err := request.Get(path)
|
|
|
+ //log.Println("respHeader---", getCodeResp.Header)
|
|
|
+ //log.Println("respCookie---", getCodeResp.Cookies())
|
|
|
if err != nil {
|
|
|
log.Println("Get Code By Path Error: ", err)
|
|
|
continue
|
|
|
}
|
|
|
+ //savefile(getCodeResp)
|
|
|
//解析验证码
|
|
|
data := map[string]string{
|
|
|
"grant_type": "",
|
|
@@ -93,8 +100,8 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
SetFileReader("file", "1", bytes.NewReader(getCodeResp.Bytes())).
|
|
|
SetFormData(data).
|
|
|
Post(spiderutil.Config.ServerCodeAddress + stype)
|
|
|
- //SetFile("file", "C:/Users/topnet/Desktop/code.jpg").
|
|
|
- // Post(spiderutil.Config.ServerCodeAddress)
|
|
|
+ //SetFile("file", "C:/Users/topnet/Desktop/code.jpg").
|
|
|
+ // Post(spiderutil.Config.ServerCodeAddress)
|
|
|
if err != nil {
|
|
|
log.Println("analysis code by path err: ", err)
|
|
|
continue
|
|
@@ -111,6 +118,12 @@ func getCode(path, stype, head, cookie string) (code string, respheader http.Hea
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+func savefile(resp *req.Response) {
|
|
|
+ filename := fmt.Sprint(time.Now().Unix()) + ".jpg"
|
|
|
+ file, _ := os.Create(filename)
|
|
|
+ defer file.Close()
|
|
|
+ file.Write(resp.Bytes())
|
|
|
+}
|
|
|
|
|
|
//func main() {
|
|
|
// log.Println(util.Config)
|