forked from 15921483570/wechat_spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.go
74 lines (60 loc) · 1.37 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
package wechat_spider
import (
"log"
"net/http"
"strings"
"github.com/elazarl/goproxy"
)
type spider struct {
proxy *goproxy.ProxyHttpServer
}
var _spider = newSpider()
func Regist(proc Processor) {
_spider.Regist(proc)
}
func Run(port string) {
_spider.Run(port)
}
func newSpider() *spider {
sp := &spider{}
sp.proxy = goproxy.NewProxyHttpServer()
sp.proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
return sp
}
func (s *spider) Regist(proc Processor) {
s.proxy.OnResponse().DoFunc(ProxyHandle(proc))
}
func (s *spider) Run(port string) {
if rootConfig.Compress {
s.proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
host := req.URL.Host
if !strings.Contains(host, "mp.weixin.qq.com") {
req, _ = http.NewRequest("GET", "http://mp.weixin.qq.com/notfound", nil)
return req, nil
}
return req, nil
})
}
log.Println("server will at port:" + port)
log.Fatal(http.ListenAndServe(":"+port, s.proxy))
}
var (
rootConfig = &Config{
Verbose: false,
AutoScroll: false,
Compress: true,
}
)
type Config struct {
Verbose bool // Debug
AutoScroll bool // Auto scroll page to hijack all history articles
Compress bool // To ingore other request just to save the net cost
}
func InitConfig(conf *Config) {
rootConfig = conf
}
func orPanic(err error) {
if err != nil {
panic(err)
}
}