Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const (
SwarmEnvStoreCapacity = "SWARM_STORE_CAPACITY"
SwarmEnvStoreCacheCapacity = "SWARM_STORE_CACHE_CAPACITY"
SwarmEnvBootnodeMode = "SWARM_BOOTNODE_MODE"
SwarmEnvNATInterface = "SWARM_NAT_INTERFACE"
SwarmAccessPassword = "SWARM_ACCESS_PASSWORD"
SwarmAutoDefaultPath = "SWARM_AUTO_DEFAULTPATH"
SwarmGlobalstoreAPI = "SWARM_GLOBALSTORE_API"
Expand Down
5 changes: 5 additions & 0 deletions cmd/swarm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ var (
Usage: "Swarm local http api port",
EnvVar: SwarmEnvPort,
}
SwarmNATInterfaceFlag = cli.StringFlag{
Name: "natif",
Usage: "Announce the IP address of a given network interface (e.g. eth0)",
EnvVar: SwarmEnvNATInterface,
}
SwarmNetworkIdFlag = cli.IntFlag{
Name: "bzznetworkid",
Usage: "Network identifier (integer, default 3=swarm testnet)",
Expand Down
29 changes: 29 additions & 0 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"runtime"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethersphere/swarm"
bzzapi "github.com/ethersphere/swarm/api"
Expand Down Expand Up @@ -170,6 +172,7 @@ func init() {
utils.IPCDisabledFlag,
utils.IPCPathFlag,
utils.PasswordFileFlag,
SwarmNATInterfaceFlag,
// bzzd-specific flags
CorsStringFlag,
EnsAPIFlag,
Expand Down Expand Up @@ -291,6 +294,9 @@ func bzzd(ctx *cli.Context) error {
//disable dynamic dialing from p2p/discovery
cfg.P2P.NoDial = true

//optionally set the NAT IP from a network interface
setSwarmNATFromInterface(ctx, &cfg)

stack, err := node.New(&cfg)
if err != nil {
utils.Fatalf("can't create node: %v", err)
Expand Down Expand Up @@ -525,3 +531,26 @@ func setSwarmBootstrapNodes(ctx *cli.Context, cfg *node.Config) {
}

}

func setSwarmNATFromInterface(ctx *cli.Context, cfg *node.Config) {
ifacename := ctx.GlobalString(SwarmNATInterfaceFlag.Name)

if ifacename == "" {
return
}

iface, err := net.InterfaceByName(ifacename)
if err != nil {
utils.Fatalf("can't get network interface %s", ifacename)
}
addrs, err := iface.Addrs()
if err != nil || len(addrs) == 0 {
utils.Fatalf("could not get address from interface %s: %v", ifacename, err)
}

ip, _, err := net.ParseCIDR(addrs[0].String())
if err != nil {
utils.Fatalf("could not parse IP addr from interface %s: %v", ifacename, err)
}
cfg.P2P.NAT = nat.ExtIP(ip)
}