/
listener.go
184 lines (160 loc) 路 4.59 KB
/
listener.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
package net
import (
"context"
"crypto/tls"
"net"
"net/http"
"github.com/drand/drand/log"
"github.com/drand/drand/metrics"
"github.com/drand/drand/protobuf/drand"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
http_grpc "github.com/weaveworks/common/httpgrpc"
http_grpc_server "github.com/weaveworks/common/httpgrpc/server"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func registerGRPCMetrics() {
if err := metrics.PrivateMetrics.Register(grpc_prometheus.DefaultServerMetrics); err != nil {
log.DefaultLogger().Warn("grpc Listener", "failed metrics registration", "err", err)
}
}
// NewGRPCListenerForPrivate creates a new listener for the Public and Protocol APIs over GRPC.
func NewGRPCListenerForPrivate(
ctx context.Context,
bindingAddr, certPath, keyPath string,
s Service,
insecure bool,
opts ...grpc.ServerOption) (Listener, error) {
lis, err := net.Listen("tcp", bindingAddr)
if err != nil {
return nil, err
}
if !insecure {
grpcCreds, err := credentials.NewServerTLSFromFile(certPath, keyPath)
if err != nil {
return nil, err
}
opts = append(opts, grpc.Creds(grpcCreds))
}
opts = append(opts,
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor))
grpcServer := grpc.NewServer(opts...)
drand.RegisterPublicServer(grpcServer, s)
drand.RegisterProtocolServer(grpcServer, s)
var g Listener
if insecure {
g = &grpcListener{
Service: s,
grpcServer: grpcServer,
lis: lis,
}
} else {
x509KeyPair, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
gr := &restListener{
restServer: buildTLSServer(grpcServer, &x509KeyPair),
}
gr.lis = tls.NewListener(lis, gr.restServer.TLSConfig)
g = gr
}
http_grpc.RegisterHTTPServer(grpcServer, http_grpc_server.NewServer(metrics.GroupHandler()))
grpc_prometheus.Register(grpcServer)
registerGRPCMetrics()
return g, nil
}
// NewRESTListenerForPublic creates a new listener for the Public API over REST with TLS.
func NewRESTListenerForPublic(
ctx context.Context,
bindingAddr, certPath, keyPath string,
handler http.Handler,
insecure bool) (Listener, error) {
lis, err := net.Listen("tcp", bindingAddr)
if err != nil {
return nil, err
}
g := &restListener{
lis: lis,
}
if insecure {
g.restServer = &http.Server{
Addr: bindingAddr,
Handler: handler,
}
} else {
x509KeyPair, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
g.restServer = buildTLSServer(handler, &x509KeyPair)
g.lis = tls.NewListener(lis, g.restServer.TLSConfig)
}
return g, nil
}
func buildTLSServer(httpHandler http.Handler, x509KeyPair *tls.Certificate) *http.Server {
return &http.Server{
Handler: httpHandler,
TLSConfig: &tls.Config{
// From https://blog.cloudflare.com/exposing-go-on-the-internet/
// Causes servers to use Go's default ciphersuite preferences,
// which are tuned to avoid attacks. Does nothing on clients.
PreferServerCipherSuites: true,
// Only use curves which have assembly implementations
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
// Drand clients and servers are all modern software, and so we
// can require TLS 1.2 and the best cipher suites.
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
// End Cloudflare recommendations.
Certificates: []tls.Certificate{*x509KeyPair},
NextProtos: []string{"h2"},
},
}
}
type restListener struct {
restServer *http.Server
lis net.Listener
}
func (g *restListener) Addr() string {
return g.lis.Addr().String()
}
func (g *restListener) Start() {
_ = g.restServer.Serve(g.lis)
}
func (g *restListener) Stop(ctx context.Context) {
if err := g.lis.Close(); err != nil {
log.DefaultLogger().Debug("grpc listener", "grpc shutdown", "err", err)
}
if err := g.restServer.Shutdown(ctx); err != nil {
log.DefaultLogger().Debug("grpc listener", "http shutdown", "err", err)
}
}
type grpcListener struct {
Service
grpcServer *grpc.Server
lis net.Listener
}
func (g *grpcListener) Addr() string {
return g.lis.Addr().String()
}
func (g *grpcListener) Start() {
go func() {
_ = g.grpcServer.Serve(g.lis)
}()
}
func (g *grpcListener) Stop(ctx context.Context) {
_ = g.lis.Close()
g.grpcServer.Stop()
}