-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
477 lines (432 loc) · 13.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package eudore
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"log"
"math/big"
"net"
"net/http"
"net/http/fcgi"
"os"
"strings"
"sync"
"sync/atomic"
"time"
)
// Server defines the object that starts the http service.
type Server interface {
SetHandler(h http.Handler)
Serve(ln net.Listener) error
Shutdown(ctx context.Context) error
}
// ServerConfig defines the configuration used by [NewServer].
type ServerConfig struct {
// set default ServerHandler
Handler http.Handler `alias:"handler" json:"-" yaml:"-"`
// ReadTimeout is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// there will be no timeout.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout TimeDuration `alias:"readTimeout" json:"readTimeout" yaml:"readTimeout"`
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout TimeDuration `alias:"writeTimeout" json:"writeTimeout" yaml:"writeTimeout"`
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body. If zero, the value of
// ReadTimeout is used. If negative, or if zero and ReadTimeout
// is zero or negative, there is no timeout.
ReadHeaderTimeout TimeDuration `alias:"readHeaderTimeout" json:"readHeaderTimeout" yaml:"readHeaderTimeout"`
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If zero, the value
// of ReadTimeout is used. If negative, or if zero and ReadTimeout
// is zero or negative, there is no timeout.
IdleTimeout TimeDuration `alias:"idleTimeout" json:"idleTimeout" yaml:"idleTimeout"`
// MaxHeaderBytes controls the maximum number of bytes the
// server will read parsing the request header's keys and
// values, including the request line. It does not limit the
// size of the request body.
// If zero, DefaultMaxHeaderBytes is used.
MaxHeaderBytes int `alias:"maxHeaderBytes" json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
// ErrorLog specifies an optional logger for errors accepting
// connections, unexpected behavior from handlers, and
// underlying FileSystem errors.
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger `alias:"errorLog" json:"-" yaml:"-"`
// BaseContext optionally specifies a function that returns
// the base context for incoming requests on this server.
// The provided Listener is the specific Listener that's
// about to start accepting requests.
// If BaseContext is nil, the default is context.Background().
// If non-nil, it must return a non-nil context.
BaseContext func(net.Listener) context.Context `alias:"baseContext" json:"-" yaml:"-"`
// ConnContext optionally specifies a function that modifies
// the context used for a new connection c. The provided ctx
// is derived from the base context and has a ServerContextKey
// value.
ConnContext func(context.Context, net.Conn) context.Context `alias:"connContext" json:"-" yaml:"-"`
}
// serverStd defines using [http.Server] to start the http server.
type serverStd struct {
*http.Server `alias:"server"`
Mutex sync.Mutex `alias:"mutex"`
listener internalListener `alias:"listener"`
Ports []string `alias:"ports"`
Counter int64 `alias:"counter"`
}
type MetadataServer struct {
Health bool `json:"health" protobuf:"1,name=health" yaml:"health"`
Name string `json:"name" protobuf:"2,name=name" yaml:"name"`
Ports []string `json:"ports" protobuf:"3,name=ports" yaml:"ports"`
ErrorCount int64 `json:"errorCount" protobuf:"4,name=errorCount" yaml:"errorCount"`
}
// serverStd defines using [fcgi.Serve] to start the http server.
type serverFcgi struct {
http.Handler
sync.Mutex
listeners []net.Listener
}
// ServerListenConfig defines a common port listening configuration.
type ServerListenConfig struct {
Addr string `alias:"addr" json:"addr" yaml:"addr"`
HTTPS bool `alias:"https" json:"https" yaml:"https"`
HTTP2 bool `alias:"http2" json:"http2" yaml:"http2"`
Mutual bool `alias:"mutual" json:"mutual" yaml:"mutual"`
Certfile string `alias:"certfile" json:"certfile" yaml:"certfile"`
Keyfile string `alias:"keyfile" json:"keyfile" yaml:"keyfile"`
Trustfile string `alias:"trustfile" json:"trustfile" yaml:"trustfile"`
Certificate *x509.Certificate `alias:"certificate" json:"certificate" yaml:"certificate"`
}
// The NewServer function creates a [Server] implemented by warp [http.Server].
func NewServer(config *ServerConfig) Server {
if config == nil {
config = &ServerConfig{}
}
srv := &serverStd{
Server: &http.Server{
Handler: config.Handler,
ReadTimeout: GetAnyDefault(
time.Duration(config.ReadTimeout),
DefaultServerReadTimeout,
),
ReadHeaderTimeout: GetAnyDefault(
time.Duration(config.ReadHeaderTimeout),
DefaultServerReadHeaderTimeout,
),
WriteTimeout: GetAnyDefault(
time.Duration(config.WriteTimeout),
DefaultServerWriteTimeout,
),
IdleTimeout: GetAnyDefault(
time.Duration(config.IdleTimeout),
DefaultServerIdleTimeout,
),
MaxHeaderBytes: config.MaxHeaderBytes,
ErrorLog: config.ErrorLog,
BaseContext: config.BaseContext,
ConnContext: config.ConnContext,
},
}
// fix http2 server in golang ?-1.22
// https://github.com/golang/go/issues/65785
if srv.Server.ReadTimeout < 0 {
srv.Server.ReadTimeout = 0
}
if srv.Server.ReadHeaderTimeout < 0 {
srv.Server.ReadHeaderTimeout = 0
}
if srv.Server.WriteTimeout < 0 {
srv.Server.WriteTimeout = 0
}
if srv.Server.IdleTimeout < 0 {
srv.Server.IdleTimeout = 0
}
return srv
}
// The Mount method gets [ContextKeyHTTPHandler] or [ContextKeyApp] from
// [context.Context] as [http.Handler],
//
// Get [ContextKeyApp] or [ContextKeyLogger] as [Logger] to
// receive [http.Server.ErrorLog].
func (srv *serverStd) Mount(ctx context.Context) {
if srv.Handler == nil {
for _, key := range [...]any{ContextKeyHTTPHandler, ContextKeyApp} {
h, ok := ctx.Value(key).(http.Handler)
if ok {
srv.SetHandler(h)
break
}
}
}
if srv.ErrorLog == nil {
// Capture the error content output by net/http.Server.
for _, key := range [...]any{ContextKeyApp, ContextKeyLogger} {
logger, ok := ctx.Value(key).(Logger)
if ok {
out := &serverLogger{
Logger: logger,
Counter: &srv.Counter,
}
srv.ErrorLog = log.New(out, "", 0)
break
}
}
}
}
// Unmount method waits for [DefaulerServerShutdownWait] to use
// [http.Server.Shutdown] to shut down [Server] listening.
func (srv *serverStd) Unmount(context.Context) {
ctx, cancel := context.WithTimeout(context.Background(),
DefaultServerShutdownWait,
)
defer cancel()
_ = srv.Shutdown(ctx)
}
func (srv *serverStd) SetHandler(h http.Handler) {
srv.Mutex.Lock()
defer srv.Mutex.Unlock()
srv.Server.Handler = h
}
func (srv *serverStd) Serve(ln net.Listener) error {
srv.Mutex.Lock()
srv.Ports = append(srv.Ports, ln.Addr().String())
srv.Mutex.Unlock()
err := srv.Server.Serve(ln)
if errors.Is(err, http.ErrServerClosed) {
err = nil
}
return err
}
// The ServeConn method handles a [net.Conn].
//
// Implement [net.Listen] to pass [net.Conn] to [http.Servr].
func (srv *serverStd) ServeConn(conn net.Conn) {
srv.Mutex.Lock()
if srv.listener.Ch == nil {
srv.listener.Ch = make(chan net.Conn)
srv.Ports = append(srv.Ports, srv.listener.Addr().String())
go func() {
_ = srv.Server.Serve(&srv.listener)
}()
}
srv.Mutex.Unlock()
srv.listener.Ch <- conn
}
// The Metadata method returns [MetadataServer].
func (srv *serverStd) Metadata() any {
srv.Mutex.Lock()
defer srv.Mutex.Unlock()
return MetadataServer{
Health: true,
Name: "eudore.serverStd",
Ports: srv.Ports,
ErrorCount: atomic.LoadInt64(&srv.Counter),
}
}
type serverLogger struct {
Logger Logger
Counter *int64
}
func (srv *serverLogger) Write(p []byte) (n int, err error) {
atomic.AddInt64(srv.Counter, 1)
log := srv.Logger.WithField(ParamDepth, DefaultLoggerDepthKindDisable).
WithField(ParamCaller, "serverStd.ErrorLog")
strs := strings.Split(string(p), "\n")
if strings.HasPrefix(strs[0], "http: panic serving ") {
lines := []string{}
for i := 2; i < len(strs)-1; i += 2 {
if strings.HasPrefix(strs[i], "created by ") {
strs[i] = strs[i][11:]
} else {
end := strings.LastIndexByte(strs[i], '(')
if end != -1 {
strs[i] = strs[i][:end]
}
}
pos := strings.IndexByte(strs[i+1], ' ')
if pos != -1 {
strs[i+1] = strs[i+1][:pos]
}
lines = append(lines,
strings.TrimPrefix(strs[i+1], "\t")+" "+strs[i],
)
}
log.WithField("stack", lines).Errorf("%s %s",
strs[0], strs[1][:len(strs[1])-1],
)
} else {
log.Errorf(strs[0])
}
return 0, nil
}
type internalListener struct {
Ch chan net.Conn
close bool
}
func (ln *internalListener) Accept() (net.Conn, error) {
for conn := range ln.Ch {
if conn != nil {
return conn, nil
}
}
return nil, http.ErrServerClosed
}
func (ln *internalListener) Close() error {
if !ln.close {
close(ln.Ch)
ln.close = true
}
return nil
}
func (ln *internalListener) Addr() net.Addr {
return &net.IPAddr{
IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1},
}
}
// The NewServerFcgi function creates a [Server] using [fcgi.Serve].
func NewServerFcgi() Server {
return &serverFcgi{}
}
// The Mount method gets [ContextKeyHTTPHandler] or [ContextKeyApp] from
// [context.Context] as [http.Handler].
func (srv *serverFcgi) Mount(ctx context.Context) {
if srv.Handler == nil {
for _, key := range [...]any{ContextKeyHTTPHandler, ContextKeyApp} {
h, ok := ctx.Value(key).(http.Handler)
if ok {
srv.SetHandler(h)
break
}
}
}
}
// Unmount method waits for [DefaulerServerShutdownWait] shuts down all
// fcgi listeners.
func (srv *serverFcgi) Unmount(context.Context) {
ctx, cancel := context.WithTimeout(context.Background(),
DefaultServerShutdownWait,
)
defer cancel()
_ = srv.Shutdown(ctx)
}
func (srv *serverFcgi) SetHandler(h http.Handler) {
srv.Handler = h
}
func (srv *serverFcgi) Serve(ln net.Listener) error {
srv.Lock()
srv.listeners = append(srv.listeners, ln)
srv.Unlock()
return fcgi.Serve(ln, srv.Handler)
}
// The Shutdown method shuts down all fcgi listeners.
func (srv *serverFcgi) Shutdown(context.Context) error {
srv.Lock()
defer srv.Unlock()
var errs mulitError
for _, ln := range srv.listeners {
errs.Handle(ln.Close())
}
return errs.Unwrap()
}
// The Listen method uses the port configuration to create a listener,
// and uses Certificate to save the parsed TLS certificate.
//
// If https is enabled but there is no certificate, a private certificate will
// be created.
func (slc *ServerListenConfig) Listen() (net.Listener, error) {
// set default port
if slc.Addr == "" {
if slc.HTTPS {
slc.Addr = ":80"
} else {
slc.Addr = ":443"
}
}
if !slc.HTTPS {
return DefaultServerListen("tcp", slc.Addr)
}
// set tls
config := &tls.Config{
NextProtos: []string{"http/1.1"},
Certificates: make([]tls.Certificate, 1),
}
if slc.HTTP2 {
config.NextProtos = []string{"h2"}
}
cert, ca, err := loadCertificate(slc.Certfile, slc.Keyfile)
if err != nil {
return nil, err
}
config.Certificates[0], slc.Certificate = cert, ca
// set mutual tls
if slc.Mutual {
data, err := os.ReadFile(slc.Trustfile)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(data)
config.ClientCAs = pool
config.ClientAuth = tls.RequireAndVerifyClientCert
}
ln, err := DefaultServerListen("tcp", slc.Addr)
if err != nil {
return nil, err
}
return tls.NewListener(ln, config), nil
}
func loadCertificate(cret, key string) (tls.Certificate, *x509.Certificate,
error,
) {
if cret != "" && key != "" {
cret509, err := tls.LoadX509KeyPair(cret, key)
if err != nil {
return cret509, nil, err
}
ca, _ := x509.ParseCertificate(cret509.Certificate[0])
return cret509, ca, err
}
ca := &x509.Certificate{
SerialNumber: big.NewInt(1653),
Subject: pkix.Name{
Country: []string{"China"},
Organization: []string{"eudore"},
OrganizationalUnit: []string{"eudore"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 5},
BasicConstraintsValid: true,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageClientAuth,
x509.ExtKeyUsageServerAuth,
},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
DNSNames: []string{"localhost"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
pool := x509.NewCertPool()
pool.AddCert(ca)
priv, _ := rsa.GenerateKey(rand.Reader, 2048)
caByte, err := x509.CreateCertificate(rand.Reader, ca, ca,
&priv.PublicKey, priv,
)
return tls.Certificate{
Certificate: [][]byte{caByte},
PrivateKey: priv,
}, ca, err
}