Skip to content

Commit

Permalink
Merge pull request #574 from vkost/vkost/announce-addr
Browse files Browse the repository at this point in the history
Announce Address - allows running ThreadsDB behind a NAT
  • Loading branch information
sanderpick authored Jan 11, 2022
2 parents 3be6a41 + afc9f08 commit a512e0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
23 changes: 20 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,24 @@ func DefaultNetwork(opts ...NetOption) (NetBoostrapper, error) {
return nil, fin.Cleanup(err)
}

libp2pOptions := []libp2p.Option{
libp2p.Peerstore(pstore),
libp2p.ConnectionManager(config.ConnManager),
libp2p.DisableRelay(),
}
if config.AnnounceAddr != nil {
libp2pOptions = append(libp2pOptions, libp2p.AddrsFactory(func([]ma.Multiaddr) []ma.Multiaddr {
return []ma.Multiaddr{config.AnnounceAddr}
}))
}

h, d, err := ipfslite.SetupLibp2p(
ctx,
hostKey,
nil,
[]ma.Multiaddr{config.HostAddr},
litestore,
libp2p.Peerstore(pstore),
libp2p.ConnectionManager(config.ConnManager),
libp2p.DisableRelay(),
libp2pOptions...,
)
if err != nil {
return nil, fin.Cleanup(err)
Expand Down Expand Up @@ -292,6 +301,7 @@ type NetConfig struct {
MongoUri string
MongoDB string
HostAddr ma.Multiaddr
AnnounceAddr ma.Multiaddr
ConnManager cconnmgr.ConnManager
GRPCServerOptions []grpc.ServerOption
GRPCDialOptions []grpc.DialOption
Expand Down Expand Up @@ -388,6 +398,13 @@ func WithNetDebug(enabled bool) NetOption {
}
}

func WithAnnounceAddr(addr ma.Multiaddr) NetOption {
return func(c *NetConfig) error {
c.AnnounceAddr = addr
return nil
}
}

type netBoostrapper struct {
app.Net
litepeer *ipfslite.Peer
Expand Down
14 changes: 14 additions & 0 deletions threadsd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {

repo := fs.String("repo", ".threads", "Repo location")
hostAddrStr := fs.String("hostAddr", "/ip4/0.0.0.0/tcp/4006", "Libp2p host bind address")
announceAddrStr := fs.String("announceAddr", "", "Libp2p announce address") // Should be supplied as multiaddr, /ip4/<your_public_ip>/tcp/4006
apiAddrStr := fs.String("apiAddr", "/ip4/127.0.0.1/tcp/6006", "gRPC API bind address")
apiProxyAddrStr := fs.String("apiProxyAddr", "/ip4/127.0.0.1/tcp/6007", "gRPC API web proxy bind address")
connLowWater := fs.Uint("connLowWater", 100, "Low watermark of libp2p connections that'll be maintained")
Expand All @@ -60,6 +61,13 @@ func main() {
if err != nil {
log.Fatal(err)
}
var announceAddr ma.Multiaddr
if *announceAddrStr != "" {
announceAddr, err = ma.NewMultiaddr(*announceAddrStr)
if err != nil {
log.Fatal(err)
}
}
apiAddr, err := ma.NewMultiaddr(*apiAddrStr)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -93,6 +101,9 @@ func main() {

log.Debugf("repo: %v", *repo)
log.Debugf("hostAddr: %v", *hostAddrStr)
if announceAddr != nil {
log.Debugf("announceAddr: %v", *announceAddrStr)
}
log.Debugf("apiAddr: %v", *apiAddrStr)
log.Debugf("apiProxyAddr: %v", *apiProxyAddrStr)
log.Debugf("connLowWater: %v", *connLowWater)
Expand Down Expand Up @@ -128,6 +139,9 @@ func main() {
} else {
opts = append(opts, common.WithNetBadgerPersistence(*repo))
}
if announceAddr != nil {
opts = append(opts, common.WithAnnounceAddr(announceAddr))
}
n, err := common.DefaultNetwork(opts...)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit a512e0a

Please sign in to comment.