3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 11:12:44 +01:00

deduplicate d-line and throttle code

This commit is contained in:
Shivaram Lingamneni 2017-09-28 14:19:39 -04:00 committed by Daniel Oaks
parent 8b1f722655
commit a9e4ed237e

View File

@ -273,12 +273,27 @@ func (server *Server) Run() {
continue 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 // check DLINEs
isBanned, info := server.dlines.CheckIP(ipaddr) isBanned, info := server.dlines.CheckIP(ipaddr)
if isBanned { if isBanned {
conn.Conn.Write([]byte(info.BanMessage(bannedFromServerMsg))) return true, []byte(info.BanMessage(bannedFromServerMsg))
conn.Conn.Close()
continue
} }
// check connection limits // check connection limits
@ -288,9 +303,7 @@ func (server *Server) Run() {
if err != nil { if err != nil {
// too many connections from one client, tell the client and close the connection // 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 // 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)) return true, []byte(tooManyClientsMsg)
conn.Conn.Close()
continue
} }
// check connection throttle // check connection throttle
@ -310,18 +323,10 @@ func (server *Server) Run() {
server.connectionThrottle.ResetFor(ipaddr) 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 // 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)) return true, []byte(server.connectionThrottle.BanMessageBytes)
conn.Conn.Close()
continue
} }
server.logger.Debug("localconnect-ip", fmt.Sprintf("Client connecting from %v", ipaddr)) return false, nil
// prolly don't need to alert snomasks on this, only on connection reg
go NewClient(server, conn.Conn, conn.IsTLS)
continue
}
}
} }
// //
@ -2189,39 +2194,9 @@ func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
return true return true
} }
// check DLINEs isBanned, banMsg := server.checkBans(parsedProxiedIP)
isBanned, info := server.dlines.CheckIP(parsedProxiedIP)
if isBanned { if isBanned {
client.Quit(info.BanMessage("You are banned from this server (%s)")) client.Quit(string(banMsg))
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)
return true return true
} }