From ac9ac5ef19d40456c3b3d01cf6997249f98a8d6b Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 9 Oct 2017 01:58:57 -0400 Subject: [PATCH] create separate irc/connection_limiting package --- irc/config.go | 28 ++----------------- .../limits.go} | 11 +++++++- .../throttle.go} | 16 ++++++++++- irc/server.go | 5 ++-- 4 files changed, 31 insertions(+), 29 deletions(-) rename irc/{connection_limits.go => connection_limiting/limits.go} (92%) rename irc/{connection_throttling.go => connection_limiting/throttle.go} (85%) diff --git a/irc/config.go b/irc/config.go index be5b3d37..92e98ab0 100644 --- a/irc/config.go +++ b/irc/config.go @@ -15,6 +15,7 @@ import ( "time" "code.cloudfoundry.org/bytefmt" + "github.com/oragono/oragono/irc/connection_limiting" "github.com/oragono/oragono/irc/custime" "github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/passwd" @@ -108,29 +109,6 @@ func (conf *OperConfig) PasswordBytes() []byte { return bytes } -// ConnectionLimitsConfig controls the automated connection limits. -type ConnectionLimitsConfig struct { - Enabled bool - CidrLenIPv4 int `yaml:"cidr-len-ipv4"` - CidrLenIPv6 int `yaml:"cidr-len-ipv6"` - IPsPerCidr int `yaml:"ips-per-subnet"` - Exempted []string -} - -// ConnectionThrottleConfig controls the automated connection throttling. -type ConnectionThrottleConfig struct { - Enabled bool - CidrLenIPv4 int `yaml:"cidr-len-ipv4"` - CidrLenIPv6 int `yaml:"cidr-len-ipv6"` - ConnectionsPerCidr int `yaml:"max-connections"` - DurationString string `yaml:"duration"` - Duration time.Duration `yaml:"duration-time"` - BanDurationString string `yaml:"ban-duration"` - BanDuration time.Duration - BanMessage string `yaml:"ban-message"` - Exempted []string -} - // LineLenConfig controls line lengths. type LineLenConfig struct { Tags int @@ -184,8 +162,8 @@ type Config struct { ProxyAllowedFrom []string `yaml:"proxy-allowed-from"` MaxSendQString string `yaml:"max-sendq"` MaxSendQBytes uint64 - ConnectionLimits ConnectionLimitsConfig `yaml:"connection-limits"` - ConnectionThrottle ConnectionThrottleConfig `yaml:"connection-throttling"` + ConnectionLimits connection_limiting.ConnectionLimitsConfig `yaml:"connection-limits"` + ConnectionThrottle connection_limiting.ConnectionThrottleConfig `yaml:"connection-throttling"` } Datastore struct { diff --git a/irc/connection_limits.go b/irc/connection_limiting/limits.go similarity index 92% rename from irc/connection_limits.go rename to irc/connection_limiting/limits.go index 207c51f9..3402a452 100644 --- a/irc/connection_limits.go +++ b/irc/connection_limiting/limits.go @@ -1,7 +1,7 @@ // Copyright (c) 2016-2017 Daniel Oaks // released under the MIT license -package irc +package connection_limiting import ( "errors" @@ -10,6 +10,15 @@ import ( "sync" ) +// ConnectionLimitsConfig controls the automated connection limits. +type ConnectionLimitsConfig struct { + Enabled bool + CidrLenIPv4 int `yaml:"cidr-len-ipv4"` + CidrLenIPv6 int `yaml:"cidr-len-ipv6"` + IPsPerCidr int `yaml:"ips-per-subnet"` + Exempted []string +} + var ( errTooManyClients = errors.New("Too many clients in subnet") ) diff --git a/irc/connection_throttling.go b/irc/connection_limiting/throttle.go similarity index 85% rename from irc/connection_throttling.go rename to irc/connection_limiting/throttle.go index f2737481..91925217 100644 --- a/irc/connection_throttling.go +++ b/irc/connection_limiting/throttle.go @@ -1,7 +1,7 @@ // Copyright (c) 2016-2017 Daniel Oaks // released under the MIT license -package irc +package connection_limiting import ( "fmt" @@ -10,6 +10,20 @@ import ( "time" ) +// ConnectionThrottleConfig controls the automated connection throttling. +type ConnectionThrottleConfig struct { + Enabled bool + CidrLenIPv4 int `yaml:"cidr-len-ipv4"` + CidrLenIPv6 int `yaml:"cidr-len-ipv6"` + ConnectionsPerCidr int `yaml:"max-connections"` + DurationString string `yaml:"duration"` + Duration time.Duration `yaml:"duration-time"` + BanDurationString string `yaml:"ban-duration"` + BanDuration time.Duration + BanMessage string `yaml:"ban-message"` + Exempted []string +} + // ThrottleDetails holds the connection-throttling details for a subnet/IP. type ThrottleDetails struct { Start time.Time diff --git a/irc/server.go b/irc/server.go index 5062ce79..6b22de34 100644 --- a/irc/server.go +++ b/irc/server.go @@ -24,6 +24,7 @@ import ( "github.com/goshuirc/irc-go/ircfmt" "github.com/goshuirc/irc-go/ircmsg" "github.com/oragono/oragono/irc/caps" + "github.com/oragono/oragono/irc/connection_limiting" "github.com/oragono/oragono/irc/isupport" "github.com/oragono/oragono/irc/logger" "github.com/oragono/oragono/irc/passwd" @@ -86,8 +87,8 @@ type Server struct { commands chan Command configFilename string configurableStateMutex sync.RWMutex // generic protection for server state modified by rehash() - connectionLimits *ConnectionLimits - connectionThrottle *ConnectionThrottle + connectionLimits *connection_limiting.ConnectionLimits + connectionThrottle *connection_limiting.ConnectionThrottle ctime time.Time defaultChannelModes Modes dlines *DLineManager