diff --git a/config/config.go b/config/config.go index d9d4ae8..b793e43 100644 --- a/config/config.go +++ b/config/config.go @@ -19,7 +19,7 @@ import ( // package. type Config struct { Workspace string - ListenAddrs []ma.Multiaddr + ListenAddrs ma.Multiaddr ConnManager connmgr.ConnManager } diff --git a/core/node.go b/core/node.go index 8c02a3e..2d347ea 100644 --- a/core/node.go +++ b/core/node.go @@ -11,6 +11,7 @@ import ( "context" "crypto/rand" "errors" + "fmt" "log" "os" "path/filepath" @@ -48,6 +49,7 @@ type Node struct { host host.Host // lib-p2p host workspace string // data privatekeyPath string + multiaddr string } // NewBasicNode constructs a new *Node @@ -57,7 +59,7 @@ type Node struct { // privatekeypath: private key file // If it's empty, automatically created in the program working directory // If it's a directory, it will be created in the specified directory -func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath string) (*Node, error) { +func NewBasicNode(multiaddr ma.Multiaddr, workspace string, privatekeypath string) (*Node, error) { if multiaddr == nil || workspace == "" { return nil, errors.New("invalid parameter") } @@ -68,7 +70,7 @@ func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath str } host, err := libp2p.New( - libp2p.ListenAddrs(multiaddr...), + libp2p.ListenAddrs(multiaddr), libp2p.Identity(prvKey), yamuxOpt, mplexOpt, @@ -86,6 +88,7 @@ func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath str host: host, workspace: workspace, privatekeyPath: privatekeypath, + multiaddr: fmt.Sprintf("%s/p2p/%s", multiaddr.String(), host.ID()), } return n, nil } @@ -127,6 +130,10 @@ func (n Node) Workspace() string { return n.workspace } +func (n Node) Multiaddr() string { + return n.multiaddr +} + func (n Node) ID() peer.ID { return n.host.ID() } diff --git a/defaults.go b/defaults.go index 422124e..479a628 100644 --- a/defaults.go +++ b/defaults.go @@ -11,24 +11,13 @@ import ( "os" "github.com/libp2p/go-libp2p/p2p/net/connmgr" - "github.com/multiformats/go-multiaddr" ) // DefaultListenAddrs configures libp2p to use default listen address. var DefaultListenAddrs = func(cfg *Config) error { - addrs := []string{ - "/ip4/0.0.0.0/tcp/0", - "/ip6/::/tcp/0", - } - listenAddrs := make([]multiaddr.Multiaddr, 0, len(addrs)) - for _, s := range addrs { - addr, err := multiaddr.NewMultiaddr(s) - if err != nil { - return err - } - listenAddrs = append(listenAddrs, addr) - } - return cfg.Apply(ListenAddrs(listenAddrs...)) + ip := "0.0.0.0" + port := 80 + return cfg.Apply(ListenAddrStrings(ip, port)) } // DefaultListenAddrs configures libp2p to use default listen address. diff --git a/examples/readfile/example_readfile.go b/examples/readfile/example_readfile.go index 1f819fa..1f53d8d 100644 --- a/examples/readfile/example_readfile.go +++ b/examples/readfile/example_readfile.go @@ -28,9 +28,7 @@ func main() { // To construct a simple host with all the default settings, just use `New` h1, err := p2pgo.New( ".private1", - p2pgo.ListenAddrStrings( - fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort1), // regular tcp connections - ), + p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort1), // regular tcp connections ) if err != nil { panic(err) @@ -42,9 +40,7 @@ func main() { // To construct a simple host with all the default settings, just use `New` h2, err := p2pgo.New( ".private2", - p2pgo.ListenAddrStrings( - fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort2), // regular tcp connections - ), + p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort2), // regular tcp connections ) if err != nil { panic(err) diff --git a/examples/writefile/example_writefile.go b/examples/writefile/example_writefile.go index faaab83..1cfa76f 100644 --- a/examples/writefile/example_writefile.go +++ b/examples/writefile/example_writefile.go @@ -28,9 +28,7 @@ func main() { // To construct a simple host with all the default settings, just use `New` h1, err := p2pgo.New( ".private1", - p2pgo.ListenAddrStrings( - fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort1), // regular tcp connections - ), + p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort1), // regular tcp connections ) if err != nil { panic(err) @@ -42,9 +40,7 @@ func main() { // To construct a simple host with all the default settings, just use `New` h2, err := p2pgo.New( ".private2", - p2pgo.ListenAddrStrings( - fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort2), // regular tcp connections - ), + p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort2), // regular tcp connections ) if err != nil { panic(err) diff --git a/options.go b/options.go index c3042f6..5429acf 100644 --- a/options.go +++ b/options.go @@ -16,23 +16,21 @@ import ( // ListenAddrStrings configures libp2p to listen on the given (unparsed) // addresses. -func ListenAddrStrings(s ...string) Option { +func ListenAddrStrings(ip string, port int) Option { return func(cfg *Config) error { - for _, addrstr := range s { - a, err := ma.NewMultiaddr(addrstr) - if err != nil { - return err - } - cfg.ListenAddrs = append(cfg.ListenAddrs, a) + a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port)) + if err != nil { + return err } + cfg.ListenAddrs = a return nil } } // ListenAddrs configures libp2p to listen on the given addresses. -func ListenAddrs(addrs ...ma.Multiaddr) Option { +func ListenAddrs(addrs ma.Multiaddr) Option { return func(cfg *Config) error { - cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...) + cfg.ListenAddrs = addrs return nil } }