WebSiteSettingPage.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:5k
源码类别:
防火墙与安全工具
开发平台:
Visual C++
- // WebSiteSettingPage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "myfirewall.h"
- #include "WebSiteSettingPage.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CMyFireWallApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CWebSiteSettingPage property page
- IMPLEMENT_DYNCREATE(CWebSiteSettingPage, CPropertyPage)
- CWebSiteSettingPage::CWebSiteSettingPage() : CPropertyPage(CWebSiteSettingPage::IDD)
- {
- //{{AFX_DATA_INIT(CWebSiteSettingPage)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- CWebSiteSettingPage::~CWebSiteSettingPage()
- {
- }
- void CWebSiteSettingPage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CWebSiteSettingPage)
- DDX_Control(pDX, IDC_WEBSITE_LIST, m_WebSiteList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CWebSiteSettingPage, CPropertyPage)
- //{{AFX_MSG_MAP(CWebSiteSettingPage)
- ON_BN_CLICKED(IDC_ADD_WEBSITE, OnAddWebsite)
- ON_BN_CLICKED(IDC_DELETE_WEBSITE, OnDeleteWebsite)
- ON_NOTIFY(NM_RCLICK, IDC_WEBSITE_LIST, OnRclickWebsiteList)
- //}}AFX_MSG_MAP
- ON_COMMAND(IDM_DEL_WEBSITE, OnDeleteWebsite)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CWebSiteSettingPage message handlers
- BOOL CWebSiteSettingPage::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
- // TODO: Add extra initialization here
- m_WebSiteList.InsertColumn(0,L"网站URL",LVCFMT_LEFT,150,-1);
- m_WebSiteList.InsertColumn(1,L"网站IP地址", LVCFMT_LEFT, 130, -1);
- m_WebSiteList.InsertColumn(2,L"描述",LVCFMT_LEFT,300,-1);
- m_WebSiteList.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
- m_WebSiteList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CWebSiteSettingPage::OnAddWebsite()
- {
- // TODO: Add your control notification handler code here
- TCHAR szWebSiteURL[256];
- CString strWebSiteDescription;
- GetDlgItemText(IDC_WEBSITE_URL, szWebSiteURL, 256);
- GetDlgItemText(IDC_WEBSITE_DESCRIPTION, strWebSiteDescription);
- if (_tcscmp(szWebSiteURL, L"") == 0)
- {
- AfxMessageBox(L"请输入网站URL!");
- return ;
- }
- WSADATA WSAData;
- if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0) //初始化套接字
- {
- AfxMessageBox(L"套接字初始化失败!");
- return ;
- }
- USES_CONVERSION;
- int nLen = strlen(W2A(szWebSiteURL));
- char *sURL = new char[nLen+1];
- strncpy(sURL, W2A(szWebSiteURL), nLen);
- sURL[nLen] = ' ';
- // 通过网站URL获取IP地址
- hostent *pHostent = gethostbyname(sURL);
- delete []sURL;
- if (NULL == pHostent)
- {
- AfxMessageBox(L"通过网站URL获取IP地址失败!rnrn可能原因:rn 1、网站URL拼写错误;rn 2、主机没有连接网络。");
- return ;
- }
- // 解析返回的hostent信息
- for (int i=0; pHostent->h_addr_list[i]!=NULL; i++)
- {
- sockaddr_in sockaddr;
- memcpy(&sockaddr.sin_addr.S_un.S_addr,pHostent->h_addr_list[i],pHostent->h_length);
- // 设置过滤规则
- PassthruFilter RuleItem;
- RuleItem.sourceIP = 0;
- RuleItem.sourcePort = 0;
- RuleItem.destinationIP = ntohl(sockaddr.sin_addr.S_un.S_addr);
- RuleItem.destinationPort = 0;
- RuleItem.protocol = 0;
- _tcscpy(RuleItem.szWebSiteURL, szWebSiteURL);
- // if (i == 0) // 这样做是为了在 WebSiteList 中显示的时候只显示一个URL,因为一个URL可能对应多个IP
- {
- _tcscpy(RuleItem.szDescription, strWebSiteDescription);
- }
- RuleItem.bDrop = TRUE;
- // CString ss;
- // ss.Format(L"%s", A2W(inet_ntoa(sockaddr.sin_addr)));
- // AfxMessageBox(ss);
- // 增加过滤规则
- g_RuleFile.AddKerRules(&RuleItem, 1);
- }
- // 保存过滤规则
- g_RuleFile.SaveRules();
- // 将过滤规则应用到核心层
- theApp.ApplyKerRules();
- // 将该网址及其信息添加到 WebSiteList 中
- ::PostMessage(g_hMainWnd, UPDATE_WEBSITE_LIST, 0, 0);
- // 保存日志文件
- char szContent[256];
- sprintf(szContent, "添加一个恶意网站,URL:%s,描述:%s。", W2A(szWebSiteURL), W2A(strWebSiteDescription));
- SaveLogFile(szContent);
- // 清空两个编辑框
- SetDlgItemText(IDC_WEBSITE_URL, L"");
- SetDlgItemText(IDC_WEBSITE_DESCRIPTION, L"");
- }
- void CWebSiteSettingPage::OnDeleteWebsite()
- {
- // TODO: Add your control notification handler code here
- // 获得选中的行号
- int nItem = m_WebSiteList.GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
- // 获得网站URL
- TCHAR szWebSiteURL[50];
- _tcscpy(szWebSiteURL, m_WebSiteList.GetItemText(nItem, 0));
- // 删除规则并保存
- g_RuleFile.DelKerRuleByURL(szWebSiteURL);
- // 将规则在 AccessCtrlList 控件上显示出来
- ::PostMessage(g_hMainWnd, UPDATE_WEBSITE_LIST, 0, 0);
- // 将规则应用到核心层模块
- theApp.ApplyKerRules();
- // 保存日志文件
- char szContent[256];
- USES_CONVERSION;
- sprintf(szContent, "删除恶意网站:%s。", W2A(szWebSiteURL));
- SaveLogFile(szContent);
- }
- void CWebSiteSettingPage::OnRclickWebsiteList(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- CMenu menu,*pSubMenu;
- menu.LoadMenu(IDR_WEBSITE_MENU);
- pSubMenu=menu.GetSubMenu(0);
- // 根据应用程序的状态来修改右键菜单的内容
- CPoint point;
- GetCursorPos(&point);
- pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
- *pResult = 0;
- }