Added IP address checking.

This commit is contained in:
cheatfate 2018-04-30 10:24:51 +03:00 committed by zah
parent 4fbc4104cc
commit 9c86166827

View File

@ -7,7 +7,7 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
import parseopt, strutils
import parseopt, strutils, net
const
NimbusName* = "Nimbus"
@ -44,9 +44,9 @@ type
RpcConfiguration* = object
## JSON-RPC configuration object
flags*: set[RpcFlags] ## RPC flags
bindAddress*: string ## RPC bind address string
bindAddress*: IpAddress ## RPC bind address
bindPort*: uint16 ## RPC bind port
allowedIPs*: seq[string] ## Sequence of allowed IP addresses
allowedIPs*: seq[IpAddress] ## Sequence of allowed IP addresses
username*: string ## RPC authorization username
password*: string ## RPC authorization password
@ -94,11 +94,11 @@ proc initConfiguration(): NimbusConfiguration =
## RPC defaults
result.rpc.flags = {}
result.rpc.bindAddress = "127.0.0.1"
result.rpc.bindAddress = parseIpAddress("127.0.0.1")
result.rpc.bindPort = uint16(7654)
result.rpc.username = ""
result.rpc.password = ""
result.rpc.allowedIPs = newSeq[string]()
result.rpc.allowedIPs = newSeq[IpAddress]()
## Network defaults
result.net.flags = {TestNet}
@ -125,12 +125,22 @@ proc processList(v: string, o: var seq[string]) =
o.add(n)
proc processInteger(v: string, o: var int): ConfigStatus =
result = Success
try:
o = parseInt(v)
result = Success
except:
result = ErrorParseOption
proc processIpAddress(v: string, o: var IpAddress): ConfigStatus =
try:
o = parseIpAddress(v)
result = Success
except:
result = ErrorParseOption
#proc processENode(v: string, o: var ENode): ConfigStatus =
# discard
proc processRpcArguments(key, value: string): ConfigStatus =
## Processes only `RPC` related command line options
result = Success
@ -139,7 +149,7 @@ proc processRpcArguments(key, value: string): ConfigStatus =
if skey == "rpc":
config.rpc.flags.incl(Enabled)
elif skey == "rpcbind":
config.rpc.bindAddress = value
result = processIpAddress(value, config.rpc.bindAddress)
elif skey == "rpcport":
var res = 0
result = processInteger(value, res)
@ -150,7 +160,16 @@ proc processRpcArguments(key, value: string): ConfigStatus =
elif skey == "rpcpassword":
config.rpc.password = value
elif skey == "rpcallowip":
processList(value, config.rpc.allowedIPs)
var list = newSeq[string]()
var res = ConfigStatus.Success
processList(value, list)
for item in list:
var address: IpAddress
result = processIpAddress(item, address)
if result == Success:
config.rpc.allowedIPs.add(address)
else:
break
else:
result = EmptyOption
@ -262,7 +281,7 @@ API AND CONSOLE OPTIONS:
--rpcport:<value> HTTP-RPC server listening port (default: 7654)
--rpcuser:<value> HTTP-RPC authorization username
--rpcpassword:<value> HTTP-RPC authorization password
--rpcallowip:<value> Allow HTTP-RPC connections from specified sources
--rpcallowip:<value> Allow HTTP-RPC connections from specified IP addresses
LOGGING AND DEBUGGING OPTIONS:
--debug Enable debug mode