mirror of
https://github.com/ergochat/ergo.git
synced 2024-12-23 03:02:48 +01:00
deduplicate d-line and throttle code
This commit is contained in:
parent
8b1f722655
commit
a9e4ed237e
@ -273,12 +273,27 @@ func (server *Server) Run() {
|
||||
continue
|
||||
}
|
||||
|
||||
isBanned, banMsg := server.checkBans(ipaddr)
|
||||
if isBanned {
|
||||
conn.Conn.Write(banMsg)
|
||||
conn.Conn.Close()
|
||||
continue
|
||||
}
|
||||
|
||||
server.logger.Debug("localconnect-ip", fmt.Sprintf("Client connecting from %v", ipaddr))
|
||||
// prolly don't need to alert snomasks on this, only on connection reg
|
||||
|
||||
go NewClient(server, conn.Conn, conn.IsTLS)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (server *Server) checkBans(ipaddr net.IP) (banned bool, message []byte) {
|
||||
// check DLINEs
|
||||
isBanned, info := server.dlines.CheckIP(ipaddr)
|
||||
if isBanned {
|
||||
conn.Conn.Write([]byte(info.BanMessage(bannedFromServerMsg)))
|
||||
conn.Conn.Close()
|
||||
continue
|
||||
return true, []byte(info.BanMessage(bannedFromServerMsg))
|
||||
}
|
||||
|
||||
// check connection limits
|
||||
@ -288,9 +303,7 @@ func (server *Server) Run() {
|
||||
if err != nil {
|
||||
// too many connections from one client, tell the client and close the connection
|
||||
// this might not show up properly on some clients, but our objective here is just to close it out before it has a load impact on us
|
||||
conn.Conn.Write([]byte(tooManyClientsMsg))
|
||||
conn.Conn.Close()
|
||||
continue
|
||||
return true, []byte(tooManyClientsMsg)
|
||||
}
|
||||
|
||||
// check connection throttle
|
||||
@ -310,18 +323,10 @@ func (server *Server) Run() {
|
||||
server.connectionThrottle.ResetFor(ipaddr)
|
||||
|
||||
// this might not show up properly on some clients, but our objective here is just to close it out before it has a load impact on us
|
||||
conn.Conn.Write([]byte(server.connectionThrottle.BanMessageBytes))
|
||||
conn.Conn.Close()
|
||||
continue
|
||||
return true, []byte(server.connectionThrottle.BanMessageBytes)
|
||||
}
|
||||
|
||||
server.logger.Debug("localconnect-ip", fmt.Sprintf("Client connecting from %v", ipaddr))
|
||||
// prolly don't need to alert snomasks on this, only on connection reg
|
||||
|
||||
go NewClient(server, conn.Conn, conn.IsTLS)
|
||||
continue
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
//
|
||||
@ -2189,39 +2194,9 @@ func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// check DLINEs
|
||||
isBanned, info := server.dlines.CheckIP(parsedProxiedIP)
|
||||
isBanned, banMsg := server.checkBans(parsedProxiedIP)
|
||||
if isBanned {
|
||||
client.Quit(info.BanMessage("You are banned from this server (%s)"))
|
||||
return true
|
||||
}
|
||||
|
||||
// check connection limits
|
||||
server.connectionLimitsMutex.Lock()
|
||||
err := server.connectionLimits.AddClient(parsedProxiedIP, false)
|
||||
server.connectionLimitsMutex.Unlock()
|
||||
if err != nil {
|
||||
client.Quit("Too many clients from your network")
|
||||
return true
|
||||
}
|
||||
|
||||
// check connection throttle
|
||||
server.connectionThrottleMutex.Lock()
|
||||
err = server.connectionThrottle.AddClient(parsedProxiedIP)
|
||||
server.connectionThrottleMutex.Unlock()
|
||||
if err != nil {
|
||||
// too many connections too quickly from client, tell them and close the connection
|
||||
length := &IPRestrictTime{
|
||||
Duration: server.connectionThrottle.BanDuration,
|
||||
Expires: time.Now().Add(server.connectionThrottle.BanDuration),
|
||||
}
|
||||
server.dlines.AddIP(parsedProxiedIP, length, server.connectionThrottle.BanMessage, "Exceeded automated connection throttle")
|
||||
|
||||
// they're DLINE'd for 15 minutes or whatever, so we can reset the connection throttle now,
|
||||
// and once their temporary DLINE is finished they can fill up the throttler again
|
||||
server.connectionThrottle.ResetFor(parsedProxiedIP)
|
||||
|
||||
client.Quit(server.connectionThrottle.BanMessage)
|
||||
client.Quit(string(banMsg))
|
||||
return true
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user