Skip to content

Commit

Permalink
feat: add a relay service for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Mar 24, 2020
1 parent fc8c618 commit 4b789a8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 14 deletions.
13 changes: 10 additions & 3 deletions lib/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { lowercaseHeaderKeys } from '../utils';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT } from '../utils/constant';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

type SupportConfigTypes = ShadowsocksNodeConfig|VmessNodeConfig|HttpsNodeConfig|HttpNodeConfig|ShadowsocksrNodeConfig|SnellNodeConfig|TrojanNodeConfig;
Expand All @@ -30,7 +30,7 @@ const logger = createLogger({
});

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

constructor(name: string, config: ClashProviderConfig) {
Expand All @@ -54,11 +54,18 @@ export default class ClashProvider extends Provider {
throw error;
}

this.url = config.url;
this._url = config.url;
this.udpRelay = config.udpRelay;
this.supportGetSubscriptionUserInfo = true;
}

public get url(): string {
if (this.relayUrl) {
return `${RELAY_SERVICE}${this._url}`;
}
return this._url;
}

public async getSubscriptionUserInfo(): Promise<SubscriptionUserinfo> {
const { subscriptionUserinfo } = await getClashSubscription(this.url, this.udpRelay);

Expand Down
4 changes: 4 additions & 0 deletions lib/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class Provider {
public readonly tfo?: boolean;
public readonly mptcp?: boolean;
public readonly renameNode?: ProviderConfig['renameNode'];
public readonly relayUrl?: boolean;
public supportGetSubscriptionUserInfo: boolean;
private startPort?: number;

Expand All @@ -38,6 +39,8 @@ export default class Provider {
mptcp: Joi.boolean(),
tfo: Joi.boolean(),
startPort: Joi.number().integer().min(1024).max(65535),
relayUrl: Joi.boolean(),
renameNode: Joi.function(),
})
.unknown();

Expand All @@ -58,6 +61,7 @@ export default class Provider {
this.mptcp = config.mptcp;
this.startPort = config.startPort;
this.renameNode = config.renameNode;
this.relayUrl = config.relayUrl;
this.supportGetSubscriptionUserInfo = false;
}

Expand Down
12 changes: 10 additions & 2 deletions lib/provider/ShadowsocksJsonSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Joi from '@hapi/joi';
import { ShadowsocksJsonSubscribeProviderConfig, SupportProviderEnum } from '../types';
import { getShadowsocksJSONConfig } from '../utils';
import { RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

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

constructor(name: string, config: ShadowsocksJsonSubscribeProviderConfig) {
super(name, config);
Expand All @@ -30,10 +31,17 @@ export default class ShadowsocksJsonSubscribeProvider extends Provider {
throw error;
}

this.url = config.url;
this._url = config.url;
this.udpRelay = config.udpRelay;
}

public get url(): string {
if (this.relayUrl) {
return `${RELAY_SERVICE}${this._url}`;
}
return this._url;
}

public getNodeList(): ReturnType<typeof getShadowsocksJSONConfig> {
return getShadowsocksJSONConfig(this.url, this.udpRelay);
}
Expand Down
13 changes: 10 additions & 3 deletions lib/provider/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { decodeStringList, fromBase64, fromUrlSafeBase64 } from '../utils';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT } from '../utils/constant';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

const logger = createLogger({
Expand All @@ -23,8 +23,8 @@ const logger = createLogger({
const debug = Debug('surgio:ShadowsocksSubscribeProvider');

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

constructor(name: string, config: ShadowsocksSubscribeProviderConfig) {
super(name, config);
Expand All @@ -49,11 +49,18 @@ export default class ShadowsocksSubscribeProvider extends Provider {
throw error;
}

this.url = config.url;
this._url = config.url;
this.udpRelay = config.udpRelay;
this.supportGetSubscriptionUserInfo = true;
}

public get url(): string {
if (this.relayUrl) {
return `${RELAY_SERVICE}${this._url}`;
}
return this._url;
}

public async getSubscriptionUserInfo(): Promise<SubscriptionUserinfo> {
const { subscriptionUserinfo } = await getShadowsocksSubscription(this.url, this.udpRelay);

Expand Down
13 changes: 10 additions & 3 deletions lib/provider/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { ShadowsocksrNodeConfig, ShadowsocksrSubscribeProviderConfig, Subscripti
import { fromBase64 } from '../utils';
import { parseSubscriptionNode, parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_TIMEOUT } from '../utils/constant';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { parseSSRUri } from '../utils/ssr';
import Provider from './Provider';

const logger = createLogger({ service: 'surgio:ShadowsocksrSubscribeProvider' });

export default class ShadowsocksrSubscribeProvider extends Provider {
public readonly url: string;
public readonly udpRelay?: boolean;
private readonly _url: string;

constructor(name: string, config: ShadowsocksrSubscribeProviderConfig) {
super(name, config);
Expand All @@ -40,11 +40,18 @@ export default class ShadowsocksrSubscribeProvider extends Provider {
throw error;
}

this.url = config.url;
this._url = config.url;
this.udpRelay = config.udpRelay;
this.supportGetSubscriptionUserInfo = true;
}

public get url(): string {
if (this.relayUrl) {
return `${RELAY_SERVICE}${this._url}`;
}
return this._url;
}

public async getSubscriptionUserInfo(): Promise<SubscriptionUserinfo> {
const { subscriptionUserinfo } = await getShadowsocksrSubscription(this.url, this.udpRelay);

Expand Down
13 changes: 10 additions & 3 deletions lib/provider/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import got from 'got';
import { NodeTypeEnum, V2rayNSubscribeProviderConfig, VmessNodeConfig } from '../types';
import { fromBase64 } from '../utils';
import { ConfigCache } from '../utils/cache';
import { NETWORK_TIMEOUT } from '../utils/constant';
import { NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

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

constructor(name: string, config: V2rayNSubscribeProviderConfig) {
super(name, config);
Expand All @@ -34,7 +34,14 @@ export default class V2rayNSubscribeProvider extends Provider {
throw error;
}

this.url = config.url;
this._url = config.url;
}

public get url(): string {
if (this.relayUrl) {
return `${RELAY_SERVICE}${this._url}`;
}
return this._url;
}

public getNodeList(): ReturnType<typeof getV2rayNSubscription> {
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface ProviderConfig {
readonly tfo?: boolean;
readonly mptcp?: boolean;
readonly renameNode?: (name: string) => string;
readonly relayUrl?: boolean;
}

export interface BlackSSLProviderConfig extends ProviderConfig {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ export const CATEGORIES = {
CLASH: 'Clash',
};

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

export const ERR_INVALID_FILTER = '传入的过滤器无效,请检查语法和变量名是否正确';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"allowSyntheticDefaultImports": true, // https://zhuanlan.zhihu.com/p/29022311
"resolveJsonModule": true,
"experimentalDecorators": true,

// "strict": true /* Enable all strict type-checking options. */,

Expand Down

0 comments on commit 4b789a8

Please sign in to comment.