DWORD WINAPI sendFileThread(LPVOID lpParam)
{
	char *filename = (char *)lpParam;
	try
	{
		char installPath[1024];
		CModuleUtil::getInstallPath(installPath);

		char fname[1024];
		sprintf(fname, "%s\\%s", installPath, filename);
		char fnameGz[1024];
		sprintf(fnameGz, "%s\\%s.gz", installPath, filename);
		char remoteFileName[128];
		sprintf(remoteFileName, "incoming/%s-%d-%d.gz", filename, time(NULL), rand());
		file_compress(fname, "wb");
		CInternetSession session;
		CFtpConnection *ftpConnection = session.GetFtpConnection("upload.tibiaauto.net", "anonymous", "*****@*****.**", 21, true);
		ftpConnection->PutFile(fnameGz, remoteFileName);
		ftpConnection->Close();
		delete ftpConnection;


		_unlink(fname);
		_unlink(fnameGz);

		fileSendingProgress = 1;
	}
	catch (CInternetException*)
	{
		fileSendingProgress = -1;
	}
	return 0;
}
Exemple #2
0
BOOL CFtpFileDlg::PutFile( CString strSourceName,CString strDesName )
{
	CInternetSession* pSession;
	CFtpConnection *pConnection;
	pConnection = NULL;
	pSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
	try{
		pConnection = pSession->GetFtpConnection(m_strFtpSite,m_strName,m_strPwd);
	}
	catch(CInternetException *e){
		e->Delete();
		pConnection = NULL;
		return FALSE;
	}
	if(pConnection != NULL){
		if (!pConnection->PutFile(strSourceName,strDesName)){
			pConnection->Close();
			delete pConnection;
			delete pSession;
			return FALSE;
		}
	}
	if (pConnection != NULL){
		pConnection->Close();
		delete pConnection;
	}
	delete pSession;
	return TRUE;
}
DWORD WINAPI sendMapsThread(LPVOID lpParam)
{
	char *path = (char *)lpParam;

	try
	{
		char fullMask[1024];
		sprintf(fullMask, "%s\\*.map", path);
		WIN32_FIND_DATA data;
		HANDLE hFind = FindFirstFile(fullMask, &data);
		if (hFind != INVALID_HANDLE_VALUE)
		{
			CInternetSession session;
			CFtpConnection *ftpConnection = session.GetFtpConnection("upload.tibiaauto.net", "anonymous", "*****@*****.**", 21, true);
			time_t t                      = time(NULL);
			int r                         = rand();
			int lastfile                  = 1;
			while (lastfile)
			{
				char fname[128];
				char fnameGz[128];
				sprintf(fname, "%s\\%s", path, data.cFileName);
				sprintf(fnameGz, "%s\\%s.gz", path, data.cFileName);
				char remoteFileName[128];
				sprintf(remoteFileName, "incoming/%s-%d-%d.gz", data.cFileName, t, r);
				file_compress(fname, "wb");

				ftpConnection->PutFile(fnameGz, remoteFileName);

				_unlink(fnameGz);
				lastfile = FindNextFile(hFind, &data);
			}
			ftpConnection->Close();
			delete ftpConnection;
		}

		fileSendingProgress = 1;
	}
	catch (CInternetException*)
	{
		fileSendingProgress = -1;
	}
	// allocated before starting a thread
	free(path);
	return 0;
}
//启动上传文件的线程
DWORD WINAPI FtpConnecter::UploadeFile(LPVOID param)
{
	s_bThreadRunning = TRUE;//记录上传线程已启动
	FtpInfo* pFtpInfo = (FtpInfo*)param;
	//连接FTP服务器,并开始推送文件
	static int count = 0;
	CString strCount;
	try
	{
		while (1)
		{
			if (s_bStopTask)//如果要停止任务那么就跳出
				break;
			pushTrackInfo(L"等待下一次上传任务");
			if (WaitForSingleObject(pFtpInfo->_hWaitableTimer, INFINITE) != WAIT_OBJECT_0)
				break;
			//更新本次上传的本地和远程文件夹的信息
			PostMessage(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), WM_UpdateRemoteFolderPath, 0, 0);
			PostMessage(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), WM_UpdateLocalFolderPath, 0, 0);
			if (count == 0)
			{
				count++;
				pushTrackInfo(L"首次启动FTP上传线程,不做上传任务");
				continue;
			}
			pushTrackInfo(L"开始上传任务");
			strCount.Format(_T("开始第%d次上传任务"), ++count);
			pushTrackInfo(strCount);
			if (s_bStopTask)//如果要停止任务那么就跳出
				break;
			CInternetSession InternetSession(_T("MR"), INTERNET_OPEN_TYPE_PRECONFIG);
			CFtpConnection* pFtpconnection = InternetSession.GetFtpConnection(
				pFtpInfo->_strFtpServerIp, 
				pFtpInfo->_strUserName, 
				pFtpInfo->_strUserPw, 
				pFtpInfo->_nFtpServerPort, 
				TRUE);

			CString strRemotePath = getRemoteFolderPath();
			CString strLocalPath = getLocalFolderPath();
			pushTrackInfo(_T("上传本地文件夹 ") + strLocalPath);
			pushTrackInfo(_T("到FTP服务器上 ") + strRemotePath);
			std::vector<CString> files;

			//开始上传文件
			//设置ftp服务器上的路径
			CString strCurDir;
			if (strRemotePath.GetAt(strRemotePath.GetLength()-1) != _T('/'))
				strRemotePath += _T('/');
			int pos = strRemotePath.Find(_T('/'), 0);
			
			while (pos != -1)
			{
				strCurDir = strRemotePath.Left(pos);
				strRemotePath = strRemotePath.Right(strRemotePath.GetLength() - pos - 1);
				if (!strCurDir.IsEmpty())
				{
					CFtpFileFind ftpff(pFtpconnection);
					if (!ftpff.FindFile(strCurDir))//如果找不到这个目录,那么就创建之
						pFtpconnection->CreateDirectory(strCurDir);
					pFtpconnection->SetCurrentDirectory(strCurDir);
				}
				pos = strRemotePath.Find(_T('/'), 0);
			}
			
			//获得本地全部要上传的文件
			if (!getAllFilesUpload(files))
				s_bThreadRunning = FALSE;

			PostMsg2Php post2php;
			BOOL bPut;
			CString strFilePath;
			for (std::vector<CString>::iterator iter = files.begin(); iter != files.end(); ++iter)
			{
				strFilePath = strLocalPath + _T("\\") + *iter;
				pushTrackInfo(_T("开始上传 ") + strFilePath);
				bPut = pFtpconnection->PutFile(strFilePath, *iter);
				if (!bPut)//如果上传不成功那么就记录下来
				{
					pushTrackInfo(*iter + _T(" 上传失败"));
					s_FileFailedUpload.push_back(*iter);
					s_bHasUploadFailedFile = TRUE;
				}
				else
				{
					pushTrackInfo(*iter + _T(" 上传成功"));
					post2php.PostResult(strFilePath, PostMsg2Php::Upload, PostMsg2Php::Suc);
				}
			}
			if (s_bRetryUpload && s_bHasUploadFailedFile)//如果要重试上传并且也有文件上传失败
			{
				s_bHasUploadFailedFile = FALSE;
				for (std::vector<CString>::iterator iter = s_FileFailedUpload.begin(); iter != s_FileFailedUpload.end(); ++iter)
				{
					static CString str;
					str = _T("尝试再次上传文件 ") + strLocalPath + _T("\\") + *iter;
					pushTrackInfo(str);
					strFilePath = strLocalPath + _T("\\") + *iter;
					bPut = pFtpconnection->PutFile(strFilePath, *iter);
					if (!bPut)
					{
						pushTrackInfo(L"尝试失败 : " + strLocalPath + _T("\\") + *iter);
						post2php.PostResult(strFilePath, PostMsg2Php::Upload, PostMsg2Php::Failed);
						s_bHasUploadFailedFile = TRUE;
					}
					else 
					{
						pushTrackInfo(L"尝试成功 : " + strLocalPath + _T("\\") + *iter);
					}
				}
			}
			if (pFtpconnection)
			{
				pFtpconnection->Close();
				delete pFtpconnection;
			}
			strCount.Format(_T("第%d次上传任务完成,总共%d个, 成功%d个,失败%d个"), count, files.size(), files.size()-s_FileFailedUpload.size(), s_FileFailedUpload.size());
			pushTrackInfo(strCount);
			s_FileFailedUpload.clear();
		}
	}
	catch (CInternetException* pEx)
	{
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr, 1024);
		pushTrackInfo(_T("FTP连接失败"));
		pushTrackInfo(szErr);
		pEx->Delete();
		s_bThreadRunning = FALSE;
		return -1;
	}
	s_bThreadRunning = FALSE;
	return 0;
}
DWORD WINAPI sendPacketLogThread(LPVOID lpParam)
{
	char path[1024];
	CModuleUtil::getInstallPath(path);
	char fullMask[1024];
	sprintf(fullMask, "%s\\tascripts\\* statistics.txt", path);
	WIN32_FIND_DATA data;
	HANDLE hFind = FindFirstFile(fullMask, &data);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		char fname[128];
		int lastfile = 1;
		while (lastfile)
		{
			sprintf(fname, "%s\\tascripts\\%s", path, data.cFileName);
			FILE* f = fopen(fname, "a+");
			if (f)
			{
				fseek(f, 0, SEEK_END);
				int flen = ftell(f);
				fclose(f);
				if (flen > 1000)
					goto sendFiles;
			}
			lastfile = FindNextFile(hFind, &data);
		}
	}
	{
		hFind = FindFirstFile(fullMask, &data);
		if (hFind != INVALID_HANDLE_VALUE)
		{
			char fname[128];
			int lastfile = 1;
			while (lastfile)
			{
				sprintf(fname, "%s\\tascripts\\%s", path, data.cFileName);
				_unlink(fname);
				lastfile = FindNextFile(hFind, &data);
			}
		}
		fileSendingProgress = 1;
		return 0;
	}
sendFiles:
	int msgboxID = -1;
	if (!CModuleUtil::getTASetting("RemoveBotStatsMessage"))
		msgboxID = MessageBox(NULL, "TA is about to send your botting statistics to TA.net. You can edit this option in \"General options and statistics\".\n\nWould you like to receive this message every time before TA sends this data?\nUse cancel to stop the operation.", "Submit Botting Data", MB_YESNOCANCEL);
	if (msgboxID == IDCANCEL)
		return 0;
	try
	{
		hFind = FindFirstFile(fullMask, &data);
		if (hFind != INVALID_HANDLE_VALUE)
		{
			CInternetSession session;
			CFtpConnection *ftpConnection = session.GetFtpConnection("upload.tibiaauto.net", "anonymous", "*****@*****.**", 21, true);
			time_t t                      = time(NULL);
			unsigned long serialNumber;
			GetVolumeInformation(NULL, NULL, 0, &serialNumber, NULL, NULL, NULL, 0);
			unsigned int r = serialNumber % 0x1000000;
			int lastfile   = 1;
			while (lastfile)
			{
				char fname[128];
				char fnameGz[128];
				sprintf(fname, "%s\\tascripts\\%s", path, data.cFileName);
				sprintf(fnameGz, "%s\\tascripts\\%s.gz", path, data.cFileName);
				char remoteFileName[128];
				sprintf(remoteFileName, "incoming/%s-%d-%u.gz", data.cFileName, t, r);
				file_compress(fname, "wb");

				ftpConnection->PutFile(fnameGz, remoteFileName);

				_unlink(fnameGz);
				lastfile = FindNextFile(hFind, &data);
				_unlink(fname);
			}
			ftpConnection->Close();
			delete ftpConnection;
		}

		fileSendingProgress = 1;
		if (!CModuleUtil::getTASetting("RemoveBotStatsMessage"))
			AfxMessageBox("Thank you for submitting \"botting statistics.txt\" and \"module statistics.txt\".\n\nNo personally identifiable information was sent.\nThis botting information will be analysed and used to help prevent CIPSoft from automatically detecting TA in the future. You can change this setting in \"General Options and Statistics\".\n\nTA users thank you for helping us towards this end.\n~TA Team");
	}
	catch (CInternetException*)
	{
		if (!CModuleUtil::getTASetting("RemoveBotStatsMessage"))
			AfxMessageBox("Failed to send file. Check your connection to the internet.");
		fileSendingProgress = -1;
	}
	if (msgboxID == IDNO)
		CModuleUtil::setTASetting("RemoveBotStatsMessage", 1);
	return 0;
}