-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathrelay.go
66 lines (51 loc) · 2.71 KB
/
relay.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
// Copyright © 2022-2024 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1
package cmd
import (
"context"
libp2plog "github.com/ipfs/go-log/v2"
"github.com/spf13/cobra"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/cmd/relay"
)
func newRelayCmd(runFunc func(context.Context, relay.Config) error) *cobra.Command {
var config relay.Config
cmd := &cobra.Command{
Use: "relay",
Short: "Start a libp2p relay server",
Long: "Starts a libp2p circuit relay that charon clients can use to discover and connect to their peers.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { //nolint:revive // keep args variable name for clarity
if err := log.InitLogger(config.LogConfig); err != nil {
return err
}
libp2plog.SetPrimaryCore(log.LoggerCore()) // Set libp2p logger to use charon logger
printLicense(cmd.Context())
printFlags(cmd.Context(), cmd.Flags())
return runFunc(cmd.Context(), config)
},
}
bindDataDirFlag(cmd.Flags(), &config.DataDir)
bindRelayFlag(cmd, &config)
bindDebugMonitoringFlags(cmd, &config.MonitoringAddr, &config.DebugAddr, "")
bindP2PFlags(cmd, &config.P2PConfig)
bindLogFlags(cmd.Flags(), &config.LogConfig)
bindLokiFlags(cmd.Flags(), &config.LogConfig)
return cmd
}
func bindRelayFlag(cmd *cobra.Command, config *relay.Config) {
cmd.Flags().StringVar(&config.HTTPAddr, "http-address", "127.0.0.1:3640", "Listening address (ip and port) for the relay http server serving runtime ENR.")
cmd.Flags().BoolVar(&config.AutoP2PKey, "auto-p2pkey", true, "Automatically create a p2pkey (secp256k1 private key used for p2p authentication and ENR) if none found in data directory.")
cmd.Flags().StringVar(&config.LibP2PLogLevel, "p2p-relay-loglevel", "", "Libp2p circuit relay log level. E.g., debug, info, warn, error.")
// Decrease defaults after this has been addressed https://github.com/libp2p/go-libp2p/issues/1713
cmd.Flags().IntVar(&config.MaxResPerPeer, "p2p-max-reservations", 512, "Updates max circuit reservations per peer (each valid for 30min)")
cmd.Flags().IntVar(&config.MaxConns, "p2p-max-connections", 16384, "Libp2p maximum number of peers that can connect to this relay.")
var advertisePriv bool
cmd.Flags().BoolVar(&advertisePriv, "p2p-advertise-private-addresses", false, "Enable advertising of libp2p auto-detected private addresses. This doesn't affect manually provided p2p-external-ip/hostname.")
wrapPreRunE(cmd, func(*cobra.Command, []string) error {
// Invert p2p-advertise-private-addresses flag boolean:
// -- Do not ADVERTISE private addresses by default in the binary.
// -- Do not FILTER private addresses in unit tests.
config.FilterPrivAddrs = !advertisePriv
return nil
})
}