www.pudn.com > HTTPFilter_src.zip > HTTPFilterDlg.cpp


// HTTPFilterDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "HTTPFilter.h" 
#include "HTTPFilterDlg.h" 
#include ".\httpfilterdlg.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
/* Global program variables */ 
SProgramVariables			g_ProgVar; 
 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	enum { IDD = IDD_ABOUTBOX }; 
 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
 
// Implementation 
protected: 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 
 
 
// CHTTPFilterDlg dialog 
 
 
 
CHTTPFilterDlg::CHTTPFilterDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CHTTPFilterDlg::IDD, pParent), 
	m_pFiletLog(NULL), 
	m_pProcessPkt(NULL) 
{ 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CHTTPFilterDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	DDX_Control(pDX, IDC_EDIT_BANLIST, m_edtBlackLstPath); 
	DDX_Control(pDX, IDC_COMBO_IF, m_cbxInterface); 
	DDX_Control(pDX, IDC_LIST_LOG, m_lstLog); 
} 
 
BEGIN_MESSAGE_MAP(CHTTPFilterDlg, CDialog) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	//}}AFX_MSG_MAP 
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnBnClickedButtonBrowse) 
	ON_CBN_SELCHANGE(IDC_COMBO_IF, OnCbnSelchangeComboIf) 
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButtonStart) 
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButtonStop) 
	ON_WM_TIMER() 
END_MESSAGE_MAP() 
 
 
// CHTTPFilterDlg message handlers 
 
BOOL CHTTPFilterDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
 
	// TODO: Add extra initialization here 
 
	/* Load program variables and store them in a global struct variable */ 
	LoadINI(); 
 
	m_lstLog.InsertString(m_lstLog.GetCount(),"Init loaded."); 
	 
	/* Filter object is a place which holds access to the blacklist */ 
	m_pFiletLog = new CFilterLog();// two way logger 
 
	/* Instansiate the main filtering object of the program */ 
	m_pProcessPkt = new CProcessPacket(m_pFiletLog); 
	 
 
	if (g_ProgVar.m_szAdapterName != "") 
	{ 
		/* Retrieve IP address corresponding to each system adapter */ 
		FetchInterfaceIPAddress(); 
	} 
 
	 
 
	// Load Controls 
	GetDlgItem(IDC_STATIC_IPADDRESS)->SetWindowText(g_ProgVar.m_szLocalIPAddr); 
	m_edtBlackLstPath.SetWindowText(g_ProgVar.m_szLstFilterPath); 
	 
	/* Showing adapters and their IP address in a combo box */ 
	int i = 0; 
	PIP_ADAPTER_INFO pAdapterInfo = m_pProcessPkt->m_AdapterInfo;		 
	do { 
		m_cbxInterface.InsertString(i, pAdapterInfo->Description); 
 
		if (g_ProgVar.m_szAdapterName == pAdapterInfo->AdapterName) 
		{ 
			m_cbxInterface.SetCurSel(i); 
		} 
		i++; 
		pAdapterInfo = pAdapterInfo->Next;    // Progress through 
	} 
	while(pAdapterInfo);   
	 
/* Timer mainly used for logging */ 
	SetTimer(1, 300, NULL); 
	 
	 
	UpdateData(FALSE); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CHTTPFilterDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CHTTPFilterDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this function to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CHTTPFilterDlg::OnQueryDragIcon() 
{ 
	return static_cast(m_hIcon); 
} 
 
void CHTTPFilterDlg::LoadINI() 
{ 
 
	char dirname[256]; 
	GetCurrentDirectory(256, dirname); 
 
	/* INI must be inside executable binary */ 
	char* filename = strcat(dirname, "\\httpfilter.ini"); 
	char szRet[256]; 
	 
 
	 
 
	GetPrivateProfileString("Settings", 
		"BlackList", 
		0, 
		szRet, 
		256, 
		filename); 
 
	 
	g_ProgVar.m_szLstFilterPath = szRet; 
	ZeroMemory(szRet, 256); 
	 
 
 
	GetPrivateProfileString("Settings", 
		"SourceAdapter", 
		0, 
		szRet, 
		256, 
		filename); 
	 
 
	g_ProgVar.m_szAdapterName = szRet; 
	 
	 
} 
 
void CHTTPFilterDlg::SaveINI() 
{ 
	char dirname[256]; 
	GetCurrentDirectory(256, dirname); 
 
	char* filename = strcat(dirname, "\\httpfilter.ini"); 
 
	WritePrivateProfileString("Settings", 
						"SourceAdapter", 
						g_ProgVar.m_szAdapterName, 
						filename); 
 
	WritePrivateProfileString("Settings", 
							"BlackList", 
							g_ProgVar.m_szLstFilterPath, 
							filename ); 
 
 
	 
} 
void CHTTPFilterDlg::OnBnClickedButtonBrowse() 
{ 
	// TODO: Add your control notification handler code here 
	char* filters = "BlackList Files (*.ban)|*.ban|All Files (*.*)|*.*||"; 
	CFileDialog fileDlg(TRUE, 
						"ban", 
						"*.ban", 
						OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, 
						filters 
						); 
	if (fileDlg.DoModal() == IDOK) 
	{		 
		g_ProgVar.m_szLstFilterPath = fileDlg.GetPathName(); 
 
		m_edtBlackLstPath.SetWindowText(g_ProgVar.m_szLstFilterPath); 
		UpdateData(FALSE); 
	} 
	 
} 
 
void CHTTPFilterDlg::FetchInterfaceIPAddress() 
{	 
	PIP_ADAPTER_INFO pAdapterInfo = m_pProcessPkt->m_AdapterInfo;		 
	do { 
		if (pAdapterInfo->AdapterName == g_ProgVar.m_szAdapterName) 
		{			 
			g_ProgVar.m_szLocalIPAddr = pAdapterInfo->IpAddressList.IpAddress.String;	 
 
			break; 
		} 
		pAdapterInfo = pAdapterInfo->Next;    // Progress through 
	} 
	while(pAdapterInfo);   
} 
 
void CHTTPFilterDlg::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	KillTimer(1); 
 
	/* Save program variables in INI */ 
	SaveINI(); 
 
	if (m_pFiletLog) 
		delete m_pFiletLog; 
	if (m_pProcessPkt) 
		delete m_pProcessPkt; 
	CDialog::OnCancel(); 
} 
 
void CHTTPFilterDlg::OnCbnSelchangeComboIf() 
{ 
	// TODO: Add your control notification handler code here 
	int idx = m_cbxInterface.GetCurSel();	 
	g_ProgVar.m_szAdapterName = m_pProcessPkt->m_AdapterInfo[idx].AdapterName; 
	g_ProgVar.m_szLocalIPAddr = m_pProcessPkt->m_AdapterInfo[idx].IpAddressList.IpAddress.String; 
	 
	 
 
	GetDlgItem(IDC_STATIC_IPADDRESS)->SetWindowText(g_ProgVar.m_szLocalIPAddr); 
} 
 
void CHTTPFilterDlg::OnBnClickedButtonStart() 
{ 
	// TODO: Add your control notification handler code here 
	if (m_pProcessPkt->StartEngine(g_ProgVar.m_szAdapterName) == 0) 
	{		 
		m_lstLog.InsertString(m_lstLog.GetCount(), "Engine cannot start on this interface."); 
		return; 
	} 
	 
	m_lstLog.SetCurSel(m_lstLog.GetCount()); 
	m_lstLog.InsertString(m_lstLog.GetCount(), "Engine started."); 
	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE); 
} 
 
void CHTTPFilterDlg::OnBnClickedButtonStop() 
{ 
	// TODO: Add your control notification handler code here 
	m_pProcessPkt->StopEngine(); 
	m_lstLog.InsertString(m_lstLog.GetCount(), "Engine stopped."); 
	GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); 
} 
 
 
void CHTTPFilterDlg::OnTimer(UINT nIDEvent) 
{ 
	// TODO: Add your message handler code here and/or call default 
	if (m_pFiletLog->GetCount() > 0) 
		m_lstLog.InsertString(m_lstLog.GetCount(), m_pFiletLog->GetLog());	 
 
	m_lstLog.SetCurSel(m_lstLog.GetCount()); 
	CDialog::OnTimer(nIDEvent); 
}