Browse Source

configure laddr & seed node from command line.

pull/9/head
Jae Kwon 10 years ago
parent
commit
78663a09fe
4 changed files with 39 additions and 27 deletions
  1. +8
    -11
      config/config.go
  2. +1
    -1
      log.go
  3. +2
    -2
      main.go
  4. +28
    -13
      p2p/listener.go

+ 8
- 11
config/config.go View File

@ -21,8 +21,7 @@ var Config Config_
func initFlags(printHelp *bool) {
flag.BoolVar(printHelp, "help", false, "Print this help message.")
flag.StringVar(&Config.IP, "ip", Config.IP, "Listen IP. (0.0.0.0 means any)")
flag.IntVar(&Config.Port, "port", Config.Port, "Listen port. (0 means any)")
flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node")
}
@ -64,9 +63,8 @@ func init() {
/* Default configuration */
var defaultConfig = Config_{
IP: "0.0.0.0",
Port: 8770,
Seed: "",
LAddr: "0.0.0.0:0",
Seed: "",
Db: DbConfig{
Type: "level",
Dir: AppDir + "/data",
@ -77,8 +75,7 @@ var defaultConfig = Config_{
/* Configuration types */
type Config_ struct {
IP string
Port int
LAddr string
Seed string
Db DbConfig
Twilio TwilioConfig
@ -98,11 +95,11 @@ type DbConfig struct {
}
func (cfg *Config_) validate() error {
if cfg.IP == "" {
return errors.New("IP must be set")
if cfg.LAddr == "" {
cfg.LAddr = defaultConfig.LAddr
}
if cfg.Port == 0 {
return errors.New("Port must be set")
if cfg.Seed == "" {
cfg.Seed = defaultConfig.Seed
}
if cfg.Db.Type == "" {
return errors.New("Db.Type must be set")


+ 1
- 1
log.go View File

@ -10,7 +10,7 @@ var log seelog.LoggerInterface
func init() {
// TODO: replace with configuration file in the ~/.tendermint directory.
config := `
<seelog type="asyncloop" minlevel="debug">
<seelog type="sync" minlevel="debug">
<outputs formatid="colored">
<console/>
</outputs>


+ 2
- 2
main.go View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"os"
"os/signal"
"time"
@ -176,7 +175,8 @@ func main() {
// Create & start node
n := NewNode()
l := p2p.NewDefaultListener("tcp", fmt.Sprintf("%v:%v", config.Config.IP, config.Config.Port))
log.Warnf(">> %v", config.Config.LAddr)
l := p2p.NewDefaultListener("tcp", config.Config.LAddr)
n.AddListener(l)
n.Start()


+ 28
- 13
p2p/listener.go View File

@ -32,38 +32,48 @@ type DefaultListener struct {
const (
numBufferedConnections = 10
defaultExternalPort = 8770
)
func NewDefaultListener(protocol string, listenAddr string) Listener {
listenHost, listenPortStr, err := net.SplitHostPort(listenAddr)
func splitHostPort(addr string) (host string, port int) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
listenPort, err := strconv.Atoi(listenPortStr)
port, err = strconv.Atoi(portStr)
if err != nil {
panic(err)
}
return host, port
}
func NewDefaultListener(protocol string, lAddr string) Listener {
// Local listen IP & port
lAddrIP, lAddrPort := splitHostPort(lAddr)
// Create listener
listener, err := net.Listen(protocol, lAddr)
if err != nil {
panic(err)
}
// Actual listener local IP & port
listenerIP, listenerPort := splitHostPort(listener.Addr().String())
log.Infof("Local listener: %v:%v", listenerIP, listenerPort)
// Determine external address...
var extAddr *NetAddress
// If the listenHost is INADDR_ANY, try UPnP
if listenHost == "" || listenHost == "0.0.0.0" {
extAddr = getUPNPExternalAddress(listenPort, listenPort)
// If the lAddrIP is INADDR_ANY, try UPnP
if lAddrIP == "" || lAddrIP == "0.0.0.0" {
extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
}
// Otherwise just use the local address...
if extAddr == nil {
extAddr = getNaiveExternalAddress(listenPort)
extAddr = getNaiveExternalAddress(listenerPort)
}
if extAddr == nil {
panic("Could not determine external address!")
}
// Create listener
listener, err := net.Listen(protocol, listenAddr)
if err != nil {
panic(err)
}
dl := &DefaultListener{
listener: listener,
extAddr: extAddr,
@ -135,6 +145,11 @@ func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress {
return nil
}
// UPnP can't seem to get the external port, so let's just be explicit.
if externalPort == 0 {
externalPort = defaultExternalPort
}
externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0)
if err != nil {
log.Infof("Could not get UPNP external address: %v", err)


Loading…
Cancel
Save