Skip to content

Commit

Permalink
feat: assign start port
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Sep 30, 2019
1 parent 8c4626d commit 47bcf0e
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 150 deletions.
18 changes: 18 additions & 0 deletions lib/class/BlackSSLProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BlackSSLProviderConfig } from '../types';
import { getBlackSSLConfig } from '../utils';
import Provider from './Provider';

export default class BlackSSLProvider extends Provider {
public readonly username: string;
public readonly password: string;

constructor(config: BlackSSLProviderConfig) {
super(config);
this.username = config.username;
this.password = config.password;
}

public getNodeList(): ReturnType<typeof getBlackSSLConfig> {
return getBlackSSLConfig(this);
}
}
17 changes: 17 additions & 0 deletions lib/class/CustomProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from 'assert';
import { CustomProviderConfig, PossibleNodeConfigType } from '../types';
import Provider from './Provider';

export default class CustomProvider extends Provider {
public readonly nodeList: ReadonlyArray<PossibleNodeConfigType>;

constructor(config: CustomProviderConfig) {
super(config);
assert(config.nodeList, 'Lack of nodeList.');
this.nodeList = config.nodeList;
}

public async getNodeList(): Promise<ReadonlyArray<PossibleNodeConfigType>> {
return this.nodeList;
}
}
34 changes: 34 additions & 0 deletions lib/class/Provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'assert';

import {
NodeFilterType,
NodeNameFilterType,
ProviderConfig,
SupportProviderEnum,
} from '../types';

let globalPort: number = 61100;

export default class Provider {
public readonly type: SupportProviderEnum;
public readonly nodeFilter?: NodeFilterType;
public readonly netflixFilter?: NodeNameFilterType;
public readonly youtubePremiumFilter?: NodeNameFilterType;
private startPort?: number;

constructor(config: ProviderConfig) {
assert(config.type, 'You must specify a provider type.');
this.type = config.type;
this.nodeFilter = config.nodeFilter;
this.netflixFilter = config.netflixFilter;
this.youtubePremiumFilter = config.youtubePremiumFilter;
this.startPort = config.startPort;
}

public get nextPort(): number {
if (this.startPort) {
return this.startPort++;
}
return globalPort++;
}
}
21 changes: 21 additions & 0 deletions lib/class/ShadowsocksJsonSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ShadowsocksJsonSubscribeProviderConfig } from '../types';
import { getShadowsocksJSONConfig } from '../utils';
import Provider from './Provider';

export default class ShadowsocksJsonSubscribeProvider extends Provider {
public readonly url: string;
public readonly udpRelay?: boolean;

constructor(config: ShadowsocksJsonSubscribeProviderConfig) {
super(config);
this.url = config.url;
this.udpRelay = config.udpRelay;
}

public getNodeList(): ReturnType<typeof getShadowsocksJSONConfig> {
return getShadowsocksJSONConfig({
url: this.url,
udpRelay: this.udpRelay,
});
}
}
21 changes: 21 additions & 0 deletions lib/class/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ShadowsocksSubscribeProviderConfig } from '../types';
import { getShadowsocksSubscription } from '../utils';
import Provider from './Provider';

export default class ShadowsocksSubscribeProvider extends Provider {
public readonly url: string;
public readonly udpRelay?: boolean;

constructor(config: ShadowsocksSubscribeProviderConfig) {
super(config);
this.url = config.url;
this.udpRelay = config.udpRelay;
}

public getNodeList(): ReturnType<typeof getShadowsocksSubscription> {
return getShadowsocksSubscription({
url: this.url,
udpRelay: this.udpRelay,
});
}
}
16 changes: 16 additions & 0 deletions lib/class/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ShadowsocksrSubscribeProviderConfig } from '../types';
import { getShadowsocksrSubscription } from '../utils';
import Provider from './Provider';

export default class ShadowsocksrSubscribeProvider extends Provider {
public readonly url: string;

constructor(config: ShadowsocksrSubscribeProviderConfig) {
super(config);
this.url = config.url;
}

public getNodeList(): ReturnType<typeof getShadowsocksrSubscription> {
return getShadowsocksrSubscription(this);
}
}
16 changes: 16 additions & 0 deletions lib/class/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { V2rayNSubscribeProviderConfig } from '../types';
import { getV2rayNSubscription } from '../utils';
import Provider from './Provider';

export default class V2rayNSubscribeProvider extends Provider {
public readonly url: string;

constructor(config: V2rayNSubscribeProviderConfig) {
super(config);
this.url = config.url;
}

public getNodeList(): ReturnType<typeof getV2rayNSubscription> {
return getV2rayNSubscription(this);
}
}
34 changes: 5 additions & 29 deletions lib/command/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ import fs from 'fs';
import path from 'path';

import {
BlackSSLProviderConfig,
CustomProviderConfig,
PossibleNodeConfigType,
ProviderConfig,
ShadowsocksJsonSubscribeProviderConfig,
SupportProviderEnum,
} from '../types';
import {
getBlackSSLConfig,
getShadowsocksJSONConfig,
loadConfig
} from '../utils';
import getProvider from '../utils/getProvider';

class CheckCommand extends Command {
private options: object;
Expand All @@ -38,36 +29,21 @@ class CheckCommand extends Command {
const providerName = ctx.argv._[0];
const config = loadConfig(ctx.cwd, ctx.argv.config);
const filePath = path.resolve(config.providerDir, `./${providerName}.js`);
const file: ProviderConfig|Error = fs.existsSync(filePath) ? require(filePath) : new Error('Provider file cannot be found.');
const file: any|Error = fs.existsSync(filePath) ? require(filePath) : new Error('Provider file cannot be found.');

if (file instanceof Error) {
throw file;
}

const configList = await this.requestRemoteFile(file);
const provider = getProvider(file);
const nodeList = await provider.getNodeList();

console.log(JSON.stringify(configList, null ,2));
console.log(JSON.stringify(nodeList, null ,2));
}

public get description(): string {
return 'Check configurations from provider';
}

private async requestRemoteFile(file: ProviderConfig): Promise<ReadonlyArray<PossibleNodeConfigType>> {
switch (file.type) {
case SupportProviderEnum.BlackSSL:
return getBlackSSLConfig(file as BlackSSLProviderConfig);

case SupportProviderEnum.ShadowsocksJsonSubscribe:
return getShadowsocksJSONConfig(file as ShadowsocksJsonSubscribeProviderConfig);

case SupportProviderEnum.Custom:
return (file as CustomProviderConfig).nodeList;

default:
throw new Error(`Unsupported provider type: ${file.type}`);
}
}
}

export = CheckCommand;
46 changes: 21 additions & 25 deletions lib/command/speed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import path from 'path';
import shelljs from 'shelljs';
import speedTest from 'speedtest-net';
import winston, { format, Logger } from 'winston';
import Provider from '../class/Provider';

import { ShadowsocksNodeConfig, ProviderConfig, SupportProviderEnum, ShadowsocksJsonSubscribeProviderConfig, CustomProviderConfig } from '../types';
import { getClashNodes, toYaml, getShadowsocksJSONConfig, loadConfig } from '../utils';
import { NodeTypeEnum, PossibleNodeConfigType, ShadowsocksNodeConfig } from '../types';
import { getClashNodes, loadConfig, toYaml } from '../utils';
import getProvider from '../utils/getProvider';

const { combine, timestamp, printf } = format;
const speedDebug = debug('speed');
Expand Down Expand Up @@ -51,14 +53,16 @@ class SpeedCommand extends Command {
const providerName = ctx.argv._[0];
const config = loadConfig(ctx.cwd, ctx.argv.config);
const filePath = path.resolve(config.providerDir, `./${providerName}.js`);
const file: ProviderConfig|Error = fs.existsSync(filePath) ? require(filePath) : new Error('Provider file cannot be found.');
const file: any|Error = fs.existsSync(filePath) ? require(filePath) : new Error('Provider file cannot be found.');

if (file instanceof Error) {
throw file;
}

const configList = await this.requestRemoteFile(file);
const nodeConfig = await this.promptSelections(configList);
const provider = getProvider(file);

const nodeList = await provider.getNodeList();
const nodeConfig = await this.promptSelections(nodeList);

await this.runTest(nodeConfig);
}
Expand Down Expand Up @@ -201,36 +205,28 @@ class SpeedCommand extends Command {
});
}

private async promptSelections(arr: ReadonlyArray<ShadowsocksNodeConfig>): Promise<ShadowsocksNodeConfig> {
private async promptSelections(arr: ReadonlyArray<PossibleNodeConfigType>): Promise<ShadowsocksNodeConfig> {
const choices = arr
.filter(item => {
return item.type === NodeTypeEnum.Shadowsocks;
})
.map(item => {
return {
name: `${item.nodeName} - ${chalk.gray(item.hostname + ':' + item.port)}`,
value: item,
};
});
const answer = await inquirer.prompt([
{
name: 'server',
message: 'Which server?',
type: 'list',
choices: arr.map(item => {
return {
name: `${item.nodeName} - ${chalk.gray(item.hostname + ':' + item.port)}`,
value: item,
};
}),
choices,
}
]);

return (answer as any).server as ShadowsocksNodeConfig;
}

private async requestRemoteFile(file: ProviderConfig): Promise<ReadonlyArray<ShadowsocksNodeConfig>> {
switch (file.type) {
case SupportProviderEnum.ShadowsocksJsonSubscribe:
return getShadowsocksJSONConfig(file as ShadowsocksJsonSubscribeProviderConfig);

case SupportProviderEnum.Custom:
return (file as CustomProviderConfig).nodeList as ReadonlyArray<ShadowsocksNodeConfig>;

default:
throw new Error(`Unsupported provider type: ${file.type}`);
}
}
}

export = SpeedCommand;
Loading

0 comments on commit 47bcf0e

Please sign in to comment.