Skip to content

Commit

Permalink
feat: Provider 处理改为并发
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 14, 2019
1 parent 7552fa0 commit 8bf2738
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/class/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NodeNameFilterType,
ProviderConfig,
SupportProviderEnum,
PossibleNodeConfigType,
} from '../types';

let globalPort: number = 61100;
Expand Down Expand Up @@ -56,4 +57,8 @@ export default class Provider {
}
return globalPort++;
}

public getNodeList(): Promise<ReadonlyArray<PossibleNodeConfigType>> {
return Promise.resolve([]);
};
}
19 changes: 13 additions & 6 deletions lib/generate.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<void> => {
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<PossibleNodeConfigType>;

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;
}

Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -1154,7 +1154,7 @@ export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSni
text: str, // 原始内容
}));
}, {
concurrency: 5,
concurrency: NETWORK_CONCURRENCY,
});
};

Expand Down

0 comments on commit 8bf2738

Please sign in to comment.