-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
364 lines (338 loc) · 9.91 KB
/
helper.js
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
const verbose = false
const global_lock = []
const google_api_key = ''
const google_api_cs = ''
const grid_url = ''
const proxy = ''
let tecert_file = ''
const detection_level = {
extreme: {
fast: 'normal',
slow: 'normal,advanced,ocr',
detections: 'true',
count: 1,
found: 2
},
high: {
fast: 'normal',
slow: 'normal,advanced,ocr',
detections: 'true,false',
count: 2,
found: 1
},
current: 'high'
}
const profile_template = {
found: 0,
username: '',
image: '',
link: '',
rate: '',
status: '',
title: '',
language: '',
country: '',
rank: '',
text: '',
type: '',
metadata: '',
extracted: '',
good: '',
method: ''
}
const detected_websites = {
normal: 0,
advanced: 0,
ocr: 0,
true: 0,
false: 0,
count: 0
}
const header_options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0'
}
}
import https from 'follow-redirects'
import fs from 'fs'
import url from 'url'
import {franc} from 'franc'
import langs from 'langs'
import cheerio from 'cheerio'
import path from 'path'
import slash from 'slash'
import colors from 'colors/safe.js'
import {QBIxora} from 'ixora'
import {fileURLToPath} from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const sites_json_path = slash(path.join(__dirname, '..', 'data', 'sites.json'))
const names_json_path = slash(path.join(__dirname, '..', 'data', 'names.json'))
const dict_json_path = slash(path.join(__dirname, '..', 'data', 'dict.json'))
const countries_json_path = slash(path.join(__dirname, '..', 'data', 'names.json'))
const public_graph_path = slash(path.join(__dirname, '..', 'public', 'graph.html'))
let temp_ixora = new QBIxora('Social-Analyzer', false)
temp_ixora.save_base_html(public_graph_path)
temp_ixora = null
const websites_entries = JSON.parse(fs.readFileSync(sites_json_path)).websites_entries
const shared_detections = JSON.parse(fs.readFileSync(sites_json_path)).shared_detections
const parsed_names_origins = JSON.parse(fs.readFileSync(names_json_path))
const parsed_json = JSON.parse(fs.readFileSync(dict_json_path))
const parsed_countries = JSON.parse(fs.readFileSync(names_json_path))
let logs_queue = Promise.resolve()
const strings_pages = new RegExp('captcha-info|Please enable cookies|Completing the CAPTCHA', 'i')
const strings_titles = new RegExp('not found|blocked|attention required|cloudflare', 'i')
const top_websites = new RegExp('^top([0-9]+)$', 'i')
function get_log_file (uuid) {
const _uuid = uuid.replace(/[^a-zA-Z0-9\-]+/g, '')
const _string = slash(path.join('logs', _uuid + '_log.txt'))
return _string
}
function log_to_file_queue (uuid, msg, table = false, argv = undefined) {
logs_queue = logs_queue.then(function () {
return new Promise(function (resolve) {
const temp_log_file = slash(path.join('logs', uuid + '_log.txt'))
fs.appendFile(temp_log_file, msg + '\n', function (err, data) {
if (table) {
msg.forEach((item, index) => {
if (index === 0) {
console.log('-----------------------')
}
for (const [key, value] of Object.entries(item)) {
if (key === 'extracted' || key === 'metadata') {
if ((key === 'extracted' && argv.extract) || (key === 'metadata' && argv.metadata)) {
if (value !== 'unavailable') {
try {
value.forEach((metadata_item, i) => {
let temp_string_meta = key + ' ' + i
temp_string_meta = temp_string_meta.padEnd(13)
temp_string_meta = colors.blue(temp_string_meta) + ': '
for (const [metadata_key, metadata_value] of Object.entries(metadata_item)) {
if (metadata_value.length > 80 && argv.trim) {
temp_string_meta += colors.blue(metadata_key) + ' : ' + colors.yellow(metadata_value.substring(0, 80).replace(/\r?\n|\r/g, '') + '..') + ' '
} else {
temp_string_meta += colors.blue(metadata_key) + ' : ' + colors.yellow(metadata_value.replace(/\r?\n|\r/g, '')) + ' '
}
};
console.log(temp_string_meta)
})
} catch (err) {
}
} else {
console.log(colors.blue(key.padEnd(12)) + ' : ' + colors.yellow(value))
}
}
} else {
console.log(colors.blue(key.padEnd(12)) + ' : ' + colors.yellow(value))
}
}
console.log('-----------------------')
})
} else {
console.log(msg)
}
resolve()
})
})
})
}
function get_language_by_parsing (body) {
let language = 'unavailable'
try {
const $ = cheerio.load(body)
const code = $('html').attr('lang')
if (code !== '') {
if (langs.where('1', code) !== 'undefined' && langs.where('1', code)) {
language = langs.where('1', code).name
}
}
} catch (err) {
verbose && console.log(err)
}
return language
}
function get_language_by_guessing (text) {
let language = 'unavailable'
try {
if (text !== 'unavailable' && text !== '') {
const code = franc(text)
if (code !== 'und') {
if (langs.where('3', code) !== 'undefined' && langs.where('3', code)) {
language = langs.where('3', code).name + ' (Maybe)'
}
}
}
} catch (err) {
verbose && console.log(err)
}
return language
}
function get_site_from_url (_url) {
const temp = url.parse(_url.replace('{username}', 'nothinghere')).hostname
return temp.replace('nothinghere.', '')
}
async function get_url_wrapper_json (url, time = 2) {
try {
const http_promise = new Promise((resolve, reject) => {
const request = https.https.get(url, header_options, function (res) {
let body = ''
res.on('data', function (chunk) {
body += chunk
})
res.on('end', function () {
resolve({
data: JSON.parse(body.toString())
})
})
})
const timeout = (time !== 0) ? time * 1000 : 5000
request.setTimeout(timeout, function() {
reject({
data: ''
})
});
request.on('error', function (e) {
reject({
data: ''
})
})
request.on('socket', function (socket) {
const timeout = (time !== 0) ? time * 1000 : 5000
socket.setTimeout(timeout, function () {
request.abort()
})
})
})
const response_body = await http_promise
return response_body
} catch (err) {
verbose && console.log(err)
}
}
async function get_url_wrapper_text (url, time = 2) {
const response_body = 'error-get-url'
const ret = 500
try {
const http_promise = new Promise((resolve, reject) => {
const request = https.https.get(url, header_options, function (res) {
let body = ''
res.on('data', function (chunk) {
body += chunk
})
res.on('end', function () {
resolve([res.statusCode,body])
})
})
const timeout = (time !== 0) ? time * 1000 : 5000
request.setTimeout(timeout, function() {
reject({
data: ''
})
});
request.on('error', function (e) {
reject({
data: ''
})
})
request.on('socket', function (socket) {
const timeout = (time !== 0) ? time * 1000 : 5000
socket.setTimeout(timeout, function () {
request.abort()
})
})
})
const [ret, response_body] = await http_promise
return [ret, response_body]
} catch (err) {
verbose && console.log(err)
return [ret, response_body]
}
}
function compare_objects (object1, object2, key) {
try {
if (object1[key] === '') {
object1[key] = '%0.0'
}
if (object2[key] === '') {
object2[key] = '%0.0'
}
if (parseInt(object1[key].replace('%', '')) > parseInt(object2[key].replace('%', ''))) {
return -1
} else if (parseInt(object1[key].replace('%', '')) < parseInt(object2[key].replace('%', ''))) {
return 1
} else {
return 0
}
} catch (err) {
return 0
}
}
function find_country (code) {
let ctr = ''
try {
if (code.toUpperCase() in parsed_countries) {
ctr = parsed_countries[code.toUpperCase()]
}
} catch (error) {
}
return ctr
}
async function setup_tecert () {
if (!fs.existsSync('eng.traineddata')) {
const file = fs.createWriteStream('eng.traineddata')
const http_promise = new Promise((resolve, reject) => {
const request = https.https.get('https://raw.githubusercontent.com/tesseract-ocr/tessdata/master/eng.traineddata', function (response) {
response.pipe(file)
resolve(1)
request.setTimeout(12000, function () {
request.abort()
})
file.on('finish', function () {
file.close()
})
resolve(0)
})
})
const get_eng = await http_promise
if (get_eng === 1) {
if (fs.existsSync('eng.traineddata')) {
tecert_file = path.resolve(__dirname, 'eng.traineddata')
}
}
} else {
if (tecert_file === '') {
if (fs.existsSync('eng.traineddata')) {
tecert_file = path.resolve(__dirname, 'eng.traineddata')
}
}
}
}
export default {
strings_pages,
strings_titles,
top_websites,
tecert_file,
setup_tecert,
compare_objects,
get_log_file,
find_country,
profile_template,
detection_level,
detected_websites,
shared_detections,
get_language_by_parsing,
get_language_by_guessing,
websites_entries,
parsed_names_origins,
parsed_json,
verbose,
global_lock,
google_api_key,
google_api_cs,
grid_url,
header_options,
proxy,
get_site_from_url,
log_to_file_queue,
get_url_wrapper_text,
get_url_wrapper_json
}