forked from andeya/pholcus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantom.go
230 lines (215 loc) · 5.4 KB
/
phantom.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package surfer
import (
"encoding/json"
"io/ioutil"
"log"
"mime"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/henrylee2cn/pholcus/app/downloader/surfer/util"
)
// 基于Phantomjs的下载器实现,作为surfer的补充
// 效率较surfer会慢很多,但是因为模拟浏览器,破防性更好
// 支持UserAgent/TryTimes/RetryPause/自定义js
type (
Phantom struct {
PhantomjsFile string //Phantomjs完整文件名
TempJsDir string //临时js存放目录
jsFileMap map[string]string //已存在的js文件
}
Response struct {
Cookie string
Body string
}
)
func NewPhantom(phantomjsFile, tempJsDir string) Surfer {
phantom := &Phantom{
PhantomjsFile: phantomjsFile,
TempJsDir: tempJsDir,
jsFileMap: make(map[string]string),
}
if !filepath.IsAbs(phantom.PhantomjsFile) {
phantom.PhantomjsFile, _ = filepath.Abs(phantom.PhantomjsFile)
}
if !filepath.IsAbs(phantom.TempJsDir) {
phantom.TempJsDir, _ = filepath.Abs(phantom.TempJsDir)
}
// 创建/打开目录
err := os.MkdirAll(phantom.TempJsDir, 0777)
if err != nil {
log.Printf("[E] Surfer: %v\n", err)
return phantom
}
phantom.createJsFile("get", getJs)
phantom.createJsFile("post", postJs)
return phantom
}
// 实现surfer下载器接口
func (self *Phantom) Download(req Request) (resp *http.Response, err error) {
var encoding = "utf-8"
if _, params, err := mime.ParseMediaType(req.GetHeader().Get("Content-Type")); err == nil {
if cs, ok := params["charset"]; ok {
encoding = strings.ToLower(strings.TrimSpace(cs))
}
}
req.GetHeader().Del("Content-Type")
param, err := NewParam(req)
if err != nil {
return nil, err
}
resp = param.writeback(resp)
var args []string
switch req.GetMethod() {
case "GET":
args = []string{
self.jsFileMap["get"],
req.GetUrl(),
param.header.Get("Cookie"),
encoding,
param.header.Get("User-Agent"),
}
case "POST", "POST-M":
args = []string{
self.jsFileMap["post"],
req.GetUrl(),
param.header.Get("Cookie"),
encoding,
param.header.Get("User-Agent"),
req.GetPostData(),
}
}
for i := 0; i < param.tryTimes; i++ {
cmd := exec.Command(self.PhantomjsFile, args...)
if resp.Body, err = cmd.StdoutPipe(); err != nil {
time.Sleep(param.retryPause)
continue
}
if cmd.Start() != nil || resp.Body == nil {
time.Sleep(param.retryPause)
continue
}
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
time.Sleep(param.retryPause)
continue
}
retResp := Response{}
err = json.Unmarshal(b, &retResp)
if err != nil {
time.Sleep(param.retryPause)
continue
}
resp.Header = param.header
resp.Header.Set("Set-Cookie", retResp.Cookie)
resp.Body = ioutil.NopCloser(strings.NewReader(retResp.Body))
break
}
if err == nil {
resp.StatusCode = http.StatusOK
resp.Status = http.StatusText(http.StatusOK)
} else {
resp.StatusCode = http.StatusBadGateway
resp.Status = http.StatusText(http.StatusBadGateway)
}
return
}
//销毁js临时文件
func (self *Phantom) DestroyJsFiles() {
p, _ := filepath.Split(self.TempJsDir)
if p == "" {
return
}
for _, filename := range self.jsFileMap {
os.Remove(filename)
}
if len(util.WalkDir(p)) == 1 {
os.Remove(p)
}
}
func (self *Phantom) createJsFile(fileName, jsCode string) {
fullFileName := filepath.Join(self.TempJsDir, fileName)
// 创建并写入文件
f, _ := os.Create(fullFileName)
f.Write([]byte(jsCode))
f.Close()
self.jsFileMap[fileName] = fullFileName
}
/*
* GET method
* system.args[0] == get.js
* system.args[1] == url
* system.args[2] == cookie
* system.args[3] == pageEncode
* system.args[4] == userAgent
*/
const getJs string = `
var system = require('system');
var page = require('webpage').create();
var url = system.args[1];
var cookie = system.args[2];
var pageEncode = system.args[3];
var userAgent = system.args[4];
page.onResourceRequested = function(requestData, request) {
request.setHeader('Cookie', cookie)
};
phantom.outputEncoding = pageEncode;
page.settings.userAgent = userAgent;
page.open(url, function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var cookie = page.evaluate(function(s) {
return document.cookie;
});
var resp = {
"Cookie": cookie,
"Body": page.content
};
console.log(JSON.stringify(resp));
}
phantom.exit();
});
`
/*
* POST method
* system.args[0] == post.js
* system.args[1] == url
* system.args[2] == cookie
* system.args[3] == pageEncode
* system.args[4] == userAgent
* system.args[5] == postdata
*/
const postJs string = `
var system = require('system');
var page = require('webpage').create();
var url = system.args[1];
var cookie = system.args[2];
var pageEncode = system.args[3];
var userAgent = system.args[4];
var postdata = system.args[5];
page.onResourceRequested = function(requestData, request) {
request.setHeader('Cookie', cookie)
};
phantom.outputEncoding = pageEncode;
page.settings.userAgent = userAgent;
page.open(url, 'post', postdata, function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var cookie = page.evaluate(function(s) {
return document.cookie;
});
var resp = {
"Cookie": cookie,
"Body": page.content
};
console.log(JSON.stringify(resp));
}
phantom.exit();
});
`