forked from netwboy/stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmitm.go
291 lines (232 loc) · 5.26 KB
/
mitm.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bytes"
"fmt"
"io"
"log"
"net"
"strings"
"time"
)
func checkAllowIP(addr string) bool {
Mutex.RLock()
defer Mutex.RUnlock()
for i := 0; i < len(Data.AllowedIPs); i++ {
if Data.AllowedIPs[i] == addr {
return true
}
}
return false
}
func checkAllowDomain(domain string) bool {
Mutex.RLock()
defer Mutex.RUnlock()
for i := 0; i < len(Data.Domain); i++ {
if strings.HasSuffix(domain, Data.Domain[i]) {
return true
}
}
return false
}
func startHTTP() {
for {
log.Printf("[APP][HTTP] %v", beginHTTP())
time.Sleep(time.Second * 10)
}
}
func beginHTTP() error {
ln, err := net.Listen("tcp", ":80")
if err != nil {
return err
}
defer ln.Close()
log.Println("[APP][HTTP] Started")
for {
client, err := ln.Accept()
if err != nil {
if errno, ok := err.(net.Error); ok {
if errno.Temporary() {
continue
}
}
return err
}
_ = client.(*net.TCPConn).SetNoDelay(false)
_ = client.(*net.TCPConn).SetKeepAlive(true)
_ = client.(*net.TCPConn).SetKeepAlivePeriod(time.Second * 10)
go handleHTTP(client)
}
}
func handleHTTP(client net.Conn) {
defer client.Close()
if addr, _, _ := net.SplitHostPort(client.RemoteAddr().String()); !checkAllowIP(addr) {
log.Printf("[APP][HTTP][%s] IP Not Allow", addr)
return
}
data := make([]byte, 1400)
size, err := client.Read(data)
if err != nil {
return
}
data = data[:size]
list := make(map[string]string)
{
hdr := bytes.Split(bytes.Split(data, []byte{0x0d, 0x0a, 0x0d, 0x0a})[0], []byte{0x0d, 0x0a})
for i := 0; i < len(hdr); i++ {
text := string(hdr[i])
if i == 0 {
continue
}
SPL := strings.SplitN(text, ":", 2)
list[strings.ToUpper(strings.TrimSpace(SPL[0]))] = strings.TrimSpace(SPL[1])
}
}
if domain, ok := list["HOST"]; ok {
if !checkAllowDomain(domain) {
return
}
} else {
return
}
log.Printf("[APP][HTTP] %s <-> %s", client.RemoteAddr(), list["HOST"])
remote, err := net.Dial("tcp", fmt.Sprintf("%s:80", list["HOST"]))
if err != nil {
return
}
defer remote.Close()
if _, err := remote.Write(data); err != nil {
return
}
data = nil
go func() {
_, _ = io.CopyBuffer(remote, client, make([]byte, 1400))
_ = client.SetDeadline(time.Now())
_ = remote.SetDeadline(time.Now())
}()
_, _ = io.CopyBuffer(client, remote, make([]byte, 1400))
_ = client.SetDeadline(time.Now())
_ = remote.SetDeadline(time.Now())
}
func startTLS() {
for {
log.Printf("[APP][TLS] %v", beginTLS())
time.Sleep(time.Second * 10)
}
}
func beginTLS() error {
ln, err := net.Listen("tcp", ":443")
if err != nil {
return err
}
defer ln.Close()
log.Println("[APP][TLS] Started")
for {
client, err := ln.Accept()
if err != nil {
if errno, ok := err.(net.Error); ok {
if errno.Temporary() {
continue
}
}
return err
}
_ = client.(*net.TCPConn).SetNoDelay(false)
_ = client.(*net.TCPConn).SetKeepAlive(true)
_ = client.(*net.TCPConn).SetKeepAlivePeriod(time.Second * 10)
go handleTLS(client)
}
}
func handleTLS(client net.Conn) {
defer client.Close()
if addr, _, _ := net.SplitHostPort(client.RemoteAddr().String()); !checkAllowIP(addr) {
log.Printf("[APP][TLS][%s] IP Not Allow", addr)
return
}
data := make([]byte, 1400)
size, err := client.Read(data)
if err != nil {
return
}
data = data[:size]
if data[0] != 0x16 {
return
}
offset := 0
offset += 1 // Content Type
offset += 2 // Version
offset += 2 // Length
// Handshake Type
if data[offset] != 0x01 {
log.Printf("[APP][TLS][%s] Not Client Hello", client.RemoteAddr())
return
}
offset += 1
offset += 3 // Length
offset += 2 // Version
offset += 32 // Random
// Session ID
length := int(data[offset])
offset += 1
offset += length
// Cipher Suites
length = (int(data[offset]) << 8) + int(data[offset+1])
offset += 2
offset += length
// Compression Methods
length = int(data[offset])
offset += 1
offset += length
// Extension Length
offset += 2
domain := ""
for offset < size && domain == "" {
// Extension Type
name := (int(data[offset]) << 8) + int(data[offset+1])
offset += 2
// Extension Length
length = (int(data[offset]) << 8) + int(data[offset+1])
offset += 2
// Extension: Server Name
if name == 0 {
// Server Name List Length
offset += 2
// Server Name Type
if data[offset] != 0x00 {
log.Printf("[APP][TLS][%s] Not Host Name", client.RemoteAddr())
return
}
offset += 1
// Server Name Length
length = (int(data[offset]) << 8) + int(data[offset+1])
offset += 2
// Server Name
domain = string(data[offset : offset+length])
// Get Out
break
}
// Extension Data
offset += length
}
if !checkAllowDomain(domain) {
log.Printf("[APP][TLS][%s] Domain Not Allow: %s", client.RemoteAddr(), domain)
return
}
log.Printf("[APP][TLS] %s <-> %s", client.RemoteAddr(), domain)
remote, err := net.Dial("tcp", fmt.Sprintf("%s:443", domain))
if err != nil {
return
}
defer remote.Close()
if _, err := remote.Write(data); err != nil {
return
}
data = nil
go func() {
_, _ = io.CopyBuffer(remote, client, make([]byte, 1400))
_ = client.SetDeadline(time.Now())
_ = remote.SetDeadline(time.Now())
}()
_, _ = io.CopyBuffer(client, remote, make([]byte, 1400))
_ = client.SetDeadline(time.Now())
_ = remote.SetDeadline(time.Now())
}