This repository has been archived by the owner on Aug 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
spider.go
127 lines (115 loc) · 3.68 KB
/
spider.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
package wechat_brain
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"os/exec"
"strconv"
"time"
"github.com/coreos/goproxy"
)
var (
_spider = newSpider()
Mode int
AutoMatic int
)
type spider struct {
proxy *goproxy.ProxyHttpServer
}
func Run(port string, mode, automatic int) {
Mode = mode
AutoMatic = automatic
_spider.Init()
_spider.Run(port)
}
func Close() {
memoryDb.Close()
}
func newSpider() *spider {
sp := &spider{}
sp.proxy = goproxy.NewProxyHttpServer()
sp.proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
return sp
}
func (s *spider) Run(port string) {
log.Println("server will at port:" + port)
log.Fatal(http.ListenAndServe(":"+port, s.proxy))
}
func (s *spider) Init() {
requestHandleFunc := func(request *http.Request, ctx *goproxy.ProxyCtx) (req *http.Request, resp *http.Response) {
req = request
if ctx.Req.URL.Path == `/question/bat/findQuiz` || ctx.Req.URL.Path == `/question/fight/findQuiz` {
bs, _ := ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewReader(bs))
handleQuestionReq(bs)
} else if ctx.Req.URL.Path == `/question/bat/choose` || ctx.Req.URL.Path == `/question/fight/choose` {
bs, _ := ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewReader(bs))
handleChooseReq(bs)
} else if ctx.Req.URL.Host == `abc.com` {
resp = new(http.Response)
resp.StatusCode = 200
resp.Header = make(http.Header)
resp.Header.Add("Content-Disposition", "attachment; filename=ca.crt")
resp.Header.Add("Content-Type", "application/octet-stream")
resp.Body = ioutil.NopCloser(bytes.NewReader(goproxy.CA_CERT))
}
return
}
responseHandleFunc := func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if resp == nil {
return resp
}
if ctx.Req.URL.Path == "/question/bat/findQuiz" || ctx.Req.URL.Path == "/question/fight/findQuiz" {
bs, _ := ioutil.ReadAll(resp.Body)
bsNew, ansPos := handleQuestionResp(bs)
resp.Body = ioutil.NopCloser(bytes.NewReader(bsNew))
if AutoMatic == 1 {
go clickProcess(ansPos)
} // click answer
} else if ctx.Req.URL.Path == "/question/bat/choose" || ctx.Req.URL.Path == "/question/fight/choose" {
bs, _ := ioutil.ReadAll(resp.Body)
resp.Body = ioutil.NopCloser(bytes.NewReader(bs))
go handleChooseResponse(bs)
} else if ctx.Req.URL.Path == "/question/bat/fightResult" || ctx.Req.URL.Path == "/question/fight/fightResult" {
if AutoMatic == 1 {
go clickProcess(-1)
} // go to next match
}
return resp
}
s.proxy.OnResponse().DoFunc(responseHandleFunc)
s.proxy.OnRequest().DoFunc(requestHandleFunc)
}
func clickProcess(ansPos int) {
var screanCenterX = 550 // center of screen
var firstItemY = 1280 // center of first item (y)
var qualifyingItemY = 2000 // 排位列表最后一项 y 坐标
if ansPos >= 0 {
log.Printf("【点击】正在点击选项:%d", ansPos)
time.Sleep(time.Millisecond * 3800) //延迟
go clickAction(screanCenterX, firstItemY+200*(ansPos-1)) // process click
} else {
// go to next match
log.Printf("【点击】将点击继续挑战按钮...")
time.Sleep(time.Millisecond * 7500)
go clickAction(screanCenterX, firstItemY+400) // 继续挑战 按钮在第三个item处
log.Printf("【点击】将点击排位列表底部一项,进行比赛匹配...")
time.Sleep(time.Millisecond * 2000)
go clickAction(screanCenterX, qualifyingItemY)
}
}
func clickAction(posX int, posY int) {
var err error
touchX, touchY := strconv.Itoa(posX), strconv.Itoa(posY)
_, err = exec.Command("adb", "shell", "input", "swipe", touchX, touchY, touchX, touchY).Output()
if err != nil {
log.Fatal("error: check adb connection.")
}
}
func orPanic(err error) {
if err != nil {
panic(err)
}
}