cmd/swarm: allow using a network interface by name for nat purposes (#1557)
This commit is contained in:
@ -81,6 +81,7 @@ const (
|
|||||||
SwarmEnvStoreCapacity = "SWARM_STORE_CAPACITY"
|
SwarmEnvStoreCapacity = "SWARM_STORE_CAPACITY"
|
||||||
SwarmEnvStoreCacheCapacity = "SWARM_STORE_CACHE_CAPACITY"
|
SwarmEnvStoreCacheCapacity = "SWARM_STORE_CACHE_CAPACITY"
|
||||||
SwarmEnvBootnodeMode = "SWARM_BOOTNODE_MODE"
|
SwarmEnvBootnodeMode = "SWARM_BOOTNODE_MODE"
|
||||||
|
SwarmEnvNATInterface = "SWARM_NAT_INTERFACE"
|
||||||
SwarmAccessPassword = "SWARM_ACCESS_PASSWORD"
|
SwarmAccessPassword = "SWARM_ACCESS_PASSWORD"
|
||||||
SwarmAutoDefaultPath = "SWARM_AUTO_DEFAULTPATH"
|
SwarmAutoDefaultPath = "SWARM_AUTO_DEFAULTPATH"
|
||||||
SwarmGlobalstoreAPI = "SWARM_GLOBALSTORE_API"
|
SwarmGlobalstoreAPI = "SWARM_GLOBALSTORE_API"
|
||||||
|
@ -45,6 +45,11 @@ var (
|
|||||||
Usage: "Swarm local http api port",
|
Usage: "Swarm local http api port",
|
||||||
EnvVar: SwarmEnvPort,
|
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{
|
SwarmNetworkIdFlag = cli.IntFlag{
|
||||||
Name: "bzznetworkid",
|
Name: "bzznetworkid",
|
||||||
Usage: "Network identifier (integer, default 3=swarm testnet)",
|
Usage: "Network identifier (integer, default 3=swarm testnet)",
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -38,6 +39,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
"github.com/ethersphere/swarm"
|
"github.com/ethersphere/swarm"
|
||||||
bzzapi "github.com/ethersphere/swarm/api"
|
bzzapi "github.com/ethersphere/swarm/api"
|
||||||
@ -170,6 +172,7 @@ func init() {
|
|||||||
utils.IPCDisabledFlag,
|
utils.IPCDisabledFlag,
|
||||||
utils.IPCPathFlag,
|
utils.IPCPathFlag,
|
||||||
utils.PasswordFileFlag,
|
utils.PasswordFileFlag,
|
||||||
|
SwarmNATInterfaceFlag,
|
||||||
// bzzd-specific flags
|
// bzzd-specific flags
|
||||||
CorsStringFlag,
|
CorsStringFlag,
|
||||||
EnsAPIFlag,
|
EnsAPIFlag,
|
||||||
@ -291,6 +294,9 @@ func bzzd(ctx *cli.Context) error {
|
|||||||
//disable dynamic dialing from p2p/discovery
|
//disable dynamic dialing from p2p/discovery
|
||||||
cfg.P2P.NoDial = true
|
cfg.P2P.NoDial = true
|
||||||
|
|
||||||
|
//optionally set the NAT IP from a network interface
|
||||||
|
setSwarmNATFromInterface(ctx, &cfg)
|
||||||
|
|
||||||
stack, err := node.New(&cfg)
|
stack, err := node.New(&cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("can't create node: %v", err)
|
utils.Fatalf("can't create node: %v", err)
|
||||||
@ -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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user