-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
296 lines (246 loc) · 7.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
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
package main
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/CAFxX/httpcompression"
sddaemon "github.com/coreos/go-systemd/v22/daemon"
"github.com/felixge/httpsnoop"
drclient "github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/server"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
var logger = logging.Logger(name)
func withRequestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(next, w, r)
logger.Debugw(r.Method, "url", r.URL, "host", r.Host, "code", m.Code, "duration", m.Duration, "written", m.Written, "accept", r.Header.Get("Accept"), "ua", r.UserAgent(), "referer", r.Referer())
})
}
type config struct {
listenAddress string
acceleratedDHTClient bool
cachedAddrBook bool
cachedAddrBookActiveProbing bool
cachedAddrBookRecentTTL time.Duration
contentEndpoints []string
peerEndpoints []string
ipnsEndpoints []string
libp2pListenAddress []string
connMgrLow int
connMgrHi int
connMgrGrace time.Duration
maxMemory uint64
maxFD int
tracingAuth string
samplingFraction float64
}
func start(ctx context.Context, cfg *config) error {
h, err := newHost(cfg)
if err != nil {
return err
}
fmt.Printf("Someguy libp2p host listening on %v\n", h.Addrs())
var dhtRouting routing.Routing
if cfg.acceleratedDHTClient {
wrappedDHT, err := newBundledDHT(ctx, h)
if err != nil {
return err
}
dhtRouting = wrappedDHT
} else {
standardDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeClient), dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
if err != nil {
return err
}
dhtRouting = standardDHT
}
var cachedAddrBook *cachedAddrBook
if cfg.cachedAddrBook {
fmt.Printf("Using cached address book to speed up provider discovery (active probing enabled: %t)\n", cfg.cachedAddrBookActiveProbing)
opts := []AddrBookOption{}
if cfg.cachedAddrBookRecentTTL > 0 {
opts = append(opts, WithRecentlyConnectedTTL(cfg.cachedAddrBookRecentTTL))
}
opts = append(opts, WithActiveProbing(cfg.cachedAddrBookActiveProbing))
cachedAddrBook, err = newCachedAddrBook(opts...)
if err != nil {
return err
}
go cachedAddrBook.background(ctx, h)
}
crRouters, err := getCombinedRouting(cfg.contentEndpoints, dhtRouting, cachedAddrBook)
if err != nil {
return err
}
prRouters, err := getCombinedRouting(cfg.peerEndpoints, dhtRouting, cachedAddrBook)
if err != nil {
return err
}
ipnsRouters, err := getCombinedRouting(cfg.ipnsEndpoints, dhtRouting, cachedAddrBook)
if err != nil {
return err
}
_, port, err := net.SplitHostPort(cfg.listenAddress)
if err != nil {
return err
}
tp, err := setupTracing(ctx, cfg.samplingFraction)
if err != nil {
return err
}
defer func() {
_ = tp.Shutdown(ctx)
}()
handlerOpts := []server.Option{
server.WithPrometheusRegistry(prometheus.DefaultRegisterer),
}
handler := server.Handler(&composableRouter{
providers: crRouters,
peers: prRouters,
ipns: ipnsRouters,
}, handlerOpts...)
// Add CORS.
handler = cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet, http.MethodOptions},
MaxAge: 600,
}).Handler(handler)
// Add compression.
compress, err := httpcompression.DefaultAdapter()
if err != nil {
return err
}
handler = compress(handler)
// Add request logging.
handler = withRequestLogger(handler)
// Add request tracing
handler = withTracingAndDebug(handler, cfg.tracingAuth)
http.Handle("/", handler)
http.Handle("/debug/metrics/prometheus", promhttp.Handler())
http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Client: %s\n", name)
fmt.Fprintf(w, "Version: %s\n", version)
})
server := &http.Server{Addr: cfg.listenAddress, Handler: nil}
quit := make(chan os.Signal, 3)
var wg sync.WaitGroup
wg.Add(1)
fmt.Printf("Metrics endpoint: http://127.0.0.1:%s/debug/metrics/prometheus\n", port)
fmt.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1\n", port)
go func() {
defer wg.Done()
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("Failed to start /routing/v1 server: %v", err)
quit <- os.Interrupt
}
}()
sddaemon.SdNotify(false, sddaemon.SdNotifyReady)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
<-quit
sddaemon.SdNotify(false, sddaemon.SdNotifyStopping)
fmt.Printf("\nClosing /routing/v1 server...\n")
// Attempt a graceful shutdown
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Graceful shutdown failed:%+v\n", err)
}
go server.Close()
wg.Wait()
fmt.Println("Shutdown finished.")
return nil
}
func newHost(cfg *config) (host.Host, error) {
cmgr, err := connmgr.NewConnManager(cfg.connMgrLow, cfg.connMgrHi, connmgr.WithGracePeriod(cfg.connMgrGrace))
if err != nil {
return nil, err
}
rcmgr, err := makeResourceMgrs(cfg.maxMemory, cfg.maxFD, cfg.connMgrHi)
if err != nil {
return nil, err
}
opts := []libp2p.Option{
libp2p.UserAgent("someguy/" + buildVersion()),
libp2p.ConnectionManager(cmgr),
libp2p.ResourceManager(rcmgr),
libp2p.NATPortMap(),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.EnableHolePunching(),
}
if len(cfg.libp2pListenAddress) == 0 {
// Note: because the transports are set above we must also set the listen addresses
// We need to set listen addresses in order for hole punching to work
opts = append(opts, libp2p.DefaultListenAddrs)
} else {
opts = append(opts, libp2p.ListenAddrStrings(cfg.libp2pListenAddress...))
}
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
return h, nil
}
func getCombinedRouting(endpoints []string, dht routing.Routing, cachedAddrBook *cachedAddrBook) (router, error) {
var dhtRouter router
if cachedAddrBook != nil {
cachedRouter := NewCachedRouter(libp2pRouter{routing: dht}, cachedAddrBook)
dhtRouter = sanitizeRouter{cachedRouter}
} else {
dhtRouter = sanitizeRouter{libp2pRouter{routing: dht}}
}
if len(endpoints) == 0 {
return dhtRouter, nil
}
var delegatedRouters []router
for _, endpoint := range endpoints {
drclient, err := drclient.New(endpoint,
drclient.WithUserAgent("someguy/"+buildVersion()),
// override default filters, we want all results from remote endpoint, then someguy's user can use IPIP-484 to narrow them down
drclient.WithProtocolFilter([]string{}),
drclient.WithDisabledLocalFiltering(true),
)
if err != nil {
return nil, err
}
delegatedRouters = append(delegatedRouters, clientRouter{Client: drclient})
}
return parallelRouter{
routers: append(delegatedRouters, dhtRouter),
}, nil
}
func withTracingAndDebug(next http.Handler, authToken string) http.Handler {
next = otelhttp.NewHandler(next, "someguy.request")
// Remove tracing and cache skipping headers if not authorized
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Disable tracing/debug headers if auth token missing or invalid
if authToken == "" || request.Header.Get("Authorization") != authToken {
if request.Header.Get("Traceparent") != "" {
request.Header.Del("Traceparent")
}
if request.Header.Get("Tracestate") != "" {
request.Header.Del("Tracestate")
}
}
next.ServeHTTP(writer, request)
})
}