-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintercept.go
179 lines (144 loc) · 2.81 KB
/
intercept.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"unicode"
)
var (
cntntEncPfx = []byte("Content-Encoding:")
cntntTypPfx = []byte("Content-Type:")
cntntLenPfx = []byte("Content-Length:")
httpPfx = []byte("HTTP")
gzipToken = []byte("gzip")
htmlToken = []byte("text/html")
)
func intercept(dst io.Writer, src io.Reader, secure bool) (written int64, err error) {
if secure {
return io.Copy(dst, src)
}
return scanAndModify(dst, src)
}
func scanAndModify(dst io.Writer, src io.Reader) (written int64, err error) {
sc := bufio.NewScanner(src)
var body, html, gzip, brk, clPrinted bool
var clen int
for sc.Scan() {
bs := sc.Bytes()
if isCntntLen(bs) {
clen = cntntLen(bs)
continue
}
if isPad(bs) {
if !body && clen == 0 {
n, err := writeLine(dst, nil)
written += n
if err != nil {
return written, err
}
break
}
body = !body
continue
}
html = triggerHTML(bs, html)
gzip = triggerGzipped(bs, gzip)
if isHTTP(bs) {
if body {
n, err := writeLine(dst, nil)
written += n
if err != nil {
return written, err
}
}
body = false
html = false
gzip = false
clen = 0
brk = false
clPrinted = false
}
if body {
if html {
var err error
bs, err = scanAndModifyHTML(sc, bs, gzip, clen)
if err != nil {
return written, err
}
clen = len(bs)
brk = true
}
if !clPrinted {
clPrinted = true
tl := strconv.Itoa(clen)
x := "Content-Length: " + tl + "\n"
n, err := writeLine(dst, []byte(x))
written += n
if err != nil {
return written, err
}
}
}
n, err := writeLine(dst, bs)
written += n
if err != nil {
return written, err
}
if brk {
break
}
}
return written, sc.Err()
}
func writeLine(w io.Writer, b []byte) (int64, error) {
n := 0
fmt.Print(string(b))
if len(b) > 0 {
nb, err := w.Write(b)
n = nb
if err != nil {
return int64(n), err
}
}
nn, err := w.Write([]byte("\n"))
fmt.Println()
return int64(n + nn), err
}
func isHTTP(b []byte) bool {
return bytes.HasPrefix(b, httpPfx)
}
func cntntLen(b []byte) int {
for i := len(b) - 1; i >= 0; i-- {
if !unicode.IsDigit(rune(b[i])) {
cl, _ := strconv.Atoi(string(b[i+1:]))
return cl
}
}
return 0
}
func isCntntLen(b []byte) bool {
return bytes.HasPrefix(b, cntntLenPfx)
}
func triggerHTML(b []byte, v bool) bool {
if isHTML(b) {
return true
}
return v
}
func isHTML(b []byte) bool {
return bytes.HasPrefix(b, cntntTypPfx) && bytes.Contains(b[len(cntntTypPfx):], htmlToken)
}
func triggerGzipped(b []byte, v bool) bool {
if isGzipped(b) {
return true
}
return v
}
func isGzipped(b []byte) bool {
return bytes.HasPrefix(b, cntntEncPfx) && bytes.Contains(b[len(cntntEncPfx):], gzipToken)
}
func isPad(b []byte) bool {
return len(b) == 0
}