-
Notifications
You must be signed in to change notification settings - Fork 655
/
server.go
93 lines (77 loc) · 1.84 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
package goflyway
import (
"bytes"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/coyove/goflyway/toh"
. "github.com/coyove/goflyway/v"
)
type commonConfig struct {
WriteBuffer int64
Key string
Timeout time.Duration
Stat *Traffic
}
func (config *commonConfig) check() {
if config.Timeout == 0 {
config.Timeout = time.Second * 15
}
if config.WriteBuffer == 0 {
config.WriteBuffer = 1024 * 1024 // 1M
}
}
type ServerConfig struct {
commonConfig
ProxyPassAddr string
SpeedThrot *TokenBucket
}
func NewServer(listen string, config *ServerConfig) error {
config.check()
rp := append([]toh.Option{}, toh.WithMaxWriteBuffer(int(config.WriteBuffer)))
if config.ProxyPassAddr != "" {
if strings.HasPrefix(config.ProxyPassAddr, "http") {
u, err := url.Parse(config.ProxyPassAddr)
if err != nil {
return err
}
rp = append(rp, toh.WithBadRequest(httputil.NewSingleHostReverseProxy(u).ServeHTTP))
} else {
rp = append(rp, toh.WithBadRequest(http.FileServer(http.Dir(config.ProxyPassAddr)).ServeHTTP))
}
}
listener, err := toh.Listen(config.Key, listen, rp...)
if err != nil {
return err
}
for {
conn, err := listener.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
down := toh.NewBufConn(conn)
defer down.Close()
buf, err := down.ReadBytes('\n')
if err != nil || len(buf) < 2 {
Vprint(err)
return
}
host := string(bytes.TrimRight(buf, "\n"))
dialstart := time.Now()
up, err := net.DialTimeout("tcp", host, config.Timeout)
if err != nil {
Vprint(host, err)
down.Write([]byte(err.Error() + "\n"))
return
}
Vprint("dial ", host, " in ", time.Since(dialstart).Nanoseconds()/1e6, "ms")
defer up.Close()
down.Write([]byte("OK\n"))
Bridge(up, down, config.SpeedThrot, config.Stat)
}(conn)
}
}