#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <geoip>
#define LOGFILE "addons/sourcemod/logs/block_connect.log"
#define LOGMSG "SteamID: %s, 国家(地区): %s, IP地址: %s, 昵称: %s, 原因: %s"
ConVar g_hBlockCountries, g_hLogging;
int g_iLogging;
char g_sBlockCountries[256];
public void OnPluginStart()
{
// 初始化配置
InitConVar();
// 生成配置文件
AutoExecConfig(true, "block_connect");
}
public void OnConfigsExecuted()
{
GetCvarValue();
}
void InitConVar()
{
g_hBlockCountries = CreateConVar("bc_block_country_code", "KR", "禁止国家(地区)代码(全大写, 用空格隔开)");
g_hLogging = CreateConVar("bc_logging", "1", "是否记录日志", _, true, 0.0, true, 1.0);
g_hBlockCountries.AddChangeHook(Hook_CvarChanged);
g_hLogging.AddChangeHook(Hook_CvarChanged);
}
public void Hook_CvarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
GetCvarValue();
}
void GetCvarValue()
{
g_hBlockCountries.GetString(g_sBlockCountries, sizeof(g_sBlockCountries));
g_iLogging = g_hLogging.IntValue;
}
public bool OnClientConnect(int client, char[] rejectmsg, int maxlength)
{
// 跳过BOT
if(IsFakeClient(client)) return true;
char rejectReson[128], ccode[3], name[64], steamID[32], ip[32];
// 获取IP地址
GetClientIP(client, ip, sizeof(ip));
// 获取国家(地区)代码
GeoipCode2(ip, ccode);
// 获取昵称
FormatEx(name, sizeof(name), "%N", client);
// 获取SteamID
GetClientAuthId(client, AuthId_Steam2, steamID, sizeof(steamID), false);
if(g_sBlockCountries[0] && StrContains(g_sBlockCountries, ccode, false) != -1)
{
FormatEx(rejectReson, sizeof(rejectReson), "IP所属的国家(地区)已禁连");
// 记录日志
if(g_iLogging) LogToFileEx(LOGFILE, LOGMSG, steamID, ccode, ip, name, rejectReson);
// 拒绝原因
strcopy(rejectmsg, maxlength, rejectReson);
// 拦截连接
return false;
}
return true;
}
星座王
活动截止:2100-01-01
去徽章馆》