forked from 15921483570/wechat_spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.go
91 lines (73 loc) · 1.75 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
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 OnReq(f func(ctx *goproxy.ProxyCtx) (*http.Request, *http.Response)) {
_spider.OnReq(f)
}
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 Header() http.Header {
return header
}
func (s *spider) Regist(proc Processor) {
s.proxy.OnResponse().DoFunc(ProxyHandle(proc))
}
func (s *spider) OnReq(f func(ctx *goproxy.ProxyCtx) (*http.Request, *http.Response)) {
handler := func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
return f(ctx)
}
s.proxy.OnRequest().DoFunc(handler)
}
func (s *spider) Run(port string) {
if rootConfig.Compress {
s.OnReq(func(ctx *goproxy.ProxyCtx) (req *http.Request, resp *http.Response) {
host := ctx.Req.URL.Host
req = ctx.Req
if !strings.Contains(host, "mp.weixin.qq.com") {
resp = goproxy.NewResponse(req, "text/html", http.StatusNotFound, "")
}
return
})
}
log.Println("server will at port:" + port)
log.Fatal(http.ListenAndServe(":"+port, s.proxy))
}
var (
header http.Header //全局缓存wechat header
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)
}
}