Browse Source

解析验证码新增是否使用代理参数

maxiaoshan 1 year ago
parent
commit
0a4cb917eb

+ 6 - 6
src/analysiscode/client/client.go

@@ -3,12 +3,11 @@ package client
 import (
 	"analysiscode/proto"
 	"context"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/credentials/insecure"
 	"log"
 	qu "qfw/util"
 	"spiderutil"
-
-	"google.golang.org/grpc"
-	"google.golang.org/grpc/credentials/insecure"
 )
 
 var Client proto.GetCodeClient
@@ -23,7 +22,7 @@ func InitCodeGrpcClient() {
 	Client = proto.NewGetCodeClient(conn)
 }
 
-func GetCodeByPath(path, stype, head, cookie string) (string, string, string) {
+func GetCodeByPath(path, stype, head, cookie string, proxy bool) (string, string, string) {
 	log.Println("path:", path)
 	defer qu.Catch()
 	if path == "" {
@@ -35,15 +34,16 @@ func GetCodeByPath(path, stype, head, cookie string) (string, string, string) {
 		Stype:   stype,
 		Head:    head,
 		Cookie:  cookie,
+		Proxy:   proxy,
 	}
 	resp, err := Client.GetCodeByImgPath(context.Background(), getCodeReq)
 	if err != nil {
-		log.Println("Get Code Err: ", err)
+		log.Println("Get Code Err: ", path, err)
 		return "", "", ""
 	}
 	//log.Println(resp)
 	if resp.Code == "" {
-		log.Println("Get Result Err: ", err)
+		log.Println("Get Result Err: ", path, err)
 	}
 	return resp.Code, resp.Head, resp.Cookie
 }

+ 30 - 20
src/analysiscode/proto/code.pb.go

@@ -30,6 +30,7 @@ type GetCodebyImgPathReq struct {
 	Stype   string `protobuf:"bytes,2,opt,name=stype,proto3" json:"stype,omitempty"`
 	Head    string `protobuf:"bytes,3,opt,name=head,proto3" json:"head,omitempty"`
 	Cookie  string `protobuf:"bytes,4,opt,name=cookie,proto3" json:"cookie,omitempty"`
+	Proxy   bool   `protobuf:"varint,5,opt,name=proxy,proto3" json:"proxy,omitempty"`
 }
 
 func (x *GetCodebyImgPathReq) Reset() {
@@ -92,6 +93,13 @@ func (x *GetCodebyImgPathReq) GetCookie() string {
 	return ""
 }
 
+func (x *GetCodebyImgPathReq) GetProxy() bool {
+	if x != nil {
+		return x.Proxy
+	}
+	return false
+}
+
 type GetCodebyImgPathResp struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
@@ -159,26 +167,28 @@ var File_proto_code_proto protoreflect.FileDescriptor
 
 var file_proto_code_proto_rawDesc = []byte{
 	0x0a, 0x10, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f,
-	0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x71, 0x0a, 0x13, 0x47, 0x65, 0x74,
-	0x43, 0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52, 0x65, 0x71,
-	0x12, 0x18, 0x0a, 0x07, 0x69, 0x6d, 0x67, 0x70, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28,
-	0x09, 0x52, 0x07, 0x69, 0x6d, 0x67, 0x70, 0x61, 0x74, 0x68, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74,
-	0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x79, 0x70, 0x65,
-	0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
-	0x68, 0x65, 0x61, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x18, 0x04,
-	0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x22, 0x56, 0x0a, 0x14,
-	0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68,
-	0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01,
-	0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64,
-	0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68, 0x65, 0x61, 0x64, 0x12, 0x16, 0x0a, 0x06,
-	0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x6f,
-	0x6f, 0x6b, 0x69, 0x65, 0x32, 0x56, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x12,
-	0x4b, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x42, 0x79, 0x49, 0x6d, 0x67, 0x50,
-	0x61, 0x74, 0x68, 0x12, 0x1a, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x43,
-	0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a,
-	0x1b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x62,
-	0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x42, 0x08, 0x5a, 0x06,
-	0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x87, 0x01, 0x0a, 0x13, 0x47, 0x65,
+	0x74, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52, 0x65,
+	0x71, 0x12, 0x18, 0x0a, 0x07, 0x69, 0x6d, 0x67, 0x70, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01,
+	0x28, 0x09, 0x52, 0x07, 0x69, 0x6d, 0x67, 0x70, 0x61, 0x74, 0x68, 0x12, 0x14, 0x0a, 0x05, 0x73,
+	0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x79, 0x70,
+	0x65, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
+	0x04, 0x68, 0x65, 0x61, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x18,
+	0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x12, 0x14, 0x0a,
+	0x05, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x70, 0x72,
+	0x6f, 0x78, 0x79, 0x22, 0x56, 0x0a, 0x14, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x79,
+	0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63,
+	0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12,
+	0x12, 0x0a, 0x04, 0x68, 0x65, 0x61, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68,
+	0x65, 0x61, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x18, 0x03, 0x20,
+	0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x32, 0x56, 0x0a, 0x07, 0x47,
+	0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x4b, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64,
+	0x65, 0x42, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x12, 0x1a, 0x2e, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50,
+	0x61, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x1b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47,
+	0x65, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x79, 0x49, 0x6d, 0x67, 0x50, 0x61, 0x74, 0x68, 0x52,
+	0x65, 0x73, 0x70, 0x42, 0x08, 0x5a, 0x06, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70,
+	0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 1 - 0
src/analysiscode/proto/code.proto

@@ -11,6 +11,7 @@ message GetCodebyImgPathReq {
   string stype = 2;
   string head = 3;
   string cookie = 4;
+  bool proxy = 5;
 }
 message GetCodebyImgPathResp {
   string code = 1;

+ 17 - 17
src/analysiscode/server.go

@@ -48,7 +48,7 @@ func (cs *CodeService) GetCodeByImgPath(ctx context.Context, req *proto.GetCodeb
 	defer util.Catch()
 	//根据图片地址解析出code
 	util.Debug("Img Path:", req.Imgpath)
-	code, headers, cookies := getCode(req.Imgpath, req.Stype, req.Head, req.Cookie)
+	code, headers, cookies := getCode(req.Imgpath, req.Stype, req.Head, req.Cookie, req.Proxy)
 	head, _ := json.Marshal(headers)
 	cookie, _ := json.Marshal(cookies)
 	return &proto.GetCodebyImgPathResp{
@@ -58,22 +58,22 @@ func (cs *CodeService) GetCodeByImgPath(ctx context.Context, req *proto.GetCodeb
 	}, nil
 }
 
-func getCode(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
+func getCode(path, stype, head, cookie string, proxy bool) (code string, respheader http.Header, respcookie []*http.Cookie) {
 	//先用免费,为识别再用收费
 	coderesp := &req.Response{}
 	ok := false
-	code, respheader, respcookie, coderesp, ok = getCodeByFree(path, stype, head, cookie) //自己的服务
-	util.Debug("Get Code By Free Result:", ok, code)
+	code, respheader, respcookie, coderesp, ok = getCodeByFree(path, stype, head, cookie, proxy) //自己的服务
+	util.Debug("Get Code By Free Result:", path, ok, code)
 	//if util.IntAll(stype) > 0 && len(code) < 4 { //免费的未能识别使用超级鹰
 	if util.IntAll(stype) > 0 && !ok {
-		savefile(path, coderesp)                                               //保存图片
-		code, respheader, respcookie = getCodeByPay(path, stype, head, cookie) //超级鹰收费
+		savefile(path, coderesp)                                                      //保存图片
+		code, respheader, respcookie = getCodeByPay(path, stype, head, cookie, proxy) //超级鹰收费
 	}
 	//}
 	return
 }
 
-func getCodeByFree(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie, getCodeResp *req.Response, ok bool) {
+func getCodeByFree(path, stype, head, cookie string, proxy bool) (code string, respheader http.Header, respcookie []*http.Cookie, getCodeResp *req.Response, ok bool) {
 	defer util.Catch()
 	client := req.C().
 		SetTimeout(spiderutil.Config.TimeOut * time.Second).
@@ -90,12 +90,12 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
 		json.Unmarshal([]byte(cookie), &cookies)
 	}
 	for times := 1; times <= 6; times++ { //重试三次
-		if times > 4 { //重试第4次开始,使用代理ip
+		if times > 2 || proxy { //重试第4次开始,使用代理ip
 			if stype == "-1" {
 				return
 			}
 			proxyIp := spiderutil.GetProxyAddr(spiderutil.Config.ProxyAddr, spiderutil.Config.ProxyAuthor) //获取代理地址
-			util.Debug("proxy:", proxyIp)
+			util.Debug("proxy:", path, proxyIp)
 			client.SetProxyURL(proxyIp) //设置代理IP
 		}
 		request := client.R()
@@ -119,7 +119,7 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
 			resultByte = getCodeResp.Bytes()
 		}
 		if err != nil {
-			util.Debug("Get Code By Path Error: ", err)
+			util.Debug("Get Code By Path Error: ", path, err)
 			continue
 		}
 		//解析验证码
@@ -128,12 +128,12 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
 			SetFileReader("file", "1", bytes.NewReader(resultByte)).
 			Post(address)
 		if err != nil {
-			util.Debug("analysis code by path err: ", err)
+			util.Debug("analysis code by path err: ", path, err)
 			continue
 		}
 		yzmResult := map[string]interface{}{}
 		json.Unmarshal(codeResp.Bytes(), &yzmResult)
-		util.Debug(yzmResult)
+		util.Debug(path, yzmResult)
 		result := yzmResult["r"].(map[string]interface{})
 		yzm := fmt.Sprint(result["code"])
 		if yzm != "" {
@@ -151,7 +151,7 @@ func getCodeByFree(path, stype, head, cookie string) (code string, respheader ht
 	return
 }
 
-func getCodeByPay(path, stype, head, cookie string) (code string, respheader http.Header, respcookie []*http.Cookie) {
+func getCodeByPay(path, stype, head, cookie string, proxy bool) (code string, respheader http.Header, respcookie []*http.Cookie) {
 	defer util.Catch()
 	client := req.C().
 		SetTimeout(spiderutil.Config.TimeOut * time.Second).
@@ -169,9 +169,9 @@ func getCodeByPay(path, stype, head, cookie string) (code string, respheader htt
 	}
 	for times := 1; times <= 2; times++ { //重试三次
 		atomic.AddInt64(&PyTimes, 1)
-		if times > 1 { //重试第2次开始,使用代理ip
+		if times > 1 || proxy { //重试第2次开始,使用代理ip
 			proxyIp := spiderutil.GetProxyAddr(spiderutil.Config.ProxyAddr, spiderutil.Config.ProxyAuthor) //获取代理地址
-			util.Debug("proxy:", proxyIp)
+			util.Debug("proxy:", path, proxyIp)
 			client.SetProxyURL(proxyIp) //设置代理IP
 		}
 		request := client.R()
@@ -186,7 +186,7 @@ func getCodeByPay(path, stype, head, cookie string) (code string, respheader htt
 		//log.Println("respHeader---", getCodeResp.Header)
 		//log.Println("respCookie---", getCodeResp.Cookies())
 		if err != nil {
-			util.Debug("Get Code By Path Error: ", err)
+			util.Debug("Get Code By Path Error: ", path, err)
 			continue
 		}
 		//解析验证码
@@ -206,7 +206,7 @@ func getCodeByPay(path, stype, head, cookie string) (code string, respheader htt
 		//SetFile("file", "C:/Users/topnet/Desktop/code.jpg").
 		// Post(spiderutil.Config.ServerCodeAddress)
 		if err != nil {
-			util.Debug("analysis code by path err: ", err)
+			util.Debug("analysis code by path err: ", path, err)
 			continue
 		}
 		codeResult := map[string]interface{}{}

+ 6 - 0
src/spiderutil/chromedputil.go

@@ -1,5 +1,11 @@
 package spiderutil
 
+type ChromeActionMap struct {
+	BaseActions  []ChromeActions `json:"base-actions"`
+	RangeActions []ChromeActions `json:"range-actions"`
+	RangeTimes   int             `json:"range-times"`
+}
+
 type ChromeActions struct {
 	Action   string `json:"action"`   //执行动作
 	Param    string `json:"param"`    //选择器语句