-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusers.js
337 lines (297 loc) · 8.1 KB
/
users.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
'use strict'
const fs = require('fs').promises
const puppeteer = require('puppeteer')
const loc = require('./loc.js')
/* understand/
* keep track of the users and their browsers
*/
let USERS = {}
let PUPPET_SHOW = false
const DEFAULT_TIMEOUT = 30 * 1000
let TIMEOUT = DEFAULT_TIMEOUT
/* understand/
* keep unique objects to track errors
*/
const NEEDS_CAPCHA = {}
const LOGIN_ERR = {}
const PREMIUM_ERR = {}
function setPuppetShow(show) { PUPPET_SHOW = show }
function setTimeout(tm) {
tm = parseInt(tm)
if(isNaN(tm)) TIMEOUT = DEFAULT_TIMEOUT
else TIMEOUT = tm
}
/* way/
* Close any existing browsers and link up the ui's (user
* info's) with the associated users - keeping any
* additional information we may have cached
*/
function set(uis) {
closeBrowsers()
let users = USERS
USERS = {}
if(uis) {
uis.forEach(ui => {
USERS[ui.id] = Object.assign({ ui }, users[ui.id])
delete users[ui.id]
})
}
}
function closeBrowsers() {
for(let k in USERS) {
if(USERS[k].browserCache) {
USERS[k].browserCache.browser.close()
delete USERS[k].browserCache
}
}
}
/* way/
* If there is a valid cached browser return that - other
* wise launch a new browser with the (proxy and show) settings
* and return that - adding it to the cache for next time.
*/
function browser(user) {
if(user.browserCache) {
if(user.browserCache.proxy == user.proxy &&
user.browserCache.puppetShow == PUPPET_SHOW) {
return Promise.resolve(user.browserCache.browser)
}
user.browserCache.browser.close()
delete user.browserCache
}
let args = []
if(user.proxy) {
args.push(`--proxy-server=socks5://localhost:${uctx.proxy}`)
}
return new Promise((resolve, reject) => {
let puppetShow = PUPPET_SHOW
let pexe = puppeteer.executablePath()
let rx_isasar1 = /\\app.asar\\/
if(pexe.match(rx_isasar1)) {
pexe = pexe.replace(rx_isasar1, "\\app.asar.unpacked\\")
}
let rx_isasar2 = /\/app.asar\//
if(pexe.match(rx_isasar2)) {
pexe = pexe.replace(rx_isasar2, "/app.asar.unpacked/")
}
puppeteer.launch({
headless:!puppetShow,
executablePath: pexe,
args
}).then(browser => {
user.browserCache = {
proxy: user.proxy,
puppetShow,
browser,
}
browser.on("disconnect", () => {
if(user.browserCache) {
user.browserCache = null
delete user.browserCache
}
})
resolve(browser)
})
.catch(reject)
})
}
/* way/
* launch a browser with the current configuration, login to
* linkedin, check navigator and cache the result with it's
* configuration
* TODO: should this be configurable? - Facebook browser? Reddit browser? Twitter browser? ...
*/
async function linkedInPage(cfg, auth, browser) {
const page = await browser.newPage()
await page.setViewport({ width: 1920, height: 1080 })
page.setCacheEnabled(false)
if(cfg.timeout) await page.setDefaultTimeout(cfg.timeout)
else await page.setDefaultTimeout(TIMEOUT)
if(process.env.DEBUG) {
page.on("console", msg => console.log(msg.text()))
}
const cookie_f1 = loc.savedCookieFile(auth.id)
const cookie_f2 = loc.cookieFile(auth.id)
const loggedin = await cookie_login_1(cookie_f1, page)
|| await cookie_login_1(cookie_f2, page)
if(!loggedin) {
await auth_login_1(auth, page)
await save_login_cookie_1(page, auth)
}
//TODO: re-enable this check after QA cycle
//if(!process.env.DEBUG) await check_premium_enabled_1(page)
await randomly_scroll_sometimes_1()
await check_capcha_1()
return page
async function randomly_scroll_sometimes_1() {
const call = Math.floor(Math.random() * 16)
if(call == 8) await autoScroll(page)
}
async function check_capcha_1() {
try {
await page.waitForSelector('#rc-anchor-container')
throw NEEDS_CAPCHA
} catch(e) {}
}
/* outcome/
* We check if we have an element (li-icon) with type
* "premium-wordmark" which would indicate that we have premium
* enabled. Otherwise we check if we have a li-icon with type
* "sales-navigator-app-icon" which would indicate we have a Sales
* Navigator subscription.
*/
async function check_premium_enabled_1(page) {
let enabled = await page.evaluate(async () => {
let elems = document.getElementsByTagName('li-icon')
for(let i = 0;i < elems.length;i++) {
let t = elems[i].getAttribute('type')
if(t == 'premium-wordmark' ||
t == 'sales-navigator-app-icon') return true
}
})
if(!enabled) throw PREMIUM_ERR
}
async function cookie_login_1(cookie_f, page) {
try {
let cookie_s = await fs.readFile(cookie_f)
let cookies = JSON.parse(cookie_s)
await page.setCookie(...cookies)
await page.goto('https://www.linkedin.com/')
await page.waitFor('input[role=combobox]')
return true
} catch(e) {}
try {
await page.waitFor('[data-resource="feed/badge"]')
return true
} catch (e) {}
return false
}
async function save_login_cookie_1(page, auth) {
try {
let cookie_f = loc.cookieFile(auth.id)
let cookies = await page.cookies()
await fs.writeFile(cookie_f, JSON.stringify(cookies))
} catch(e) {
console.error(e)
}
}
async function auth_login_1(auth, page) {
if(!auth.linkedinUsername || !auth.linkedinPassword) throw LOGIN_ERR
try {
await page.goto('https://www.linkedin.com/uas/login')
const user_name = "input#username"
await page.waitForSelector(user_name)
await page.type(user_name, auth.linkedinUsername)
const pass_word = "input#password"
await page.waitForSelector(pass_word)
await page.type(pass_word, auth.linkedinPassword)
const submitButton = "button.btn__primary--large"
await page.waitForSelector(submitButton)
await page.click(submitButton)
await page.waitFor('input[role=combobox]',{timeout:200000})
} catch(e) {
throw LOGIN_ERR
}
}
}
/* outcome/
* Scroll down the page a bit
*/
async function autoScroll(page) {
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
let totalHeight = 0
let distance = 100
let maxdistance = 5000 + Math.floor(Math.random() * 10000)
let timer = setInterval(() => {
let scrollHeight = document.body.scrollHeight
window.scrollBy(0, distance)
totalHeight += distance
if(totalHeight >= scrollHeight ||
totalHeight > maxdistance){
clearInterval(timer)
resolve()
}
}, 100)
})
})
}
/* way/
* return user information (including associated proxy
* port)
*/
function get(id) {
let r = USERS[id]
if(r) r.proxy = UIPS[id]
return r
}
/* way/
* return relevant user information about all users
*/
function info() {
const r = []
for(let k in USERS) {
const curr = USERS[k].ui
r.push({
id: curr.id,
tenant: curr.tenant,
userName: curr.userName,
firstName: curr.firstName,
lastName: curr.lastName,
title: curr.title,
email: curr.email,
phone: curr.phone,
twitter: curr.twitter,
linkedin: curr.linkedin,
facebook: curr.facebook,
linkedinUsername: curr.linkedinUsername,
timeZone: curr.timeZone,
pic: curr.pic,
bots: curr.bots,
})
}
return r
}
/* understand/
* holds user to proxy ip mapping
*/
let UIPS = {}
function setips(uips) {
UIPS = {}
if(uips) {
uips.forEach(m => UIPS[m[0]] = m[1])
}
}
/* understand/
* save the cookie provided to use manually by the user
*/
function saveCookieFile(info) {
const cookie_f = loc.savedCookieFile(info.userid)
let data = []
data.push(info.cookie)
fs.writeFile(cookie_f, JSON.stringify(data), err => {
if(err) {
console.error("Error while writing cookie file")
console.error(err)
throw err
} else {
users.closeBrowsers()
}
})
}
module.exports = {
set,
get,
setips,
browser,
setPuppetShow,
setTimeout,
linkedInPage,
autoScroll,
closeBrowsers,
info,
saveCookieFile,
NEEDS_CAPCHA,
LOGIN_ERR,
PREMIUM_ERR,
}