-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
157 lines (149 loc) · 3.89 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
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
package websocket
import (
"crypto/tls"
"log"
"net"
"strings"
"time"
)
type Server struct {
Config *Config
Stats *Stats
}
type Config struct {
Handshake HandshakeFunc
Addr string
CertFile string
KeyFile string
MaxMsgLen int
SockReadBuffer int
SockWriteBuffer int
HttpReadBuffer int
HttpWriteBuffer int
WsReadBuffer int
WsWriteBuffer int
IOStatistics bool
LogLevel uint8
CloseTimeout time.Duration
HandshakeReadTimeout time.Duration
HandshakeWriteTimeout time.Duration
TCPKeepAlive time.Duration
}
func NewServer(config Config) *Server {
if config.Handshake == nil {
panic("config.Handshake is not set")
}
if config.Addr == "" {
panic("config.Addr is not set")
}
if config.SockReadBuffer == 0 {
config.SockReadBuffer = DefaultSockReadBuffer
}
if config.SockWriteBuffer == 0 {
config.SockWriteBuffer = DefaultSockWriteBuffer
}
if config.HttpReadBuffer == 0 {
config.HttpReadBuffer = DefaultHttpReadBuffer
}
if config.HttpWriteBuffer == 0 {
config.HttpWriteBuffer = DefaultHttpWriteBuffer
}
if config.WsReadBuffer == 0 {
config.WsReadBuffer = DefaultWsReadBuffer
}
if config.WsWriteBuffer == 0 {
config.WsWriteBuffer = DefaultWsWriteBuffer
}
if config.MaxMsgLen == 0 {
config.MaxMsgLen = DefaultMaxMsgLen
}
if config.HandshakeReadTimeout == 0 {
config.HandshakeReadTimeout = DefaultHandshakeReadTimeout
}
if config.HandshakeWriteTimeout == 0 {
config.HandshakeWriteTimeout = DefaultHandshakeWriteTimeout
}
if config.CloseTimeout == 0 {
config.CloseTimeout = DefaultCloseTimeout
}
s := &Server{
Config: &config,
Stats: newStats(),
}
return s
}
func (s *Server) serve(ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("ERROR: Failed to accept connection %s", err)
time.Sleep(AcceptErrorTimeout)
continue
}
go func() {
wsc := newConnection(s, conn)
wsc.serve()
}()
}
}
func (s *Server) Serve() (err error) {
ln, err := net.Listen("tcp", s.Config.Addr)
if err != nil {
return err
}
s.serve(ln)
return
}
func (s *Server) ServeTLS() (err error) {
ln, err := net.Listen("tcp", s.Config.Addr)
if err != nil {
return err
}
if s.Config.CertFile == "" || s.Config.KeyFile == "" {
panic("cert-file or key-file not specified")
}
config := new(tls.Config)
certs := strings.Split(s.Config.CertFile, ",")
keyfiles := strings.Split(s.Config.KeyFile, ",")
minLen := len(certs)
if len(keyfiles) < minLen {
minLen = len(keyfiles)
}
config.Certificates = make([]tls.Certificate, minLen)
for i := 0; i < minLen; i++ {
config.Certificates[i], err = tls.LoadX509KeyPair(certs[i], keyfiles[i])
}
config.NextProtos = []string{"http/1.1"}
// select only strong ciphers from this list https://golang.org/pkg/crypto/tls/#pkg-constants
config.CipherSuites = []uint16{
// TLS 1.0 - 1.2 ciphers
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
// TLS 1.3 ciphers
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
}
if err != nil {
return err
}
tlsLn := tls.NewListener(ln, config)
s.serve(tlsLn)
return
}