📄 rangedlg.cpp
字号:
// rangeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "range.h"
#include "rangeDlg.h"
#include "IPDialog.h"
#include "ProxyDlg.h"
#include "AboutDlg.h"
#include <math.h>
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int totalnum;
HWND hWnd;
CWinThread* pThread;
BOOL useProxy=FALSE,beTail=FALSE;
LPTSTR ProxyServer;
int ProxyPort;
long running;
CRangeDlg* r_dlg;
/////////////////////////////////////////////////////////////////////////////
// CRangeDlg dialog
CRangeDlg::CRangeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRangeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRangeDlg)
m_from = _T("");
m_to = _T("");
m_math = _T("GET");
m_cont = _T("");
m_port = 80;
m_thre = 100;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRangeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRangeDlg)
DDX_Text(pDX, IDC_EDIT_FROM, m_from);
DDX_Text(pDX, IDC_EDIT_TO, m_to);
DDX_CBString(pDX, IDC_COMBO_MATH, m_math);
DDX_Text(pDX, IDC_EDIT_CONT, m_cont);
DDX_Text(pDX, IDC_EDIT_PORT, m_port);
DDX_Text(pDX, IDC_EDIT_THRE, m_thre);
DDV_MinMaxInt(pDX, m_port, 0, 65535);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRangeDlg, CDialog)
//{{AFX_MSG_MAP(CRangeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_MENU_DEL, OnMenuDel)
ON_BN_CLICKED(IDC_SCAN, OnScan)
ON_EN_UPDATE(IDC_EDIT_FROM, OnUpdateEditFrom)
ON_BN_CLICKED(IDC_NAME2IP, OnName2ip)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_PROXY, OnProxy)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_LOG, OnLog)
ON_WM_CONTEXTMENU()
ON_MESSAGE(WM_GETFOCUS,OnGetfocus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRangeDlg message handlers
BOOL CRangeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
hWnd=GetSafeHwnd();
// TODO: Add extra initialization here
m_spin=(CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_THRE);
m_prog=(CProgressCtrl *)GetDlgItem(IDC_PROG);
m_list=(CListCtrl *)GetDlgItem(IDC_RES);
m_contlist=(CListBox *)GetDlgItem(IDC_LIST_CONT);
m_spin->SetRange(0,1000);
m_spin->SetPos(100);
LV_COLUMN lvcol;
lvcol.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH;
lvcol.fmt=LVCFMT_LEFT;
lvcol.pszText=" Host";
lvcol.iSubItem=0;
lvcol.cx=100;
m_list->InsertColumn(0,&lvcol);
lvcol.pszText=" result content";
lvcol.iSubItem=1;
lvcol.cx=300;
m_list->InsertColumn(1,&lvcol);
r_dlg=this;
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CRangeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRangeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRangeDlg::OnUpdateEditFrom()
{
int i;
int dot=0;
LPTSTR from=NULL;
LPTSTR to=NULL;
UpdateData();
int fromCount,toCount;
fromCount=m_from.GetLength();
from=m_from.GetBuffer(fromCount);
toCount=m_to.GetLength();
if((fromCount-toCount)>1)
{
i=1;
m_to=m_from;
to=m_to.GetBuffer(fromCount);
while(to[fromCount-i]!='.')
{
to[fromCount-i]=0x0;
i++;
if(i>fromCount)
break;
}
m_to.ReleaseBuffer();
m_to+="254";
beTail=TRUE;
UpdateData(FALSE);
return;
}
for(i=0;i<fromCount;i++)
{
if(from[i]=='.')
dot++;
}
m_from.ReleaseBuffer();
if(dot<3)
{
beTail=FALSE;
m_to=m_from;
}
if(dot==3)
{
if(beTail==FALSE)
{
m_to=m_from;
m_to+="254";
beTail=TRUE;
}
}
m_from.ReleaseBuffer();
m_to.ReleaseBuffer();
UpdateData(FALSE);
}
void CRangeDlg::OnName2ip()
{
CIPDialog ipdlg;
int nResponse = ipdlg.DoModal();
if (nResponse == IDOK)
{
m_from=ipdlg.c_start;
m_to=ipdlg.c_end;
UpdateData(FALSE);
}
}
void CRangeDlg::OnAdd()
{
UpdateData();
if(!m_cont.IsEmpty())
m_contlist->AddString(m_cont);
m_cont.Empty();
UpdateData(FALSE);
GetDlgItem(IDC_EDIT_CONT)->SetFocus();
}
void CRangeDlg::OnScan()
{
UpdateData();
if(m_from.IsEmpty()||m_to.IsEmpty())
{
PostMessage(WM_GETFOCUS,0,0);
return;
}
if(m_contlist->GetCount()==0)
{
MessageBox("plz fill out the content","wrong",MB_OK);
return;
}
GetDlgItem(IDC_SCAN)->EnableWindow(FALSE);
pThread=AfxBeginThread(sScan,this);
}
void CRangeDlg::OnStop()
{
if(pThread!=NULL)
{
TerminateThread(pThread->m_hThread,0);
}
GetDlgItem(IDC_SCAN)->EnableWindow(TRUE);
}
void CRangeDlg::OnClear()
{
SetDlgItemText(IDC_STATIC_NUM,"");
m_prog->SetPos(0);
m_list->DeleteAllItems();
m_from.Empty();
m_to.Empty();
UpdateData(FALSE);
}
void CRangeDlg::OnProxy()
{
CProxyDlg dlg;
if(dlg.DoModal()==IDOK)
{
useProxy=dlg.m_use;
ProxyServer=dlg.m_server.GetBuffer(100);
ProxyPort=dlg.m_port;
}
}
void CRangeDlg::OnAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CRangeDlg::OnLog()
{
int listCount;
char list[1024];
UpdateData();
SYSTEMTIME time;
CString content,temp;
temp=m_from+"-"+m_to+".html";
if(m_from.IsEmpty())
temp="noname.html";
GetLocalTime(&time);
CFile file;
file.Open(temp,CFile::modeCreate|CFile::modeWrite);
content="<HTML>\n<HEAD>\n<TITLE> RangeScan Report </TITLE>\n</HEAD>\n<BODY>\n";
content+="<br><p align=\"center\"><b>RangeScan Report </b> <a href=\"http://www.xfocus.org\">【xfocus.org】</a></p>\n";
content+="<br><hr color=\"#0\" size=\"1\" width=\"100%\">\n";
content+="<br>Date: ";
temp.Format("%d-%d-%d %d:%d:%d\n",time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond);
content+=temp;
content+="<br><br>Scan range: "+m_from+"->"+m_to+"\n";
content+="<br><br>Result:<br>";
listCount=m_list->GetItemCount();
for(int i=0;i<listCount;i++)
{
content+="<br>\n";
memset(list,0x0,1024);
m_list->GetItemText(i,0,list,16);
content+=list;
memset(list,0x0,1024);
m_list->GetItemText(i,1,list,1024);
content+=list;
}
content+="<br><br><br><br><hr color=\"#0\" size=\"1\" width=\"100%\">\n";
content+="<br><center>Copyright © 2000-2001 xfocus.org</center><br>\n";
content+="</BODY>\n</HTML>\n";
file.Write(content,content.GetLength());
file.Close();
MessageBox("make report successful!","success",MB_OK);
}
void CRangeDlg::OnContextMenu(CWnd* pWnd, CPoint pos)
{
CPoint PointInList = pos;
m_contlist->ScreenToClient(&PointInList);
if(PointInList.x>200||PointInList.y>105||PointInList.x<0||PointInList.y<0)
return;
CMenu menu;
CMenu *ptrMenu;
menu.LoadMenu(IDR_RCLICK_MENU);
ptrMenu=menu.GetSubMenu(0);
ptrMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
pos.x,
pos.y,
this,
NULL);
}
void CRangeDlg::OnMenuDel()
{
int listCount=m_contlist->GetCount();
if(listCount!=0)
m_contlist->DeleteString(m_contlist->GetCurSel());
}
LONG CRangeDlg::OnGetfocus(LONG lParam, UINT wParam)
{
((CEdit *)GetDlgItem(IDC_EDIT_FROM))->SetSel(0,m_from.GetLength());
((CEdit *)GetDlgItem(IDC_EDIT_FROM))->SetFocus();
return 0;
}
UINT sScan(LPVOID pParam)
{
int i,j,k;
int hostnum;
int contnum;
int threadnum;
int port;
int run[4];
int range=0;
int scanned=0;
CString mathed;
CString cont;
struct hostinfo* thehost;
CRangeDlg* dlg=(CRangeDlg*)pParam;
dlg->UpdateData();
if(dlg->theip.GetIP(dlg->m_from,dlg->m_to)==FALSE)
{
PostMessage(hWnd,WM_GETFOCUS,0,0);
dlg->GetDlgItem(IDC_SCAN)->EnableWindow(TRUE);
return 0;
}
threadnum=dlg->m_thre;
hostnum=dlg->theip.ComputeHostNum();
if(hostnum<0)
{
dlg->MessageBox("wrong ip order!","wrong",MB_OK);
PostMessage(hWnd,WM_GETFOCUS,0,0);
dlg->GetDlgItem(IDC_SCAN)->EnableWindow(TRUE);
return 0;
}
contnum=dlg->m_contlist->GetCount();
if(contnum==0) contnum=1;
totalnum=hostnum*contnum;
dlg->m_prog->SetRange(0,totalnum);
dlg->m_prog->SetPos(0);
CString numrange;
numrange.Format("%d/%d",0,totalnum);
dlg->SetDlgItemText(IDC_STATIC_NUM,numrange);
threadnum=dlg->m_thre;
port=dlg->m_port;
mathed=dlg->m_math;
thehost=new hostinfo[totalnum+1];
for(i=0;i<hostnum;i++)
{
thehost[i].port=port;
thehost[i].mathed=mathed;
}
int thread=0;
running=0;
for(i=0;i<contnum;i++)
{
scanned=0;
dlg->m_contlist->GetText(i,cont);
for(k=0;k<4;k++)
{
run[k]=dlg->theip.start[k];
}
if(i==0)
{
for(j=0;j<threadnum;j++)
{
for(k=0;k<4;k++)
{
thehost[j].ip[k]=run[k];
}
thehost[j].cont=cont;
AfxBeginThread(getScan,(LPVOID)&thehost[j]);
thread++;scanned++;range++;
dlg->m_prog->SetPos(range);
numrange.Format("%d/%d",range,totalnum);
dlg->SetDlgItemText(IDC_STATIC_NUM,numrange);
ComputeNext(run,1);
if(scanned==hostnum)
break;
}
}
else
j=0;
Sleep(5000);
while(1)
{
if(running>threadnum)
{
Sleep(1000);
continue;
}
j++;
for(k=0;k<4;k++)
{
thehost[j].ip[k]=run[k];
}
thehost[j].cont=cont;
AfxBeginThread(getScan,(LPVOID)&thehost[j]);
scanned++;range++;
dlg->m_prog->SetPos(range);
numrange.Format("%d/%d",range,totalnum);
dlg->SetDlgItemText(IDC_STATIC_NUM,numrange);
ComputeNext(run,1);
if(scanned==hostnum)
break;
Sleep(100);
}
}
dlg->m_prog->SetPos(hostnum*contnum);
dlg->GetDlgItem(IDC_SCAN)->EnableWindow(TRUE);
return 0;
}
UINT getScan(LPVOID pParam)
{
BOOL r;
CSocket scanSocket;
scanSocket.Create();
int port;
char *scancont;
char rmt_host[16];
char rbuff[1024];
InterlockedIncrement(&running);
struct hostinfo* thehost=(struct hostinfo *)pParam;
port=thehost->port;
scancont=new char[thehost->cont.GetLength()];
scancont=thehost->cont.GetBuffer(thehost->cont.GetLength());
sprintf(rmt_host,"%d.%d.%d.%d",(unsigned char )thehost->ip[0],
(unsigned char ) thehost->ip[1],
(unsigned char ) thehost->ip[2],
(unsigned char ) thehost->ip[3]);
CString url=thehost->mathed;
url+=" ";
if(useProxy==TRUE)
{
url+="http://";
url+=rmt_host;
}
url+=scancont;
url+=" HTTP/1.0\r\n\r\n";
if(useProxy==FALSE)
r=scanSocket.Connect(rmt_host,port);
else
r=scanSocket.Connect(ProxyServer,ProxyPort);
if(r == FALSE)
{
InterlockedDecrement(&running);
return 0;
}
scanSocket.Send(url,url.GetLength());
memset(rbuff,0,1024);
//Sleep(1000);
scanSocket.Receive(rbuff,1024);
if (strstr(rbuff,"200 OK")!=NULL)
{
LV_ITEM lvitem;
lvitem.mask=LVIF_TEXT|LVIF_PARAM;
lvitem.iSubItem=0;
lvitem.iImage=0;
lvitem.lParam=1;
lvitem.iItem=0;
lvitem.pszText=rmt_host;
int iPos=r_dlg->m_list->InsertItem(&lvitem);
lvitem.mask=LVIF_TEXT;
lvitem.iItem=iPos;
lvitem.iSubItem=1;
lvitem.pszText=scancont;
r_dlg->m_list->SetItem(&lvitem);
}
scanSocket.Close();
thehost->cont.ReleaseBuffer();
InterlockedDecrement(&running);
return 0;
}
void ComputeNext(int *pIp,int plusNum)
{
for(int i=0;i<plusNum;i++)
{
pIp[3]++;
if(pIp[3]==255)
{
pIp[2]++;
if(pIp[2]==255)
{
pIp[1]++;
if(pIp[1]==255)
{
pIp[0]++;
pIp[1]=1;
}
pIp[2]=1;
}
pIp[3]=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -