MyFireWall.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:8k
- // MyFireWall.cpp : Defines the class behaviors for the application.
- //
- #include "stdafx.h"
- #include "MyFireWall.h"
- #include "MyFireWallDlg.h"
- #include "Winver.h"
- #pragma comment(lib, "Version.lib") // 提供获得文件版本的函数
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyFireWallApp
- BEGIN_MESSAGE_MAP(CMyFireWallApp, CWinApp)
- //{{AFX_MSG_MAP(CMyFireWallApp)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyFireWallApp construction
- CMyFireWallApp::CMyFireWallApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CMyFireWallApp object
- CMyFireWallApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CMyFireWallApp initialization
- BOOL CMyFireWallApp::InitInstance()
- {
- // 运行一次
- TCHAR szModule[] = L"MyFireWall";
- m_hSemaphore = ::CreateSemaphore(NULL, 0, 1, szModule);
- if(::GetLastError() == ERROR_ALREADY_EXISTS)
- {
- AfxMessageBox(L" MyFireWall 已经在运行!");
- return FALSE;
- }
- // 加载过滤文件
- if (!g_RuleFile.LoadRules())
- {
- AfxMessageBox(L"加载配置文件时失败!");
- return FALSE;
- }
- // 获得防火墙当前路径
- TCHAR szCurrentPath[MAX_PATH];
- GetModuleFileName(NULL, szCurrentPath, MAX_PATH);
- strCurrentPath = GetFilePath(szCurrentPath);
- // 加载LSP
- if (!IsProviderInstalled())
- {
- TCHAR szPathName[256];
- // 注意,安装LSP需要使用完整DLL路径。这样的话,CPIOControl类在加载DLL时也应使用
- // 完整路径,否则CPIOControl类加载的DLL不能和作为LSP的DLL共享内存
-
- wsprintf(szPathName, _T("%s%s"), strCurrentPath, _T("MyLsp.dll"));
- if(!InstallProvider(szPathName))
- {
- AfxMessageBox(L" 应用层过滤安装失败!");
- return FALSE;
- }
- }
-
- //创建Dll I/O控制对象,加载Dll模块
- g_pIoControl = new CPIOControl;
-
- //应用文件中的数据,设置应用层和核心层的过滤规则
- ApplyLspRules();
- ApplyKerRules();
-
-
- //加载皮肤
- InitializeSkin("Topax.ssk");
- AfxEnableControlContainer();
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
- CMyFireWallDlg dlg;
- m_pMainWnd = &dlg;
- int nResponse = dlg.DoModal();
- if (nResponse == IDOK)
- {
- // TODO: Place code here to handle when the dialog is
- // dismissed with OK
- }
- else if (nResponse == IDCANCEL)
- {
- // TODO: Place code here to handle when the dialog is
- // dismissed with Cancel
- }
- // Since the dialog has been closed, return FALSE so that we exit the
- // application, rather than start the application's message pump.
- return FALSE;
- }
- int CMyFireWallApp::ExitInstance()
- {
- // TODO: Add your specialized code here and/or call the base class
- if(g_pIoControl != NULL)
- {
- g_pIoControl->SetWorkMode(PF_PASS_ALL);
- g_pIoControl->SetInstance(NULL, L"");
- delete g_pIoControl;
- }
- int nKerWorkMode = IM_PASS_ALL;
- PtSetWorkMode(&nKerWorkMode);
- IMClearRules();
- PtSetARPRules(FALSE);
-
- ::CloseHandle(m_hSemaphore);
- return CWinApp::ExitInstance();
- }
- BOOL CMyFireWallApp::ApplyFileData()
- {
- // 设置应用层工作模式
- g_pIoControl->SetWorkMode(g_RuleFile.m_header.ucLspWorkMode);
-
- // 设置应用层规则文件
- g_pIoControl->SetRuleFile(&g_RuleFile.m_header, g_RuleFile.m_pLspRules);
- // 设置核心层工作模式
- int nWorkMode = g_RuleFile.m_header.ucKerWorkMode;
- PtSetWorkMode(&nWorkMode);
-
- // 设置核心层规则文件
- IMClearRules();
- if(g_RuleFile.m_header.ucKerWorkMode == IM_START_FILTER)
- {
- if(!IMSetRules(g_RuleFile.m_pKerRules, g_RuleFile.m_header.ulKerRuleCount))
- {
- AfxMessageBox(L" 设置核心层规则出错!n");
- return FALSE;
- }
- }
-
- return TRUE;
- }
- BOOL CMyFireWallApp::ApplyLspRules()
- {
- // 设置应用层工作模式
- g_pIoControl->SetWorkMode(g_RuleFile.m_header.ucLspWorkMode);
-
- // 设置应用层规则文件
- g_pIoControl->SetRuleFile(&g_RuleFile.m_header, g_RuleFile.m_pLspRules);
- return TRUE;
- }
- BOOL CMyFireWallApp::ApplyKerRules()
- {
- // 设置核心层工作模式
- int nWorkMode = g_RuleFile.m_header.ucKerWorkMode;
- PtSetWorkMode(&nWorkMode);
-
- // 设置核心层规则文件
- IMClearRules();
- if(g_RuleFile.m_header.ucKerWorkMode == IM_START_FILTER)
- {
- if(!IMSetRules(g_RuleFile.m_pKerRules, g_RuleFile.m_header.ulKerRuleCount))
- {
- AfxMessageBox(L" 设置核心层规则出错!n");
- return FALSE;
- }
- }
-
- return TRUE;
- }
- BOOL CMyFireWallApp::ApplyIPRules(int nIndex)
- {
- int nPing = (int)g_RuleFile.m_pIPRules[0].bState *10 + (int)g_RuleFile.m_pIPRules[1].bState;
- // 设置Ping入和Ping出
- PtSetPing(&nPing);
- PassthruFilter RuleItem;
- // RPC
- if (nIndex == -1 || nIndex == 2)
- {
- if (g_RuleFile.m_pIPRules[2].bState == TRUE)
- {
- // 如果改为“允许”的话,就删除相应的规则
- for (int i=0; i<g_RuleFile.m_header.ulKerRuleCount; i++)
- {
- if (_tcscmp(g_RuleFile.m_pKerRules[i].szDescription, L"RPC") == 0)
- {
- g_RuleFile.DelKerRule(i);
- break;
- }
- }
- }
- else
- {
- // 如果是“禁止”,就添加相应的规则
- RuleItem.bDrop = !g_RuleFile.m_pIPRules[2].bState;
- RuleItem.protocol = g_RuleFile.m_pIPRules[2].protocol; // 1:ICMP,6:TCP,17:UDP
- RuleItem.sourceIP = 0;
- RuleItem.sourcePort = 0;
- RuleItem.destinationIP = 0;
- RuleItem.destinationPort = g_RuleFile.m_pIPRules[2].port;
- _tcscpy(RuleItem.szWebSiteURL, L"");
- _tcscpy(RuleItem.szDescription, L"RPC");
- // 添加核心层规则
- g_RuleFile.AddKerRules(&RuleItem, 1);
- }
- }
- // DNS
- if (nIndex == -1 || nIndex == 3)
- {
- if (g_RuleFile.m_pIPRules[3].bState == TRUE)
- {
- // 如果改为“允许”的话,就删除相应的规则
- for (int i=0; i<g_RuleFile.m_header.ulKerRuleCount; i++)
- {
- if (_tcscmp(g_RuleFile.m_pKerRules[i].szDescription, L"DNS") == 0)
- {
- g_RuleFile.DelKerRule(i);
- break;
- }
- }
- }
- else
- {
- // 如果是“禁止”,就添加相应的规则
- RuleItem.bDrop = !g_RuleFile.m_pIPRules[3].bState;
- RuleItem.protocol = g_RuleFile.m_pIPRules[3].protocol; // 1:ICMP,6:TCP,17:UDP
- RuleItem.sourceIP = 0;
- RuleItem.sourcePort = 0;
- RuleItem.destinationIP = 0;
- RuleItem.destinationPort = g_RuleFile.m_pIPRules[3].port;
- _tcscpy(RuleItem.szWebSiteURL, L"");
- _tcscpy(RuleItem.szDescription, L"DNS");
- // 添加到核心层规则
- g_RuleFile.AddKerRules(&RuleItem, 1);
- }
- }
- // 局域网共享
- if (nIndex == -1 || nIndex == 4)
- {
- if (g_RuleFile.m_pIPRules[4].bState == TRUE)
- {
- // 如果改为“允许”的话,就删除相应的规则
- for (int i=0; i<g_RuleFile.m_header.ulKerRuleCount; i++)
- {
- if (_tcscmp(g_RuleFile.m_pKerRules[i].szDescription, L"LAN139") == 0 || _tcscmp(g_RuleFile.m_pKerRules[i].szDescription, L"LAN445") == 0)
- {
- g_RuleFile.DelKerRule(i);
- }
- }
- }
- else
- {
- // 如果是“禁止”,就添加相应的规则
- // 端口139
- RuleItem.bDrop = !g_RuleFile.m_pIPRules[4].bState;
- RuleItem.protocol = g_RuleFile.m_pIPRules[4].protocol; // 1:ICMP,6:TCP,17:UDP
- RuleItem.sourceIP = 0;
- RuleItem.sourcePort = 0;
- RuleItem.destinationIP = 0;
- RuleItem.destinationPort = 139;
- _tcscpy(RuleItem.szWebSiteURL, L"");
- _tcscpy(RuleItem.szDescription, L"LAN139");
- // 添加到核心层规则
- g_RuleFile.AddKerRules(&RuleItem, 1);
-
- // 端口445
- RuleItem.bDrop = !g_RuleFile.m_pIPRules[4].bState;
- RuleItem.protocol = g_RuleFile.m_pIPRules[4].protocol; // 1:ICMP,6:TCP,17:UDP
- RuleItem.sourceIP = 0;
- RuleItem.sourcePort = 0;
- RuleItem.destinationIP = 0;
- RuleItem.destinationPort = 445;
- _tcscpy(RuleItem.szWebSiteURL, L"");
- _tcscpy(RuleItem.szDescription, L"LAN445");
- // 添加到核心层规则
- g_RuleFile.AddKerRules(&RuleItem, 1);
- }
- }
- // 保存规则
- g_RuleFile.SaveRules();
- // 应用到核心层
- theApp.ApplyKerRules();
- return TRUE;
- }