#include <sourcemod>
#include <SteamWorks>
#include <multicolors>
#include <smjansson>

#undef REQUIRE_PLUGIN
#tryinclude <PlayerManager>
#define REQUIRE_PLUGIN

#pragma newdecls required
#pragma	semicolon 1

#define STATUS_ERROR		-2
#define STATUS_NONE		-1
#define STATUS_SAFE		0
#define STATUS_BAD		1
#define STATUS_WARNING	2

ConVar g_cvBlockNoSteamVPN;

Database g_hDatabase;

int g_bStatus[MAXPLAYERS+1] = {STATUS_NONE,...};

bool g_bPMLoaded;

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "VPN-Check",
	author       = "Neon",
	description  = "",
	version      = "2.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	g_cvBlockNoSteamVPN = CreateConVar("sm_vpn_block", "1", "Kick unauthenticated people that use a VPN.", FCVAR_NONE, true, 0.0, true, 1.0);

	RegAdminCmd("sm_vpn", Command_CheckVPN, ADMFLAG_RCON);

	AutoExecConfig();
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void SQL_OnTableCreated(Database db, DBResultSet results, const char[] error, any data)
{
	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsValidClient(i))
			OnClientPostAdminCheck(i);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnConfigsExecuted()
{
	Database.Connect(SQL_OnDatabaseConnect, "vpn_check");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
{
	if(!db || strlen(error))
	{
		LogError("Database error: %s", error);
		return;
	}

	g_hDatabase = db;

	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);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientPostAdminCheck(int client)
{
	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);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] error, int iSerial)
{
	int client;
	if ((client = GetClientFromSerial(iSerial)) == 0)
		return;

	if (!db || strlen(error))
	{
		g_bStatus[client] = STATUS_ERROR;
		LogError("Query error: %s", error);
		return;
	}

	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;

		if ((iCurrentTime - iLastCheck) < 86400)
		{
			g_bStatus[client] = iType;
			NotifyAdmins(client);
			return;
		}
	}

	char sIP[32];
	GetClientIP(client, sIP, sizeof(sIP));

	char sRequest[256];
	FormatEx(sRequest, sizeof(sRequest), "http://v2.api.iphub.info/ip/%s", sIP);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest);
	if (!hRequest ||
		!SteamWorks_SetHTTPRequestHeaderValue(hRequest, "X-Key", "NjA5MDpnZG52emJHWjNSQmdEVTMySUwxSlhTUXBEZGt6MWtabg==") ||
		!SteamWorks_SetHTTPCallbacks(hRequest, OnTransferComplete) ||
		!SteamWorks_SetHTTPRequestContextValue(hRequest, iSerial) ||
		!SteamWorks_SendHTTPRequest(hRequest))
	{
		delete hRequest;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode, int iSerial)
{
	int client = GetClientFromSerial(iSerial);

	if (!client) //Player disconnected.
	{
		delete hRequest;
		return;
	}

	if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
	{
		delete hRequest;
		g_bStatus[client] = STATUS_ERROR;
		LogError("Request-Error: %d", eStatusCode);
		return;
	}

	SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnTransferResponse, iSerial);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int OnTransferResponse(char[] sData, int iSerial)
{
	int client = GetClientFromSerial(iSerial);

	if (!client) //Player disconnected.
		return;

	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;

	Format(sBuffer, sizeof(sBuffer), "VPN STATUS:\n");
	Format(sBuffer, sizeof(sBuffer), "%s#########################################\n", sBuffer);
	for (int i = 1; i <= MaxClients; i++)
	{
		if (!IsValidClient(i))
			continue;

		if (g_bStatus[i] == STATUS_SAFE || g_bStatus[i] == STATUS_NONE)
			continue;

		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)
	{
		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))
			{
				if (g_bPMLoaded)
				{
					if (!PM_IsPlayerSteam(client))
					{
						if (g_cvBlockNoSteamVPN.BoolValue)
						{
							CPrintToChat(i, "{green}[SM]{default} %L[NOSTEAM] is using a {red}VPN {default}(IP: %s). Client will be kicked.", client, sIP);
							KickClient(client, "VPN not allowed");
							LogAction(client, -1, "\"%L\"[NOSTEAM] is using a VPN (IP: %s). Client got kicked.", client, sIP);
						}
						else
						{
							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);
						}
					}
					else
					{
						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);
					}
				}
				else
				{
					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);
				}
			}
		}
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bStatus[client] = STATUS_NONE;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int IsValidClient(int client, bool nobots = true)
{
	if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
		return false;

	return IsClientInGame(client);
}

//----------------------------------------------------------------------------------------------------
// 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;
}