-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
233 lines (197 loc) · 5.36 KB
/
main.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
//
// Very Simple SNIProxy with GO Language
// Code by Jioh L. Jung([email protected])
//
package main
import (
"flag"
"net"
"io"
"log"
"strconv"
"bufio"
"container/list"
"strings"
)
func on_disconnect(dst io.WriteCloser, con_chk chan int){
// On Close-> Force Disconnect another pair of connection.
dst.Close()
con_chk <- 1
}
func ioReflector(dst io.WriteCloser, src io.Reader, con_chk chan int) {
// Reflect IO stream to another.
defer on_disconnect(dst, con_chk)
written, _ := io.Copy(dst, src)
log.Printf("Written %d", written) //TODO: Update to Metric Info
dst.Close()
con_chk <- 1
}
func handle_simpleHTTP(conn net.Conn) {
headers := bufio.NewReader(conn)
hostname := ""
readLines := list.New()
for {
bytes, _, error := headers.ReadLine()
if error != nil {
conn.Close()
return
}
line := string(bytes)
log.Printf("%s", line)
readLines.PushBack(line)
if line == "" {
// End of HTTP headers
break
}
//Check Host Header.
if strings.HasPrefix(line, "Host: ") {
hostname = strings.TrimPrefix(line, "Host: ")
}
}
backend, error := net.Dial("tcp", hostname + ":80")
if error != nil {
log.Fatal("Couldn't connect to backend", error)
conn.Close()
return
}
for element := readLines.Front(); element != nil; element = element.Next() {
line := element.Value.(string)
backend.Write([]byte(line))
backend.Write([]byte("\n"))
log.Printf("> %s", line)
}
con_chk := make(chan int)
go ioReflector(backend, conn, con_chk)
go ioReflector(conn, backend, con_chk)
}
func handle_simpleSNI(conn net.Conn) {
// Simple SNI Protocol : SNI Handling Code from https://github.com/gpjt/stupid-proxy/
firstByte := make([]byte, 1)
_, error := conn.Read(firstByte)
if error != nil {
log.Printf("Couldn't read first byte :-(")
return
}
if firstByte[0] != 0x16 {
log.Printf("Not TLS :-(")
}
versionBytes := make([]byte, 2)
_, error = conn.Read(versionBytes)
if error != nil {
log.Printf("Couldn't read version bytes :-(")
return
}
if versionBytes[0] < 3 || (versionBytes[0] == 3 && versionBytes[1] < 1) {
log.Printf("SSL < 3.1 so it's still not TLS v%d.%d", versionBytes[0], versionBytes[1])
return
}
restLengthBytes := make([]byte, 2)
_, error = conn.Read(restLengthBytes)
if error != nil {
log.Printf("Couldn't read restLength bytes :-(")
return
}
restLength := (int(restLengthBytes[0]) << 8) + int(restLengthBytes[1])
rest := make([]byte, restLength)
_, error = conn.Read(rest)
if error != nil {
log.Printf("Couldn't read rest of bytes")
return
}
current := 0
handshakeType := rest[0]
current += 1
if handshakeType != 0x1 {
log.Printf("Not a ClientHello")
return
}
// Skip over another length
current += 3
// Skip over protocolversion
current += 2
// Skip over random number
current += 4 + 28
// Skip over session ID
sessionIDLength := int(rest[current])
current += 1
current += sessionIDLength
cipherSuiteLength := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
current += cipherSuiteLength
compressionMethodLength := int(rest[current])
current += 1
current += compressionMethodLength
if current > restLength {
log.Println("no extensions")
return
}
// Skip over extensionsLength
// extensionsLength := (int(rest[current]) << 8) + int(rest[current + 1])
current += 2
hostname := ""
for current < restLength && hostname == "" {
extensionType := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
extensionDataLength := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
if extensionType == 0 {
// Skip over number of names as we're assuming there's just one
current += 2
nameType := rest[current]
current += 1
if nameType != 0 {
log.Printf("Not a hostname")
return
}
nameLen := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
hostname = string(rest[current : current+nameLen])
}
current += extensionDataLength
}
if hostname == "" {
log.Printf("No hostname")
return
}
backend, error := net.Dial("tcp", hostname + ":443")
if error != nil {
log.Fatal("Couldn't connect to backend", error)
backend.Close()
return
}
backend.Write(firstByte)
backend.Write(versionBytes)
backend.Write(restLengthBytes)
backend.Write(rest)
con_chk := make(chan int)
go ioReflector(backend, conn, con_chk)
go ioReflector(conn, backend, con_chk)
}
func listen_defered(term chan int){
}
func start_listen(ip string, port int, handle func(net.Conn), term chan int) {
defer listen_defered(term)
listener, error := net.Listen("tcp", ip + ":" + strconv.Itoa(port))
if error != nil {
log.Printf("Couldn't start listening", error)
return
}
log.Printf("Started proxy on %s:%d -- listening", ip, port)
for {
connection, error := listener.Accept()
if error != nil {
log.Printf("Accept error", error)
return
}
log.Printf("From: %s", connection.RemoteAddr().String())
go handle(connection)
}
}
func main() {
bindip := flag.String("bindip", "0.0.0.0", "Bind Specific IP Address")
flag.Parse()
tchan := make (chan int)
go start_listen(*bindip, 80, handle_simpleHTTP, tchan)
go start_listen(*bindip, 443, handle_simpleSNI, tchan)
<- tchan
}