-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
80 lines (74 loc) · 1.47 KB
/
Copy pathserver.go
File metadata and controls
80 lines (74 loc) · 1.47 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
package main
import (
"bytes"
"fmt"
"log"
"net"
"net/url"
"strings"
)
func startServer(serverPort uint16) {
address := fmt.Sprintf("0.0.0.0:%d", serverPort)
log.Printf("start server on %s\n", address)
listen, err := net.Listen("tcp", address)
if errPrint(err) {
log.Panic(err)
}
for {
conn, err := listen.Accept()
if errPrint(err) {
continue
}
go serve(conn)
}
}
func serve(client net.Conn) {
defer func(client net.Conn) {
_ = client.Close()
}(client)
buf, err := DecryptUnpackOne(client)
if checkNetError(err) {
return
}
var method, host, address string
_, err = fmt.Sscanf(string(buf[:bytes.IndexByte(buf[:], '\n')]), "%s%s", &method, &host)
if errPrint(err) {
return
}
hostPortURL, err := url.Parse(host)
if errPrint(err) {
return
}
if hostPortURL.Opaque == "443" {
address = hostPortURL.Scheme + ":443"
} else {
if strings.Index(hostPortURL.Host, ":") == -1 {
address = hostPortURL.Host + ":80"
} else {
address = hostPortURL.Host
}
}
server, err := net.Dial("tcp", address)
if errPrint(err) {
return
}
if method == "CONNECT" {
_, _ = client.Write(EncryptPack([]byte("HTTP/1.1 200 Connection established\r\n\r\n")))
} else {
cnt := 0
for i, b := range buf {
if b == '/' {
cnt++
if cnt == 3 {
_, err = server.Write(append(buf[:len(method)+1], buf[i:]...))
if errPrint(err) {
return
}
break
}
}
}
}
go transformIoDecrypt(server, client)
transformIoEncrypt(client, server)
}