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

📄 ip2country.cpp

📁 非常难得的eMule(电骡) V0.45b 源码下载 值得研究
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//EastShare Start - added by AndCycle, IP to Country

/*
the IP to country data is provided by http://ip-to-country.webhosting.info/

"IP2Country uses the IP-to-Country Database
 provided by WebHosting.Info (http://www.webhosting.info),
 available from http://ip-to-country.webhosting.info."

 */

/*

flags are from http://sf.net/projects/flags/

*/

// by Superlexx, based on IPFilter by Bouc7

#include "StdAfx.h"
#include "IP2Country.h"
#include "emule.h"
#include "otherfunctions.h"
#include <flag/resource.h>
#include "Log.h"

//refresh list
#include "serverlist.h"
#include "clientlist.h"

//refresh server list ctrl
#include "emuledlg.h"
#include "serverwnd.h"
#include "serverlistctrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// N/A flag is the first Res, so it should at index zero
#define NO_FLAG 0

CString FirstCharCap(CString target){

	target.TrimRight();//clean out the space at the end, prevent exception for index++
	if(target.IsEmpty()) return _T("");
	target.MakeLower();
	target.SetAt(0, target.Left(1).MakeUpper().GetAt(0));
	for(int index = target.Find(' '); index != -1; index = target.Find(' ', index)){
		index++;//set the character next to space be Upper
		target.SetAt(index, target.Mid(index, 1).MakeUpper().GetAt(0));
	}
	return target;
}

CIP2Country::CIP2Country(){

	m_bRunning = false;

	defaultCountry.ShortCountryName = "N/A";
	defaultCountry.MidCountryName = "N/A";
	defaultCountry.LongCountryName = "N/A";
	defaultCountry.FlagIndex = NO_FLAG;

	EnableIP2Country = false;
	EnableCountryFlag = false;

	Load();

	//AddLogLine(false, "IP2Country uses the IP-to-Country Database provided by WebHosting.Info (http://www.webhosting.info), available from http://ip-to-country.webhosting.info.");

	m_bRunning = true;
}

CIP2Country::~CIP2Country(){

	m_bRunning = false;

	Unload();
}

void CIP2Country::Load()
{
	bool isModified = false;

	if(thePrefs.IsIP2CountryShowFlag()) {
		if (!EnableCountryFlag) {
			EnableCountryFlag = LoadCountryFlagLib();//flag lib first, so ip range can map to flag
			if (EnableCountryFlag) {
				isModified = true;
				EnableIP2Country = false;
				RemoveAllIPs();
			}
		}
	} else if (EnableCountryFlag) {
		EnableCountryFlag = false;
		RemoveAllFlags();
		isModified = true;
	}

	if (thePrefs.IsIP2CountryShowFlag() || thePrefs.m_iIP2CountryNameMode!=IP2CountryName_DISABLE) {
		if (!EnableIP2Country) {
			EnableIP2Country = LoadFromFile();
			if (EnableIP2Country) {
				isModified = true;
			}
		}
	} else if (EnableIP2Country) {
		EnableIP2Country = false;
		RemoveAllIPs();
		isModified = true;
	}

	if (!isModified) return;

	if(m_bRunning) Reset();
}

void CIP2Country::Unload(){

	EnableIP2Country = false;
	EnableCountryFlag = false;

	if(m_bRunning) Reset();

	RemoveAllIPs();
	RemoveAllFlags();

	AddDebugLogLine(false, _T("IP2Country unloaded"));
}

void CIP2Country::Reset(){
	theApp.serverlist->ResetIP2Country();
	theApp.clientlist->ResetIP2Country();
}

void CIP2Country::Refresh(){
	theApp.emuledlg->serverwnd->serverlistctrl.RefreshAllServer();
}

bool CIP2Country::LoadFromFile(){

	char buffer[1024];
	int	lenBuf = 1024;
//new 2004-12-04
#ifdef _UNICODE
	WCHAR bufferunicode[1024];
	bool IsUnicode=false;
#endif
//new 2004-12-04

	CString ip2countryCSVfile = thePrefs.GetConfigDir()+_T("ip-to-country.csv");

	//FILE* readFile = fopen(CStringA(ip2countryCSVfile), "r");
	FILE* readFile = _tfsopen(CString(ip2countryCSVfile), _T("rb"), 0x20);//new 2004-12-03

	try{
		if (readFile != NULL) {

		//new 2004-12-04
			WORD wBOM = fgetwc(readFile);
			if (wBOM == 0xFEFF)
		#ifdef _UNICODE
				IsUnicode=true;
		#else
				throw CString(_T("File is Unicode, Can't to load in"));
		#endif

		#ifdef _UNICODE
			fseek(readFile, 0, SEEK_SET);
		#endif
		//new 2004-12-04

			int count = 0;

			while (!feof(readFile)) {
				//if (fgets(buffer,lenBuf,readFile)==0) break;
				//CString	sbuffer;
				//sbuffer = buffer;

				//new 2004-12-04
				CString sbuffer;
		#ifdef _UNICODE
				if(IsUnicode)
				{
					if (_fgetts(bufferunicode,lenBuf,readFile)==0) break;
					sbuffer = bufferunicode;
				}
				else
		#endif
				{
					if (fgets(buffer,lenBuf,readFile)==0) break;
					sbuffer = buffer;
				}
				//new 2004-12-04
				/*
					http://ip-to-country.webhosting.info/node/view/54

					This is a sample of how the CSV file is structured:

					"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
					"0050331648","0083886079","US","USA","UNITED STATES"
					"0094585424","0094585439","SE","SWE","SWEDEN"

					FIELD  			DATA TYPE		  	FIELD DESCRIPTION
					IP_FROM 		NUMERICAL (DOUBLE) 	Beginning of IP address range.
					IP_TO			NUMERICAL (DOUBLE) 	Ending of IP address range.
					COUNTRY_CODE2 	CHAR(2)				Two-character country code based on ISO 3166.
					COUNTRY_CODE3 	CHAR(3)				Three-character country code based on ISO 3166.
					COUNTRY_NAME 	VARCHAR(50) 		Country name based on ISO 3166
				*/
				// we assume that the ip-to-country.csv is valid and doesn't cause any troubles
				// get & process IP range
				sbuffer.Remove('"'); // get rid of the " signs

				CString tempStr[5];

				int curPos;
				curPos = 0;

				for(int forCount = 0; forCount !=  5; forCount++){
					tempStr[forCount] = sbuffer.Tokenize(_T(","), curPos);
					if(tempStr[forCount].IsEmpty()) {
						throw CString(_T("error line in"));
					}
				}
				
				//tempStr[4] is full country name, capitalize country name from rayita
				tempStr[4] = FirstCharCap(tempStr[4]);

				count++;
				AddIPRange(_tstoi(tempStr[0]),_tstoi(tempStr[1]), tempStr[2], tempStr[3], tempStr[4]);
			}
			fclose(readFile);
		}
		else{
			throw CString(_T("Failed to load in"));
		}
	}
	catch(CString error){
		AddLogLine(false, _T("%s %s"), error, ip2countryCSVfile);
		RemoveAllIPs();
		AddDebugLogLine(false, _T("IP2Countryfile load failed"));
		return false;
	}
	AddDebugLogLine(false, _T("IP2Countryfile has been loaded"));
	return true;

}

bool CIP2Country::LoadCountryFlagLib(){

	CString ip2countryCountryFlag;

	try{

		//detect windows version
		//if(thePrefs.GetWindowsVersion() == _WINVER_XP_){
		//	//it's XP, we can use beautiful 32bits flags with alpha channel :)
		//	ip2countryCountryFlag = thePrefs.GetConfigDir()+_T("countryflag32.dll");
		//}
		//else{
		//	//oh~ it's not XP, but we still can load the 24bits flags
			ip2countryCountryFlag = thePrefs.GetConfigDir()+_T("countryflag.dll");
		//}

		_hCountryFlagDll = LoadLibrary(ip2countryCountryFlag); 
		if (_hCountryFlagDll == NULL) 
		{ 
			throw CString(_T("CountryFlag Disabled, failed to load"));

⌨️ 快捷键说明

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