-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.go
269 lines (237 loc) · 5.34 KB
/
js.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package retracer
import (
"bytes"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"strings"
"sync/atomic"
"syscall"
"time"
"h12.io/errors"
"h12.io/mitm"
"h12.io/uuid"
)
type JSTracer struct {
Timeout time.Duration
Certs *mitm.CertPool
}
func (t *JSTracer) Trace(uri string, header http.Header, body []byte) (string, error) {
body = neutralizeIFrame(body)
if t.Timeout == 0 {
t.Timeout = 10 * time.Second
}
proxy := newProxy(uri, header, body, t.Timeout, t.Certs)
defer proxy.Close()
browser, err := startBrowser(uri, proxy.URL())
if err != nil {
return "", err
}
defer browser.Close()
select {
case <-time.After(t.Timeout):
return "", ErrJSRedirectionTimeout
case redirectURL := <-proxy.RedirectURLChan():
return redirectURL, nil
case err := <-proxy.ErrChan():
return "", err
case err := <-errChan(browser.Wait):
return "", err
}
}
func neutralizeIFrame(body []byte) []byte {
body = bytes.Replace(body, []byte("<iframe"), []byte("<div"), -1)
body = bytes.Replace(body, []byte("/iframe>"), []byte("/div>"), -1)
return body
}
type fakeProxy struct {
uri string
header http.Header
body []byte
certs *mitm.CertPool
timeout time.Duration
proxy *httptest.Server
redirectChan chan string
errChan chan error
respondCount int32
}
func newProxy(uri string, header http.Header, body []byte, timeout time.Duration, certs *mitm.CertPool) *fakeProxy {
fp := &fakeProxy{
uri: uri,
header: header,
body: body,
certs: certs,
timeout: timeout,
redirectChan: make(chan string),
errChan: make(chan error),
}
fp.proxy = httptest.NewServer(http.HandlerFunc(fp.serve))
return fp
}
func (p *fakeProxy) URL() string {
return p.proxy.URL
}
func (p *fakeProxy) RedirectURLChan() <-chan string {
return p.redirectChan
}
func (p *fakeProxy) ErrChan() <-chan error {
return p.errChan
}
func (p *fakeProxy) setError(err error) {
select {
case p.errChan <- err:
default:
}
}
func (p *fakeProxy) setRedirectURL(uri string) {
select {
case p.redirectChan <- uri:
default:
}
}
func (p *fakeProxy) Close() error {
p.proxy.Close() // make should all serve goroutines have exited
return nil
}
func (p *fakeProxy) serve(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
p.serveHTTP(w, req)
} else if req.Method == "CONNECT" {
err := p.certs.ServeHTTPS(w, req, p.serveHTTP)
if err != nil {
p.setError(errors.Wrap(err))
}
}
}
func (p *fakeProxy) serveHTTP(w http.ResponseWriter, req *http.Request) {
if atomic.AddInt32(&p.respondCount, 1) == 1 {
for k, v := range p.header {
w.Header()[k] = v
}
w.Write(p.body)
} else {
if isJS(req.RequestURI) {
jsResp, err := http.Get(req.RequestURI)
if err != nil {
p.setError(errors.Wrap(err))
return
}
for k, v := range jsResp.Header {
w.Header()[k] = v
}
w.WriteHeader(jsResp.StatusCode)
if _, err := io.Copy(w, jsResp.Body); err != nil {
p.setError(errors.Wrap(err))
}
jsResp.Body.Close()
return
}
if isResource(req.RequestURI, req.Header) {
return
}
p.setRedirectURL(req.RequestURI)
}
}
func isJS(uri string) bool {
return strings.ToLower(path.Ext(uri)) == ".js"
}
func isResource(uri string, header http.Header) bool {
switch strings.ToLower(path.Ext(uri)) {
case ".css", ".png", ".gif", ".jpg", ".jpeg":
return true
}
accept := header.Get("Accept")
if !strings.Contains(accept, "text/html") {
return true
}
return false
}
type browser struct {
id string
cmd *exec.Cmd
}
func startBrowser(uri, proxy string) (*browser, error) {
// id is for debugging only
id, _ := uuid.NewTime(time.Now())
cmd := exec.Command(
"surf",
"-bdfgikmnp",
"-t", os.DevNull,
uri,
id.String(),
)
// set pgid so all child processes can be killed together
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Env = []string{
"DISPLAY=" + os.Getenv("DISPLAY"),
"http_proxy=" + proxy,
}
return &browser{id: id.String(), cmd: cmd}, cmd.Start()
}
func (b *browser) pid() int {
if b.cmd.Process != nil {
return b.cmd.Process.Pid
}
return 0
}
func (b *browser) Wait() error {
err := b.cmd.Wait()
if _, ok := err.(*exec.ExitError); !ok {
return errors.Wrap(err)
}
return nil
}
func (b *browser) Close() error {
if b.cmd.Process == nil {
log.Printf("cannot kill surf %s because it is not started", b.id)
return nil
}
// kill -pgid (-pid)
// https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773#.g2krdc3ir
if err := syscall.Kill(-b.cmd.Process.Pid, syscall.SIGKILL); err != nil {
log.Printf("fail to kill surf %s (%d)", b.id, b.pid())
return err
}
return nil
}
func forceKill(p *os.Process) error {
if err := p.Kill(); err != nil {
return err
}
for i := 0; processExists(p.Pid); i++ {
if err := p.Kill(); err != nil {
return err
}
time.Sleep(time.Second)
if i > 10 {
log.Printf("try to kill surf %d for the %d times", p.Pid, i)
}
}
return nil
}
func processExists(pid int) bool {
process, err := os.FindProcess(pid)
if err != nil {
// non-unix system
return false
}
return nil == process.Signal(syscall.Signal(0))
}
func strChan(f func() string) chan string {
ch := make(chan string)
go func() {
ch <- f()
}()
return ch
}
func errChan(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}