sm-plugins/VPN-Check/scripting/VPN-Check.sp

414 lines
12 KiB
SourcePawn
Raw Normal View History

2019-02-24 11:59:18 +01:00
#include <sourcemod>
#include <SteamWorks>
2019-02-24 11:59:18 +01:00
#include <multicolors>
2019-09-01 15:45:13 +02:00
#include <smjansson>
2019-06-08 12:48:24 +02:00
#undef REQUIRE_PLUGIN
2019-09-01 15:45:13 +02:00
#tryinclude <PlayerManager>
2019-06-08 12:48:24 +02:00
#define REQUIRE_PLUGIN
2019-02-24 11:59:18 +01:00
#pragma newdecls required
#pragma semicolon 1
2019-09-01 15:45:13 +02:00
#define STATUS_ERROR -2
#define STATUS_NONE -1
#define STATUS_SAFE 0
#define STATUS_BAD 1
#define STATUS_WARNING 2
2019-06-08 12:48:24 +02:00
ConVar g_cvBlockNoSteamVPN;
2019-09-01 15:45:13 +02:00
Database g_hDatabase;
int g_bStatus[MAXPLAYERS+1] = {STATUS_NONE,...};
2019-02-24 11:59:18 +01:00
2019-06-08 15:42:33 +02:00
bool g_bPMLoaded;
2019-06-08 12:48:24 +02:00
2019-02-24 11:59:18 +01:00
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
name = "VPN-Check",
author = "Neon",
description = "",
2019-09-01 15:45:13 +02:00
version = "2.0.0"
2019-02-24 11:59:18 +01:00
};
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
2019-06-08 15:42:33 +02:00
g_cvBlockNoSteamVPN = CreateConVar("sm_vpn_block", "1", "Kick unauthenticated people that use a VPN.", FCVAR_NONE, true, 0.0, true, 1.0);
2019-06-08 12:48:24 +02:00
RegAdminCmd("sm_vpn", Command_CheckVPN, ADMFLAG_RCON);
AutoExecConfig();
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public void SQL_OnTableCreated(Database db, DBResultSet results, const char[] error, any data)
2019-06-08 12:48:24 +02:00
{
2019-09-01 15:45:13 +02:00
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(i))
OnClientPostAdminCheck(i);
}
2019-06-08 12:48:24 +02:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public void OnConfigsExecuted()
2019-06-08 12:48:24 +02:00
{
2019-09-01 15:45:13 +02:00
Database.Connect(SQL_OnDatabaseConnect, "vpn_check");
2019-02-24 11:59:18 +01:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
2019-02-24 11:59:18 +01:00
{
2019-09-01 15:45:13 +02:00
if(!db || strlen(error))
2019-02-24 11:59:18 +01:00
{
2019-09-01 15:45:13 +02:00
LogError("Database error: %s", error);
return;
2019-02-24 11:59:18 +01:00
}
2019-09-01 15:45:13 +02:00
g_hDatabase = db;
2019-02-24 11:59:18 +01:00
2019-09-01 15:45:13 +02:00
char sQuery[512];
Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS ip_table (`ip` varchar(32), `type` int(64), `last_check` int(64), PRIMARY KEY (`ip`))");
g_hDatabase.Query(SQL_OnTableCreated, sQuery, _, DBPrio_High);
2019-02-24 11:59:18 +01:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public void OnClientPostAdminCheck(int client)
2019-02-24 11:59:18 +01:00
{
2019-09-01 15:45:13 +02:00
if (!IsValidClient(client))
return;
char sIP[32];
GetClientIP(client, sIP, sizeof(sIP));
char sQuery[512];
Format(sQuery, sizeof(sQuery), "SELECT * FROM ip_table WHERE ip='%s'", sIP);
g_hDatabase.Query(SQL_OnQueryCompleted, sQuery, GetClientSerial(client), DBPrio_Low);
2019-02-24 11:59:18 +01:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] error, int iSerial)
2019-02-24 11:59:18 +01:00
{
2019-09-01 15:45:13 +02:00
int client;
if ((client = GetClientFromSerial(iSerial)) == 0)
return;
if (!db || strlen(error))
{
g_bStatus[client] = STATUS_ERROR;
LogError("Query error: %s", error);
2019-02-24 11:59:18 +01:00
return;
2019-09-01 15:45:13 +02:00
}
int iCurrentTime = GetTime();
if (results.RowCount && results.FetchRow())
{
int iFieldNum;
results.FieldNameToNum("type", iFieldNum);
int iType = results.FetchInt(iFieldNum);
results.FieldNameToNum("last_check", iFieldNum);
int iLastCheck = results.FetchInt(iFieldNum);
delete results;
2019-02-24 11:59:18 +01:00
2019-09-01 15:45:13 +02:00
if ((iCurrentTime - iLastCheck) < 86400)
{
g_bStatus[client] = iType;
NotifyAdmins(client);
return;
}
}
2019-02-24 11:59:18 +01:00
char sIP[32];
GetClientIP(client, sIP, sizeof(sIP));
char sRequest[256];
2019-09-01 15:45:13 +02:00
FormatEx(sRequest, sizeof(sRequest), "http://v2.api.iphub.info/ip/%s", sIP);
2019-02-24 11:59:18 +01:00
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest);
if (!hRequest ||
2019-09-01 15:45:13 +02:00
!SteamWorks_SetHTTPRequestHeaderValue(hRequest, "X-Key", "NjA5MDpnZG52emJHWjNSQmdEVTMySUwxSlhTUXBEZGt6MWtabg==") ||
!SteamWorks_SetHTTPCallbacks(hRequest, OnTransferComplete) ||
2019-02-24 11:59:18 +01:00
!SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) ||
!SteamWorks_SendHTTPRequest(hRequest))
{
delete hRequest;
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public int OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode, int iSerial)
2019-02-24 11:59:18 +01:00
{
int client = GetClientFromSerial(iSerial);
if (!client) //Player disconnected.
{
delete hRequest;
return;
}
if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
{
delete hRequest;
2019-09-01 15:45:13 +02:00
g_bStatus[client] = STATUS_ERROR;
LogError("Request-Error: %d", eStatusCode);
2019-02-24 11:59:18 +01:00
return;
}
2019-09-01 15:45:13 +02:00
SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnTransferResponse, iSerial);
2019-02-24 11:59:18 +01:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
2019-09-01 15:45:13 +02:00
public int OnTransferResponse(char[] sData, int iSerial)
2019-02-24 11:59:18 +01:00
{
int client = GetClientFromSerial(iSerial);
if (!client) //Player disconnected.
return;
2019-09-01 15:45:13 +02:00
JSONValue hJSON = json_load(sData);
if (hJSON == null)
{
delete hJSON;
g_bStatus[client] = STATUS_ERROR;
LogError("JSON-Error: Could not load: %s", sData);
return;
}
if(!json_is_object(view_as<JSONValue>(hJSON)))
{
delete hJSON;
g_bStatus[client] = STATUS_ERROR;
LogError("JSON-Error: Object not found");
return;
}
JSONObjectIterator hJSONIterator = json_object_iter_at(view_as<JSONObject>(hJSON), "block");
if (hJSONIterator == null)
{
delete hJSON;
delete hJSONIterator;
g_bStatus[client] = STATUS_ERROR;
LogError("JSON-Error: Key not found");
return;
}
char sKey[128];
json_object_iter_key(hJSONIterator, sKey, sizeof(sKey));
JSONValue hJSONValue = json_object_iter_value(hJSONIterator);
if(!json_is_integer(view_as<JSONValue>(hJSONValue)))
{
delete hJSON;
delete hJSONIterator;
delete hJSONValue;
g_bStatus[client] = STATUS_ERROR;
LogError("JSON-Error: Integer value not found");
return;
}
g_bStatus[client] = json_integer_value(view_as<JSONInteger>(hJSONValue));
NotifyAdmins(client);
delete hJSON;
delete hJSONIterator;
delete hJSONValue;
char sIP[32];
GetClientIP(client, sIP, sizeof(sIP));
int iCurrentTime = GetTime();
char sQuery[512];
Format(sQuery, sizeof(sQuery), "INSERT INTO ip_table (ip, type, last_check) VALUES ('%s', '%d', '%d') ON DUPLICATE KEY UPDATE type='%d', last_check='%d';", sIP, g_bStatus[client], iCurrentTime, g_bStatus[client], iCurrentTime);
g_hDatabase.Query(SQL_OnQueryCompleted, sQuery, _, DBPrio_Low);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_CheckVPN(int client, int args)
{
char sBuffer[4096];
char sIP[32];
char sSteamID[32];
bool bFound = false;
2019-02-24 11:59:18 +01:00
2019-09-01 15:45:13 +02:00
Format(sBuffer, sizeof(sBuffer), "VPN STATUS:\n");
Format(sBuffer, sizeof(sBuffer), "%s#########################################\n", sBuffer);
for (int i = 1; i <= MaxClients; i++)
2019-02-24 11:59:18 +01:00
{
2019-09-01 15:45:13 +02:00
if (!IsValidClient(i))
continue;
if (g_bStatus[i] == STATUS_SAFE || g_bStatus[i] == STATUS_NONE)
continue;
2019-02-24 11:59:18 +01:00
2019-09-01 15:45:13 +02:00
if (g_bStatus[i] == STATUS_BAD)
{
GetClientAuthId(i, AuthId_Steam2, sSteamID, sizeof(sSteamID));
GetClientIP(i, sIP, sizeof(sIP));
if (g_bPMLoaded)
{
if (!PM_IsPlayerSteam(i))
Format(sBuffer, sizeof(sBuffer), "%s\"%L\"[NOSTEAM] is using a VPN (%s).\n", sBuffer, i, sIP);
else
Format(sBuffer, sizeof(sBuffer), "%s\"%L\"[STEAM] is using a VPN (%s).\n", sBuffer, i, sIP);
}
else
Format(sBuffer, sizeof(sBuffer), "%s\"%L\" is using a VPN (%s).\n", sBuffer, i, sIP);
bFound = true;
}
else if (g_bStatus[i] == STATUS_WARNING)
{
GetClientAuthId(i, AuthId_Steam2, sSteamID, sizeof(sSteamID));
GetClientIP(i, sIP, sizeof(sIP));
if (g_bPMLoaded)
{
if (!PM_IsPlayerSteam(i))
Format(sBuffer, sizeof(sBuffer), "%s\"%L\"[NOSTEAM] is maybe using a VPN (%s).\n", sBuffer, i, sIP);
else
Format(sBuffer, sizeof(sBuffer), "%s\"%L\"[STEAM] is maybe using a VPN (%s).\n", sBuffer, i, sIP);
}
else
Format(sBuffer, sizeof(sBuffer), "%s\"%L\" is maybe using a VPN (%s).\n", sBuffer, i, sIP);
bFound = true;
}
else if (g_bStatus[i] == STATUS_ERROR)
{
Format(sBuffer, sizeof(sBuffer), "%s\"%L\" Error: VPN-Check failed, check the error logs.\n", sBuffer, i);
bFound = true;
}
}
if (!bFound)
Format(sBuffer, sizeof(sBuffer), "%sCould not find any possible VPNs\n", sBuffer);
Format(sBuffer, sizeof(sBuffer), "%s#########################################", sBuffer);
ReplyToCommand(client, sBuffer);
return Plugin_Handled;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void NotifyAdmins(int client)
{
if (g_bStatus[client] == STATUS_BAD)
{
2019-02-24 11:59:18 +01:00
char sIP[32];
GetClientIP(client, sIP, sizeof(sIP));
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(i) && CheckCommandAccess(i, "sm_vpn", ADMFLAG_RCON))
{
2019-06-08 15:42:33 +02:00
if (g_bPMLoaded)
2019-02-24 11:59:18 +01:00
{
2019-06-08 15:42:33 +02:00
if (!PM_IsPlayerSteam(client))
2019-06-08 12:48:24 +02:00
{
if (g_cvBlockNoSteamVPN.BoolValue)
{
2019-09-01 15:45:13 +02:00
CPrintToChat(i, "{green}[SM]{default} %L[NOSTEAM] is using a {red}VPN {default}(IP: %s). Client will be kicked.", client, sIP);
2019-06-08 12:48:24 +02:00
KickClient(client, "VPN not allowed");
2019-09-01 15:45:13 +02:00
LogAction(client, -1, "\"%L\"[NOSTEAM] is using a VPN (IP: %s). Client got kicked.", client, sIP);
2019-06-08 12:48:24 +02:00
}
else
2019-09-01 15:45:13 +02:00
{
CPrintToChat(i, "{green}[SM]{default} %L[NOSTEAM] is using a {red}VPN {default}(IP: %s).", client, sIP);
LogMessage("%L[NOSTEAM] is using a VPN (IP: %s).", client, sIP);
}
2019-06-08 12:48:24 +02:00
}
else
2019-09-01 15:45:13 +02:00
{
CPrintToChat(i, "{green}[SM]{default} %L[STEAM] is using a {red}VPN {default}(IP: %s).", client, sIP);
LogMessage("%L[STEAM] is using a VPN (IP: %s).", client, sIP);
}
2019-02-24 11:59:18 +01:00
}
else
2019-09-01 15:45:13 +02:00
{
CPrintToChat(i, "{green}[SM]{default} %L is using a {red}VPN {default}(IP: %s).", client, sIP);
LogMessage("%L is using a VPN (IP: %s).", client, sIP);
}
2019-02-24 11:59:18 +01:00
}
}
}
2019-09-01 15:45:13 +02:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
g_bStatus[client] = STATUS_NONE;
2019-02-24 11:59:18 +01:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int IsValidClient(int client, bool nobots = true)
{
if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
return false;
return IsClientInGame(client);
2019-09-01 15:45:13 +02:00
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnAllPluginsLoaded()
{
g_bPMLoaded = LibraryExists("PlayerManager");
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnLibraryAdded(const char[] sName)
{
if (strcmp(sName, "PlayerManager", false) == 0)
g_bPMLoaded = true;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnLibraryRemoved(const char[] sName)
{
if (strcmp(sName, "PlayerManager", false) == 0)
g_bPMLoaded = false;
2019-02-24 11:59:18 +01:00
}