-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (81 loc) · 1.85 KB
/
main.go
File metadata and controls
85 lines (81 loc) · 1.85 KB
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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net"
"net/url"
"strings"
)
func main() {
port := flag.Int64("p", 9444, "specify the port to listen")
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
panic(err)
}
log.Printf("http proxy server start. address: [:%d]\n", *port)
for i := 0; true; i++ {
client, err := listen.Accept()
if err != nil {
panic(err)
}
go handle(client, i)
}
}
func handle(client net.Conn, seq int) {
defer client.Close()
const count = 1 << 10 // 1kb is sufficient to read from the target address
var b [count]byte
n, err := client.Read(b[:])
if err != nil {
return
}
var method, host string
bIdx := bytes.IndexByte(b[:], '\n')
if bIdx < 0 {
return
}
if _, err = fmt.Sscanf(string(b[:bIdx]), "%s%s", &method, &host); err != nil {
return
}
address := host
if strings.HasPrefix(host, "http") {
hostPortURL, err := url.Parse(host)
if err != nil {
return
}
if strings.Contains(hostPortURL.Host, ":") {
address = hostPortURL.Host
} else {
if hostPortURL.Opaque == "443" {
address = hostPortURL.Scheme + ":443"
} else {
address = hostPortURL.Host + ":80"
}
}
}
log.Printf("seq: %-8d ==> new request. addr: %s, method: %s, target: %s \n",
seq, client.RemoteAddr(), method, address)
server, err := net.Dial("tcp", address)
if err != nil {
return
}
defer server.Close()
if method == "CONNECT" || strings.HasSuffix(address, "443") {
if _, err = fmt.Fprint(client, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil {
return
}
} else {
if _, err = server.Write(b[:n]); err != nil {
return
}
}
go io.Copy(client, server)
io.Copy(server, client)
log.Printf("seq: %-8d <== request done. addr: %s, method: %s, target: %s \n",
seq, client.RemoteAddr(), method, address)
}