Add --rpclisten that behaves frighteningly similar to --listen.

Except it works for the rpcserver instead of the main server.

Closes #34
This commit is contained in:
Owain G. Ainsworth 2013-11-14 01:51:37 +00:00
parent 5ec4aaff09
commit 5da5dfe1c4
4 changed files with 53 additions and 54 deletions

View file

@ -53,7 +53,7 @@ type config struct {
BanDuration time.Duration `long:"banduration" description:"How long to ban misbehaving peers. Valid time units are {s, m, h}. Minimum 1 second"` BanDuration time.Duration `long:"banduration" description:"How long to ban misbehaving peers. Valid time units are {s, m, h}. Minimum 1 second"`
RPCUser string `short:"u" long:"rpcuser" description:"Username for RPC connections"` RPCUser string `short:"u" long:"rpcuser" description:"Username for RPC connections"`
RPCPass string `short:"P" long:"rpcpass" default-mask:"-" description:"Password for RPC connections"` RPCPass string `short:"P" long:"rpcpass" default-mask:"-" description:"Password for RPC connections"`
RPCPort string `short:"r" long:"rpcport" description:"Listen for JSON/RPC messages on this port"` RPCListeners []string `long:"rpclisten" description:"Listen for RPC connections on this interface/port (default no listening. default port: 8334, testnet: 18334)"`
DisableRPC bool `long:"norpc" description:"Disable built-in RPC server -- NOTE: The RPC server is disabled by default if no rpcuser/rpcpass is specified"` DisableRPC bool `long:"norpc" description:"Disable built-in RPC server -- NOTE: The RPC server is disabled by default if no rpcuser/rpcpass is specified"`
DisableDNSSeed bool `long:"nodnsseed" description:"Disable DNS seeding for peers"` DisableDNSSeed bool `long:"nodnsseed" description:"Disable DNS seeding for peers"`
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"` Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
@ -147,33 +147,6 @@ func normalizeAddresses(addrs []string, defaultPort string) []string {
return removeDuplicateAddresses(addrs) return removeDuplicateAddresses(addrs)
} }
// cleanListenAddresses returns a new slice with all the passed peer addresses
// normalized and duplicates removed.
func cleanListenAddresses(addrs []string) []string {
return normalizeAddresses(addrs, activeNetParams.listenPort)
}
// cleanPeerAddresses returns a new slice with all the passed peer addresses
// normalized and duplicates removed.
func cleanPeerAddresses(addrs []string) []string {
return normalizeAddresses(addrs, activeNetParams.peerPort)
}
// updateConfigWithActiveParams update the passed config with parameters
// from the active net params if the relevant options in the passed config
// object are the default so options specified by the user on the command line
// are not overridden.
func updateConfigWithActiveParams(cfg *config) {
// Even though there should only be one default, a duplicate might
// have been specified via the config file or CLI options. So, make
// sure to update all default entries rather than only the first one
// found. Duplicates are removed later.
if cfg.RPCPort == netParams(defaultBtcnet).rpcPort {
cfg.RPCPort = activeNetParams.rpcPort
}
}
// filesExists reports whether the named file or directory exists. // filesExists reports whether the named file or directory exists.
func fileExists(name string) bool { func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil { if _, err := os.Stat(name); err != nil {
@ -200,7 +173,6 @@ func loadConfig() (*config, []string, error) {
// Default config. // Default config.
cfg := config{ cfg := config{
DebugLevel: defaultLogLevel, DebugLevel: defaultLogLevel,
RPCPort: netParams(defaultBtcnet).rpcPort,
MaxPeers: defaultMaxPeers, MaxPeers: defaultMaxPeers,
BanDuration: defaultBanDuration, BanDuration: defaultBanDuration,
ConfigFile: defaultConfigFile, ConfigFile: defaultConfigFile,
@ -268,7 +240,6 @@ func loadConfig() (*config, []string, error) {
} else if cfg.RegressionTest { } else if cfg.RegressionTest {
activeNetParams = netParams(btcwire.TestNet) activeNetParams = netParams(btcwire.TestNet)
} }
updateConfigWithActiveParams(&cfg)
// Validate debug log level. // Validate debug log level.
if !validLogLevel(cfg.DebugLevel) { if !validLogLevel(cfg.DebugLevel) {
@ -363,14 +334,28 @@ func loadConfig() (*config, []string, error) {
cfg.DisableRPC = true cfg.DisableRPC = true
} }
// Add default port to all listner addresses if needed and remove if len(cfg.RPCListeners) == 0 {
cfg.RPCListeners = []string{
net.JoinHostPort("", activeNetParams.rpcPort),
}
}
// Add default port to all listener addresses if needed and remove
// duplicate addresses. // duplicate addresses.
cfg.Listeners = cleanListenAddresses(cfg.Listeners) cfg.Listeners = normalizeAddresses(cfg.Listeners,
activeNetParams.listenPort)
// Add default port to all rpc listener addresses if needed and remove
// duplicate addresses.
cfg.RPCListeners = normalizeAddresses(cfg.RPCListeners,
activeNetParams.rpcPort)
// Add default port to all added peer addresses if needed and remove // Add default port to all added peer addresses if needed and remove
// duplicate addresses. // duplicate addresses.
cfg.AddPeers = cleanPeerAddresses(cfg.AddPeers) cfg.AddPeers = normalizeAddresses(cfg.AddPeers,
cfg.ConnectPeers = cleanPeerAddresses(cfg.ConnectPeers) activeNetParams.peerPort)
cfg.ConnectPeers = normalizeAddresses(cfg.ConnectPeers,
activeNetParams.peerPort)
return &cfg, remainingArgs, nil return &cfg, remainingArgs, nil
} }

View file

@ -44,7 +44,6 @@ type rpcServer struct {
server *server server *server
ws wsContext ws wsContext
wg sync.WaitGroup wg sync.WaitGroup
rpcport string
username string username string
password string password string
listeners []net.Listener listeners []net.Listener
@ -313,13 +312,12 @@ func (s *rpcServer) Stop() error {
} }
// newRPCServer returns a new instance of the rpcServer struct. // newRPCServer returns a new instance of the rpcServer struct.
func newRPCServer(s *server) (*rpcServer, error) { func newRPCServer(listenAddrs []string, s *server) (*rpcServer, error) {
rpc := rpcServer{ rpc := rpcServer{
server: s, server: s,
quit: make(chan int), quit: make(chan int),
} }
// Get values from config // Get values from config
rpc.rpcport = cfg.RPCPort
rpc.username = cfg.RPCUser rpc.username = cfg.RPCUser
rpc.password = cfg.RPCPass rpc.password = cfg.RPCPass
@ -330,24 +328,33 @@ func newRPCServer(s *server) (*rpcServer, error) {
rpc.ws.spentNotifications = make(map[btcwire.OutPoint]*list.List) rpc.ws.spentNotifications = make(map[btcwire.OutPoint]*list.List)
rpc.ws.minedTxNotifications = make(map[btcwire.ShaHash]*list.List) rpc.ws.minedTxNotifications = make(map[btcwire.ShaHash]*list.List)
// IPv4 listener. // TODO(oga) this code is identical to that in server, should be
var listeners []net.Listener // factored into something shared.
listenAddr4 := net.JoinHostPort("127.0.0.1", rpc.rpcport) ipv4ListenAddrs, ipv6ListenAddrs, err := parseListeners(listenAddrs)
listener4, err := net.Listen("tcp4", listenAddr4) listeners := make([]net.Listener, 0,
len(ipv6ListenAddrs)+len(ipv4ListenAddrs))
for _, addr := range ipv4ListenAddrs {
listener, err := net.Listen("tcp4", addr)
if err != nil { if err != nil {
log.Errorf("RPCS: Couldn't create listener: %v", err) log.Warnf("RPCS: Can't listen on %s: %v", addr,
return nil, err err)
continue
}
listeners = append(listeners, listener)
} }
listeners = append(listeners, listener4)
// IPv6 listener. for _, addr := range ipv6ListenAddrs {
listenAddr6 := net.JoinHostPort("::1", rpc.rpcport) listener, err := net.Listen("tcp6", addr)
listener6, err := net.Listen("tcp6", listenAddr6)
if err != nil { if err != nil {
log.Errorf("RPCS: Couldn't create listener: %v", err) log.Warnf("RPCS: Can't listen on %s: %v", addr,
return nil, err err)
continue
}
listeners = append(listeners, listener)
}
if len(listeners) == 0 {
return nil, errors.New("RPCS: No valid listen address")
} }
listeners = append(listeners, listener6)
rpc.listeners = listeners rpc.listeners = listeners

View file

@ -127,7 +127,14 @@
; norpc=1 ; norpc=1
; The port used to listen for RPC connections. ; The port used to listen for RPC connections.
; rpcport=8334 ; rpclisten=:8334
;
; Listen on a given address on default port.
; rpclisten=1.2.3.4
;
; Listen on a given address and port.
; rpclisten=1.2.3.4:8335
;
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------

View file

@ -871,7 +871,7 @@ func newServer(listenAddrs []string, db btcdb.Db, btcnet btcwire.BitcoinNet) (*s
s.txMemPool = newTxMemPool(&s) s.txMemPool = newTxMemPool(&s)
if !cfg.DisableRPC { if !cfg.DisableRPC {
s.rpcServer, err = newRPCServer(&s) s.rpcServer, err = newRPCServer(cfg.RPCListeners, &s)
if err != nil { if err != nil {
return nil, err return nil, err
} }