Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Aug 1, 2024
1 parent 354cc8e commit 5aca780
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 69 deletions.
1 change: 0 additions & 1 deletion nodebuilder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func ConstructModule(tp node.Type, network p2p.Network, cfg *Config, store Store
fx.Supply(tp),
fx.Supply(network),
fx.Supply(ks),
fx.Supply(p2p.TLSPath(tlsPath(store.Path()))),
fx.Provide(p2p.BootstrappersFor),
fx.Provide(func(lc fx.Lifecycle) context.Context {
return fxutil.WithLifecycle(context.Background(), lc)
Expand Down
15 changes: 15 additions & 0 deletions nodebuilder/p2p/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,18 @@ func (cfg *Config) Validate() error {
}
return nil
}

// Upgrade updates the `ListenAddresses` and `NoAnnounceAddresses` to
// include support for websocket connections.
func (cfg *Config) Upgrade() {
cfg.ListenAddresses = append(
cfg.ListenAddresses,
"/ip4/0.0.0.0/tcp/2122/wss",
"/ip6/::/tcp/2122/wss",
)
cfg.NoAnnounceAddresses = append(
cfg.NoAnnounceAddresses,
"/ip4/127.0.0.1/tcp/2122/wss",
"/ip6/::/tcp/2122/wss",
)
}
32 changes: 15 additions & 17 deletions nodebuilder/p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func (ua *UserAgent) String() string {
func host(params hostParams) (HostBase, error) {
ua := newUserAgent().WithNetwork(params.Net).WithNodeType(params.Tp)

wss, isEnabled, err := enableWss()
if err != nil {
return nil, err
}

if isEnabled {
params.Cfg.Upgrade()
}

opts := []libp2p.Option{
libp2p.NoListenAddrs, // do not listen automatically
libp2p.AddrsFactory(params.AddrF),
Expand All @@ -82,7 +91,12 @@ func host(params hostParams) (HostBase, error) {
libp2p.DisableRelay(),
libp2p.BandwidthReporter(params.Bandwidth),
libp2p.ResourceManager(params.ResourceManager),
enableTransport(params.Cfg, params.TLS),
libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(quic.NewTransport),
libp2p.Transport(webtransport.New),
wss,
),
// to clearly define what defaults we rely upon
libp2p.DefaultSecurity,
libp2p.DefaultMuxers,
Expand Down Expand Up @@ -111,28 +125,12 @@ func host(params hostParams) (HostBase, error) {
return h, nil
}

func enableTransport(cfg *Config, tls *tls) libp2p.Option {
options := []libp2p.Option{
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(quic.NewTransport),
libp2p.Transport(webtransport.New),
}

wsTransport := tls.transport()
if wsTransport != nil {
options = append(options, wsTransport)
tls.upgrade(cfg)
}
return libp2p.ChainOptions(options...)
}

type HostBase hst.Host

type hostParams struct {
fx.In

Cfg *Config
TLS *tls
Net Network
Lc fx.Lifecycle
ID peer.ID
Expand Down
3 changes: 0 additions & 3 deletions nodebuilder/p2p/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
baseComponents := fx.Options(
fx.Error(cfgErr),
fx.Supply(cfg),
fx.Provide(fx.Annotate(func(path TLSPath) (*tls, error) {
return tlsConfig(string(path))
})),
fx.Provide(Key),
fx.Provide(id),
fx.Provide(peerStore),
Expand Down
1 change: 0 additions & 1 deletion nodebuilder/p2p/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func testModule(tp node.Type) fx.Option {
ConstructModule(tp, &cfg),
fx.Provide(context.Background),
fx.Supply(Private),
fx.Supply(TLSPath("")),
fx.Supply(Bootstrappers{}),
fx.Supply(tp),
fx.Provide(keystore.NewMapKeystore),
Expand Down
60 changes: 13 additions & 47 deletions nodebuilder/p2p/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,38 @@ package p2p

import (
cfg "crypto/tls"

"github.com/libp2p/go-libp2p"
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
"os"

"github.com/celestiaorg/celestia-node/libs/utils"
)

var (
tlsPath = "TLS_PATH"
)

const (
cert = "/cert.pem"
key = "/key.pem"
)

// TLSPath is an alias of the file path of TLS certificates and keys.
type TLSPath string

type tls struct {
*cfg.Config
ListenAddresses []string
NoAnnounceAddresses []string
}
func enableWss() (libp2p.Option, bool, error) {
path := os.Getenv(tlsPath)
exist := utils.Exists(path+cert) && utils.Exists(path+key)
if !exist {
return libp2p.Transport(ws.New), exist, nil
}

func newTLS(path string) (*tls, error) {
var certificates []cfg.Certificate
if path != "" {
cert, err := cfg.LoadX509KeyPair(path+cert, path+key)
if err != nil {
return nil, err
return nil, false, err
}
certificates = append(certificates, cert)
}
config := &cfg.Config{MinVersion: cfg.VersionTLS12, Certificates: certificates}

return &tls{
Config: config,
ListenAddresses: []string{
"/ip4/0.0.0.0/tcp/2122/wss",
"/ip6/::/tcp/2122/wss",
},
NoAnnounceAddresses: []string{
"/ip4/127.0.0.1/tcp/2122/wss",
"/ip6/::/tcp/2122/wss",
},
}, nil
}

func tlsConfig(path string) (*tls, error) {
exist := utils.Exists(path+cert) && utils.Exists(path+key)
if !exist {
return newTLS("")
}

return newTLS(path)
}

func (tls *tls) upgrade(cfg *Config) {
if len(tls.Certificates) == 0 {
return
}

cfg.ListenAddresses = append(cfg.ListenAddresses, tls.ListenAddresses...)
cfg.NoAnnounceAddresses = append(cfg.NoAnnounceAddresses, tls.NoAnnounceAddresses...)
}

func (tls *tls) transport() libp2p.Option {
if len(tls.Config.Certificates) == 0 {
return nil
}
return libp2p.Transport(ws.New, ws.WithTLSConfig(tls.Config))
return libp2p.Transport(ws.New, ws.WithTLSConfig(config)), true, nil
}

0 comments on commit 5aca780

Please sign in to comment.