mirror of
https://github.com/shanmiteko/LotteryAutoScript.git
synced 2026-06-12 21:03:13 +08:00
feat: 支持代理IP
This commit is contained in:
parent
8c53ddd23f
commit
8cf676a38a
4
.vscode/httpRequest.code-snippets
vendored
4
.vscode/httpRequest.code-snippets
vendored
@ -17,7 +17,7 @@
|
||||
" success: res => {",
|
||||
" $4",
|
||||
" },",
|
||||
" error: err => {",
|
||||
" failure: err => {",
|
||||
" $5",
|
||||
" }",
|
||||
"})$6"
|
||||
@ -43,7 +43,7 @@
|
||||
" success: res => {",
|
||||
" $4",
|
||||
" },",
|
||||
" error: err => {",
|
||||
" failure: err => {",
|
||||
" $5",
|
||||
" }",
|
||||
"})$6"
|
||||
|
||||
@ -164,6 +164,8 @@ Chrome浏览器:
|
||||
|
||||
程序便会每30天清理一次动态和关注
|
||||
|
||||
注: 短时大量清理动态会导致动态数显示异常
|
||||
|
||||
---
|
||||
|
||||
## 其他细节
|
||||
|
||||
@ -11,7 +11,6 @@ module.exports = (() => {
|
||||
url,
|
||||
query: queryStringsObj,
|
||||
headers: {
|
||||
"user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
|
||||
"accept": 'application/json, text/plain, */*',
|
||||
"cookie": GlobalVar.cookie
|
||||
},
|
||||
@ -25,7 +24,6 @@ module.exports = (() => {
|
||||
url,
|
||||
contents: data,
|
||||
headers: {
|
||||
"user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
|
||||
"accept": 'application/json, text/plain, */*',
|
||||
"content-type": 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
"cookie": GlobalVar.cookie
|
||||
|
||||
@ -107,7 +107,7 @@ const Base = {
|
||||
resolve('发条动态证明自己是真人[doge][doge][doge]')
|
||||
}
|
||||
},
|
||||
error: () => {
|
||||
failure: () => {
|
||||
resolve('发条动态证明自己是真人[doge][doge][doge]')
|
||||
}
|
||||
})
|
||||
|
||||
@ -13,11 +13,17 @@
|
||||
* @param {string} err
|
||||
* @returns {void}
|
||||
*
|
||||
* @typedef Proxy
|
||||
* @property {string} hostname
|
||||
* @property {string} [host]
|
||||
* @property {number} port
|
||||
*
|
||||
* @typedef {object} RequestOptions Http请求选项
|
||||
* @property {string} method 请求方法
|
||||
* @property {string} url 完整链接
|
||||
* @property {object} [query] 查询选项
|
||||
* @property {object} [contents] 内容
|
||||
* @property {Proxy} proxy 代理
|
||||
* @property {Object.<string, string|number>} [query] 查询选项
|
||||
* @property {Object.<string, string|number>} [contents] 内容
|
||||
* @property {HttpHeaders} [headers] 请求头
|
||||
* @property {SuccessCb} [success] 成功回调
|
||||
* @property {FailureCb} [failure] 失败回调
|
||||
@ -26,37 +32,38 @@ const { request: http_request } = require('http');
|
||||
const { request: https_request } = require('https');
|
||||
const { stringify } = require('querystring');
|
||||
/**超时时间 */
|
||||
const TIMEOUT = 20000;
|
||||
const TIMEOUT = 5000;
|
||||
/**出错等待时间 */
|
||||
const WAIT = 10000;
|
||||
/**错误尝试次数 */
|
||||
let retry = 6;
|
||||
/**Google Chrome */
|
||||
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36';
|
||||
/**
|
||||
* @description 简化HTTP请求
|
||||
* @param {RequestOptions} detail
|
||||
* @returns {void}
|
||||
*/
|
||||
const HttpRequest = detail => {
|
||||
const { method, url, query = {}, contents = {}, headers = {}, success, failure } = detail;
|
||||
const { method, url, proxy, query, contents, headers = {}, success, failure } = detail;
|
||||
const thisURL = new URL(url)
|
||||
, content = formatContents(headers, contents)
|
||||
, request = thisURL.protocol === 'http:' ? http_request : https_request;
|
||||
, content = formatContents(headers["content-type"], contents)
|
||||
, request = thisURL.protocol === 'https:' ? https_request : http_request;
|
||||
let options = {
|
||||
timeout: TIMEOUT,
|
||||
method,
|
||||
method: method.toUpperCase(),
|
||||
host: thisURL.host,
|
||||
path: thisURL.pathname,
|
||||
path: thisURL.pathname + thisURL.hash,
|
||||
headers,
|
||||
};
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
if (Object.keys(query).length !== 0) options.path += '?' + stringify(query);
|
||||
break;
|
||||
case 'POST':
|
||||
options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
|
||||
break;
|
||||
default:
|
||||
return failure(`未实现的请求方法: ${method}`);
|
||||
if (!headers["user-agent"]) headers["user-agent"] = UA;
|
||||
if (query) options.path += '?' + stringify(query);
|
||||
if (contents) options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
|
||||
if (proxy) {
|
||||
options.headers.host = thisURL.host;
|
||||
[options.hostname, options.port] = proxy.host ?
|
||||
proxy.host.split(':') : [proxy.hostname, proxy.port]
|
||||
options.path = thisURL.href;
|
||||
}
|
||||
const req = request(options, res => {
|
||||
let protodata = '';
|
||||
@ -81,7 +88,6 @@ const HttpRequest = detail => {
|
||||
}
|
||||
})
|
||||
});
|
||||
if (method === 'POST') req.write(content);
|
||||
req.on('timeout', () => { req.destroy(new Error('请求超时')) })
|
||||
.on('error', async err => {
|
||||
console.log(err.message);
|
||||
@ -93,22 +99,20 @@ const HttpRequest = detail => {
|
||||
failure(`[请求失败]: ${err.message}`);
|
||||
}
|
||||
})
|
||||
.end();
|
||||
.end(content);
|
||||
}
|
||||
/**
|
||||
* 处理请求体
|
||||
* 默认url编码字符串
|
||||
* @private
|
||||
* @param {HttpHeaders} headers 请求的内容格式
|
||||
* @param {string} type 请求的内容格式
|
||||
* @param {object} contents 请求体
|
||||
* @returns {string} 格式化字符串
|
||||
*/
|
||||
function formatContents(headers, contents) {
|
||||
const contentstype = headers['content-type'];
|
||||
if (typeof contentstype === 'undefined') return '';
|
||||
if (/application\/x-www-form-urlencoded/i.test(contentstype)) return stringify(contents);
|
||||
if (/application\/json/i.test(contentstype)) return JSON.stringify(contents);
|
||||
return '';
|
||||
function formatContents(type, contents) {
|
||||
if (/application\/json/i.test(type)) return JSON.stringify(contents);
|
||||
if (contents) return stringify(contents)
|
||||
return contents;
|
||||
}
|
||||
/**
|
||||
* 延时函数
|
||||
|
||||
Loading…
Reference in New Issue
Block a user