| 1 | |
| 2 | import helper from './helper.js' |
| 3 | import extraction from './extraction.js' |
| 4 | import async from 'async' |
| 5 | import sanitizeHtml from 'sanitize-html' |
| 6 | import firefox from 'selenium-webdriver/firefox.js' |
| 7 | import {Builder,By} from 'selenium-webdriver' |
| 8 | import path from 'path' |
| 9 | import engine from './engine.js' |
| 10 | import {createRequire} from 'node:module'; |
| 11 | |
| 12 | const require = createRequire(import.meta.url); |
| 13 | |
| 14 | if (process.platform === 'win32') { |
| 15 | const package_path = path.join(path.dirname(require.resolve('geckodriver')), '..') |
| 16 | process.env.PATH = process.env.PATH + ';' + package_path |
| 17 | } |
| 18 | |
| 19 | async function find_username_advanced (req) { |
| 20 | 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) |
| 21 | const time = new Date() |
| 22 | const functions = [] |
| 23 | helper.websites_entries.forEach((site) => { |
| 24 | if ('status' in site) { |
| 25 | if (site.status === 'bad') { |
| 26 | return Promise.resolve() |
| 27 | } |
| 28 | } |
| 29 | if (site.selected === 'true' && site.detections.length > 0 && !helper.global_lock.includes(req.body.uuid)) { |
| 30 | functions.push(find_username_site.bind(null, req.body.uuid, req.body.string, req.body.option, site)) |
| 31 | } |
| 32 | }) |
| 33 | let results = await async.parallelLimit(functions, 8) |
| 34 | helper.verbose && console.log(`Total time ${new Date() - time}`) |
| 35 | results = results.filter(item => item) |
| 36 | return results.sort((first, second) => { |
| 37 | return helper.compare_objects(first, second, 'rate') |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | async function find_username_site (uuid, username, options, site) { |
| 42 | return new Promise(async (resolve, reject) => { |
| 43 | if (!helper.global_lock.includes(uuid)) { |
| 44 | helper.log_to_file_queue(uuid, '[Checking] ' + helper.get_site_from_url(site.url)) |
| 45 | let driver |
| 46 | if (helper.grid_url === '') { |
| 47 | driver = new Builder() |
| 48 | .forBrowser('firefox') |
| 49 | .setFirefoxOptions(new firefox.Options().headless().windowSize({ |
| 50 | width: 640, |
| 51 | height: 480 |
| 52 | })) |
| 53 | .build() |
| 54 | } else { |
| 55 | driver = new Builder() |
| 56 | .forBrowser('firefox') |
| 57 | .setFirefoxOptions(new firefox.Options().headless().windowSize({ |
| 58 | width: 640, |
| 59 | height: 480 |
| 60 | })) |
| 61 | .usingServer(helper.grid_url) |
| 62 | .build() |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | let timeouts = { |
| 67 | implicit: 0, |
| 68 | pageLoad: 5000, |
| 69 | script: 5000 |
| 70 | } |
| 71 | |
| 72 | const timeout = (site.timeout !== 0) ? site.timeout * 1000 : 5000 |
| 73 | const implicit = (site.implicit !== 0) ? site.implicit * 1000 : 0 |
| 74 | |
| 75 | timeouts = { |
| 76 | implicit: implicit, |
| 77 | pageLoad: timeout, |
| 78 | script: timeout |
| 79 | } |
| 80 | |
| 81 | helper.verbose && console.log(timeouts) |
| 82 | |
| 83 | let source = '' |
| 84 | let screen_shot = '' |
| 85 | let language = 'unavailable' |
| 86 | let text_only = 'unavailable' |
| 87 | let title = 'unavailable' |
| 88 | const link = site.url.replace('{username}', username) |
| 89 | // await driver.manage().setTimeouts(timeouts); |
| 90 | await driver.get(link) |
| 91 | source = await driver.getPageSource() |
| 92 | screen_shot = await driver.takeScreenshot() |
| 93 | title = await driver.getTitle() |
| 94 | text_only = await driver.findElement(By.tagName('body')).getText() |
| 95 | await driver.quit() |
| 96 | const [temp_profile, temp_detected, detections_count] = await engine.detect('slow', uuid, username, options, site, source, text_only, screen_shot) |
| 97 | if (options.includes('ShowUserProfilesSlow')) { |
| 98 | temp_profile.image = 'data:image/png;base64,{image}'.replace('{image}', screen_shot) |
| 99 | } |
| 100 | if (temp_profile.found >= helper.detection_level[helper.detection_level.current].found && detections_count >= helper.detection_level[helper.detection_level.current].count) { |
| 101 | temp_profile.good = 'true' |
| 102 | try { |
| 103 | language = helper.get_language_by_parsing(source) |
| 104 | if (language === 'unavailable') { |
| 105 | language = helper.get_language_by_guessing(text_only) |
| 106 | } |
| 107 | } catch (err) { |
| 108 | helper.verbose && console.log(err) |
| 109 | } |
| 110 | |
| 111 | temp_profile.username = username |
| 112 | temp_profile.text = sanitizeHtml(text_only) |
| 113 | temp_profile.title = sanitizeHtml(title) |
| 114 | temp_profile.language = language |
| 115 | |
| 116 | temp_profile.text = temp_profile.text.replace(/(\r\n|\n|\r)/gm, '') |
| 117 | temp_profile.title = temp_profile.title.replace(/(\r\n|\n|\r)/gm, '') |
| 118 | |
| 119 | if (helper.strings_titles.test(temp_profile.title) || helper.strings_pages.test(temp_profile.text)) { |
| 120 | temp_profile.title = 'filtered' |
| 121 | temp_profile.text = 'filtered' |
| 122 | } |
| 123 | |
| 124 | if (temp_profile.good === 'true') { |
| 125 | const temp_value = ((temp_profile.found / detections_count) * 100).toFixed(2) |
| 126 | temp_profile.rate = '%' + temp_value |
| 127 | if (temp_value >= 100.00) { |
| 128 | temp_profile.status = 'good' |
| 129 | } else if (temp_value >= 50.00 && temp_value < 100.00) { |
| 130 | temp_profile.status = 'maybe' |
| 131 | } else { |
| 132 | temp_profile.status = 'bad' |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (temp_profile.status === 'good') { |
| 137 | if (options.includes('ExtractPatterns')) { |
| 138 | let temp_extracted_list = [] |
| 139 | temp_extracted_list = await extraction.extract_patterns(site, source) |
| 140 | if (temp_extracted_list.length > 0) { |
| 141 | temp_profile.extracted = temp_extracted_list |
| 142 | } |
| 143 | } |
| 144 | if (options.includes('ExtractMetadata')) { |
| 145 | let temp_metadata_list = [] |
| 146 | temp_metadata_list = await extraction.extract_metadata(site, source) |
| 147 | if (temp_metadata_list.length > 0) { |
| 148 | temp_profile.metadata = temp_metadata_list |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | temp_profile.rank = site.global_rank |
| 154 | temp_profile.country = site.country |
| 155 | |
| 156 | if (temp_profile.rank === 0) { |
| 157 | temp_profile.rank = 'unavailable' |
| 158 | } |
| 159 | |
| 160 | ['title', 'language', 'text', 'type', 'metadata', 'extracted', 'country'].forEach((item) => { |
| 161 | if (temp_profile[item] === '') { |
| 162 | temp_profile[item] = 'unavailable' |
| 163 | } |
| 164 | }) |
| 165 | |
| 166 | temp_profile.link = site.url.replace('{username}', username) |
| 167 | temp_profile.type = site.type |
| 168 | resolve(temp_profile) |
| 169 | } else if (temp_profile.image !== '') { |
| 170 | temp_profile.username = username |
| 171 | temp_profile.text = 'unavailable' |
| 172 | temp_profile.title = 'unavailable' |
| 173 | temp_profile.language = 'unavailable' |
| 174 | temp_profile.rate = '%00.0' |
| 175 | temp_profile.link = site.url.replace('{username}', username) |
| 176 | temp_profile.type = site.type |
| 177 | resolve(temp_profile) |
| 178 | } else { |
| 179 | resolve(undefined) |
| 180 | } |
| 181 | } catch (err) { |
| 182 | if (driver !== undefined) { |
| 183 | try { |
| 184 | await driver.quit() |
| 185 | } catch (err) { |
| 186 | helper.verbose && console.log('Driver Session Issue') |
| 187 | } |
| 188 | } |
| 189 | resolve(undefined) |
| 190 | } |
| 191 | } else { |
| 192 | resolve(undefined) |
| 193 | } |
| 194 | }) |
| 195 | } |
| 196 | |
| 197 | export default{ |
| 198 | find_username_advanced |
| 199 | } |
| 200 |