Anti-IP Duplication - Limiting Maximum 5 Connections from the Same IP Address D

Started by Matite, May 23, 2023, 05:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Matite

Description:
This code implements an anti-IP duplication mechanism in SA-MP that restricts the number of simultaneous connections from the same IP address to a maximum of 5. It helps prevent multiple connections from the same IP, which can be used for malicious purposes or to exploit server resources.


new connectedPlayers[MAX_PLAYERS];
new maxConnectionsPerIP = 5;

public OnPlayerConnect(playerid)
{
    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    // Check the number of existing connections from the same IP
    new connectionCount = 0;
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        if (IsPlayerConnected(i) && !strcmp(ip, connectedPlayers[i], true))
        {
            connectionCount++;
            if (connectionCount >= maxConnectionsPerIP)
            {
                Kick(playerid);
                SendClientMessage(playerid, COLOR_RED, "Too many connections from your IP address. Please try again later.");
                return 0;
            }
        }
    }

    // Store the IP address for the new connection
    strcpy(connectedPlayers[playerid], ip);

    // Rest of the connection handling logic
    // ...

    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    // Clear the stored IP address for the disconnected player
    connectedPlayers[playerid][0] = EOS;
}


Explanation:

This code tracks the number of connections from each IP address using the connectedPlayers array, which stores the last recorded IP address for each connected player.
In the OnPlayerConnect event, the code retrieves the IP address of the new connection and compares it with the existing connections in the connectedPlayers array.
If the number of connections from the same IP address exceeds or equals the maxConnectionsPerIP value (set to 5 in this example), the new connection is kicked and an error message is sent to the player.
The OnPlayerDisconnect event is used to clear the stored IP address for a disconnected player, allowing a new connection from the same IP.
You can adjust the maxConnectionsPerIP value according to your server's requirements and the number of allowed connections from the same IP address.
This anti-IP duplication code helps maintain fair gameplay and prevents excessive connections from a single IP address, ensuring a better experience for all players on the server.