-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
119 lines (103 loc) · 2.49 KB
/
server.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
package http
import (
"bytes"
"github.com/valyala/fasthttp"
"io"
"log"
"net"
"os"
"strings"
"sync"
"time"
)
var L = log.New(os.Stderr, "http: ", log.Lshortfile|log.LstdFlags)
var bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 1024)
},
}
type closeWriter interface {
CloseWrite() error
}
type connector struct {
remoteAddr string
}
func newConnector(host string) connector {
_, _, err := net.SplitHostPort(host)
if err != nil && strings.Contains(err.Error(), "missing port in address") {
host += ":443"
}
return connector{
remoteAddr: host,
}
}
func (c connector) connect(src net.Conn) {
start := time.Now()
dst, err := net.Dial("tcp", c.remoteAddr)
if err != nil {
L.Printf("Dial: %s\n", err.Error())
src.Write([]byte("HTTP/1.1 500 proxy error\r\n\r\n"))
return
}
defer dst.Close()
// Proxy is no need to know anything, just exchange data between the client
// the the remote server.
copyAndWait := func(dst, src net.Conn, c chan int64) {
buf := bufPool.Get().([]byte) // smaller buf smaller latency
n, err := io.CopyBuffer(dst, src, buf)
bufPool.Put(buf)
if err != nil {
L.Printf("Copy: %s\n", err.Error())
// FIXME: how to report error to dst ?
}
if tcpConn, ok := dst.(closeWriter); ok {
tcpConn.CloseWrite()
}
c <- n
}
// client to remote
stod := make(chan int64)
go copyAndWait(dst, src, stod)
// remote to client
dtos := make(chan int64)
go copyAndWait(src, dst, dtos)
var nstod, ndtos int64
for i := 0; i < 2; {
select {
case nstod = <-stod:
i++
case ndtos = <-dtos:
i++
}
}
d := BeautifyDuration(time.Since(start))
L.Printf("CLOSE %s after %s ->%s <-%s\n",
c.remoteAddr, d, BeautifySize(nstod), BeautifySize(ndtos))
}
func doHttp(ctx *fasthttp.RequestCtx) {
header := ctx.Request.Header
header.Del("Proxy-Connection")
header.Del("Connection")
err := fasthttp.Do(&ctx.Request, &ctx.Response)
if err != nil {
log.Printf("do http failed: %v\n", err)
}
}
func ServeFastHTTP(ctx *fasthttp.RequestCtx) {
L.Printf("%s %s\n", ctx.Method(), ctx.RequestURI())
if bytes.Equal(ctx.Method(), []byte("CONNECT")) {
var c = newConnector(string(ctx.URI().Host()))
ctx.Hijack(c.connect)
ctx.Write([]byte{}) // close stream and do hijack
} else if uri := ctx.URI(); len(uri.Host()) > 0 {
doHttp(ctx)
} else {
L.Printf("%s is not a full URL path\n", ctx.URI())
}
}
func ServeConn(conn net.Conn) {
err := fasthttp.ServeConn(conn, ServeFastHTTP)
if err != nil {
log.Printf("serve failed: %v\n", err)
}
}