-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
394 lines (292 loc) · 10.3 KB
/
Copy pathmain.cpp
File metadata and controls
394 lines (292 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <iostream>
#include <fstream>
#include "string"
#include <vector>
#include "curl/curl.h"
#include <cassert>
#include <cstring>
#include <iterator>
#include <string>
#include <cstdio>
#include <ctime>
#include <zip.h>
#include "basen.hpp"
#include <set>
#include <map>
#include<sstream>
#include<exception>
#include<profile.h>
#include <future>
#include<functional>
#include<whoisds.h>
class Config
{
private:
public:
Config() {}
};
using namespace std;
//Буфер для курла !
string curlBuffer;
size_t curlWriteFunc(char *data,size_t size,size_t nmemb, string *buffer){
size_t result = 0;
if(buffer != NULL){
buffer->append(data, size * nmemb);
result = size * nmemb;
}
return result;
}
class Site {
private:
string url;
string suspkeyword;
int points = 0;
string rating="Not Cheked";
bool reachable = false;
public:
//constructor
explicit Site (const string &u,const unordered_map<string,int> &k ):url(u){
string toclean = u.c_str();
string cleaned;
copy_if(toclean.begin(), toclean.end(),
back_inserter(cleaned),
[] (char c) { return c != '\r' && c != '\n'; });
url = cleaned;
asignKeyword(k);
//Не знаю нужна ли здесь эта функци проверки
// checkSite();
}
//Конструктор без проверки на кейворды
explicit Site (const string &u){
string toclean = u.c_str();
string cleaned;
copy_if(toclean.begin(),toclean.end(),back_inserter(cleaned),
[](char c){return c!='\r' && c != '\n';});
url=cleaned;
}
//Доступ к переменым класа
inline string Url () const {return url ;}
inline string Keyword () const {return suspkeyword;}
inline int Points () const {return points;}
inline string Rating() const {return rating;}
inline bool Reachable() const {return reachable;}
//void reportInFile();
void inline setUrl(const string &y)
{
url = y;
}
void asignKeyword(const unordered_map<string,int> &Keywords);
//void rankPoint();
void LevensteinAnalizer(const vector<string> &bankurls) ;
//void asignRating;
void checkSite();
};
// перегрузка операторов для класса
bool operator < (const Site & lhs, const Site & rhs){
if(lhs.Points()<rhs.Points()){
return true;
}
else {
return false;
}
}
ostream & operator << (ostream & out, const Site &site){
out<<site.Url()<<","<<site.Keyword()<<","<<site.Points()<<","<<site.Rating()<<","<<site.Reachable();
return out;
}
// функция чтения файлов в вектор
void readinvect (const string &filename, vector<string> & V){
ifstream file (filename);
string line;
while (getline(file,line)){
V.push_back(move(line));
}
}
// функция чтения файла в словарь ключевых слов с значениями
void LoadKeyWords (const string & filename, unordered_map<string,int> &Keywords){
ifstream file(filename);
string line;
while(getline(file,line)){
string tmp;
int p;
stringstream ss(line);
ss>>tmp;
ss.ignore(1);
ss>>p;
Keywords[move(tmp)]=move(p);
}
}
//функция поиска ключевых слов в адресе + начисление баллов
void Site::asignKeyword (const unordered_map<string,int> &Keywords){
for( auto &keyword:Keywords){
//(stringsearch(url,keyword.first))
if(url.find(keyword.first) != url.npos){
suspkeyword +=keyword.first.c_str();
suspkeyword +=" ";
points+=keyword.second;
}
}
}
void Site::checkSite(){
const char * adress = url.c_str();
//буфер для сохраненияы ошибок
char curlErrorBuffer[CURL_ERROR_SIZE];
//Инициализируем Курл
CURL *curl = curl_easy_init();
// проверяем инициализацию
if (curl){
// Задаем опции курлу
// Буфер для ошибок с вишеописаной функцией
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlErrorBuffer );
// задаем адресс
curl_easy_setopt(curl, CURLOPT_URL, adress);
//задаем юзер агент
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
//задаем реферал
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1 );
// переходим по локейшену задоному в заголовку
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
//куки ???
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
// НЕ ПРОВЕРЯТЬ СЕРТИФИКАТ
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
//указываем что полученые даные нужно записать в буфер используя функцию
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curlBuffer);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc);
//выполянем сам запрос
cout<<"Cheking "<< url<<endl;
CURLcode curlResult = curl_easy_perform(curl);
// закрываем сеанс
curl_easy_cleanup(curl);
//проверяем резултат
if (curlResult == CURLE_OK){
reachable=1;
//здесь должна быть проверка результата
// cout<< curlBuffer;
// возвращаем стрингу
//return curlBuffer ;
}
else {
reachable=0;
cout << "FuckUp("<<curlResult<<"); LOL"<< curlErrorBuffer << endl;
}
}
}
// Поиск дистанции левинштейна
size_t LevenshteinDistance(const string &source, const string &target) {
int checklimit = 0; // стопор до трех
int count = 0;
if (source.size()<=4 ) {
return 10;
}
if(source.size() <= 6){
checklimit = 1;
}
else {
checklimit = 2;
}
if(target.find(source)!=target.npos && source.size()>5){
return 1;
} // убрать
const size_t min_size = source.size(), max_size = target.size();
vector<size_t> lev_dist(min_size + 1);
for (size_t i = 0; i <= min_size; ++i) {
lev_dist[i] = i;
}
for (size_t j = 1; j <= max_size; ++j) {
size_t previous_diagonal = lev_dist[0], previous_diagonal_save;
++lev_dist[0];
if(count>checklimit){
return 10;
}
for (size_t i = 1; i <= min_size; ++i) {
previous_diagonal_save = lev_dist[i];
if (source[i - 1] == target[j - 1]) {
lev_dist[i] = previous_diagonal;
count++;
} else {
lev_dist[i] = min(min(lev_dist[i - 1], lev_dist[i]), previous_diagonal) + 1;
}
previous_diagonal = previous_diagonal_save;
}
}
return lev_dist[min_size];
}
// Другая имплементация
/* unsigned int LevenshteinDistance(const std::string& s1, const std::string& s2)
{
const std::size_t len1 = s1.size(), len2 = s2.size();
std::vector<std::vector<unsigned int>> d(len1 + 1, std::vector<unsigned int>(len2 + 1));
d[0][0] = 0;
for(unsigned int i = 1; i <= len1; ++i) d[i][0] = i;
for(unsigned int i = 1; i <= len2; ++i) d[0][i] = i;
for(unsigned int i = 1; i <= len1; ++i)
for(unsigned int j = 1; j <= len2; ++j)
// note that std::min({arg1, arg2, arg3}) works only in C++11,
// for C++98 use std::min(std::min(arg1, arg2), arg3)
d[i][j] = std::min({ d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) });
return d[len1][len2];
}*/
void LevensteinAnalizer(const vector<string> &bankurls, const string & url ){
auto point_it = find(url.begin(),url.end(),'.');
string clean_source (url.begin(),point_it);
for ( const string &x:bankurls){
if (LevenshteinDistance(x,clean_source)<=2 ){
//заменить код и функцию под класс
cout << "Lev < 3 mask : "<< x << " source : "<< url<< endl;
break;
}
}
}
void MultythreadLevenstein (const vector<string> & banks ,const vector<string> &listofurl){
vector<future<void>> threads;
for (auto &x:listofurl){
threads.push_back(async(LevensteinAnalizer,ref(banks),ref(x)));
}
}
// нужно привести эту хуергу в порядок
int main()
{
unordered_map<string,int> keywords;
LoadKeyWords("keywords",keywords);
getWhoisds();
vector<string> sitelist ;
readinvect("domain-names.txt",sitelist);
vector<string> levi;
readinvect("banks",levi);
vector<Site> result;
vector<string> pseudoclean;
cout<<"Start Cheking sites "<<endl;
{ LOG_DURATION("Site Intital cheker")
result.reserve(sitelist.size());
pseudoclean.reserve(sitelist.size());
for ( string &x:sitelist){
Site site(x,keywords);
if(site.Points()>0){
result.push_back(site);
}
else {
pseudoclean.push_back(x);
}
}
}
result.shrink_to_fit();
pseudoclean.shrink_to_fit();
sort(result.rbegin(),result.rend());
for ( auto &x:result){
cout<<x<<endl;
}
cout<<"Cheking suspiscius sites "<<endl;
cout<<result.size()<<endl;
{
LOG_DURATION("Rest of masive Levenstein check")
cout<<"Size of remain vector "<<pseudoclean.size()<<endl;
// здесь вызвать многопоточного левинштейна !!!
MultythreadLevenstein(levi,pseudoclean);
}
// Гавно-функцию чексайт нада перепиливать целиком и полностью
for (auto & site:result){
site.checkSite();
}
cout<<"Succesful end"<<endl;
}