收藏文章 楼主

通用封禁IP段(源码)

版块:AMXX 插件及模块发布区   类型:普通   作者:海呀   查看:66   回复:0   获赞:0   时间:2025-06-07 15:56:53
/*
  功能:禁止指定IP(段)进入
  作者:Rulzy

  命令及使用说明:

  amx_addip <开始IP> [结束IP] - 禁止此IP(段)进入
  amx_writeip - 将禁止进入的IP(段)信息保存到文件
  amx_listip - 查看被禁止进入的IP(段)
  amx_removeip <列表索引> [是否重写到文件] - 移除指定的IP(段),请先用 amx_listip 查看列表索引

  使用时要注意,IP段不能从小于等于"127.255.255.255"的IP跨到大于等于"128.0.0.0"的IP,否则将无效。

*/

#include <amxmodx>
#include <amxmisc>

#define MAX_IPLIST_COUNT 2048

new g_iplist[MAX_IPLIST_COUNT][2]
new g_IpCount = 0

public plugin_init()
{
	register_plugin("IP Filter", "1.0", "Rulzy")	
	register_concmd("amx_addip", "cmdAddIp", ADMIN_BAN, "<ip from> [ip to]")
	register_concmd("amx_writeip", "cmdWriteIp", ADMIN_BAN, "- write ip list to file")
	register_concmd("amx_listip", "cmdListIp", ADMIN_BAN, "- display ip list in console")
	register_concmd("amx_removeip", "cmdRemoveIp", ADMIN_BAN, "<index> [write_to_file=0] - remove the ip by index, 0 for all")
	new dir[256]
	get_configsdir(dir, 255)
	server_cmd("exec %s/listip.cfg", dir)
	server_exec()
}

public cmdAddIp(id, level, cid)
{
	if(!cmd_access(id, level, cid, 2))
		return PLUGIN_HANDLED
	if(g_IpCount>=MAX_IPLIST_COUNT)
	{
		if(id==0)
			server_print("Max ip count reached.")
		else
			console_print(id, "Max ip count reached.")
		return PLUGIN_HANDLED
	}
	new ip[20], b1[4], b2[4], b3[4], b4[4]
	read_argv(1, ip, 19)
	replace_all(ip, 19, ".", " ")
	parse(ip, b1, 3, b2, 3, b3, 3, b4, 3)	
	g_iplist[g_IpCount][0] = str_to_num(b1)*0x1000000+str_to_num(b2)*0x10000+str_to_num(b3)*0x100+str_to_num(b4)
	read_argv(2, ip, 19)
	if(ip[0])
	{
		replace_all(ip, 19, ".", " ")
		parse(ip, b1, 3, b2, 3, b3, 3, b4, 3)
		g_iplist[g_IpCount][1] = str_to_num(b1)*0x1000000+str_to_num(b2)*0x10000+str_to_num(b3)*0x100+str_to_num(b4)
	}else{
		g_iplist[g_IpCount][1] = g_iplist[g_IpCount][0]
	}
	new maxplayers = get_maxplayers()
	new iip, userid
	for(new i=1;i<=maxplayers;i++)
	{
		if(!is_user_connected(i)) continue
		get_user_ip(i, ip, 19, 1)
		replace_all(ip, 19, ".", " ")
		parse(ip, b1, 3, b2, 3, b3, 3, b4, 3)
		iip = str_to_num(b1)*0x1000000+str_to_num(b2)*0x10000+str_to_num(b3)*0x100+str_to_num(b4)
		if(g_iplist[g_IpCount][0]<=iip && iip<=g_iplist[g_IpCount][1])
		{
			userid = get_user_userid(i)
			server_cmd("kick #%d 对不起,管理员禁止你的IP登录", userid)
			// CS1.5 使用如下命令(可将上面一行注释掉):
			//client_cmd(i, "echo ^"Sorry, your ip is forbidden by admin.^"")
			//server_cmd("kick #%d", userid)
		}
	}
	g_IpCount++
	return PLUGIN_HANDLED
}

public cmdWriteIp(id, level, cid)
{
	if(!cmd_access(id, level, cid, 1))
		return PLUGIN_HANDLED
	new filename[256], b1, b2, b3, b4
	get_configsdir(filename, 255)
	add(filename, 255, "/listip.cfg")
	new file = fopen(filename, "wt")
	for(new i=0;i<g_IpCount;i++)
	{
		b1 = g_iplist[i][0]/0x1000000
		if(b1<0) b1 += 256
		b2 = (g_iplist[i][0]&0x00FF0000)/0x10000
		b3 = (g_iplist[i][0]&0x0000FF00)/0x100
		b4 = g_iplist[i][0]&0x000000FF
		fprintf(file, "amx_addip %d.%d.%d.%d", b1, b2, b3, b4)
		if(g_iplist[i][1]!=g_iplist[i][0])
		{
			b1 = g_iplist[i][1]/0x1000000
			if(b1<0) b1 += 256
			b2 = (g_iplist[i][1]&0x00FF0000)/0x10000
			b3 = (g_iplist[i][1]&0x0000FF00)/0x100
			b4 = g_iplist[i][1]&0x000000FF
			fprintf(file, " %d.%d.%d.%d", b1, b2, b3, b4)
		}
		fprintf(file, "^n")
	}
	fclose(file)
	if(id==0)
		server_print("Writeing %d filter ip(s) to file successful.", g_IpCount)
	else		
		console_print(id, "Writeing %d filter ip(s) to file successful.", g_IpCount)
	return PLUGIN_HANDLED
}

public cmdListIp(id, level, cid)
{
	if(!cmd_access(id, level, cid, 1))
		return PLUGIN_HANDLED
	new ip1[20], ip2[20], b1, b2, b3, b4
	if(id==0)
	{
		server_print("=============== Filter IP List ===============")
		server_print("   #.  %22s%16s", "IP FROM", "IP TO")
	}else{
		console_print(id, "=========== Filter IP List ===========")
		console_print(id, "   #.  %22s%16s", "IP FROM", "IP TO")
	}
	for(new i=0;i<g_IpCount;i++)
	{
		b1 = g_iplist[i][0]/0x1000000
		if(b1<0) b1 += 256
		b2 = (g_iplist[i][0]&0x00FF0000)/0x10000
		b3 = (g_iplist[i][0]&0x0000FF00)/0x100
		b4 = g_iplist[i][0]&0x000000FF
		format(ip1, 19, "%d.%d.%d.%d", b1, b2, b3, b4)
		copy(ip2, 19, "-")
		if(g_iplist[i][1]!=g_iplist[i][0])
		{
			b1 = g_iplist[i][1]/0x1000000
			if(b1<0) b1 += 256
			b2 = (g_iplist[i][1]&0x00FF0000)/0x10000
			b3 = (g_iplist[i][1]&0x0000FF00)/0x100
			b4 = g_iplist[i][1]&0x000000FF
			format(ip2, 19, "%d.%d.%d.%d", b1, b2, b3, b4)
		}
		if(id==0)
			server_print("%4d.  %22s%16s", i+1, ip1, ip2)
		else
			console_print(id, "%4d.  %22s%16s", i+1, ip1, ip2)
	}
	if(id==0)
		server_print("==============================================")
	else
		console_print(id, "==============================")
	return PLUGIN_HANDLED
}

public cmdRemoveIp(id, level, cid)
{
	if(!cmd_access(id, level, cid, 2))
		return PLUGIN_HANDLED
	new arg[12], arg2[2], index
	read_argv(1, arg, 11)
	read_argv(2, arg2, 1)
	index = str_to_num(arg)
	if(index>g_IpCount)
	{
		if(id==0)
			server_print("Not that ip list by index.")
		else
			console_print(id, "Not that ip list by index.")
		return PLUGIN_HANDLED
	}
	if(index==0)
	{
		g_IpCount = 0
		if(id==0)
			server_print("All ip lists have been removed.")
		else
			console_print(id, "All ip lists have been removed.")
		if(arg2[0]) cmdWriteIp(id, level, cid)
		return PLUGIN_HANDLED		
	}
	new ip1[20], ip2[20], b1, b2, b3, b4
	index--
	b1 = g_iplist[index][0]/0x1000000
	if(b1<0) b1 += 256
	b2 = (g_iplist[index][0]&0x00FF0000)/0x10000
	b3 = (g_iplist[index][0]&0x0000FF00)/0x100
	b4 = g_iplist[index][0]&0x000000FF
	format(ip1, 19, "%d.%d.%d.%d", b1, b2, b3, b4)
	ip2[0] = 0
	if(g_iplist[index][1]!=g_iplist[index][0])
	{
		b1 = g_iplist[index][1]/0x1000000
		if(b1<0) b1 += 256
		b2 = (g_iplist[index][1]&0x00FF0000)/0x10000
		b3 = (g_iplist[index][1]&0x0000FF00)/0x100
		b4 = g_iplist[index][1]&0x000000FF
		format(ip2, 19, " - %d.%d.%d.%d", b1, b2, b3, b4)
	}
	for(new i=index+1;i<g_IpCount;i++)
	{
		g_iplist[i-1][0] = g_iplist[i][0]
		g_iplist[i-1][1] = g_iplist[i][1]
	}
	g_IpCount--
	if(id==0)
	{
		server_print("Filter IP removed: %s%s", ip1, ip2)
		server_cmd("amx_listip")
	}else{
		console_print(id, "Filter IP removed: %s%s", ip1, ip2)
		client_cmd(id, "amx_listip")
	}
	if(arg2[0]) cmdWriteIp(id, level, cid)
	return PLUGIN_HANDLED
}

public client_connect(id)
{
	new ip[20], b1[4], b2[4], b3[4], b4[4], iip, userid
	get_user_ip(id, ip, 19, 1)
	replace_all(ip, 19, ".", " ")
	parse(ip, b1, 3, b2, 3, b3, 3, b4, 3)
	iip = str_to_num(b1)*0x1000000+str_to_num(b2)*0x10000+str_to_num(b3)*0x100+str_to_num(b4)
	for(new i=0;i<g_IpCount;i++)
	{
		if(g_iplist[i][0]<=iip && iip<=g_iplist[i][1])
		{
			userid = get_user_userid(id)
			server_cmd("kick #%d 对不起,管理员禁止你的IP登录", userid)
			// CS1.5 使用如下命令(可将上面一行注释掉):
			//client_cmd(i, "echo ^"Sorry, your ip is forbidden by admin.^"")
			//server_cmd("kick #%d", userid)
			break;
		}
	}
}

/* UTF-8 func by www.DT-Club.net */

 
回复列表
默认   热门   正序   倒序

回复:通用封禁IP段(源码)

Powered by HadSky 8.5.2

©2015 - 2025 CND论坛 - 中国梦DOD

苏ICP备2025177870号-1

您的IP:216.73.216.160,2025-07-01 11:56:05,Processed in 0.02005 second(s).

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息