cvmts: reimplement connection limit using ipdata

This commit is contained in:
modeco80
2024-06-19 17:56:55 -04:00
parent ba8743f461
commit b485e7f689
2 changed files with 22 additions and 9 deletions

View File

@@ -40,6 +40,19 @@ export class IPDataManager {
return data; return data;
} }
static GetIPDataMaybe(address: string) {
if (IPDataManager.ipDatas.has(address)) {
// Note: We already check for if it exists, so we use ! here
// because TypeScript can't exactly tell that in this case,
// only in explicit null or undefined checks
let ref = IPDataManager.ipDatas.get(address)!;
ref.refCount++;
return ref;
}
return null;
}
static ForEachIPData(callback: (d: IPData) => void) { static ForEachIPData(callback: (d: IPData) => void) {
for (let tuple of IPDataManager.ipDatas) callback(tuple[1]); for (let tuple of IPDataManager.ipDatas) callback(tuple[1]);
} }

View File

@@ -119,15 +119,15 @@ export default class WSServer extends EventEmitter implements NetworkServer {
ip = req.socket.remoteAddress; ip = req.socket.remoteAddress;
} }
// TODO: Implement let ipdata = IPDataManager.GetIPDataMaybe(ip);
// Get the amount of active connections coming from the requesting IP. if(ipdata != null) {
//let connections = this.clients.filter((client) => client.IP.address == ip); let connections = ipdata.refCount;
// If it exceeds the limit set in the config, reject the connection with a 429. if (connections + 1 > this.Config.collabvm.maxConnections) {
//if (connections.length + 1 > this.Config.http.maxConnections) { socket.write('HTTP/1.1 429 Too Many Requests\n\n429 Too Many Requests');
// socket.write('HTTP/1.1 429 Too Many Requests\n\n429 Too Many Requests'); socket.destroy();
// socket.destroy(); }
//} }
this.wsServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { this.wsServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
this.wsServer.emit('connection', ws, req); this.wsServer.emit('connection', ws, req);