Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. #pragma comment(lib, "Wininet")
  2. #include "Windows.h"
  3. #include <Windows.h>
  4. #include <WinInet.h>
  5. #include <tlhelp32.h>
  6. #include <iostream>
  7. #include <tchar.h>
  8. #include <conio.h>
  9. #include <tlhelp32.h>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <vector>
  13. #include <algorithm>
  14.  
  15. #define MAX_KEY_LENGTH 255
  16. #define MAX_VALUE_NAME 16383
  17. #define BUF_SIZE 5000
  18.  
  19.  
  20. PCTSTR accept_types[] = { TEXT("*/*"), 0 };
  21.  
  22. using namespace std;
  23.  
  24. DWORD WINAPI secondThreadFunction(LPVOID lpParam);
  25.  
  26.  
  27. HANDLE CreateAndReturnFileMapping(const char* name)
  28. {
  29. HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, BUF_SIZE, name);
  30. if (hMap == NULL)
  31. {
  32. printf("CreateFileMapping failed with error 0x%08x\n", GetLastError());
  33. return NULL;
  34. }
  35. return hMap;
  36. }
  37.  
  38. HANDLE CreateAndReturnMutex(HANDLE hMap, const char* name)
  39. {
  40. HANDLE hMutex = CreateMutex(NULL, FALSE, name);
  41. if (hMutex == NULL)
  42. {
  43. printf("CreateMutex failed with error 0x%08x\n", GetLastError());
  44. CloseHandle(hMap);
  45. return NULL;
  46. }
  47. return hMutex;
  48. }
  49.  
  50. LPTSTR CreateAndReturnMapViewOfFile(HANDLE hMap, HANDLE hSync)
  51. {
  52. LPTSTR pMap = (LPTSTR)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, BUF_SIZE);
  53. if (pMap == NULL)
  54. {
  55. printf("MapViewOfFile failed with error 0x%08x\n", GetLastError());
  56. CloseHandle(hMap);
  57. CloseHandle(hSync);
  58. return NULL;
  59. }
  60. return pMap;
  61. }
  62.  
  63.  
  64. string get_server_link(string &link) {
  65. for (int i = 8; i < (int)link.size(); i++)
  66. if (link[i] == '/')
  67. return link.substr(8, i - 8);
  68. return "";
  69. }
  70.  
  71. string get_name(string &link) {
  72. for (int i = 8; i < (int)link.size(); i++)
  73. if (link[i] == '/')
  74. return link.substr(i + 1);
  75. return "";
  76. }
  77.  
  78. vector<string> getAllLinks(string &html) {
  79. vector<string> links;
  80.  
  81. for (int i = 0; i + 5 < html.size(); ++i) {
  82. if (html.substr(i, 6) != "href=\"")
  83. continue;
  84.  
  85. int j = i + 6;
  86.  
  87. string link;
  88. while (html[j] != '"')
  89. link += html[j++];
  90.  
  91. if (link.size())
  92. links.push_back(link);
  93.  
  94. i = j;
  95. }
  96.  
  97. sort(links.begin(), links.end());
  98. links.erase(unique(links.begin(), links.end()), links.end());
  99.  
  100. return links;
  101. }
  102.  
  103. vector<string> getFilteredLinks(vector<string>links) {
  104. vector<string> filtered;
  105. for (auto currentLink : links) {
  106. //cout << currentLink << "\n";
  107. if (currentLink.substr(currentLink.size() - 3, currentLink.size()) != "pdf" &&
  108. currentLink.substr(currentLink.size() - 3, currentLink.size()) != "css" &&
  109. currentLink.substr(currentLink.size() - 3, currentLink.size()) != "rss" &&
  110. currentLink.substr(currentLink.size() - 3, currentLink.size()) != "png" &&
  111. currentLink.substr(currentLink.size() - 3, currentLink.size()) != "zip") {
  112. //cout << currentLink << "\n";
  113. filtered.push_back(currentLink);
  114. }
  115.  
  116. }
  117.  
  118. return filtered;
  119.  
  120. }
  121.  
  122. DWORD WINAPI firstThreadFunction(LPVOID lpParam) {
  123. cout << "first";
  124.  
  125.  
  126. HKEY keyHandle;
  127. TCHAR achClass[MAX_PATH] = TEXT("");
  128. DWORD cchClassName = MAX_PATH;
  129. DWORD cSubKeys = 0;
  130. DWORD cbMaxSubKey;
  131. DWORD cchMaxClass;
  132. DWORD cValues;
  133. DWORD cchMaxValue;
  134. DWORD cbMaxValueData;
  135. DWORD cbSecurityDescriptor;
  136. FILETIME ftLastWriteTime;
  137. TCHAR achValue[MAX_VALUE_NAME];
  138. DWORD cchValue = MAX_VALUE_NAME;
  139. DWORD typeValue = REG_SZ;
  140. LPBYTE dataValue = NULL;
  141. DWORD cdataValue;
  142.  
  143.  
  144. HANDLE hEvent1 = CreateEvent(0, 0, 0, TEXT("firstEvent"));
  145. if (hEvent1 == NULL) {
  146. cout << "Couldn't create first event" << GetLastError();
  147. }
  148.  
  149. HANDLE hEvent2 = CreateEvent(0, 0, 0, TEXT("secondEvent"));
  150. if (hEvent2 == NULL) {
  151. cout << "Couldn't create second event" << GetLastError();
  152. }
  153.  
  154. HANDLE hmap = CreateFileMapping(0, 0, PAGE_EXECUTE_READWRITE, 0, 1000 * sizeof(DWORD), TEXT("fisier_threaduri"));
  155. if (hmap == NULL) {
  156. cout << "Couldn't create file map" << GetLastError();
  157. }
  158.  
  159. LPTSTR p = LPTSTR(MapViewOfFile(hmap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
  160. if (p == NULL) {
  161. cout << "Couldn't create pointer" << GetLastError();
  162. CloseHandle(hmap);
  163. }
  164.  
  165. HANDLE hThread2 = CreateThread(NULL, 0, secondThreadFunction, NULL, 0, NULL);
  166. if (hThread2 == NULL) {
  167. cout << "Err create second thread" << GetLastError() << endl;
  168. }
  169.  
  170.  
  171. DWORD resOpen = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WebCrawler"), 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &keyHandle);
  172. if (ERROR_SUCCESS != resOpen) {
  173. cout << "Couldn't open key\n";
  174. cout << resOpen;
  175. }
  176.  
  177. DWORD retCode = RegQueryInfoKey(
  178. keyHandle, // key handle
  179. achClass, // buffer for class name
  180. &cchClassName, // size of class string
  181. NULL, // reserved
  182. &cSubKeys, // number of subkeys
  183. &cbMaxSubKey, // longest subkey size
  184. &cchMaxClass, // longest class string
  185. &cValues, // number of values for this key
  186. &cchMaxValue, // longest value name
  187. &cbMaxValueData, // longest value data
  188. &cbSecurityDescriptor, // security descriptor
  189. &ftLastWriteTime); // last write time
  190.  
  191. HINTERNET hOpen = InternetOpen("VicTalif", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
  192. if (hOpen == NULL)
  193. {
  194. cout << "Err InternetOpen" << GetLastError() << endl;
  195. exit(0);
  196. }
  197.  
  198. if (cValues)
  199. {
  200. cout << "number of values: " << cValues;
  201.  
  202. for (DWORD i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
  203. {
  204. WaitForSingleObject(hEvent1, INFINITE);
  205.  
  206. cchValue = MAX_VALUE_NAME;
  207. cdataValue = MAX_VALUE_NAME;
  208. achValue[0] = '\0';
  209.  
  210. retCode = RegEnumValue(keyHandle, i, achValue, &cchValue, NULL, &typeValue, NULL, NULL);
  211. if (retCode != ERROR_SUCCESS)
  212. cerr << "err reg enum value" << retCode;
  213.  
  214. cout << "\n" << i + 1 << " " << achValue;
  215.  
  216. string link = string(achValue);
  217. string server_value = get_server_link(link);
  218. string name_value = get_name(link);
  219.  
  220. cout << "HERE : "<< server_value.c_str() << endl;
  221.  
  222. HINTERNET hConnect = InternetConnect(hOpen, TEXT(server_value.c_str()), INTERNET_INVALID_PORT_NUMBER, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
  223. if (hConnect == NULL)
  224. {
  225. cout << "Err InternetConnect" << GetLastError() << endl;
  226. }
  227.  
  228. HINTERNET hRequest = HttpOpenRequest(hConnect, TEXT("GET"), TEXT(name_value.c_str()), TEXT("1.1"), 0, accept_types, 0, 0);
  229. if (hOpen == NULL)
  230. {
  231. InternetCloseHandle(hConnect);
  232. cout << "Err HttpOpenRequest" << GetLastError() << endl;
  233. exit(0);
  234. }
  235.  
  236. HttpSendRequest(hRequest, 0, 0, 0, 0);
  237.  
  238. char* content = new char[1025];
  239. DWORD count_read;
  240. string html_content;
  241. while (InternetReadFile(hRequest, content, 1024, &count_read)) {
  242. if (!count_read)
  243. break;
  244. content[count_read] = 0;
  245. html_content += content;
  246. }
  247.  
  248. //cout << html_content;
  249.  
  250. vector<string> links = getAllLinks(html_content);
  251. /*for (auto it : links)
  252. cout << it << '\n';*/
  253.  
  254. vector<string> filteredLinks = getFilteredLinks(links);
  255. /*for (auto it : filteredLinks)
  256. cout << it << '\n';*/
  257. string linksBuff;
  258. for (auto it : filteredLinks) {
  259. cout << it << endl;
  260. linksBuff += it + "\n";
  261. }
  262.  
  263. //cout << "\n string buff " << linksBuff;
  264. CopyMemory(p, linksBuff.c_str(), linksBuff.size() * sizeof(linksBuff[0]));
  265. SetEvent(hEvent2);
  266. //Sleep(1000);
  267. }
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. return 0;
  276. }
  277.  
  278. DWORD WINAPI secondThreadFunction(LPVOID lpParam) {
  279. cout << "second";
  280.  
  281.  
  282.  
  283. HANDLE hEvent1 = OpenEvent(EVENT_ALL_ACCESS, 0, TEXT("firstEvent"));
  284. if (hEvent1 == NULL) {
  285. cout << "Couldn't create first event" << GetLastError();
  286. }
  287.  
  288. HANDLE hEvent2 = OpenEvent(EVENT_ALL_ACCESS, 0, TEXT("secondEvent"));
  289. if (hEvent2 == NULL) {
  290. cout << "Couldn't create second event" << GetLastError();
  291. }
  292.  
  293.  
  294. HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("fisier_threaduri"));
  295. if (hMap == INVALID_HANDLE_VALUE) {
  296. cout << "Could not open file map " << GetLastError();
  297. CloseHandle(hMap);
  298. }
  299.  
  300. SetEvent(hEvent1);
  301. WaitForSingleObject(hEvent2, INFINITE);
  302.  
  303. LPSTR buff = (LPSTR)(MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
  304. if (buff == NULL) {
  305. CloseHandle(hMap);
  306. cout << "err map view";
  307. }
  308.  
  309. string strBuff = string((char*)buff);
  310. cout << "\nThread 2\n" << strBuff;
  311.  
  312.  
  313. HANDLE hMap2 = CreateAndReturnFileMapping("ProjectFile");
  314. if (hMap2 == NULL) return 0;
  315.  
  316. HANDLE hMutex = CreateAndReturnMutex(hMap2, "shared_mutex");
  317. if (hMutex == NULL) return 0;
  318.  
  319. LPTSTR pMap = CreateAndReturnMapViewOfFile(hMap2, hMutex);
  320. if (pMap == NULL) return 0;
  321.  
  322. if (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0)
  323. {
  324. printf("WaitForSingleObject failed with error 0x%08x\n", GetLastError());
  325. ReleaseMutex(hMutex);
  326. CloseHandle(hMutex);
  327. CloseHandle(hMap2);
  328. UnmapViewOfFile(pMap);
  329. return 0;
  330. }
  331.  
  332.  
  333. if (!CopyMemory(pMap, strBuff.c_str(), strBuff.size()*sizeof(strBuff[0]))) {
  334. printf("CopyMemory failed with error 0x%08x\n", GetLastError());
  335. }
  336.  
  337. ReleaseMutex(hMutex);
  338.  
  339.  
  340. CloseHandle(hMutex);
  341. CloseHandle(hMap2);
  342. UnmapViewOfFile(pMap);
  343.  
  344.  
  345. return 0;
  346. }
  347.  
  348. int main() {
  349. while (1) {
  350. firstThreadFunction(NULL);
  351. //secondThreadFunction(NULL);
  352. Sleep(10000);
  353. }
  354.  
  355. /*HANDLE hThread1 = CreateThread(NULL, 0, firstThreadFunction, &eventsHandles, 0, NULL);
  356. if (hThread1 == NULL)
  357. {
  358. cout << "Err create first thread" << GetLastError() << endl; */
  359.  
  360.  
  361.  
  362. _getch();
  363. return 0;
  364.  
  365.  
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement