| 1 | import helper from './helper.js' |
| 2 | import extraction from './extraction.js' |
| 3 | import async from 'async' |
| 4 | import sanitizeHtml from 'sanitize-html' |
| 5 | import {convert} from 'html-to-text' |
| 6 | import * as cheerio from 'cheerio' |
| 7 | import engine from './engine.js' |
| 8 | |
| 9 | async function find_username_normal (req) { |
| 10 | helper.log_to_file_queue(req.body.uuid, '[init] Selected websites: ' + helper.websites_entries.filter((item) => item.selected === 'true').length + ' for username: ' + req.body.string) |
| 11 | const time = new Date() |
| 12 | let all_results = [] |
| 13 | const [first_re_try, first_profiles] = await find_username_normal_wrapper(req, helper.websites_entries) |
| 14 | helper.verbose && console.log(`Total time ${new Date() - time}`) |
| 15 | const [second_re_try, second_profiles] = await find_username_normal_wrapper(req, first_re_try) |
| 16 | helper.verbose && console.log(`Total time ${new Date() - time}`) |
| 17 | const [third_re_try, third_profiles] = await find_username_normal_wrapper(req, second_re_try) |
| 18 | helper.verbose && console.log(`Total time ${new Date() - time}`) |
| 19 | if (third_re_try.length > 0) { |
| 20 | const failed_sites = await get_failed(req, third_re_try) |
| 21 | all_results = Array.prototype.concat(first_profiles, second_profiles, third_profiles, failed_sites) |
| 22 | } else { |
| 23 | all_results = Array.prototype.concat(first_profiles, second_profiles, third_profiles) |
| 24 | } |
| 25 | |
| 26 | all_results = all_results.filter(item => item) |
| 27 | return all_results.sort((first, second) => { |
| 28 | return helper.compare_objects(first, second, 'rate') |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | async function get_failed (req, failed) { |
| 33 | const temp_result = [] |
| 34 | try { |
| 35 | await failed.forEach(site => { |
| 36 | const temp_profile = { |
| 37 | link: '', |
| 38 | method: '' |
| 39 | } |
| 40 | temp_profile.method = 'failed' |
| 41 | temp_profile.username = req.body.string |
| 42 | temp_profile.link = site.url.replace('{username}', req.body.string) |
| 43 | temp_result.push(temp_profile) |
| 44 | }) |
| 45 | } catch (err) { |
| 46 | helper.verbose && console.log(err) |
| 47 | } |
| 48 | return temp_result |
| 49 | } |
| 50 | |
| 51 | async function find_username_normal_wrapper (req, sites) { |
| 52 | const results = [] |
| 53 | const re_try = [] |
| 54 | const functions = [] |
| 55 | try { |
| 56 | const temp_sites = sites.filter(site => site.selected === 'true') |
| 57 | if (temp_sites.length > 0) { |
| 58 | await temp_sites.forEach(site => { |
| 59 | if (site.detections.length > 0 && !helper.global_lock.includes(req.body.uuid)) { |
| 60 | functions.push(find_username_site.bind(null, req.body.uuid, req.body.string, req.body.option, site)) |
| 61 | } |
| 62 | }) |
| 63 | const temp_results = await async.parallelLimit(functions, 15) |
| 64 | await temp_results.forEach(item => { |
| 65 | if (item !== undefined) { |
| 66 | if (item.return === 1) { |
| 67 | results.push(item.profile) |
| 68 | } else { |
| 69 | helper.log_to_file_queue(req.body.uuid, '[Waiting to retry] ' + helper.get_site_from_url(item.site.url)) |
| 70 | re_try.push(item.site) |
| 71 | } |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } catch (err) { |
| 76 | helper.verbose && console.log(err) |
| 77 | } |
| 78 | return [re_try, results] |
| 79 | } |
| 80 | |
| 81 | async function find_username_site (uuid, username, options, site) { |
| 82 | return new Promise(async (resolve, reject) => { |
| 83 | if (!helper.global_lock.includes(uuid)) { |
| 84 | try { |
| 85 | if (!options.includes('json')) { |
| 86 | helper.log_to_file_queue(uuid, '[Checking] ' + helper.get_site_from_url(site.url)) |
| 87 | } |
| 88 | const [ret, source] = await helper.get_url_wrapper_text(site.url.replace('{username}', username)) |
| 89 | if (source !== 'error-get-url') { |
| 90 | let title = 'unavailable' |
| 91 | let language = 'unavailable' |
| 92 | const [ |
| 93 | temp_profile, |
| 94 | temp_detected, |
| 95 | detections_count |
| 96 | ] = await engine.detect('fast', uuid, username, options, site, source) |
| 97 | if (temp_profile.found >= helper.detection_level[helper.detection_level.current].found && detections_count >= helper.detection_level[helper.detection_level.current].count) { |
| 98 | temp_profile.good = 'true' |
| 99 | } |
| 100 | |
| 101 | temp_profile.text = sanitizeHtml(convert(source, { |
| 102 | wordwrap: false, |
| 103 | hideLinkHrefIfSameAsText: true, |
| 104 | ignoreHref: true, |
| 105 | ignoreImage: true |
| 106 | })) |
| 107 | if (temp_profile.text === '') { |
| 108 | temp_profile.text = 'unavailable' |
| 109 | } |
| 110 | |
| 111 | |
| 112 | try { |
| 113 | const $ = cheerio.load(source) |
| 114 | title = sanitizeHtml($('title').text()) |
| 115 | if (title.length === 0) { |
| 116 | title = 'unavailable' |
| 117 | } |
| 118 | } catch (err) { |
| 119 | helper.verbose && console.log(err) |
| 120 | } |
| 121 | |
| 122 | try { |
| 123 | language = helper.get_language_by_parsing(source) |
| 124 | if (language === 'unavailable') { |
| 125 | language = helper.get_language_by_guessing(temp_profile.text) |
| 126 | } |
| 127 | } catch (err) { |
| 128 | helper.verbose && console.log(err) |
| 129 | } |
| 130 | |
| 131 | temp_profile.text = temp_profile.text.replace(/(\r\n|\n|\r)/gm, '') |
| 132 | temp_profile.title = title.replace(/(\r\n|\n|\r)/gm, '') |
| 133 | temp_profile.language = language |
| 134 | |
| 135 | if (helper.strings_titles.test(temp_profile.title) || helper.strings_pages.test(temp_profile.text)) { |
| 136 | temp_profile.title = 'filtered' |
| 137 | temp_profile.text = 'filtered' |
| 138 | } |
| 139 | |
| 140 | if (temp_profile.good === 'true') { |
| 141 | const temp_value = ((temp_profile.found / detections_count) * 100).toFixed(2) |
| 142 | temp_profile.rate = '%' + temp_value |
| 143 | if (temp_value >= 100.00) { |
| 144 | temp_profile.status = 'good' |
| 145 | } else if (temp_value >= 50.00 && temp_value < 100.00) { |
| 146 | temp_profile.status = 'maybe' |
| 147 | } else { |
| 148 | temp_profile.status = 'bad' |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | temp_profile.username = username |
| 153 | temp_profile.link = site.url.replace('{username}', username) |
| 154 | temp_profile.type = site.type |
| 155 | temp_profile.rank = site.global_rank |
| 156 | temp_profile.country = site.country |
| 157 | |
| 158 | if (temp_profile.rank === 0) { |
| 159 | temp_profile.rank = 'unavailable' |
| 160 | } |
| 161 | |
| 162 | if (temp_profile.status === 'good') { |
| 163 | if (options.includes('ExtractPatterns')) { |
| 164 | let temp_extracted_list = [] |
| 165 | temp_extracted_list = await extraction.extract_patterns(site, source) |
| 166 | if (temp_extracted_list.length > 0) { |
| 167 | temp_profile.extracted = temp_extracted_list |
| 168 | } |
| 169 | } |
| 170 | if (options.includes('ExtractMetadata')) { |
| 171 | let temp_metadata_list = [] |
| 172 | temp_metadata_list = await extraction.extract_metadata(site, source) |
| 173 | if (temp_metadata_list.length > 0) { |
| 174 | temp_profile.metadata = temp_metadata_list |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ['title', 'language', 'text', 'type', 'metadata', 'extracted', 'country'].forEach((item) => { |
| 180 | if (temp_profile[item] === '') { |
| 181 | temp_profile[item] = 'unavailable' |
| 182 | } |
| 183 | }) |
| 184 | |
| 185 | if (options.includes('FindUserProfilesFast') && !options.includes('GetUserProfilesFast')) { |
| 186 | temp_profile.method = 'find' |
| 187 | } else if (options.includes('GetUserProfilesFast') && !options.includes('FindUserProfilesFast')) { |
| 188 | temp_profile.method = 'get' |
| 189 | } else if (options.includes('GetUserProfilesFast') && options.includes('FindUserProfilesFast')) { |
| 190 | temp_profile.method = 'all' |
| 191 | } |
| 192 | resolve({ |
| 193 | return: 1, |
| 194 | site: site, |
| 195 | profile: temp_profile |
| 196 | }) |
| 197 | } else { |
| 198 | resolve({ |
| 199 | return: 0, |
| 200 | site: site, |
| 201 | profile: [] |
| 202 | }) |
| 203 | } |
| 204 | } catch (err) { |
| 205 | resolve(undefined) |
| 206 | } |
| 207 | } else { |
| 208 | resolve(undefined) |
| 209 | } |
| 210 | }) |
| 211 | } |
| 212 | |
| 213 | export default { |
| 214 | find_username_normal |
| 215 | } |
| 216 |