AppMonitorPage.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:5k
源码类别:
防火墙与安全工具
开发平台:
Visual C++
- // AppMonitorPage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyFireWall.h"
- #include "AppMonitorPage.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAppMonitorPage property page
- IMPLEMENT_DYNCREATE(CAppMonitorPage, CPropertyPage)
- CAppMonitorPage::CAppMonitorPage() : CPropertyPage(CAppMonitorPage::IDD)
- {
- //{{AFX_DATA_INIT(CAppMonitorPage)
- //}}AFX_DATA_INIT
- }
- CAppMonitorPage::~CAppMonitorPage()
- {
- }
- void CAppMonitorPage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAppMonitorPage)
- DDX_Control(pDX, IDC_APP_MONITOR_TREE, m_AppMonitorTree);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAppMonitorPage, CPropertyPage)
- //{{AFX_MSG_MAP(CAppMonitorPage)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAppMonitorPage message handlers
- BOOL CAppMonitorPage::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CAppMonitorPage::AddSession(SESSION *pSession)
- {
- // 首先查看所属的应用程序,如果没有,就插入一个新的应用程序项
- // 要在此应用程序项下添加会话
- HTREEITEM hAppItem = FindAppItem(pSession->szPathName);
- if(hAppItem == NULL)
- {
- hAppItem = m_AppMonitorTree.InsertItem(pSession->szPathName);
- }
- // 通过SESSION结构,构建可显示的文本
- CString sText = SetSessionText(pSession);
- // 在应用程序项下,根据套接字句柄查看此会话是否已经存在,
- // 如果存在,仅设置子项的文本,如果不存在,要创建一个新的子项
- HTREEITEM hSessionItem = FindSessionItem(hAppItem, pSession);
- if(hSessionItem != NULL)
- {
- m_AppMonitorTree.SetItemText(hSessionItem, sText);
- }
- else
- {
- hSessionItem = m_AppMonitorTree.InsertItem(sText,hAppItem);
- m_AppMonitorTree.SetItemData(hSessionItem, pSession->s);
- }
- }
- HTREEITEM CAppMonitorPage::FindAppItem(TCHAR *pszPathName)
- {
- // 遍历所有应用程序项,看看指定应用程序是否存在
- HTREEITEM hAppItem = m_AppMonitorTree.GetNextItem(TVI_ROOT, TVGN_CHILD);
- while(hAppItem != NULL)
- {
- if(m_AppMonitorTree.GetItemText(hAppItem).CompareNoCase(pszPathName) == 0)
- {
- // 存在,返回项句柄
- return hAppItem;
- }
- hAppItem = m_AppMonitorTree.GetNextItem(hAppItem, TVGN_NEXT);
- }
- return NULL;
- }
- HTREEITEM CAppMonitorPage::FindSessionItem(HTREEITEM hAppItem, SESSION *pSession)
- {
- // 变量所有会话项,看看指定会话是否存在
- HTREEITEM hSessionItem = m_AppMonitorTree.GetNextItem(hAppItem, TVGN_CHILD);
- while(hSessionItem != NULL)
- {
- if(pSession->s == m_AppMonitorTree.GetItemData(hSessionItem))
- return hSessionItem; // 存在,返回项句柄
- hSessionItem = m_AppMonitorTree.GetNextItem(hSessionItem, TVGN_NEXT);
- }
- return NULL;
- }
- CString CAppMonitorPage::SetSessionText(SESSION *pSession)
- {
- //该函数的功能时通过 SESSION 结构,在树形结构中构建可显示的文本
- CString sText;
- CString sServType, sLocal, sRemote, sDirection;
- // 本地IP地址
- BYTE *pByte = (BYTE *)&pSession->ulLocalIP; // 注意,这里的IP地址是网络字节顺序
- sLocal.Format(L"%d.%d.%d.%d:%d", pByte[0], pByte[1], pByte[2], pByte[3], pSession->usLocalPort);
- // 远程IP地址
- pByte = (BYTE *)&pSession->ulRemoteIP;
- sRemote.Format(L"%d.%d.%d.%d:%d", pByte[0], pByte[1], pByte[2], pByte[3], pSession->usRemotePort);
- // 服务类型
- sServType = L"其它";
- switch(pSession->nProtocol)
- {
- case RULE_SERVICE_TYPE_ALL:
- sServType.Format(L"所有");
- break;
- case RULE_SERVICE_TYPE_TCP:
- sServType.Format(L"TCP");
- break;
- case RULE_SERVICE_TYPE_UDP:
- sServType.Format(L"UDP");
- break;
- case RULE_SERVICE_TYPE_FTP:
- sServType.Format(L"FTP");
- break;
- case RULE_SERVICE_TYPE_TELNET:
- sServType.Format(L"TELNET");
- break;
- case RULE_SERVICE_TYPE_HTTP:
- sServType.Format(L"HTTP");
- break;
- case RULE_SERVICE_TYPE_NNTP:
- sServType.Format(L"NNTP");
- break;
- case RULE_SERVICE_TYPE_POP3:
- sServType.Format(L"POP3");
- break;
- case RULE_SERVICE_TYPE_SMTP:
- sServType.Format(L"SMTP");
- break;
- }
- // 方向
- switch(pSession->ucDirection)
- {
- case RULE_DIRECTION_IN:
- sDirection = L"<——";
- break;
- case RULE_DIRECTION_OUT:
- sDirection = L"——>";
- break;
- case RULE_DIRECTION_IN_OUT:
- sDirection = L"<——>";
- break;
- default:
- sDirection = L"——";
- }
- sText.Format(L" %s 协议 【%s】 %s 【%s】 ", sServType, sLocal, sDirection, sRemote);
- return sText;
- }
- void CAppMonitorPage::DeleteSession(SESSION *pSession, BOOL bAppExit)
- {
- HTREEITEM hAppItem = FindAppItem(pSession->szPathName);
- if(hAppItem != NULL)
- {
- if(bAppExit) // 应用程序退出,删除整个应用程序项(包含下面的会话子项)
- {
- m_AppMonitorTree.DeleteItem(hAppItem);
- }
- else // 仅会话删除,在应用程序项下面找到这个会话,将之删除
- {
- HTREEITEM hSessionItem = FindSessionItem(hAppItem, pSession);
- if(hSessionItem != NULL)
- {
- m_AppMonitorTree.DeleteItem(hSessionItem);
- }
- // 没有Session存在了,将应用程序项也删除
- if(m_AppMonitorTree.GetNextItem(hAppItem, TVGN_CHILD) == NULL)
- m_AppMonitorTree.DeleteItem(hAppItem);
- }
- }
- }