⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iprules.cpp

📁 JK Proxy Project - Version 0.1 ------------------------------ This was going to be a proxy serve
💻 CPP
字号:
#include "iprules.h"
#include "proxymain.h"
#include "filesystem.h"
#include "cfgfiles.h"
#include "patternmatch.h"

IPRULESET* LoadIPRules(CHARPTR file)
{
	// return struct
	IPRULESET* retval = new IPRULESET;
	// initialize list
	retval->rulelist = new IPRULELIST;

	COMMAND cur;
	bool firstcmd = true;		// indicates that nxt command is FIRST command

	// first of all open the file
	int ret = OpenConfigFile(file);
	
	// error?
	if (ret == 0) return 0;

	// now iterate through all lines
	cur = GetNextFullCommand();

	while (cur.command != 0)
	{
		if (stricmp(cur.command,"allow\0") == 0)
			// if first line then parameter must be 'all'
		{
			if (firstcmd == true)
			{
				if (stricmp(cur.paras[0],"all") != 0)
				{
					// parameter is NOT 'all'
					cout << endl << "Error in " << file << "(" << cur.line << ")"
					 << ": first line must be 'allow all' or 'deny all'";
					return 0;			
				}
				else
					// ok save 'allow' as basetype
					retval->basetype = IPR_ALLOW;

				firstcmd = false;
			}
			else
			{
				// if basetype is also 'allow' -> error!
				if (retval->basetype == IPR_ALLOW)
				{
					cout << endl << "Error in " << file << "(" << cur.line << ")"
					 << ": 'allow all' is base rule so only 'deny' commands are allowed.";
					return 0;			

				}
				else
				// everything is ok -> save pattern in ruleset
				{
					retval->rulelist->push_back
						((CHARPTR)CopyAndPtr(cur.paras[0],strlen(cur.paras[0])));
				}
			}
		}
		else if (stricmp(cur.command,"deny\0") == 0)
			// if first line then parameter must be 'all'
			if (firstcmd == true)
			{
				if (stricmp(cur.paras[0],"all") != 0)
				{
					// parameter is NOT 'all'
					cout << endl << "Error in " << file << "(" << cur.line << ")"
					 << ": first line must be 'allow all' or 'deny all'";
					return 0;			
				}
				else
					// ok save 'deny' as basetype
					retval->basetype = IPR_DENY;
				
				firstcmd = false;
			}
			else
			{
				// if basetype is also 'deny' -> error!
				if (retval->basetype == IPR_DENY)
				{
					cout << endl << "Error in " << file << "(" << cur.line << ")"
					 << ": 'deny all' is base rule so only 'allow' commands are allowed.";
					return 0;			

				}
				else
				// everything is ok -> save pattern in ruleset
				{
					retval->rulelist->push_back
						((CHARPTR)CopyAndPtr(cur.paras[0],strlen(cur.paras[0])));
				}
			}

		else
		{
			// not a valid command (must be "allow" or "deny")
			cout << endl << "Error in " << file << "(" << cur.line << ")"
			 << ": command must be 'allow' or 'deny'.";
			return 0;

		}

	
		// release memory allocated for this command
		ReleaseCommand(&cur);
		// get next command
		cur = GetNextFullCommand();
	}
	


	// release file (= deallocate memory)
	ReleaseConfigFile();


	return retval;

}

bool CheckIP(CHARPTR ip, IPRULESET* rules)
 // checks an IP agains a certain IP ruleset
 // returns 'true' if IP is allowed and 'false' if it is denied
{
	
	// iterate through rules list and check each pattern against IP
	UINT n = rules->rulelist->size();
	bool match = false;
	CHARPTR temp;

	for(UINT i=0; i<n ; i++ )
	{
		// if pattern-match -> set match to true and break for{}
		temp = (*(rules->rulelist))[i];
		if (patternmatch(ip, (*(rules->rulelist))[i]) == true)
		{
			match = true;
			break;
		}
	}

	// check basetype
	if (rules->basetype == IPR_ALLOW)
		// if match -> deny, no match -> allow
		return (match == true) ? false : true;
	else
		// if match -> allow, no match -> deny
		return (match == true) ? true : false;

}

void ReleaseIPRules(IPRULESET* rules)
// cleans up all memory reserver for an IPRULESET struct
{
	// delete all strings
	UINT n = rules->rulelist->size();

	for(UINT i=0; i<n ; i++ )
		delete (*(rules->rulelist))[i];

	// delete struct as such
	delete rules;



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -