2018-03-13 17:16:15 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
libp2p "github.com/libp2p/go-libp2p"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
|
|
|
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
|
|
|
|
pnet "github.com/libp2p/go-libp2p-pnet"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
2018-03-20 18:51:57 +00:00
|
|
|
// NewClusterHost creates a libp2p Host with the options from the
|
2018-03-13 17:16:15 +00:00
|
|
|
// provided cluster configuration.
|
|
|
|
func NewClusterHost(ctx context.Context, cfg *Config) (host.Host, error) {
|
|
|
|
var prot ipnet.Protector
|
2018-03-15 15:41:06 +00:00
|
|
|
var err error
|
2018-03-13 17:16:15 +00:00
|
|
|
|
|
|
|
// Create protector if we have a secret.
|
|
|
|
if cfg.Secret != nil && len(cfg.Secret) > 0 {
|
2018-03-15 15:41:06 +00:00
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], cfg.Secret)
|
|
|
|
prot, err = pnet.NewV1ProtectorFromBytes(&key)
|
2018-03-13 17:16:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:52:49 +00:00
|
|
|
return libp2p.New(
|
|
|
|
ctx,
|
2018-03-13 17:16:15 +00:00
|
|
|
libp2p.Identity(cfg.PrivateKey),
|
|
|
|
libp2p.ListenAddrs([]ma.Multiaddr{cfg.ListenAddr}...),
|
|
|
|
libp2p.PrivateNetwork(prot),
|
|
|
|
// FIXME: Enable when libp2p >= 5.0.16
|
|
|
|
// https://github.com/libp2p/go-libp2p/pull/293
|
|
|
|
//libp2p.NATPortMap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeProtectorKey converts a byte slice to its hex string representation.
|
|
|
|
func EncodeProtectorKey(secretBytes []byte) string {
|
|
|
|
return hex.EncodeToString(secretBytes)
|
|
|
|
}
|