Ejemplo n.º 1
0
void* CDTManager::NewTask(CAtlArray<CString>* urls,CString md5,CString localfile)
{
	IDownload* down;
	CreateDownloadObject(__uuidof(IDownload),(void**)&down);
	_tks[down]=urls;
	size_t ppp=0;

	CString url;
	int64 len;
	int cu=0;
	if(GetContiInfo(localfile+L"c",url,len,cu))
	{
		for (;ppp<urls->GetCount()&&url!=(*urls)[ppp];ppp++)
		{
		};
		if(ppp==urls->GetCount())
		{
			ppp=0;
			DeleteFile(localfile+DOWNLOAD_TEMP_FILE_SUFFIX);
			DeleteFile(localfile+DOWNLOAD_TEMP_FILE_INFO_SUFFIX);
			DeleteFile(localfile+L"c");
		}
	}

	down->SetDownloadInfo((*urls)[ppp],localfile);
	LastDownInfo* lst=new LastDownInfo;
	lst->pos=ppp;
	lst->cur=cu;
	lst->urls=urls;
	lst->tk=down;
	_md5s[down]=md5;
	down->SetUserData((void*)lst);
	down->SetObserver(this);
	down->Start(cu);
	return down;
}
Ejemplo n.º 2
0
DWORD CImplRepairVul::_ThreadFuncDownloadImpl()
{
	do 
	{
		m_thinfo.Reset();
		
		INT nTotalFixItem = m_arrDownloadItem.GetSize();
		if(nTotalFixItem==0)
			break;
		
		m_thinfo.totalNum = nTotalFixItem;
		
		//
		BOOL bImportLocal = m_dwRepairFlags&VULFLAG_REPAIR_IMPORT_LOCAL;
		BOOL bDownloadIfFailImport = m_dwRepairFlags&VULFLAG_REPAIR_DOWNLOAD_IF_FAIL_IMPORT;
		bImportLocal = bImportLocal && !m_strImportPath.IsEmpty();
		
		// 1. Download 

		CFileDownloader fdSingle;
		m_thinfo.m_pFileDownloaderSingle = &fdSingle;
		fdSingle.SetObserver( this );

		IDownload *fd = NULL;
		ATLVERIFY( SUCCEEDED(CreateDownloadObject(__uuidof(IDownload), (VOID**)&fd)) );
		m_thinfo.m_pFileDownloader = fd;
		fd->SetObserver( this );

		m_thinfo.m_fileDownloadLocal.SetObserver(this);
		for(int i=0; i<m_arrDownloadItem.GetSize() && !m_bCanceled; ++i)
		{
			T_RepairItem &item = m_arrDownloadItem[i];
            BOOL bNeedCheckCert = item.bCheckCert;
			DWORD dwDownfileStat = 0;
			
			Notify(EVulfix_DownloadBegin, item.nID, i, m_thinfo.totalNum);
			BOOL downSuccess = FALSE;
			if(item.type==VTYPE_SOFTLEAK && item.bDisableCOM)
			{
				downSuccess = TRUE;
			}
			else
			{
				downSuccess = FALSE;
				item.strFilename = m_strDownloadPath;
				CString strFilename;
				if(GetFileNameFromUrl(item.strURL, strFilename))
				{
					// 兼容正常文件名 
                    item.strFilename.Format(_T("%s\\%s"), m_strDownloadPath, strFilename);
					
					if(!IsFileExist(item.strFilename))
					{
                        item.strFilename.Format(_T("%s\\%s%s"), m_strDownloadPath, BK_FILE_PREFIX, strFilename);
						if(!IsFileExist(item.strFilename))
						{
							item.strFilename.Format(_T("%s\\%s"), m_strDownloadPath, strFilename);

							// download if file not exists 
							dwDownfileStat |= ERDOWN_LOCAL_NOT_EXISTS;
							BOOL toDownloadFromWeb = TRUE;
							if(bImportLocal)
							{
								m_thinfo.m_fileDownloadLocal.SetUserData((LPVOID)item.nID);

								CString strImportFilename;

								LPCTSTR lpszHttp = _T("http://");								
								if( _tcsnicmp(lpszHttp, m_strImportPath, _tcslen(lpszHttp))==0 )
								{
									// 支持本地http 导入 
									strImportFilename.Format(_T("%s/%s"), m_strImportPath, strFilename);
									fd->SetUserData((LPVOID)item.nID);
									fd->SetDownloadInfo(strImportFilename, item.strFilename);
									fd->Fetch();
								}
								else
								{
									// 本地路径导入 
									strImportFilename.Format(_T("%s\\%s"), m_strImportPath, strFilename);
									m_thinfo.m_fileDownloadLocal.SetDownloadInfo(strImportFilename, item.strFilename);
									if( !m_thinfo.m_fileDownloadLocal.Fetch() )
									{
										if(!m_bCanceled)
										{
											strImportFilename.Format(_T("%s\\%s%s"), m_strImportPath, BK_FILE_PREFIX, strFilename);
											m_thinfo.m_fileDownloadLocal.SetDownloadInfo(strImportFilename, item.strFilename);
											m_thinfo.m_fileDownloadLocal.Fetch();
										}
									}
								}

								if(PathFileExists(item.strFilename))
								{
									toDownloadFromWeb = FALSE;
									dwDownfileStat |= ERDOWN_IMPORT_LOCAL;
								}
								else
								{
									toDownloadFromWeb = bDownloadIfFailImport;
								}
							}
							
							if(toDownloadFromWeb && !m_bCanceled && !IsFileExist(item.strFilename) )
							{
								m_bDownloadHasReceivedData = FALSE;
                                for (int i = 0; i < 3 && !downSuccess && !m_bCanceled; i ++)
                                {
									// Download Fail, Wait for 3 seconds then to retry 
									if(i>0)
									{
										DWORD dwTimeWaitEnd = 3*1000 + GetTickCount();
										for(;!m_bCanceled && GetTickCount()<dwTimeWaitEnd;)
										{
											::Sleep(100);
										}
										if(m_bCanceled)
											break;
									}

                                    fd->SetUserData((LPVOID)item.nID);
                                    fd->SetDownloadInfo(item.strURL, item.strFilename);
                                    downSuccess = fd->Fetch();

									IDownStat *pdownstat = fd->GetDownloadStat();
									if(pdownstat && pdownstat->Downloaded()>0)
										m_bDownloadHasReceivedData = TRUE;

                                    if (downSuccess && bNeedCheckCert)
                                    {
                                        // 校验补丁包数字签名 
                                        HRESULT hVerifyRet = _VerifyWinTrustCert(item.strFilename);
                                        if(!IsValidPatchFile(item.strFilename) || FAILED(hVerifyRet))
                                        {
											dwDownfileStat |= ERDOWN_MULTIDOWN_ERRORSIGN;
                                            Notify(EVulfix_Download_Check_Error, item.nID, hVerifyRet, 0);
#ifdef _DEBUG
											MessageBox(NULL, _T("下载文件签名验证失败!!!"), NULL, MB_OK);
#else
                                            DeleteFile(item.strFilename);
											m_bDownloadHasReceivedData = TRUE;

											// 校验失败走单线程 
											downSuccess = fdSingle.Download(item.strURL, item.strFilename , NULL, (LPVOID)(INT_PTR)item.nID);
#endif                                      
											bNeedCheckCert = FALSE;
                                            break;
                                        }
                                        bNeedCheckCert = FALSE;
                                    }
                                }

								if(!m_bDownloadHasReceivedData)
									dwDownfileStat |= ERDOWN_CANNOT_REACH_OFFICIAL;

								// 如果未收到任何数据 
                                if (!downSuccess && !m_bCanceled && !m_bDownloadHasReceivedData && !item.strMirrorURL.IsEmpty())
                                {
									dwDownfileStat |= ERDOWN_MIRROR_USED;

									// 删除之前下载的所有文件?! , 因为如果一点数据都没有下载到的话, 就没必要删除临时文件了
                                    fd->SetUserData((LPVOID)item.nID);
                                    fd->SetDownloadInfo(item.strMirrorURL, item.strFilename);
                                    fd->Fetch(1);
                                }
							}
						}
					}
					
					downSuccess = IsFileExist(item.strFilename);
					if(downSuccess)
					{
                        if (bNeedCheckCert)
                        {
                            // 校验补丁包数字签名
                            HRESULT hVerifyRet = _VerifyWinTrustCert(item.strFilename);
                            if(FAILED(hVerifyRet))
                            {
                                Notify(EVulfix_Download_Check_Error, item.nID, hVerifyRet, 1);
                                DeleteFile(item.strFilename);
                                downSuccess = FALSE;
								dwDownfileStat |= ERDOWN_ERRORSIGN;
                            }
                        }
                        if(!IsValidPatchFile(item.strFilename))
						{
							DeleteFile(item.strFilename);
							downSuccess = FALSE;
						}
					}
				}
			}
			item.stateDownload = downSuccess;
			Notify(downSuccess ? EVulfix_DownloadEnd : EVulfix_DownloadError, item.nID, m_bCanceled, dwDownfileStat);
			
			if( downSuccess )
			{
				++m_thinfo.nDownSuccess;
				SetEvent(m_thinfo.m_hInstallEvent);
			}
		}
		{
			CObjGuard __guard__(m_thinfo.m_objLockFileDownloader, TRUE);
			m_thinfo.m_pFileDownloaderSingle = NULL;
            m_thinfo.m_pFileDownloader = NULL;
			m_thinfo.m_pFileDownloaderSingle = NULL;
			m_thinfo.m_fileDownloadLocal.SetObserver(NULL);
		}
		fd->SetObserver( NULL );
		fd->Release();
		fd = NULL;		
	} while (FALSE);
	
	m_thinfo.isDownloadDone = TRUE;
	SetEvent(m_thinfo.m_hInstallEvent);
	return 0;
}