-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
198 lines (155 loc) · 3.57 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
package main
/*
* ToTo - Go language proxy server
* Song Hyeon Sik <[email protected]> 2016
*/
import (
"fmt"
"io"
"net"
"strconv"
"strings"
)
const (
bufferLength = 8192
headerLine = 30
)
// ProxyServer stores the configuration for ProxyServer and some functions.
type ProxyServer struct {
port string
}
// ask port
func (ps *ProxyServer) askPort() int {
port := 0
fmt.Print("Port : ")
fmt.Scanf("%d", &port)
return port
}
// initialize socket server
func (ps *ProxyServer) init(port int) net.Listener {
ps.port = ":"
ps.port += strconv.Itoa(port)
server, err := net.Listen("tcp", ps.port)
if server == nil {
panic("init: port listening error : " + err.Error())
}
return server
}
// accept client
func (ps *ProxyServer) acceptClient(server net.Listener) chan net.Conn {
channel := make(chan net.Conn)
go func() {
for {
client, err := server.Accept()
if client == nil {
fmt.Println("ps: acceptClient: Couldn't accept : ", err.Error())
continue
}
channel <- client
}
}()
return channel
}
// connect host by socket
func (ps *ProxyServer) connectHost(client net.Conn) {
HeaderInfo, Datas := ps.getData(client)
if (Datas[0] == 0) || (HeaderInfo == "-1") {
return
}
requestType, host, _, port := ps.parseHTTPHeaderMethod(HeaderInfo)
if port == -1 {
return
}
connectionHost, _ := net.Dial("tcp", host+":"+strconv.Itoa(port))
if requestType == "CONNECT" {
connectionHost.Write([]byte("HTTP/1.1 200 Connection established\n"))
} else {
connectionHost.Write(Datas)
go func() {
io.Copy(connectionHost, client)
}()
io.Copy(client, connectionHost)
}
client.Close()
connectionHost.Close()
return
}
// Get first line of http header
func (ps *ProxyServer) getData(client net.Conn) (string, []byte) {
buffer := make([]byte, bufferLength)
client.Read(buffer)
return ps.splitHeader(buffer)[0], buffer
}
// parse HTTPHeader from packets
func (ps *ProxyServer) parseHTTPHeaderMethod(headerMethod string) (string, string, string, int) {
var (
requestType string
host string
protocol string
port int
)
errorCounter := false
// ex: GET http://google.com/ HTTP/1.1
temp := headerMethod[strings.Index(headerMethod, " ")+1:]
protocol = temp[strings.Index(temp, " ")+1:]
url := temp[:strings.Index(temp, " ")]
i := strings.Index(url, "://")
if i == -1 {
fmt.Println("Uncorrect URL")
errorCounter = true
} else {
host = url[i+3 : len(url)-1]
}
if errorCounter == false {
i = strings.Index(host, ":")
if i == -1 {
port = 80
} else {
port, _ = strconv.Atoi(host[i+1:])
host = host[:i]
}
i = strings.Index(host, "/")
if i != -1 {
host = host[:i]
}
requestType = headerMethod[:strings.Index(headerMethod, " ")]
return requestType, host, protocol, port
}
return "", "", "", -1
}
// split header each lines
func (ps *ProxyServer) splitHeader(bytearray []byte) []string {
result := make([]string, headerLine)
lineNumber := 0
temp := false
if bytearray[0] == 0 {
fmt.Println("ps: splitHeader: Couldn't get httpheader, zero filter")
result[0] = string("-1")
return result
}
for index, element := range bytearray {
if element == '\r' {
if bytearray[index+1] == '\n' {
temp = true
}
}
if temp != true {
result[lineNumber] += string(element)
}
if element == '\n' {
temp = false
lineNumber++
}
}
return result
}
func main() {
proxyServer := &ProxyServer{}
port := proxyServer.askPort()
server := proxyServer.init(port)
defer server.Close()
connections := proxyServer.acceptClient(server)
for {
go proxyServer.connectHost(<-connections)
}
}