This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
166 lines (154 loc) · 3.78 KB
/
index.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
import scrapers from './src/scrapers'
import Lock from './src/util/promise-lock'
import child from 'child_process'
import fetch from 'node-fetch'
import path from 'path'
import { Readable as ReadableStream } from 'stream'
import debug from 'debug'
import os from 'os'
const log = debug('proxy-scraper')
const TYPES = ['http', 'socks']
const VALID_TYPES = ['socks', 'socks5', 'socks4', 'https', 'http']
export default class ProxyScraper {
constructor({ workerCount = os.cpus().length } = {}) {
this._workers = []
for (let i = 0; i < workerCount; i++) {
log('Spawning worker %d', i)
const worker = child.fork(path.join(__dirname, './src/worker.js'), [i])
worker.on('error', error => console.error(error))
this._workers.push(new Lock(worker))
}
}
getProxies(timeout) {
return this.scrapProxies().then(proxies =>
this.testProxies(timeout, proxies)
)
}
testProxies(timeout, proxies) {
log('Testing %d proxies with %d timeout', proxies.length, timeout)
const stream = new ReadableStream({ objectMode: true })
const proxiesCount = proxies.length
const queue = proxies.slice(0) //Clone it
let testedProxies = 0
stream._read = () => {
for (const worker of this._workers) {
let done = false
const run = () => {
if (queue.length > 0) {
const proxy = queue.pop()
worker
.get(worker =>
this._testProxy(
{
url: 'http://example.com/',
proxy: proxy.url(),
timeout
},
worker
)
)
.then(time => {
done = true
proxy.time = time
log('Working proxy: %o', proxy)
stream.push(proxy)
})
.catch(e => {
if (e.type && e.type == 'missmatch')
log('Content missmatch %o for proxy %o', e, proxy)
})
.then(() => {
testedProxies++
if(testedProxies === proxiesCount)
stream.push(null)
stream.emit('progress', {
length: proxiesCount,
tested: testedProxies,
remaining: proxiesCount - testedProxies,
percentage: (testedProxies / proxiesCount) * 100,
source: proxy.source
})
if (!done) run()
})
}
}
run()
}
}
return fetch('http://example.com/')
.then(res => res.text())
.then(page =>
Promise.all(
this._workers.map(worker =>
worker.get(worker => this._setPage(page, worker))
)
)
)
.then(() => stream)
}
_testProxy(proxy, worker) {
worker.send({
event: 'test',
data: proxy
})
return new Promise((resolve, reject) => {
worker.once('message', data => {
if (data.working) {
resolve(data.time)
} else {
reject(data.error)
}
})
})
}
_setPage(page, worker) {
worker.send({
event: 'page',
data: page
})
}
scrapProxies() {
const proxies = []
log('Scrapers: %o', Object.keys(ProxyScraper.scrapers))
for (let scraper in ProxyScraper.scrapers) {
proxies.push(
scrapers
[scraper]()
.then((proxies = []) => {
log('Found %d proxies from %s', proxies.length, scraper)
return proxies
.map(proxy => this._aggregateProxy(proxy, scraper))
.reduce((prev, next) => prev.concat(next), [])
})
.catch(e => {
log('Error while scraping proxies with %s\n%o', scraper, e)
return []
})
)
}
return Promise.all(proxies).then(values =>
values.reduce((prev, next) => prev.concat(next))
)
}
stop() {
for (const worker of this._workers) {
worker.get(worker => worker.kill())
}
}
_aggregateProxy(proxy, source) {
const aproxy = {
source,
url() {
return `${this.type}://${this.ip}:${this.port}`
},
...proxy
}
return VALID_TYPES.includes(aproxy.type)
? aproxy
: TYPES.map(type => ({
...aproxy,
type
}))
}
}
ProxyScraper.scrapers = scrapers