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

📄 proxymain.cpp

📁 JK Proxy Project - Version 0.1 ------------------------------ This was going to be a proxy serve
💻 CPP
字号:
/*
+-----------------------------------------------------------------------------+
|                                                                             |
|   proxymain.cpp                                                             |
|   Last change: yyyy-mm-dd                                                   |
|                                                                             |
|   Author: Jan Krumsiek                                                      |
|   eMail:  proxy@krumsiek.com                                                |
|   Web:    http://www.krumsiek.com/proxy                                     |
|                                                                             |
|   Copyright 2003 - Jan Krumsiek                                             |
|   This source code can freely be modified and redistributed. No liability   |
|   whatsoever is taken by the author for any use of this software.           |
|                                                                             |
|   Description:                                                              |
|   Here is the entry point for the console application.                      |                                         |
|   This module contains general functions which are used by the rest of the  |
|   project. There is much platform-dependent code in here, you can see this  |
|   at the #ifdef preprocessor directives.                                    |
|                                                                             |
+-----------------------------------------------------------------------------+
*/

#include "proxymain.h"
#include "filesystem.h"
#include "cfgfiles.h"
#include "CWinsock.h"
#include "email.h"
#include "patternmatch.h"
#include "iprules.h"
#include "ruleprocessor.h"
#include "settings.h"

#include <string.h>
#include "stdio.h"
#include "memory.h"
#include "time.h"


//MAINSETTINGS mainset;


#ifdef WIN32
	// include windows header
	#include "windows.h"
	// prototype for CTRL-C console handler
	int __stdcall CtrlCHandler(unsigned long dwCtrlType);
	// prototype for timer proc
	VOID CALLBACK TimerProc(HWND  hwnd, UINT uMsg, UINT  idEvent, DWORD  dwTime);
	// variable for windows timer identifier
	UINT htimer;

#endif

int main(int argc, char* argv[])
// program entry point
{

	int retval=0;
	int pos=0;
	bool ret;
	

	// load program settings
	retval = LoadSettings();
	if (retval != true) return 1;

	
	/*CHARPTR teststr = new char[128];
	memcpy(teststr,
		"From: \"mister jester\" <aaa@bbb.com>\x0D\x0ASubject: Du sau-sack\x0D\x0A\x0D\x0AHallo, dies ist ein test\x0D\x0A\0"
		,100);
	memcpy(teststr,
		"From: <aaa@bbb.com>\x0D\x0ASubject: Du sau-sack\x0D\x0A\x0D\x0AHallo, dies ist ein test\x0D\x0A\0"
		,100);


	bool checka = CheckMsg(teststr, 
		mainset.emailsettings.rules);


	
	checka = checka;
	return 0;
	*/

	// start sockets
	StartSockets();

	// start email proxy (this can later be turned of by a config file)
	ret = StartMailProxy();
	// ret == false -> an error occured, error message is displayed by another routine
	if (ret == false) return 1;
	
	// now call OSStartUp() which also contains the main program loop
	DoEmailJobs();
	OSStartUp();

	return 0;
}


void OSStartUp()
// this function does some platform-dependent jobs for startup (if necessary)
{
	// win32 startup jobs
#ifdef WIN32
	// set event handler for CTRL-C
	SetConsoleCtrlHandler(CtrlCHandler,TRUE);

	// set timer for occasional jobs 
	// (currently using 1000 * 60 * 30 milliseconds = 30 minutes)
//	htimer = SetTimer(0,0,1000, TimerProc);

	// notification msg
	cout << "\nApplication is running... \n\0" << flush;

	// start message loop
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
	    TranslateMessage(&msg);
	    DispatchMessage(&msg);
	}

#endif
}

void OSCleanUp()
// cleans up operation system-dependent stuff
{
#ifdef WIN32
	// WIN32 clean-up

	// remove timer
	KillTimer(0,htimer);
#endif
}

void CleanUp()
// this function cleans everything up and terminates the program
// in WIN32 it is called via the CTRL-C handler
{

	cout << "\nCleaning up... \0" << flush;
	StopSockets();
	StopMailProxy();
	OSCleanUp();
	cout << "Done\n\0" << flush;

	cout << "Terminating application.\n\n\0" << flush;
	// ok everything is cleaned up. terminate application

#ifdef WIN32
	PostQuitMessage(0);
#endif
}


#ifdef WIN32
int __stdcall CtrlCHandler(unsigned long dwCtrlType)
// this is a function which is only needed in WIN32 compilations (yet)
// and handles the pressing of CTRL-C to stop the program and clean up
{
	// call clean-up proc
	CleanUp();
	// return FALSE, windows will close the app now
	return FALSE;
}
#endif

void DoOccasionalJobs()
// this function is called by a OS-timer from time to time and lets 
// all modules do occasional work (e.g. deleting old files etc.)
{
	// call email module's job proc
	DoEmailJobs();
}

#ifdef WIN32
VOID CALLBACK TimerProc(HWND  hwnd, UINT uMsg, UINT  idEvent, DWORD  dwTime)
// this function is called by windows for each timer tick
{
	// call job procedure 
	//cout ("x\0");
	DoOccasionalJobs();
}
#endif





void ErrorAbort()
// this proc aborts the program and displays an error msg.
{
	cout << endl << endl << "Program aborted! See error message(s) above" << endl;
	CleanUp();
}

void* CopyAndPtr(void* src, UINT length)
// copies 'length' bytes from src to new allocated memory, puts
// a \0 at the end and returns a pointer to that memory
{
	CHARPTR ptr_return = new char[length+1];
	memcpy(ptr_return,src,length);
	ptr_return[length] = 0;

	return ptr_return;
}

CHARPTR ConnectStrings(CHARPTR str1, CHARPTR str2)
// connects two strings and returns pointer to new string
{
	UINT len1 = strlen(str1);
	UINT len2 = strlen(str2);
	CHARPTR ret = new char[len1+len2+1];

	strcpy(ret,str1);
	strcat(ret,str2);

	return ret;
}

CHARPTR GetID()
// Produces an unique identifier of 10 chars length (+ zero)
//(unique at least for the current machine)
// using the current time
{
	CHARPTR ret = new char[11];
	static UINT count=0;
	static time_t oldtime=0;
	time_t newtime;

	newtime = time(NULL);

	// the static variable will be increased until time since last call has changed
	// for allowing the program to produce more than one ID per second
	if (oldtime != newtime)
		count = 0;
	else
		count ++;


	// write amount of seconds since 1970 to ret in hex-form
	sprintf(ret,"%08x%02x\0",newtime,count);
	// I'm adding the current counter as two hexadecimal digits
	// this will allow 256 different IDs per second

	oldtime = newtime;
	

	return ret;
}

CHARPTR GetFilename()
// creates a filename using GetID()
{
	// filename will be 16 bytes: /IIIIIIIIII.txt0
	CHARPTR ret = new char[16];
	CHARPTR  id = GetID();

	sprintf(ret,"/%s.txt\0",id);
	delete id;

	return ret;

}





void WriteToLog(FILE* logfile, CHARPTR entry)
// prints to contents of 'entry' prefixed with the current date/time
// to the log file 'logfile'
{
	// determine current time
	time_t curtime;
	curtime = time(NULL);
	// convert to local time and place in readable struct
	struct tm *loctime;
	loctime = localtime(&curtime);

	// reserve memory for string to be printed
	// length of string + 1 (zero-terminator) + 22 bytes + [LF]
	// -> 'yyyy-mm-dd hh:mm:ss - '
	CHARPTR spr = new char[22 + 1 + strlen(entry)];

	// print str
	sprintf(spr, "%04u-%02u-%02u %02u:%02u:%02u - %s\x0A\0",
		loctime->tm_year+1900, loctime->tm_mon+1, loctime->tm_mday,
		loctime->tm_hour, loctime->tm_min, loctime->tm_sec,
		entry);

	fwrite(spr, 1, strlen(spr), logfile);
}

CHARPTR CopyString(CHARPTR str)
// this functions simple makes a copy of a string, it's rather
// a macro than a real function
{
	return ((CHARPTR)CopyAndPtr(str,strlen(str)));
}

CHARPTR GetMainConfigName()
// returns full path to main config file, depending on OS
{
#ifdef WIN32
	// in windows we use the subfolder "conf" of the application directory

	// get full path to app executable
	char fullexe[256];
	CHARPTR dir, ret;
	int len = GetModuleFileName(GetModuleHandle(NULL),fullexe,256);
	// extract directory
	dir = ExtractDir(fullexe);
	

	// now add "\conf\main.conf" and return string
	ret = ConnectStrings(dir,"conf\\main.conf\0");
	delete dir;
	return ret;
#endif

}

CHARPTR ExtractDir(CHARPTR fullpath)
// extracts the directory from a full path specification
{

	int i;

	for (i=(strlen(fullpath)-1);i>=0;i--)
	{
		if (fullpath[i] == '\\')
			break;
	}

	if (i < 0)
		// no directory found -> return empty string
		return (CHARPTR)CopyAndPtr("\0",1);
	else
		// return directory
		return (CHARPTR)CopyAndPtr(fullpath,i+1);

}

CHARPTR CombinePaths(CHARPTR path1, CHARPTR path2)
// combines a directory path and a relative path to a full path, 
// for example path1="c:\proxy\archive\" and path2="test.conf" result to
// c:\proxy\archive\test.conf" where as path1="c:\proxy\archive\" and 
// path2="d:\test.conf" result to "d:\test.conf"

{
	bool fullpath=false;
	// check if path2 is a full path specification
#ifdef WIN32
	// in WIN32 we determine a full path at the ':' as the second character
	// (a full path is 'x:\dir\subdir\file', where x is the drive letter)
	if (path2[1] == ':') fullpath = true;
#endif

	// if fullpath = false -> combine, if fullpath = true -> return path2
	if (fullpath == false)
		return ConnectStrings(path1,path2);
	else
		return (CHARPTR)CopyAndPtr(path2,strlen(path2)+1);
}

⌨️ 快捷键说明

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