Skip to content

Commit

Permalink
feat: defineProvider 和 defineSurgioConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Apr 20, 2023
1 parent 1103933 commit ac81dc4
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 46 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/index';
1 change: 1 addition & 0 deletions internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/internal';
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
"name": "surgio",
"description": "Generating rules for Surge, Clash, Quantumult like a PRO",
"version": "2.25.0",
"exports": {
".": {
"types": "./build/index.d.ts",
"require": "./build/index.js"
},
"./internal": {
"types": "./build/internal.d.ts",
"require": "./build/internal.js"
}
},
"main": "./build/index.js",
"types": "./build/index.d.ts",
"bin": {
"surgio": "bin/run"
},
Expand Down
26 changes: 14 additions & 12 deletions src/constant/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export const OBFS_UA =

export const PROXY_TEST_URL = 'http://cp.cloudflare.com/generate_204';

export const INTERNET_TEST_URL = 'http://connect.rom.miui.com/generate_204';

export const PROXY_TEST_INTERVAL = 1200; // 1200s

export const CLASH_SUPPORTED_RULE: ReadonlyArray<string> = [
export const CLASH_SUPPORTED_RULE = [
'DOMAIN-SUFFIX',
'DOMAIN-KEYWORD',
'DOMAIN',
Expand All @@ -22,9 +24,9 @@ export const CLASH_SUPPORTED_RULE: ReadonlyArray<string> = [
'MATCH',
'FINAL',
'PROCESS-NAME',
];
] as const;

export const QUANTUMULT_X_SUPPORTED_RULE: ReadonlyArray<string> = [
export const QUANTUMULT_X_SUPPORTED_RULE = [
'USER-AGENT',
'HOST',
'HOST-KEYWORD',
Expand All @@ -36,10 +38,10 @@ export const QUANTUMULT_X_SUPPORTED_RULE: ReadonlyArray<string> = [
'IP-CIDR6',
'GEOIP',
'FINAL',
];
] as const;

// @see https://www.notion.so/2-967c1a07462c43ab88906162bec475a4
export const LOON_SUPPORTED_RULE: ReadonlyArray<string> = [
export const LOON_SUPPORTED_RULE = [
'DOMAIN-SUFFIX',
'DOMAIN',
'DOMAIN-KEYWORD',
Expand All @@ -48,9 +50,9 @@ export const LOON_SUPPORTED_RULE: ReadonlyArray<string> = [
'IP-CIDR',
'GEOIP',
'FINAL',
];
] as const;

export const MELLOW_UNSUPPORTED_RULE: ReadonlyArray<string> = [
export const MELLOW_UNSUPPORTED_RULE = [
'URL-REGEX',
'USER-AGENT',
'AND',
Expand All @@ -60,10 +62,10 @@ export const MELLOW_UNSUPPORTED_RULE: ReadonlyArray<string> = [
'IN-PORT',
'SRC-IP',
'RULE-SET',
];
] as const;

// @see https://getsurfboard.com/docs/profile-format/rule/
export const SURFBOARD_SUPPORTED_RULE: ReadonlyArray<string> = [
export const SURFBOARD_SUPPORTED_RULE = [
'DOMAIN-SUFFIX',
'DOMAIN',
'DOMAIN-KEYWORD',
Expand All @@ -74,7 +76,7 @@ export const SURFBOARD_SUPPORTED_RULE: ReadonlyArray<string> = [
'PROCESS-NAME',
'RULE-SET',
'DOMAIN-SET',
];
] as const;

export const CATEGORIES = {
SNIPPET: 'Snippet',
Expand All @@ -86,7 +88,7 @@ export const CATEGORIES = {
CLASH: 'Clash',
LOON: 'Loon',
SURFBOARD: 'Surfboard',
};
} as const;

export const RELAY_SERVICE = 'https://surgio-cors.herokuapp.com/';

Expand All @@ -95,4 +97,4 @@ export const TMP_FOLDER_NAME = 'surgio-config';
export const CACHE_KEYS = {
RemoteSnippets: 'remote-snippets',
Provider: 'provider',
};
} as const;
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs-extra';
import { join } from 'path';
import { PackageJson } from 'type-fest';

import {
Expand All @@ -15,11 +13,13 @@ import {
isVercel,
} from './utils';
import * as filter from './utils/filter';
import * as caches from './utils/cache';
import { CATEGORIES } from './constant';
import redis from './redis';

const pkg = fs.readJSONSync(join(__dirname, '../package.json')) as PackageJson;
export type { CommandConfigBeforeNormalize as SurgioConfig } from './types';
export * as caches from './utils/cache';
export * from './utils/configurables';

const pkg = require('../package.json') as PackageJson;

export const utils = {
...filter,
Expand All @@ -39,4 +39,4 @@ export const categories = {
...CATEGORIES,
};

export { pkg, caches, redis };
export { pkg };
3 changes: 2 additions & 1 deletion src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { loadConfig, setConfig, getConfig } from './utils/config';
import { loadRemoteSnippetList } from './utils/remote-snippet';

export * as types from './types';
export * as redis from './redis';
export * from './types';

const utils = {
loadRemoteSnippetList,
Expand Down
2 changes: 1 addition & 1 deletion src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default abstract class Provider {
}

this.supportGetSubscriptionUserInfo = false;
this.config = result.data;
this.config = result.data as ProviderConfig;
}

static async requestCacheableResource(
Expand Down
29 changes: 21 additions & 8 deletions src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SupportProviderEnum } from '../types';
import { PossibleProviderConfigType, SupportProviderEnum } from '../types';
import { defineProvider } from '../utils/configurables';
import BlackSSLProvider from './BlackSSLProvider';
import ClashProvider from './ClashProvider';
import CustomProvider from './CustomProvider';
Expand All @@ -7,16 +8,29 @@ import ShadowsocksrSubscribeProvider from './ShadowsocksrSubscribeProvider';
import ShadowsocksSubscribeProvider from './ShadowsocksSubscribeProvider';
import SsdProvider from './SsdProvider';
import TrojanProvider from './TrojanProvider';
import { PossibleProviderType } from './types';
import V2rayNSubscribeProvider from './V2rayNSubscribeProvider';
import { PossibleProviderType } from './types';

export {
BlackSSLProvider,
ClashProvider,
CustomProvider,
ShadowsocksJsonSubscribeProvider,
ShadowsocksrSubscribeProvider,
ShadowsocksSubscribeProvider,
SsdProvider,
TrojanProvider,
V2rayNSubscribeProvider,
};

export type { PossibleProviderType };

export async function getProvider(
name: string,
config: any,
config: ReturnType<typeof defineProvider> | PossibleProviderConfigType,
): Promise<PossibleProviderType> {
// 函数形式,需要先获取到返回值
if (typeof config === 'function') {
config = await config();
config = await config({});
}

switch (config.type) {
Expand All @@ -32,9 +46,8 @@ export async function getProvider(
case SupportProviderEnum.ShadowsocksrSubscribe:
return new ShadowsocksrSubscribeProvider(name, config);

case SupportProviderEnum.Custom: {
case SupportProviderEnum.Custom:
return new CustomProvider(name, config);
}

case SupportProviderEnum.V2rayNSubscribe:
return new V2rayNSubscribeProvider(name, config);
Expand All @@ -49,6 +62,6 @@ export async function getProvider(
return new TrojanProvider(name, config);

default:
throw new Error(`Unsupported provider type: ${config.type}`);
throw new Error(`Unsupported provider type: ${(config as any).type}`);
}
}
47 changes: 39 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { PossibleProviderType } from './provider';

import Provider from './provider/Provider';
import {
Expand Down Expand Up @@ -48,7 +49,10 @@ export type CommandConfigBeforeNormalize = z.infer<
typeof SurgioConfigValidator
>;

export type CommandConfig = CommandConfigBeforeNormalize & {
export type CommandConfig = Omit<
CommandConfigBeforeNormalize,
'customFilters'
> & {
publicUrl: string;
output: string;
urlBase: string;
Expand All @@ -71,36 +75,44 @@ export type ArtifactConfig = z.infer<typeof ArtifactValidator> & {
readonly template: string | undefined;
};

export type ProviderConfig = z.infer<typeof ProviderValidator> & {
readonly nodeFilter?: NodeFilterType | SortedNodeNameFilterType;
readonly netflixFilter?: NodeNameFilterType | SortedNodeNameFilterType;
readonly youtubePremiumFilter?: NodeNameFilterType | SortedNodeNameFilterType;
readonly customFilters?: {
readonly [name: string]: NodeNameFilterType | SortedNodeNameFilterType;
export type ProviderConfig = Omit<
z.infer<typeof ProviderValidator>,
'nodeFilter' | 'netflixFilter' | 'youtubePremiumFilter' | 'customFilters'
> & {
nodeFilter?: NodeFilterType | SortedNodeNameFilterType;
netflixFilter?: NodeNameFilterType | SortedNodeNameFilterType;
youtubePremiumFilter?: NodeNameFilterType | SortedNodeNameFilterType;
customFilters?: {
[name: string]: NodeNameFilterType | SortedNodeNameFilterType;
};
};

export interface BlackSSLProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.BlackSSL;
readonly username: string;
readonly password: string;
}

export interface ShadowsocksJsonSubscribeProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.ShadowsocksJsonSubscribe;
readonly url: string;
readonly udpRelay?: boolean;
}

export interface ShadowsocksSubscribeProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.ShadowsocksSubscribe;
readonly url: string;
readonly udpRelay?: boolean;
}

export interface ShadowsocksrSubscribeProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.ShadowsocksrSubscribe;
readonly url: string;
readonly udpRelay?: boolean;
}

export interface V2rayNSubscribeProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.V2rayNSubscribe;
readonly url: string;
readonly compatibleMode?: boolean;
readonly skipCertVerify?: boolean;
Expand All @@ -109,21 +121,25 @@ export interface V2rayNSubscribeProviderConfig extends ProviderConfig {
}

export interface ClashProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.Clash;
readonly url: string;
readonly udpRelay?: boolean;
readonly tls13?: boolean;
}

export interface SsdProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.Ssd;
readonly url: string;
readonly udpRelay?: boolean;
}

export interface CustomProviderConfig extends ProviderConfig {
readonly nodeList: ReadonlyArray<unknown>;
readonly type: SupportProviderEnum.Custom;
readonly nodeList: ReadonlyArray<PossibleNodeConfigType>;
}

export interface TrojanProviderConfig extends ProviderConfig {
readonly type: SupportProviderEnum.Trojan;
readonly url: string;
readonly udpRelay?: boolean;
readonly tls13?: boolean;
Expand Down Expand Up @@ -208,3 +224,18 @@ export type PossibleNodeConfigType =
| Socks5NodeConfig
| TuicNodeConfig
| WireguardNodeConfig;

export type PossibleProviderConfigType =
| BlackSSLProviderConfig
| ClashProviderConfig
| CustomProviderConfig
| ShadowsocksJsonSubscribeProviderConfig
| ShadowsocksrSubscribeProviderConfig
| ShadowsocksSubscribeProviderConfig
| SsdProviderConfig
| TrojanProviderConfig
| V2rayNSubscribeProviderConfig;

export type ProviderConfigFactory = (
options: Record<string, string>,
) => Promise<PossibleProviderConfigType> | PossibleProviderConfigType;
9 changes: 9 additions & 0 deletions src/utils/configurables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CommandConfigBeforeNormalize, ProviderConfigFactory } from '../types';
import { PossibleProviderConfigType } from '../types';

export const defineSurgioConfig = (config: CommandConfigBeforeNormalize) =>
config;

export const defineProvider = (
config: PossibleProviderConfigType | ProviderConfigFactory,
) => config;
4 changes: 4 additions & 0 deletions src/validators/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export const ProviderValidator = z.object({
relayUrl: z.union([z.boolean(), z.string().url()]).optional(),
requestUserAgent: z.ostring(),
renameNode: z.function().args(z.string()).returns(z.string()).optional(),
customFilters: z.record(z.function()).optional(),
nodeFilter: z.function().optional(),
netflixFilter: z.function().optional(),
youtubePremiumFilter: z.function().optional(),
});

0 comments on commit ac81dc4

Please sign in to comment.