Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
cmd/swarm: allow using a network interface by name for nat purposes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
skylenet authored Jul 16, 2019
1 parent 9d96d9b commit ad77f43
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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)
}

0 comments on commit ad77f43

Please sign in to comment.