Skip to content

Commit

Permalink
feat: 节点增加 tfo 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 2, 2019
1 parent f3eaaed commit a820b89
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 15 deletions.
25 changes: 20 additions & 5 deletions lib/class/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import Provider from './Provider';
type SupportConfigTypes = ShadowsocksNodeConfig|VmessNodeConfig|HttpsNodeConfig|ShadowsocksrNodeConfig|SnellNodeConfig;

export default class ClashProvider extends Provider {
public static async getClashSubscription(url: string, udpRelay?: boolean): Promise<ReadonlyArray<SupportConfigTypes>> {
public static async getClashSubscription(url: string, udpRelay?: boolean, tfo?: boolean): Promise<ReadonlyArray<SupportConfigTypes>> {
assert(url, '未指定订阅地址 url');

return ConfigCache.has(url) ?
ConfigCache.get(url) :
await requestConfigFromRemote(url, udpRelay);
await requestConfigFromRemote(url, udpRelay, tfo);
}

public readonly url: string;
Expand Down Expand Up @@ -58,11 +58,11 @@ export default class ClashProvider extends Provider {
}

public getNodeList(): ReturnType<typeof ClashProvider.getClashSubscription> {
return ClashProvider.getClashSubscription(this.url, this.udpRelay);
return ClashProvider.getClashSubscription(this.url, this.udpRelay, this.tfo);
}
}

async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise<ReadonlyArray<SupportConfigTypes>> {
async function requestConfigFromRemote(url: string, udpRelay?: boolean, tfo?: boolean): Promise<ReadonlyArray<SupportConfigTypes>> {
const response = await axios.get(url, {
timeout: NETWORK_TIMEOUT,
responseType: 'text',
Expand Down Expand Up @@ -104,7 +104,10 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
...(item.obfs ? {
obfs: item.obfs,
'obfs-host': item['obfs-host'] || 'www.bing.com',
} : null)
} : null),
...(tfo !== void 0 ? {
tfo,
} : null),
};

case 'vmess':
Expand All @@ -123,6 +126,9 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
path: _.get(item, 'ws-path', '/'),
host: _.get(item, 'ws-headers.Host', ''),
} : null),
...(tfo !== void 0 ? {
tfo,
} : null),
};

case 'http':
Expand All @@ -139,6 +145,9 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
port: item.port,
username: item.username || '',
password: item.password || '',
...(tfo !== void 0 ? {
tfo,
} : null),
};

case 'snell':
Expand All @@ -149,6 +158,9 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
port: item.port,
psk: item.psk,
obfs: _.get(item, 'obfs-opts.mode', 'http'),
...(tfo !== void 0 ? {
tfo,
} : null),
};

case 'ssr':
Expand All @@ -163,6 +175,9 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
protocol: item.protocol,
protoparam: item.protocolparam,
method: item.cipher,
...(tfo !== void 0 ? {
tfo,
} : null),
};

default:
Expand Down
2 changes: 2 additions & 0 deletions lib/class/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class Provider {
public readonly youtubePremiumFilter?: NodeNameFilterType;
public readonly customFilters?: ProviderConfig['customFilters'];
public readonly addFlag?: boolean;
public readonly tfo?: boolean;
private startPort?: number;

constructor(config: ProviderConfig) {
Expand Down Expand Up @@ -44,6 +45,7 @@ export default class Provider {
this.youtubePremiumFilter = config.youtubePremiumFilter;
this.customFilters = config.customFilters;
this.addFlag = config.addFlag;
this.tfo = config.tfo;
this.startPort = config.startPort;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/class/ShadowsocksJsonSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default class ShadowsocksJsonSubscribeProvider extends Provider {
}

public getNodeList(): ReturnType<typeof getShadowsocksJSONConfig> {
return getShadowsocksJSONConfig(this.url, this.udpRelay);
return getShadowsocksJSONConfig(this.url, this.udpRelay, this.tfo);
}
}
2 changes: 1 addition & 1 deletion lib/class/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default class ShadowsocksSubscribeProvider extends Provider {
}

public getNodeList(): ReturnType<typeof getShadowsocksSubscription> {
return getShadowsocksSubscription(this.url, this.udpRelay);
return getShadowsocksSubscription(this.url, this.udpRelay, this.tfo);
}
}
2 changes: 1 addition & 1 deletion lib/class/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export default class ShadowsocksrSubscribeProvider extends Provider {
}

public getNodeList(): ReturnType<typeof getShadowsocksrSubscription> {
return getShadowsocksrSubscription(this.url);
return getShadowsocksrSubscription(this.url, this.tfo);
}
}
2 changes: 1 addition & 1 deletion lib/class/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export default class V2rayNSubscribeProvider extends Provider {
}

public getNodeList(): ReturnType<typeof getV2rayNSubscription> {
return getV2rayNSubscription(this.url);
return getV2rayNSubscription(this.url, this.tfo);
}
}
3 changes: 2 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export interface ProviderConfig {
readonly customFilters?: {
readonly [name: string]: NodeNameFilterType;
};
readonly addFlag: boolean;
readonly addFlag?: boolean;
readonly tfo?: boolean;
}

export interface BlackSSLProviderConfig extends ProviderConfig {
Expand Down
41 changes: 36 additions & 5 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ export const getBlackSSLConfig = async (username: string, password: string): Pro
await requestConfigFromBlackSSL();
};

export const getShadowsocksJSONConfig = async (url: string, udpRelay: boolean): Promise<ReadonlyArray<ShadowsocksNodeConfig>> => {
export const getShadowsocksJSONConfig = async (
url: string,
udpRelay?: boolean,
tfo?: boolean
): Promise<ReadonlyArray<ShadowsocksNodeConfig>> => {
assert(url, '未指定订阅地址 url');

async function requestConfigFromRemote(): Promise<ReadonlyArray<ShadowsocksNodeConfig>> {
Expand Down Expand Up @@ -123,6 +127,9 @@ export const getShadowsocksJSONConfig = async (url: string, udpRelay: boolean):
nodeConfig['obfs-host'] = obfsHost ? obfsHost[1] : 'www.bing.com';
}
}
if (tfo !== void 0) {
nodeConfig.tfo = tfo;
}

return nodeConfig;
});
Expand All @@ -137,7 +144,11 @@ export const getShadowsocksJSONConfig = async (url: string, udpRelay: boolean):
await requestConfigFromRemote();
};

export const getShadowsocksSubscription = async (url: string, udpRelay?: boolean): Promise<ReadonlyArray<ShadowsocksNodeConfig>> => {
export const getShadowsocksSubscription = async (
url: string,
udpRelay?: boolean,
tfo?: boolean
): Promise<ReadonlyArray<ShadowsocksNodeConfig>> => {
assert(url, '未指定订阅地址 url');

async function requestConfigFromRemote(): Promise<ReadonlyArray<ShadowsocksNodeConfig>> {
Expand Down Expand Up @@ -168,6 +179,9 @@ export const getShadowsocksSubscription = async (url: string, udpRelay?: boolean
obfs: pluginInfo.obfs,
'obfs-host': pluginInfo['obfs-host'],
} : null),
...(tfo !== void 0 ? {
tfo,
} : null),
};
});

Expand All @@ -181,7 +195,10 @@ export const getShadowsocksSubscription = async (url: string, udpRelay?: boolean
await requestConfigFromRemote();
};

export const getShadowsocksrSubscription = async (url: string): Promise<ReadonlyArray<ShadowsocksrNodeConfig>> => {
export const getShadowsocksrSubscription = async (
url: string,
tfo?: boolean,
): Promise<ReadonlyArray<ShadowsocksrNodeConfig>> => {
assert(url, '未指定订阅地址 url');

async function requestConfigFromRemote(): Promise<ReadonlyArray<ShadowsocksrNodeConfig>> {
Expand All @@ -193,7 +210,15 @@ export const getShadowsocksrSubscription = async (url: string): Promise<Readonly
const configList = fromBase64(response.data)
.split('\n')
.filter(item => !!item && item.startsWith("ssr://"));
const result = configList.map<ShadowsocksrNodeConfig>(parseSSRUri);
const result = configList.map<ShadowsocksrNodeConfig>(str => {
const nodeConfig = parseSSRUri(str);

if (tfo !== void 0) {
(nodeConfig.tfo as boolean) = tfo;
}

return nodeConfig;
});

ConfigCache.set(url, result);

Expand All @@ -205,7 +230,10 @@ export const getShadowsocksrSubscription = async (url: string): Promise<Readonly
await requestConfigFromRemote();
};

export const getV2rayNSubscription = async (url: string): Promise<ReadonlyArray<VmessNodeConfig>> => {
export const getV2rayNSubscription = async (
url: string,
tfo?: boolean
): Promise<ReadonlyArray<VmessNodeConfig>> => {
assert(url, '未指定订阅地址 url');

async function requestConfigFromRemote(): Promise<ReadonlyArray<VmessNodeConfig>> {
Expand Down Expand Up @@ -236,6 +264,9 @@ export const getV2rayNSubscription = async (url: string): Promise<ReadonlyArray<
tls: json.tls === 'tls',
host: json.host || '',
path: json.path || '/',
...(tfo !== void 0 ? {
tfo,
} : null),
};
});

Expand Down

0 comments on commit a820b89

Please sign in to comment.