Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add --maxsameip to limit # of conns to same IP #1517

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "dcrd.log"
defaultMaxSameIP = 5
defaultMaxPeers = 125
defaultBanDuration = time.Hour * 24
defaultBanThreshold = 100
Expand Down Expand Up @@ -100,6 +101,7 @@ type config struct {
ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"`
DisableListen bool `long:"nolisten" description:"Disable listening for incoming connections -- NOTE: Listening is automatically disabled if the --connect or --proxy options are used without also specifying listen interfaces via --listen"`
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 9108, testnet: 19108)"`
MaxSameIP int `long:"maxsameip" description:"Max number of connections with the same IP -- 0 to disable"`
MaxPeers int `long:"maxpeers" description:"Max number of inbound and outbound peers"`
DisableBanning bool `long:"nobanning" description:"Disable banning of misbehaving peers"`
BanDuration time.Duration `long:"banduration" description:"How long to ban misbehaving peers. Valid time units are {s, m, h}. Minimum 1 second"`
Expand Down Expand Up @@ -434,6 +436,7 @@ func loadConfig() (*config, []string, error) {
HomeDir: defaultHomeDir,
ConfigFile: defaultConfigFile,
DebugLevel: defaultLogLevel,
MaxSameIP: defaultMaxSameIP,
MaxPeers: defaultMaxPeers,
BanDuration: defaultBanDuration,
BanThreshold: defaultBanThreshold,
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Application Options:
listen interfaces via --listen
--listen= Add an interface/port to listen for connections
(default all interfaces port: 9108, testnet: 19108)
--maxsameip= Max number of connections with the same IP -- 0 to
disable (default: 5)
--maxpeers= Max number of inbound and outbound peers (125)
--nobanning Disable banning of misbehaving peers
--banduration= How long to ban misbehaving peers. Valid time units
Expand Down
35 changes: 33 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ type peerState struct {
outboundGroups map[string]int
}

// ConnectionsWithIP returns the number of connections with the given IP.
func (ps *peerState) ConnectionsWithIP(ip net.IP) int {
var total int
for _, p := range ps.inboundPeers {
if ip.Equal(p.NA().IP) {
total++
}
}
for _, p := range ps.outboundPeers {
if ip.Equal(p.NA().IP) {
total++
}
}
for _, p := range ps.persistentPeers {
if ip.Equal(p.NA().IP) {
total++
}
}
return total
}

// Count returns the count of all known peers.
func (ps *peerState) Count() int {
return len(ps.inboundPeers) + len(ps.outboundPeers) +
Expand Down Expand Up @@ -1277,11 +1298,21 @@ func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
delete(state.banned, host)
}

// TODO: Check for max peers from a single IP.
// Limit max number of connections from a single IP. However, allow
// whitelisted inbound peers and localhost connections regardless.
isInboundWhitelisted := sp.isWhitelisted && sp.Inbound()
peerIP := sp.NA().IP
if cfg.MaxSameIP > 0 && !isInboundWhitelisted && !peerIP.IsLoopback() &&
state.ConnectionsWithIP(peerIP)+1 > cfg.MaxSameIP {
srvrLog.Infof("Max connections with %s reached [%d] - "+
"disconnecting peer", sp, cfg.MaxSameIP)
sp.Disconnect()
return false
}

// Limit max number of total peers. However, allow whitelisted inbound
// peers regardless.
if state.Count() >= cfg.MaxPeers && !(sp.Inbound() && sp.isWhitelisted) {
if state.Count()+1 > cfg.MaxPeers && !isInboundWhitelisted {
srvrLog.Infof("Max peers reached [%d] - disconnecting peer %s",
cfg.MaxPeers, sp)
sp.Disconnect()
Expand Down