IPRulePage.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:5k
- // IPRulePage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "myfirewall.h"
- #include "IPRulePage.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CMyFireWallApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CIPRulePage property page
- IMPLEMENT_DYNCREATE(CIPRulePage, CPropertyPage)
- CIPRulePage::CIPRulePage() : CPropertyPage(CIPRulePage::IDD)
- {
- //{{AFX_DATA_INIT(CIPRulePage)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- CIPRulePage::~CIPRulePage()
- {
- }
- void CIPRulePage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CIPRulePage)
- DDX_Control(pDX, IDC_IP_RULE_LIST, m_IPRuleList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CIPRulePage, CPropertyPage)
- //{{AFX_MSG_MAP(CIPRulePage)
- ON_NOTIFY(NM_RCLICK, IDC_IP_RULE_LIST, OnRclickIPRuleList)
- //}}AFX_MSG_MAP
- ON_COMMAND(IDM_MODIFY_IPRULE, OnModifyIPRule)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CIPRulePage message handlers
- BOOL CIPRulePage::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
-
- // TODO: Add extra initialization here
- m_IPRuleList.InsertColumn(0,L"规则名称",LVCFMT_LEFT,120,-1);
- m_IPRuleList.InsertColumn(1,L"状态", LVCFMT_LEFT, 70, -1);
- m_IPRuleList.InsertColumn(2,L"对应协议", LVCFMT_LEFT, 70, -1);
- m_IPRuleList.InsertColumn(3,L"使用端口",LVCFMT_LEFT,70,-1);
- m_IPRuleList.InsertColumn(4,L"规则描述",LVCFMT_LEFT,238,-1);
- m_IPRuleList.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
- m_IPRuleList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
- // 如果IP规则没有保存到文件中,则保存
- if (g_RuleFile.m_header.bIsIPRuleSaved == FALSE)
- {
- IPRule IPRuleItem[10];
- // Ping入
- IPRuleItem[0].bState = FALSE;
- _tcscpy(IPRuleItem[0].description, L"Ping入可以使攻击者获得您计算机的活动状态等信息");
- _tcscpy(IPRuleItem[0].name, L"Ping入");
- IPRuleItem[0].port = 0;
- IPRuleItem[0].protocol = 1; // 为1表示是ICMP,TCP:6,UDP:17
- // Ping出
- IPRuleItem[1].bState = TRUE;
- _tcscpy(IPRuleItem[1].description, L"Ping出可以使您获得对方计算机的活动状态等信息");
- _tcscpy(IPRuleItem[1].name, L"Ping出");
- IPRuleItem[1].port = 0;
- IPRuleItem[1].protocol = 1;
- // RPC
- IPRuleItem[2].bState = FALSE;
- _tcscpy(IPRuleItem[2].description, L"RPC可使攻击者远程控制您的计算机");
- _tcscpy(IPRuleItem[2].name, L"RPC(远程过程调用)");
- IPRuleItem[2].port = 135;
- IPRuleItem[2].protocol = 0;
- // DNS
- IPRuleItem[3].bState = TRUE;
- _tcscpy(IPRuleItem[3].description, L"DNS作用是将网站域名解析为IP地址");
- _tcscpy(IPRuleItem[3].name, L"DNS(域名系统)");
- IPRuleItem[3].port = 53;
- IPRuleItem[3].protocol = 17;
- // 局域网共享
- IPRuleItem[4].bState = FALSE;
- _tcscpy(IPRuleItem[4].description, L"局域网共享可使局域网中的其他用户访问您的计算机共享文件");
- _tcscpy(IPRuleItem[4].name, L"局域网共享");
- IPRuleItem[4].port = 139;
- IPRuleItem[4].protocol = 0;
-
- // 添加IP规则
- g_RuleFile.AddIPRules(IPRuleItem, 5);
- g_RuleFile.m_header.bIsIPRuleSaved = TRUE;
- // 保存IP规则
- g_RuleFile.SaveRules();
- // 将IP规则应用到核心层
- theApp.ApplyIPRules(-1); // 参数 -1 表示作用于所有的IP规则
- }
- int nPing = (int)g_RuleFile.m_pIPRules[0].bState *10 + (int)g_RuleFile.m_pIPRules[1].bState;
- // 设置Ping入和Ping出
- PtSetPing(&nPing);
- // 更新 IPRuleList 内容
- ::PostMessage(g_hMainWnd, UPDATE_IPRULE_LIST, 0, 0);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CIPRulePage::OnRclickIPRuleList(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- CMenu menu,*pSubMenu;
- menu.LoadMenu(IDR_IPRULE_MENU);
- pSubMenu=menu.GetSubMenu(0);
-
- // 根据应用程序的状态来修改右键菜单的内容
- CString strStatus;
- strStatus = m_IPRuleList.GetItemText(m_IPRuleList.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED),1);
- if (strStatus == " 允许 ")
- {
- pSubMenu->ModifyMenu(IDM_MODIFY_IPRULE, MF_BYCOMMAND, IDM_MODIFY_IPRULE, L" 禁止 ");
- }
- else
- {
- pSubMenu->ModifyMenu(IDM_MODIFY_IPRULE, MF_BYCOMMAND, IDM_MODIFY_IPRULE, L" 允许 ");
- }
-
- CPoint point;
- GetCursorPos(&point);
-
- pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
- *pResult = 0;
- }
- void CIPRulePage::OnModifyIPRule()
- {
- // 获得选中的项
- int nItem = m_IPRuleList.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
- // 修改相应规则的状态
- g_RuleFile.m_pIPRules[nItem].bState = !g_RuleFile.m_pIPRules[nItem].bState;
- // 应用修改后的规则
- theApp.ApplyIPRules(nItem); // 参数 nItem 表示仅修改第 nItem 个IP规则
- // 更新 IPRuleList 显示内容
- ::PostMessage(g_hMainWnd, UPDATE_IPRULE_LIST, 0, 0);
- // 保存日志文件
- TCHAR szRuleItem[MAX_PATH];
- // 得到当前选中的进程路径
- _tcscpy(szRuleItem, m_IPRuleList.GetItemText(nItem,0));
- char szContent[256];
- USES_CONVERSION;
- if (g_RuleFile.m_pIPRules[nItem].bState == TRUE)
- {
- sprintf(szContent, "将IP规则“%s”的状态修改为:允许。", W2A(szRuleItem));
- }
- else
- {
- sprintf(szContent, "将IP规则“%s”的状态修改为:禁止。", W2A(szRuleItem));
- }
-
- SaveLogFile(szContent);
- }