From 8bf2738f14f9d30b9bfbf6999fbbdf91a5993469 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Thu, 14 Nov 2019 12:06:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Provider=20=E5=A4=84=E7=90=86=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=B9=B6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/class/Provider.ts | 5 +++++ lib/generate.ts | 19 +++++++++++++------ lib/utils/constant.ts | 2 ++ lib/utils/index.ts | 4 ++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/class/Provider.ts b/lib/class/Provider.ts index 62faaa664..d3c735a1f 100644 --- a/lib/class/Provider.ts +++ b/lib/class/Provider.ts @@ -5,6 +5,7 @@ import { NodeNameFilterType, ProviderConfig, SupportProviderEnum, + PossibleNodeConfigType, } from '../types'; let globalPort: number = 61100; @@ -56,4 +57,8 @@ export default class Provider { } return globalPort++; } + + public getNodeList(): Promise> { + return Promise.resolve([]); + }; } diff --git a/lib/generate.ts b/lib/generate.ts index 4ea8651b4..5a1b79480 100644 --- a/lib/generate.ts +++ b/lib/generate.ts @@ -1,6 +1,7 @@ 'use strict'; import assert from 'assert'; +import Bluebird from 'bluebird'; import chalk from 'chalk'; import fs from 'fs-extra'; import _ from 'lodash'; @@ -42,6 +43,8 @@ import { } from './utils/filter'; import getProvider from './utils/get-provider'; import { prependFlag } from './utils/flag'; +import { NETWORK_CONCURRENCY } from './utils/constant'; +import Provider from './class/Provider'; const spinner = ora(); @@ -101,33 +104,33 @@ export async function generate( let customFilters: ProviderConfig['customFilters']; let netflixFilter: NodeNameFilterType; let youtubePremiumFilter: NodeNameFilterType; + let progress = 0; if (config.binPath && config.binPath.v2ray) { config.binPath.vmess = config.binPath.v2ray; } - for (const providerName of providerList) { + const providerMapper = async (providerName: string): Promise => { const filePath = path.resolve(config.providerDir, `${providerName}.js`); if (!fs.existsSync(filePath)) { throw new Error(`文件 ${filePath} 不存在`); } - spinner.text = `正在处理 Provider: ${providerName}`; - let provider; - let nodeConfigList; + let provider: Provider; + let nodeConfigList: ReadonlyArray; try { provider = getProvider(require(filePath)); } catch (err) { - err.message = `处理 Provider 时出现错误,相关文件 ${filePath} ,错误原因: ${err.message}`; + err.message = `处理 ${chalk.cyan(providerName)} 时出现错误,相关文件 ${filePath} ,错误原因: ${err.message}`; throw err; } try { nodeConfigList = await provider.getNodeList(); } catch (err) { - err.message = `获取 Provider 节点时出现错误,相关文件 ${filePath} ,错误原因: ${err.message}`; + err.message = `获取 ${chalk.cyan(providerName)} 节点时出现错误,相关文件 ${filePath} ,错误原因: ${err.message}`; throw err; } @@ -174,8 +177,12 @@ export async function generate( nodeList.push(nodeConfig); } }); + + spinner.text = `已处理 Provider ${++progress}/${providerList.length}...`; } + await Bluebird.map(providerList, providerMapper, { concurrency: NETWORK_CONCURRENCY }); + try { return templateEngine.render(`${template}.tpl`, { downloadUrl: getDownloadUrl(config.urlBase, artifactName, true, gatewayHasToken ? gatewayConfig.accessToken : undefined), diff --git a/lib/utils/constant.ts b/lib/utils/constant.ts index 982b3fdc1..5445076f9 100644 --- a/lib/utils/constant.ts +++ b/lib/utils/constant.ts @@ -1,5 +1,7 @@ export const NETWORK_TIMEOUT = process.env.SURGIO_NETWORK_TIMEOUT ? Number(process.env.SURGIO_NETWORK_TIMEOUT) : 20000; +export const NETWORK_CONCURRENCY = process.env.SURGIO_NETWORK_CONCURRENCY ? Number(process.env.SURGIO_NETWORK_CONCURRENCY) : 5; + export const OBFS_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'; export const PROXY_TEST_URL = 'http://www.qualcomm.cn/generate_204'; diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 8557c730c..32580269b 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -33,7 +33,7 @@ import { } from '../types'; import { normalizeConfig, validateConfig } from './config'; import { parseSSRUri } from './ssr'; -import { OBFS_UA, NETWORK_TIMEOUT } from './constant'; +import { OBFS_UA, NETWORK_TIMEOUT, NETWORK_CONCURRENCY } from './constant'; import { formatVmessUri } from './v2ray'; const debug = Debug('surgio:utils'); @@ -1154,7 +1154,7 @@ export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray