w-ipproxy 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +9 -0
- package/.eslintignore +3 -0
- package/.eslintrc.js +55 -0
- package/.github/workflows/ci-test.yml +24 -0
- package/.jsdoc +25 -0
- package/LICENSE +21 -0
- package/README.md +71 -0
- package/SECURITY.md +5 -0
- package/babel.config.js +15 -0
- package/dist/w-ip-proxy.umd.js +7 -0
- package/dist/w-ip-proxy.umd.js.map +1 -0
- package/docs/WIpProxy.html +642 -0
- package/docs/WIpProxy.mjs.html +394 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
- package/docs/index.html +84 -0
- package/docs/scripts/collapse.js +39 -0
- package/docs/scripts/commonNav.js +28 -0
- package/docs/scripts/linenumber.js +25 -0
- package/docs/scripts/nav.js +12 -0
- package/docs/scripts/polyfill.js +4 -0
- package/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/docs/scripts/prettify/lang-css.js +2 -0
- package/docs/scripts/prettify/prettify.js +28 -0
- package/docs/scripts/search.js +99 -0
- package/docs/styles/jsdoc.css +776 -0
- package/docs/styles/prettify.css +80 -0
- package/g-get.mjs +23 -0
- package/g-srv.mjs +28 -0
- package/package.json +33 -0
- package/script.txt +18 -0
- package/src/WIpProxy.mjs +322 -0
- package/src/provideServer.mjs +65 -0
- package/test/all.test.mjs +10 -0
- package/toolg/addVersion.mjs +4 -0
- package/toolg/cleanFolder.mjs +4 -0
- package/toolg/gDistRollup.mjs +29 -0
- package/toolg/modifyReadme.mjs +4 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<title>WIpProxy.mjs - Documentation</title>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
<script src="scripts/prettify/prettify.js"></script>
|
|
10
|
+
<script src="scripts/prettify/lang-css.js"></script>
|
|
11
|
+
<!--[if lt IE 9]>
|
|
12
|
+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
13
|
+
<![endif]-->
|
|
14
|
+
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
|
|
15
|
+
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
|
|
16
|
+
<script src="scripts/nav.js" defer></script>
|
|
17
|
+
|
|
18
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
|
|
22
|
+
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
|
|
23
|
+
<label for="nav-trigger" class="navicon-button x">
|
|
24
|
+
<div class="navicon"></div>
|
|
25
|
+
</label>
|
|
26
|
+
|
|
27
|
+
<label for="nav-trigger" class="overlay"></label>
|
|
28
|
+
|
|
29
|
+
<nav >
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WIpProxy.html">WIpProxy</a></li></ul>
|
|
33
|
+
|
|
34
|
+
</nav>
|
|
35
|
+
|
|
36
|
+
<div id="main">
|
|
37
|
+
|
|
38
|
+
<h1 class="page-title">WIpProxy.mjs</h1>
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
<section>
|
|
47
|
+
<article>
|
|
48
|
+
<pre class="prettyprint source linenums"><code>import axios from 'axios'
|
|
49
|
+
import https from 'https'
|
|
50
|
+
import get from 'lodash-es/get.js'
|
|
51
|
+
import size from 'lodash-es/size.js'
|
|
52
|
+
import each from 'lodash-es/each.js'
|
|
53
|
+
import map from 'lodash-es/map.js'
|
|
54
|
+
import trim from 'lodash-es/trim.js'
|
|
55
|
+
import values from 'lodash-es/values.js'
|
|
56
|
+
import evem from 'wsemi/src/evem.mjs'
|
|
57
|
+
import sep from 'wsemi/src/sep.mjs'
|
|
58
|
+
import isestr from 'wsemi/src/isestr.mjs'
|
|
59
|
+
import ispint from 'wsemi/src/ispint.mjs'
|
|
60
|
+
import isbol from 'wsemi/src/isbol.mjs'
|
|
61
|
+
import isearr from 'wsemi/src/isearr.mjs'
|
|
62
|
+
import cint from 'wsemi/src/cint.mjs'
|
|
63
|
+
import waitFun from 'wsemi/src/waitFun.mjs'
|
|
64
|
+
import provideServer from './provideServer.mjs'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 抓取代理伺服器
|
|
69
|
+
*
|
|
70
|
+
* @class
|
|
71
|
+
* @param {Object} [opt={}] 輸入設定物件,預設{}
|
|
72
|
+
* @param {String} [opt.src='https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=5000&anonymity=all'] 輸入取得代理伺服器API字串,預設'https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=5000&anonymity=all'
|
|
73
|
+
* @param {String} [opt.tar='https://httpbin.org/ip'] 輸入檢測目標網址字串,預設'https://httpbin.org/ip'
|
|
74
|
+
* @param {Integer} [opt.timeGetProxies=30*1000] 輸入輪循抓取代理伺服器時間間隔整數,單位ms,預設30*1000
|
|
75
|
+
* @param {Integer} [opt.timeTestProxies=60*1000] 輸入輪循測試代理伺服器時間間隔整數,單位ms,預設60*1000
|
|
76
|
+
* @param {Boolean} [opt.withServer=false] 輸入是否創建供數據供給伺服器布林值,預設false
|
|
77
|
+
* @param {Integer} [opt.serverPort=8080] 輸入創建伺服器所用port整數,預設8080
|
|
78
|
+
* @param {String} [opt.serverApiName='getProxies'] 輸入供給數據API名稱字串,預設'getProxies'
|
|
79
|
+
* @param {Array} [opt.serverCorsOrigins=['*']] 輸入允許跨域網域陣列,若給予['*']代表允許全部,預設['*']
|
|
80
|
+
* @returns {Object} 回傳事件物件,提供函數getProxies,可監聽事件getRawProxies、add、delete、change
|
|
81
|
+
* @example
|
|
82
|
+
*
|
|
83
|
+
* import _ from 'lodash-es'
|
|
84
|
+
* import wip from './src/WIpProxy.mjs'
|
|
85
|
+
*
|
|
86
|
+
* let wo = wip({
|
|
87
|
+
* // tar: `https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT`,
|
|
88
|
+
* withServer: true,
|
|
89
|
+
* serverPort: 9000,
|
|
90
|
+
* serverCorsOrigins: ['*'],
|
|
91
|
+
* })
|
|
92
|
+
* wo.on('getRawProxies', (prxsRaw) => {
|
|
93
|
+
* console.log(`已抓取公開代理共 ${_.size(prxsRaw)} 個...`)
|
|
94
|
+
* })
|
|
95
|
+
* wo.on('add', (p, proxies) => {
|
|
96
|
+
* // console.log('新加入代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
|
|
97
|
+
* })
|
|
98
|
+
* wo.on('delete', (p, proxies) => {
|
|
99
|
+
* // console.log('刪除代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
|
|
100
|
+
* })
|
|
101
|
+
* wo.on('change', (proxies) => {
|
|
102
|
+
* // console.log(`有效代理`, _.map(proxies, 'proxy'))
|
|
103
|
+
* console.log(`已檢測有效代理共 ${_.size(proxies)} 個...`)
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* //browser view: http://localhost:9000/getProxies
|
|
107
|
+
*
|
|
108
|
+
*/
|
|
109
|
+
function WIpProxy(opt = {}) {
|
|
110
|
+
|
|
111
|
+
//src
|
|
112
|
+
let src = get(opt, 'src', '')
|
|
113
|
+
if (!isestr(src)) {
|
|
114
|
+
src = 'https://api.proxyscrape.com/v2/?request=displayproxies' +
|
|
115
|
+
`&protocol=http` +
|
|
116
|
+
// `&protocol=https` +
|
|
117
|
+
`&timeout=5000` +
|
|
118
|
+
// `&country=${join(['tw', 'cn', 'hk', 'jp', 'kr', 'sg', 'my', 'in', 'id', 'th', 'vn'])}` +
|
|
119
|
+
// `&ssl=yes` +
|
|
120
|
+
`&anonymity=all`
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//tar
|
|
124
|
+
let tar = get(opt, 'tar', '')
|
|
125
|
+
if (!isestr(tar)) {
|
|
126
|
+
tar = 'https://httpbin.org/ip' //'https://www.google.com'
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//timeGetProxies
|
|
130
|
+
let timeGetProxies = get(opt, 'timeGetProxies')
|
|
131
|
+
if (!ispint(timeGetProxies)) {
|
|
132
|
+
timeGetProxies = 30 * 1000 //30s
|
|
133
|
+
}
|
|
134
|
+
timeGetProxies = cint(timeGetProxies)
|
|
135
|
+
|
|
136
|
+
//timeTestProxies
|
|
137
|
+
let timeTestProxies = get(opt, 'timeTestProxies')
|
|
138
|
+
if (!ispint(timeTestProxies)) {
|
|
139
|
+
timeTestProxies = 60 * 1000 //1min
|
|
140
|
+
}
|
|
141
|
+
timeTestProxies = cint(timeTestProxies)
|
|
142
|
+
|
|
143
|
+
//withServer
|
|
144
|
+
let withServer = get(opt, 'withServer')
|
|
145
|
+
if (!isbol(withServer)) {
|
|
146
|
+
withServer = false
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//serverPort
|
|
150
|
+
let serverPort = get(opt, 'serverPort')
|
|
151
|
+
if (!ispint(serverPort)) {
|
|
152
|
+
serverPort = 8080
|
|
153
|
+
}
|
|
154
|
+
serverPort = cint(serverPort)
|
|
155
|
+
|
|
156
|
+
//serverApiName
|
|
157
|
+
let serverApiName = get(opt, 'serverApiName')
|
|
158
|
+
if (!isestr(serverApiName)) {
|
|
159
|
+
serverApiName = 'getProxies'
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
//serverCorsOrigins
|
|
163
|
+
let serverCorsOrigins = get(opt, 'serverCorsOrigins', [])
|
|
164
|
+
if (!isearr(serverCorsOrigins)) {
|
|
165
|
+
serverCorsOrigins = ['*']
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//prxsRaw, kpPrx
|
|
169
|
+
let prxsRaw = []
|
|
170
|
+
let kpPrx = {}
|
|
171
|
+
|
|
172
|
+
async function _getProxiesCore(src) {
|
|
173
|
+
let ps = []
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
|
|
177
|
+
//res
|
|
178
|
+
let res = await axios.get(src)
|
|
179
|
+
|
|
180
|
+
//c
|
|
181
|
+
let c = res.data
|
|
182
|
+
// console.log('c', c)
|
|
183
|
+
|
|
184
|
+
//sep
|
|
185
|
+
let ss = sep(c, '\n')
|
|
186
|
+
// console.log('ss', ss)
|
|
187
|
+
|
|
188
|
+
//ps
|
|
189
|
+
ps = map(ss, (v) => {
|
|
190
|
+
let [host, port] = v.split(':')
|
|
191
|
+
port = cint(port)
|
|
192
|
+
return {
|
|
193
|
+
proxy: v,
|
|
194
|
+
host,
|
|
195
|
+
port,
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
// console.log('ps', ps)
|
|
199
|
+
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
console.log('_getProxiesCore catch', err.message)
|
|
203
|
+
}
|
|
204
|
+
// console.log('ps', ps)
|
|
205
|
+
|
|
206
|
+
return ps
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function _getProxies() {
|
|
210
|
+
// console.log('call getProxies...')
|
|
211
|
+
prxsRaw = await _getProxiesCore(src)
|
|
212
|
+
.catch(() => {})
|
|
213
|
+
// console.log('call getProxies fin')
|
|
214
|
+
ev.emit('getRawProxies', prxsRaw)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function _testProxyCore(host, port, url) {
|
|
218
|
+
|
|
219
|
+
//ret
|
|
220
|
+
let ret = (state, msg) => {
|
|
221
|
+
return {
|
|
222
|
+
state,
|
|
223
|
+
msg,
|
|
224
|
+
p: {
|
|
225
|
+
proxy: `${host}:${port}`,
|
|
226
|
+
host,
|
|
227
|
+
port,
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
//r
|
|
233
|
+
let r = {}
|
|
234
|
+
try {
|
|
235
|
+
|
|
236
|
+
//agent
|
|
237
|
+
let agent = new https.Agent({ rejectUnauthorized: false }) //避免證書錯誤中止
|
|
238
|
+
|
|
239
|
+
//get
|
|
240
|
+
let res = await axios.get(url, {
|
|
241
|
+
proxy: {
|
|
242
|
+
host,
|
|
243
|
+
port,
|
|
244
|
+
protocol: 'http',
|
|
245
|
+
},
|
|
246
|
+
httpsAgent: agent,
|
|
247
|
+
timeout: 5000,
|
|
248
|
+
headers: {
|
|
249
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/122.0.0.0 Safari/537.36',
|
|
250
|
+
'Accept': 'application/json',
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
if (res.status === 200) {
|
|
255
|
+
r = ret('success', res.data)
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
r = ret('error', `status[${res.status}] error`)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
}
|
|
262
|
+
catch (err) {
|
|
263
|
+
r = ret('error', trim(err.message))
|
|
264
|
+
// if (r.msg.indexOf(`ssl3_get_record`) >= 0) {
|
|
265
|
+
// r.msg = `ssl3_get_record error`
|
|
266
|
+
// }
|
|
267
|
+
}
|
|
268
|
+
// console.log(r)
|
|
269
|
+
|
|
270
|
+
return r
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function _testProxiesCore(prxs) {
|
|
274
|
+
|
|
275
|
+
//pms
|
|
276
|
+
let pms = map(prxs, (p) => {
|
|
277
|
+
let pm = _testProxyCore(p.host, p.port, tar)
|
|
278
|
+
return pm
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
//rrs, allSettled
|
|
282
|
+
let rrs = await Promise.allSettled(pms)
|
|
283
|
+
// console.log('rrs', rrs)
|
|
284
|
+
|
|
285
|
+
//psValid, psInvalid
|
|
286
|
+
let psValid = []
|
|
287
|
+
let psInvalid = []
|
|
288
|
+
each(rrs, (v) => {
|
|
289
|
+
let status = get(v, 'status', '')
|
|
290
|
+
let b1 = status === 'fulfilled'
|
|
291
|
+
let state = get(v, 'value.state', '')
|
|
292
|
+
let b2 = state === 'success'
|
|
293
|
+
let b = b1 && b2
|
|
294
|
+
let p = get(v, 'value.p', null)
|
|
295
|
+
if (b) {
|
|
296
|
+
psValid.push(p)
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
psInvalid.push(p)
|
|
300
|
+
}
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
psValid,
|
|
305
|
+
psInvalid,
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async function _testProxies() {
|
|
310
|
+
// console.log('call testProxies...')
|
|
311
|
+
let r = await _testProxiesCore(prxsRaw)
|
|
312
|
+
each(r.psValid, (p) => {
|
|
313
|
+
kpPrx[p.proxy] = p
|
|
314
|
+
ev.emit('add', p, values(kpPrx))
|
|
315
|
+
})
|
|
316
|
+
each(r.psInvalid, (p) => {
|
|
317
|
+
delete kpPrx[p.proxy]
|
|
318
|
+
ev.emit('delete', p, values(kpPrx))
|
|
319
|
+
})
|
|
320
|
+
ev.emit('change', values(kpPrx))
|
|
321
|
+
// console.log('call testProxies fin')
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async function getProxies() {
|
|
325
|
+
let vs
|
|
326
|
+
await waitFun(() => {
|
|
327
|
+
vs = values(kpPrx)
|
|
328
|
+
return size(vs) > 0
|
|
329
|
+
})
|
|
330
|
+
return vs
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
//ev
|
|
334
|
+
let ev = evem()
|
|
335
|
+
|
|
336
|
+
//取得代理清單
|
|
337
|
+
if (true) {
|
|
338
|
+
_getProxies()
|
|
339
|
+
setInterval(() => {
|
|
340
|
+
_getProxies()
|
|
341
|
+
}, timeGetProxies)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
//過濾出有效代理清單, 延遲3s觸發, 給時間抓取
|
|
345
|
+
setTimeout(() => {
|
|
346
|
+
_testProxies()
|
|
347
|
+
setInterval(() => {
|
|
348
|
+
_testProxies()
|
|
349
|
+
.catch(() => {})
|
|
350
|
+
}, timeTestProxies)
|
|
351
|
+
}, 3000)
|
|
352
|
+
|
|
353
|
+
//withServer
|
|
354
|
+
if (withServer) {
|
|
355
|
+
provideServer(getProxies, {
|
|
356
|
+
port: serverPort,
|
|
357
|
+
apiName: serverApiName,
|
|
358
|
+
corsOrigins: serverCorsOrigins,
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
//save
|
|
363
|
+
ev.getProxies = getProxies
|
|
364
|
+
|
|
365
|
+
return ev
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
export default WIpProxy
|
|
370
|
+
</code></pre>
|
|
371
|
+
</article>
|
|
372
|
+
</section>
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
</div>
|
|
380
|
+
|
|
381
|
+
<br class="clear">
|
|
382
|
+
|
|
383
|
+
<footer>
|
|
384
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Fri Mar 21 2025 12:49:33 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
385
|
+
</footer>
|
|
386
|
+
|
|
387
|
+
<script>prettyPrint();</script>
|
|
388
|
+
<script src="scripts/polyfill.js"></script>
|
|
389
|
+
<script src="scripts/linenumber.js"></script>
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
</body>
|
|
394
|
+
</html>
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|