Skip to content

Commit

Permalink
do not rely on upstream fallback, close streams by default
Browse files Browse the repository at this point in the history
Signed-off-by: mudler <[email protected]>
  • Loading branch information
mudler committed Oct 25, 2023
1 parent ca52d04 commit 9c4ce7b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
75 changes: 74 additions & 1 deletion pkg/node/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,84 @@ func (e *Node) genHost(ctx context.Context) (host.Host, error) {
opts = append(opts, libp2p.NoSecurity)
}

opts = append(opts, libp2p.FallbackDefaults)
opts = append(opts, FallbackDefaults)

return libp2p.New(opts...)
}

// FallbackDefaults applies default options to the libp2p node if and only if no
// other relevant options have been applied. will be appended to the options
// passed into New.
var FallbackDefaults libp2p.Option = func(cfg *libp2p.Config) error {
for _, def := range defaults {
if !def.fallback(cfg) {
continue
}
if err := cfg.Apply(def.opt); err != nil {
return err
}
}
return nil
}

// Complete list of default options and when to fallback on them.
//
// Please *DON'T* specify default options any other way. Putting this all here
// makes tracking defaults *much* easier.
var defaults = []struct {
fallback func(cfg *libp2p.Config) bool
opt libp2p.Option
}{
{
fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
opt: libp2p.DefaultListenAddrs,
},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.PSK == nil },
opt: libp2p.DefaultTransports,
},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.PSK != nil },
opt: libp2p.DefaultPrivateTransports,
},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.Muxers == nil },
opt: libp2p.DefaultMuxers,
},
{
fallback: func(cfg *libp2p.Config) bool { return !cfg.Insecure && cfg.SecurityTransports == nil },
opt: libp2p.DefaultSecurity,
},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.PeerKey == nil },
opt: libp2p.RandomIdentity,
},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.Peerstore == nil },
opt: libp2p.DefaultPeerstore,
},
{
fallback: func(cfg *libp2p.Config) bool { return !cfg.RelayCustom },
opt: libp2p.DefaultEnableRelay,
},
//{
// fallback: func(cfg *libp2p.Config) bool { return cfg.ResourceManager == nil },
// opt: libp2p.DefaultResourceManager,
//},
//{
// fallback: func(cfg *libp2p.Config) bool { return cfg.ConnManager == nil },
// opt: libp2p.DefaultConnectionManager,
//},
{
fallback: func(cfg *libp2p.Config) bool { return cfg.MultiaddrResolver == nil },
opt: libp2p.DefaultMultiaddrResolver,
},
//{
// fallback: func(cfg *libp2p.Config) bool { return !cfg.DisableMetrics && cfg.PrometheusRegisterer == nil },
// opt: libp2p.DefaultPrometheusRegisterer,
//},
}

func (e *Node) sealkey() string {
return internalCrypto.MD5(internalCrypto.TOTP(sha256.New, e.config.SealKeyLength, e.config.SealKeyInterval, e.config.ExchangeKey))
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func streamHandler(l *blockchain.Ledger, ifce *water.Interface, c *Config, nc no
if err != nil {
stream.Reset()
}
if c.lowProfile {
stream.Close()
}
stream.Close()
}
}

Expand Down Expand Up @@ -260,15 +258,13 @@ func handleFrame(mgr streamManager, frame ethernet.Frame, c *Config, n *node.Nod
if err != nil {
return fmt.Errorf("could not open stream to %s: %w", d.String(), err)
}
defer stream.Close()

if mgr != nil {
mgr.Connected(n.Host().Network(), stream)
}

_, err = stream.Write(frame)
if c.lowProfile {
return stream.Close()
}
return err
}

Expand Down

0 comments on commit 9c4ce7b

Please sign in to comment.