@lzwme/m3u8-dl
Version:
A free, open-source, and powerful m3u8 video batch downloader with multi-threaded downloading, play-while-downloading, WebUI management, video parsing, and more.
94 lines (93 loc) • 3.91 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.initProxy = initProxy;
const utils_1 = require("./utils");
/** 初始化失败 N 次后续忽略代理配置 */
let initializFailedTimes = 3;
/**
* 初始化代理
*/
async function initProxy(options) {
if (!initializFailedTimes && !options.force)
return;
// 根据代理模式来初始化
try {
const g = global;
let globalAgent = g.GLOBAL_AGENT;
if (!globalAgent) {
// 代理未初始化且为禁用状态,则直接返回
if (options.proxyMode === 'disabled')
return;
// 为系统代理模式,但未设置环境变量,则直接返回
if (options.proxyMode === 'system' && !process.env.HTTP_PROXY && !process.env.HTTPS_PROXY)
return;
const globalAgentModule = await Promise.resolve().then(() => __importStar(require('global-agent')));
const ok = globalAgentModule.bootstrap();
if (ok)
globalAgent = g.GLOBAL_AGENT;
}
if (options.proxyMode !== 'disabled' && options.noProxy) {
options.noProxy = options.noProxy.replaceAll('\n', ',').trim();
}
if (options.proxyMode === 'custom' && options.proxyUrl) {
// 自定义代理模式
globalAgent.HTTP_PROXY = options.proxyUrl;
globalAgent.HTTPS_PROXY = options.proxyUrl;
globalAgent.NO_PROXY = options.noProxy;
utils_1.logger.info('Custom proxy enabled:', options.proxyUrl);
}
else if (options.proxyMode === 'disabled') {
globalAgent.HTTP_PROXY = undefined;
globalAgent.HTTPS_PROXY = undefined;
globalAgent.NO_PROXY = undefined;
// 关闭代理
utils_1.logger.info('Proxy disabled');
}
else {
// } else if (options.proxyMode === 'system') {
// 默认为使用系统代理,但支持自定义代理过滤
globalAgent.HTTP_PROXY = process.env.HTTP_PROXY;
globalAgent.HTTPS_PROXY = process.env.HTTPS_PROXY;
globalAgent.NO_PROXY = options.noProxy || process.env.NO_PROXY;
utils_1.logger.info('System proxy enabled');
}
}
catch (error) {
utils_1.logger.error('Failed to initialize proxy:', error);
utils_1.logger.warn('Please install global-agent to enable proxy support: npm install global-agent');
initializFailedTimes--;
}
}