-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import Joi from 'joi'; | ||
import assert from 'assert'; | ||
import { createLogger } from '@surgio/logger'; | ||
|
||
import { | ||
SubscriptionUserinfo, | ||
TrojanNodeConfig, | ||
TrojanProviderConfig, | ||
} from '../types'; | ||
import { fromBase64 } from '../utils'; | ||
import relayableUrl from '../utils/relayable-url'; | ||
import { parseTrojanUri } from '../utils/trojan'; | ||
import Provider from './Provider'; | ||
|
||
const logger = createLogger({ | ||
service: 'surgio:TrojanProvider', | ||
}); | ||
|
||
export default class TrojanProvider extends Provider { | ||
public readonly _url: string; | ||
public readonly udpRelay?: boolean; | ||
public readonly tls13?: boolean; | ||
|
||
constructor(name: string, config: TrojanProviderConfig) { | ||
super(name, config); | ||
|
||
const schema = Joi.object({ | ||
url: Joi.string() | ||
.uri({ | ||
scheme: [/https?/], | ||
}) | ||
.required(), | ||
udpRelay: Joi.bool().strict(), | ||
tls13: Joi.bool().strict(), | ||
}).unknown(); | ||
|
||
const { error } = schema.validate(config); | ||
|
||
// istanbul ignore next | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
this._url = config.url; | ||
this.udpRelay = config.udpRelay; | ||
this.tls13 = config.tls13; | ||
this.supportGetSubscriptionUserInfo = true; | ||
} | ||
|
||
// istanbul ignore next | ||
public get url(): string { | ||
return relayableUrl(this._url, this.relayUrl); | ||
} | ||
|
||
public async getSubscriptionUserInfo(): Promise< | ||
SubscriptionUserinfo | undefined | ||
> { | ||
const { subscriptionUserinfo } = await getTrojanSubscription( | ||
this.url, | ||
this.udpRelay, | ||
this.tls13, | ||
); | ||
|
||
if (subscriptionUserinfo) { | ||
return subscriptionUserinfo; | ||
} | ||
return void 0; | ||
} | ||
|
||
public async getNodeList(): Promise<ReadonlyArray<TrojanNodeConfig>> { | ||
const { nodeList } = await getTrojanSubscription( | ||
this.url, | ||
this.udpRelay, | ||
this.tls13, | ||
); | ||
|
||
return nodeList; | ||
} | ||
} | ||
|
||
/** | ||
* @see https://github.com/trojan-gfw/trojan-url/blob/master/trojan-url.py | ||
*/ | ||
export const getTrojanSubscription = async ( | ||
url: string, | ||
udpRelay?: boolean, | ||
tls13?: boolean, | ||
): Promise<{ | ||
readonly nodeList: ReadonlyArray<TrojanNodeConfig>; | ||
readonly subscriptionUserinfo?: SubscriptionUserinfo; | ||
}> => { | ||
assert(url, '未指定订阅地址 url'); | ||
|
||
const response = await Provider.requestCacheableResource(url); | ||
const config = fromBase64(response.body); | ||
const nodeList = config | ||
.split('\n') | ||
.filter((item) => !!item && item.startsWith('trojan://')) | ||
.map((item): TrojanNodeConfig => { | ||
const nodeConfig = parseTrojanUri(item); | ||
|
||
return { | ||
...nodeConfig, | ||
'udp-relay': udpRelay, | ||
tls13, | ||
}; | ||
}); | ||
|
||
return { | ||
nodeList, | ||
subscriptionUserinfo: response.subscriptionUserinfo, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import test from 'ava'; | ||
import { NodeTypeEnum } from '../../types'; | ||
|
||
import { parseTrojanUri } from '../trojan'; | ||
|
||
test('parseTrojanUri', (t) => { | ||
t.deepEqual( | ||
parseTrojanUri( | ||
'trojan://password@example.com:443?allowInsecure=1&peer=sni.example.com#Example%20%E8%8A%82%E7%82%B9', | ||
), | ||
{ | ||
hostname: 'example.com', | ||
nodeName: 'Example 节点', | ||
password: 'password', | ||
port: '443', | ||
skipCertVerify: true, | ||
sni: 'sni.example.com', | ||
type: NodeTypeEnum.Trojan, | ||
}, | ||
); | ||
|
||
t.deepEqual( | ||
parseTrojanUri( | ||
'trojan://password@example.com:443#Example%20%E8%8A%82%E7%82%B9', | ||
), | ||
{ | ||
hostname: 'example.com', | ||
nodeName: 'Example 节点', | ||
password: 'password', | ||
port: '443', | ||
type: NodeTypeEnum.Trojan, | ||
}, | ||
); | ||
|
||
t.deepEqual( | ||
parseTrojanUri( | ||
'trojan://password@example.com:443?allowInsecure=true&peer=sni.example.com', | ||
), | ||
{ | ||
hostname: 'example.com', | ||
nodeName: 'example.com:443', | ||
password: 'password', | ||
port: '443', | ||
skipCertVerify: true, | ||
sni: 'sni.example.com', | ||
type: NodeTypeEnum.Trojan, | ||
}, | ||
); | ||
|
||
t.throws( | ||
() => { | ||
parseTrojanUri('ss://'); | ||
}, | ||
null, | ||
'Invalid Trojan URI.', | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Debug from 'debug'; | ||
import { URL } from 'url'; | ||
|
||
import { NodeTypeEnum, TrojanNodeConfig } from '../types'; | ||
|
||
const debug = Debug('surgio:utils:ss'); | ||
|
||
export const parseTrojanUri = (str: string): TrojanNodeConfig => { | ||
debug('Trojan URI', str); | ||
|
||
const scheme = new URL(str); | ||
|
||
if (scheme.protocol !== 'trojan:') { | ||
throw new Error('Invalid Trojan URI.'); | ||
} | ||
|
||
const allowInsecure = | ||
scheme.searchParams.get('allowInsecure') === '1' || | ||
scheme.searchParams.get('allowInsecure') === 'true'; | ||
const sni = scheme.searchParams.get('sni') || scheme.searchParams.get('peer'); | ||
|
||
return { | ||
type: NodeTypeEnum.Trojan, | ||
hostname: scheme.hostname, | ||
port: scheme.port, | ||
password: scheme.username, | ||
nodeName: scheme.hash | ||
? decodeURIComponent(scheme.hash.slice(1)) | ||
: `${scheme.hostname}:${scheme.port}`, | ||
...(allowInsecure | ||
? { | ||
skipCertVerify: true, | ||
} | ||
: null), | ||
...(sni | ||
? { | ||
sni, | ||
} | ||
: null), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters