返回 social-analyzer
engine.js
根目录 / modules / engine.js
1 import helper from './helper.js'
2 import createWorker from 'tesseract.js'
3
4 function merge_dicts (temp_dict) {
5 const result = {}
6 temp_dict.forEach(item => {
7 for (const [key, value] of Object.entries(item)) {
8 if (result[key]) {
9 result[key] += value
10 } else {
11 result[key] = value
12 }
13 }
14 })
15 return result
16 }
17
18 async function detect (type, uuid, username, options, site, source = '', text_only = '', screen_shot = '') {
19 const temp_profile = []
20 const temp_detected = []
21 let detections_count = 0
22 await Promise.all(site.detections.map(async detection => {
23 if (detection.type === 'shared') {
24 const shared_detection = await helper.shared_detections.find(o => o.name === detection.name)
25 const [val1, val2, val3] = await detect_logic(type, uuid, username, options, shared_detection, source, text_only, screen_shot)
26 temp_profile.push(val1)
27 temp_detected.push(val2)
28 detections_count += val3
29 } else if (detection.type === 'generic') {
30 helper.verbose && console.log('None')
31 } else if (detection.type === 'special') {
32 helper.verbose && console.log('None')
33 }
34 }))
35
36 const [val1, val2, val3] = await detect_logic(type, uuid, username, options, site, source, text_only, screen_shot)
37 temp_profile.push(val1)
38 temp_detected.push(val2)
39 detections_count += val3
40 return [merge_dicts(temp_profile), merge_dicts(temp_detected), detections_count]
41 }
42
43 async function detect_logic (type, uuid, username, options, site, source = '', text_only = '', screen_shot = '') {
44 const temp_profile = Object.assign({}, helper.profile_template)
45 const temp_detected = Object.assign({}, helper.detected_websites)
46 let detections_count = 0
47 await Promise.all(site.detections.map(async detection => {
48 if (source !== '' && helper.detection_level[helper.detection_level.current][type].includes(detection.type) && detection.type !== 'shared' && detection.type !== 'generic' && detection.type !== 'special') {
49 try {
50 detections_count += 1
51 temp_detected.count += 1
52 let temp_found = 'false'
53 if (detection.type === 'ocr' && screen_shot !== '' && options.includes('FindUserProfilesSlow')) {
54 const temp_buffer_image = Buffer.from(screen_shot, 'base64')
55 const ocr_worker = createWorker()
56 try {
57 await ocr_worker.load()
58 await ocr_worker.loadLanguage('eng')
59 await ocr_worker.initialize('eng')
60 const {
61 data: {
62 text
63 }
64 } = await ocr_worker.recognize(temp_buffer_image)
65 await ocr_worker.terminate()
66 if (text !== '') {
67 if (text.toLowerCase().includes(detection.string.toLowerCase())) {
68 temp_found = 'true'
69 }
70 if (detection.return === temp_found) {
71 temp_profile.found += 1
72 temp_detected.ocr += 1
73 if (detection.return === 'true') {
74 temp_detected.true += 1
75 } else {
76 temp_detected.false += 1
77 }
78 }
79 } else {
80 detections_count -= 1
81 temp_detected.count -= 1
82 }
83 } catch (err) {
84 detections_count -= 1
85 temp_detected.count -= 1
86 }
87 } else if (detection.type === 'normal' && source !== '') {
88 if (source.toLowerCase().includes(detection.string.replace('{username}', username).toLowerCase())) {
89 temp_found = 'true'
90 }
91
92 if (detection.return === temp_found) {
93 temp_profile.found += 1
94 temp_detected.normal += 1
95 if (detection.return === 'true') {
96 temp_detected.true += 1
97 } else {
98 temp_detected.false += 1
99 }
100 }
101 } else if (detection.type === 'advanced' && text_only !== '' && text_only !== 'unavailable') {
102 if (text_only.toLowerCase().includes(detection.string.replace('{username}', username).toLowerCase())) {
103 temp_found = 'true'
104 }
105
106 if (detection.return === temp_found) {
107 temp_profile.found += 1
108 temp_detected.advanced += 1
109 if (detection.return === 'true') {
110 temp_detected.true += 1
111 } else {
112 temp_detected.false += 1
113 }
114 }
115 }
116 } catch (err) {
117 helper.verbose && console.log(err)
118 }
119 }
120 }))
121
122 helper.verbose && console.log({
123 'Temp Profile': temp_profile,
124 Detected: temp_detected
125 })
126
127 return [temp_profile, temp_detected, detections_count]
128 }
129
130 export default{
131 detect
132 }
133
133 lines JAVASCRIPT