QueryDlg.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:7k
- // QueryDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyFireWall.h"
- #include "QueryDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define PASS_ALL 1
- #define PASS_ONCE 2
- #define DENY_ONCE 3
- #define DENY_ALL 4
- extern CMyFireWallApp theApp;
- FileVersion AppVersion; // 保存程序
- // 用于时间控制的线程函数
- DWORD WINAPI TimeControlThread(LPVOID lpParam)
- {
- CQueryDlg *pQueryDlg = (CQueryDlg *)lpParam;
-
- // 等待1分钟
- for (int i=60; i>0; i--)
- {
- if (pQueryDlg->m_hWnd == NULL)
- {
- return ERROR_SUCCESS;
- }
- pQueryDlg->SetDlgItemInt(IDC_TIME, i);
- Sleep(1000);
- }
-
- // 向 CQueryDlg 发送销毁对话框信息
- ::PostMessage(pQueryDlg->m_hWnd, WM_CLOSE, 0, 0);
-
- return ERROR_SUCCESS;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CQueryDlg dialog
- CQueryDlg::CQueryDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CQueryDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CQueryDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- void CQueryDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CQueryDlg)
- DDX_Control(pDX, IDC_APP_INFO_LIST, m_AppInfoList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CQueryDlg, CDialog)
- //{{AFX_MSG_MAP(CQueryDlg)
- ON_BN_CLICKED(IDC_RADIO_PASSALL, OnRadioPassall)
- ON_BN_CLICKED(IDC_RADIO_PASSONCE, OnRadioPassonce)
- ON_BN_CLICKED(IDC_RADIO_DENYONCE, OnRadioDenyonce)
- ON_BN_CLICKED(IDC_RADIO_DENYALL, OnRadioDenyall)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CQueryDlg message handlers
- BOOL CQueryDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
-
- m_pNext = NULL;
-
- // 初始化选中“总是允许”
- m_nSelectedAction = PASS_ALL;
- CheckRadioButton(IDC_RADIO_PASSALL, IDC_RADIO_DENYALL, IDC_RADIO_PASSALL);
-
- // 绘制询问应用程序图标
- SHFILEINFOW stFileInfo;
- if( ::SHGetFileInfo(m_Session.szPathName, 0, &stFileInfo, sizeof(stFileInfo), SHGFI_ICON))
- {
- ((CStatic *)GetDlgItem(IDC_APP_ICON))->SetIcon(stFileInfo.hIcon);
- }
-
- // 填写显示的部分
- SetDlgItemText(IDC_APP_NAME, GetFileName(m_Session.szPathName));
-
- // 初始化 InfoListCtrl 的设置和风格
- m_AppInfoList.InsertColumn(0,L"程序详细信息",LVCFMT_LEFT,80,-1);
- m_AppInfoList.InsertColumn(1,L" ",LVCFMT_LEFT,240,-1);
- m_AppInfoList.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
- m_AppInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
- // 把程序的详细信息插入到 InfoListCtrl 中
- int nItem;
- CString strTemp;
- // 程序路径
- nItem = m_AppInfoList.InsertItem(0, L"程序路径");
- m_AppInfoList.SetItemText(nItem, 1, m_Session.szPathName);
- // 程序详细信息
- GetVersionFromPath(m_Session.szPathName, &AppVersion);
- nItem = m_AppInfoList.InsertItem(1, L"公司名称");
- m_AppInfoList.SetItemText(nItem, 1, AppVersion.CompanyName);
- nItem = m_AppInfoList.InsertItem(2, L"产品版本");
- m_AppInfoList.SetItemText(nItem, 1, AppVersion.ProductVersion);
- nItem = m_AppInfoList.InsertItem(3, L"法律版权");
- m_AppInfoList.SetItemText(nItem, 1, AppVersion.LegalCopyright);
- nItem = m_AppInfoList.InsertItem(4, L"程序描述");
- m_AppInfoList.SetItemText(nItem, 1, AppVersion.FileDescription);
- // 本机IP/端口
- in_addr LocalAddr;
- LocalAddr.S_un.S_addr = m_Session.ulLocalIP;
- nItem = m_AppInfoList.InsertItem(5, L"本机IP/端口");
- USES_CONVERSION;
- strTemp.Format(L"%s / %d", A2W(inet_ntoa(LocalAddr)), m_Session.usLocalPort);
- strTemp.Replace(L":", L".");
- m_AppInfoList.SetItemText(nItem, 1, strTemp);
- // 远程IP/端口
- in_addr RemoteAddr;
- RemoteAddr.S_un.S_addr = m_Session.ulRemoteIP;
- nItem = m_AppInfoList.InsertItem(6, L"远程IP/端口");
- strTemp.Format(L"%s / %d", A2W(inet_ntoa(RemoteAddr)), m_Session.usRemotePort);
- strTemp.Replace(L":", L".");
- m_AppInfoList.SetItemText(nItem, 1, strTemp);
-
- // 创建一个控制窗口显示时间的线程
- DWORD dwThreadId;
- m_hThread = CreateThread(NULL, 0, TimeControlThread, this, 0, &dwThreadId);
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CQueryDlg::OnRadioPassall()
- {
- // TODO: Add your control notification handler code here
- m_nSelectedAction = PASS_ALL;
- }
- void CQueryDlg::OnRadioPassonce()
- {
- // TODO: Add your control notification handler code here
- m_nSelectedAction = PASS_ONCE;
- }
- void CQueryDlg::OnRadioDenyonce()
- {
- // TODO: Add your control notification handler code here
- m_nSelectedAction = DENY_ONCE;
- }
- void CQueryDlg::OnRadioDenyall()
- {
- // TODO: Add your control notification handler code here
- m_nSelectedAction = DENY_ALL;
- }
- void CQueryDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- char szContent[256];
- USES_CONVERSION;
- switch ( m_nSelectedAction )
- {
- case PASS_ALL:
- // 增加LSP层规则
- AddLspRule(RULE_ACTION_PASS);
- // 将规则应用到Dll模块
- theApp.ApplyLspRules();
- // 把对询问消息的相应传递给Dll模块
- g_pIoControl->SetQuerySession(m_nIndex, RULE_ACTION_PASS);
- // 保存日志记录
- sprintf(szContent, "应用程序 %s 试图联网,用户采取的选择:总是允许。", W2A(m_Session.szPathName));
- break;
- case PASS_ONCE:
- g_pIoControl->SetQuerySession(m_nIndex, RULE_ACTION_PASS);
- // 保存日志记录
- sprintf(szContent, "应用程序 %s 试图联网,用户采取的选择:允许这次。", W2A(m_Session.szPathName));
- break;
- case DENY_ONCE:
- g_pIoControl->SetQuerySession(m_nIndex, RULE_ACTION_DENY);
- // 保存日志记录
- sprintf(szContent, "应用程序 %s 试图联网,用户采取的选择:拒绝这次。", W2A(m_Session.szPathName));
- break;
- case DENY_ALL:
- // 增加LSP层规则
- AddLspRule(RULE_ACTION_DENY);
- // 将规则应用到Dll模块
- theApp.ApplyLspRules();
- // 把对询问消息的相应传递给Dll模块
- g_pIoControl->SetQuerySession(m_nIndex, RULE_ACTION_DENY);
- // 保存日志记录
- sprintf(szContent, "应用程序 %s 试图联网,用户采取的选择:总是拒绝。", W2A(m_Session.szPathName));
- break;
- }
- // 终止计时线程
- TerminateThread(m_hThread, 0);
- // 向主对话框发送消息,表明自己要销毁
- ::PostMessage(g_hMainWnd, QUERY_DLG_DESTROY, 0, 0);
- // 保存日志文件
- SaveLogFile(szContent);
- CDialog::OnCancel();
- }
- void CQueryDlg::AddLspRule(UCHAR ucAction)
- {
- if(g_RuleFile.m_header.ulLspRuleCount > MAX_RULE_COUNT)
- {
- AfxMessageBox(L" 超过规则的最大数目,不能够再添加");
- return ;
- }
-
- // 设置规则
- RULE_ITEM tmpRule;
-
- tmpRule.ucAction = ucAction;
- tmpRule.ucDirection = m_Session.ucDirection;
- tmpRule.ucServiceType = m_Session.nProtocol;
- tmpRule.usServicePort = m_Session.usLocalPort;
- _tcscpy(tmpRule.szApplication, m_Session.szPathName);
- _tcscpy(tmpRule.sDemo, AppVersion.FileDescription);
-
- // 将规则添加到文件
- if(!g_RuleFile.AddLspRules(&tmpRule, 1))
- {
- AfxMessageBox(L"添加 ACL 规则错误。");
- return ;
- }
-
- // 将规则保存到文件
- g_RuleFile.SaveRules();
-
- // 将规则在 ListCtrl 控件上显示出来
- ::PostMessage(g_hMainWnd, UPDATE_ACCESS_CTRL_LIST, 0, 0);
- }