Skip to content

Commit

Permalink
feat: address compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 30, 2023
1 parent 39b5c94 commit ceaecb4
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 84 deletions.
73 changes: 73 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Snapshot report for `src/__tests__/index.test.ts`

The actual snapshot is saved in `index.test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## library

> Snapshot 1
{
categories: {
CLASH: 'Clash',
LOON: 'Loon',
QUANTUMULT_X: 'Quantumult X',
QUANTUMULT_X_FILTER: 'Quantumult X Filter',
QUANTUMULT_X_REWRITE: 'Quantumult X Rewrite',
QUANTUMULT_X_SERVER: 'Quantumult X Server',
SNIPPET: 'Snippet',
SURFBOARD: 'Surfboard',
SURGE: 'Surge',
},
defineProvider: Function defineProvider {},
defineSurgioConfig: Function defineSurgioConfig {},
utils: {
SortFilterWithSortedFilters: Function SortFilterWithSortedFilters {},
SortFilterWithSortedKeywords: Function SortFilterWithSortedKeywords {},
applyFilter: Function applyFilter {},
chinaBackFilter: Function chinaBackFilter {},
chinaOutFilter: Function chinaOutFilter {},
discardGlob: Function discardGlob {},
discardKeywords: Function discardKeywords {},
discardProviders: Function discardProviders {},
hkFilter: Function hkFilter {},
httpFilter: Function httpFilter {},
httpsFilter: Function httpsFilter {},
isAWS: Function isAWS {},
isAWSLambda: Function isAWSLambda {},
isFlyIO: Function isFlyIO {},
isGitHubActions: Function isGitHubActions {},
isGitLabCI: Function isGitLabCI {},
isHeroku: Function isHeroku {},
isNetlify: Function isNetlify {},
isNow: Function isNow {},
isRailway: Function isRailway {},
isVercel: Function isVercel {},
japanFilter: Function japanFilter {},
koreaFilter: Function koreaFilter {},
matchGlob: Function matchGlob {},
mergeFilters: Function mergeFilters {},
mergeSortedFilters: Function mergeSortedFilters {},
netflixFilter: Function netflixFilter {},
shadowsocksFilter: Function shadowsocksFilter {},
shadowsocksrFilter: Function shadowsocksrFilter {},
singaporeFilter: Function singaporeFilter {},
snellFilter: Function snellFilter {},
socks5Filter: Function socks5Filter {},
taiwanFilter: Function taiwanFilter {},
trojanFilter: Function trojanFilter {},
tuicFilter: Function tuicFilter {},
usFilter: Function usFilter {},
useGlob: Function useGlob {},
useKeywords: Function useKeywords {},
useProviders: Function useProviders {},
useRegexp: Function useRegexp {},
useSortedKeywords: Function useSortedKeywords {},
v2rayFilter: Function v2rayFilter {},
validateFilter: Function validateFilter {},
vmessFilter: Function vmessFilter {},
wireguardFilter: Function wireguardFilter {},
youtubePremiumFilter: Function {},
},
}
Binary file added src/__tests__/__snapshots__/index.test.ts.snap
Binary file not shown.
7 changes: 7 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from 'ava'

import * as surgio from '../'

test('library', (t) => {
t.snapshot(surgio)
})
4 changes: 1 addition & 3 deletions src/generator/__tests__/artifact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ test('render with extendRenderContext', async (t) => {

t.snapshot(
artifact.render(undefined, {
urlParams: {
foo: 'foo',
},
foo: 'foo',
}),
)
}
Expand Down
23 changes: 12 additions & 11 deletions src/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import { lowercaseHeaderKeys } from '../utils'
import { getNetworkClashUA } from '../utils/env-flag'
import relayableUrl from '../utils/relayable-url'
import Provider from './Provider'
import {
GetNodeListFunction,
GetNodeListParams,
GetSubscriptionUserInfoFunction,
} from './types'
import { GetNodeListFunction, GetSubscriptionUserInfoFunction } from './types'

type SupportConfigTypes =
| ShadowsocksNodeConfig
Expand Down Expand Up @@ -72,14 +68,17 @@ export default class ClashProvider extends Provider {
return relayableUrl(this.#originalUrl, this.config.relayUrl)
}

public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async ({
requestUserAgent,
} = {}) => {
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async (
params = {},
) => {
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { subscriptionUserinfo } = await getClashSubscription({
url: this.url,
udpRelay: this.udpRelay,
tls13: this.tls13,
requestUserAgent: requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
})

if (subscriptionUserinfo) {
Expand All @@ -92,12 +91,14 @@ export default class ClashProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<SupportConfigTypes[]> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { nodeList } = await getClashSubscription({
url: this.url,
udpRelay: this.udpRelay,
tls13: this.tls13,
requestUserAgent: requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
})

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
14 changes: 13 additions & 1 deletion src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default abstract class Provider {

// 是否支持在订阅中获取用户流量信息
public supportGetSubscriptionUserInfo: boolean
// 是否传递 Gateway 请求的 User-Agent
public passGatewayRequestUserAgent: boolean

protected constructor(public name: string, config: ProviderConfig) {
const result = ProviderValidator.safeParse(config)
Expand All @@ -36,6 +38,8 @@ export default abstract class Provider {
this.supportGetSubscriptionUserInfo = false
this.config = result.data as ProviderConfig
this.type = result.data.type
this.passGatewayRequestUserAgent =
getConfig()?.gateway?.passRequestUserAgent ?? false
}

static async requestCacheableResource(
Expand All @@ -46,7 +50,7 @@ export default abstract class Provider {
): Promise<SubsciptionCacheItem> {
const cacheType = getConfig()?.cache?.type || 'default'
const cacheKey = `${CACHE_KEYS.Provider}:${toMD5(
getUserAgent(options.requestUserAgent || '') + url,
getUserAgent(options.requestUserAgent) + url,
)}`
const requestResource = async () => {
const headers = {}
Expand Down Expand Up @@ -104,6 +108,14 @@ export default abstract class Provider {
}
}

public determineRequestUserAgent(
requestUserAgent?: string | undefined,
): string | undefined {
return this.passGatewayRequestUserAgent
? requestUserAgent || this.config.requestUserAgent
: this.config.requestUserAgent
}

public get nextPort(): number {
if (this.config.startPort) {
return this.config.startPort++
Expand Down
6 changes: 4 additions & 2 deletions src/provider/ShadowsocksJsonSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export default class ShadowsocksJsonSubscribeProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<Array<ShadowsocksNodeConfig>> => {
const { requestUserAgent } = params || {}
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const nodeList = await getShadowsocksJSONConfig(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
17 changes: 11 additions & 6 deletions src/provider/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ export default class ShadowsocksSubscribeProvider extends Provider {
return relayableUrl(this.#originalUrl, this.config.relayUrl)
}

public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async ({
requestUserAgent,
} = {}) => {
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async (
params = {},
) => {
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { subscriptionUserinfo } = await getShadowsocksSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (subscriptionUserinfo) {
Expand All @@ -58,11 +61,13 @@ export default class ShadowsocksSubscribeProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<Array<ShadowsocksNodeConfig>> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { nodeList } = await getShadowsocksSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
17 changes: 11 additions & 6 deletions src/provider/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ export default class ShadowsocksrSubscribeProvider extends Provider {
return relayableUrl(this.#originalUrl, this.config.relayUrl)
}

public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async ({
requestUserAgent,
} = {}) => {
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async (
params = {},
) => {
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { subscriptionUserinfo } = await getShadowsocksrSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (subscriptionUserinfo) {
Expand All @@ -64,11 +67,13 @@ export default class ShadowsocksrSubscribeProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<Array<ShadowsocksrNodeConfig>> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { nodeList } = await getShadowsocksrSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
17 changes: 11 additions & 6 deletions src/provider/SsdProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ export default class SsdProvider extends Provider {
return relayableUrl(this.#originalUrl, this.config.relayUrl)
}

public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async ({
requestUserAgent,
} = {}) => {
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async (
params = {},
) => {
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { subscriptionUserinfo } = await getSsdSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (subscriptionUserinfo) {
Expand All @@ -64,11 +67,13 @@ export default class SsdProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<Array<ShadowsocksNodeConfig>> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { nodeList } = await getSsdSubscription(
this.url,
this.udpRelay,
requestUserAgent || this.config.requestUserAgent,
requestUserAgent,
)

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
15 changes: 10 additions & 5 deletions src/provider/TrojanProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ export default class TrojanProvider extends Provider {
return relayableUrl(this.#originalUrl, this.config.relayUrl)
}

public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async ({
requestUserAgent,
} = {}) => {
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction = async (
params = {},
) => {
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { subscriptionUserinfo } = await getTrojanSubscription({
url: this.url,
udpRelay: this.udpRelay,
Expand All @@ -62,12 +65,14 @@ export default class TrojanProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): Promise<Array<TrojanNodeConfig>> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const { nodeList } = await getTrojanSubscription({
url: this.url,
udpRelay: this.udpRelay,
tls13: this.tls13,
requestUserAgent: requestUserAgent || this.config.requestUserAgent,
requestUserAgent: requestUserAgent,
})

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
6 changes: 4 additions & 2 deletions src/provider/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ export default class V2rayNSubscribeProvider extends Provider {
public getNodeList: GetNodeListFunction = async (
params = {},
): ReturnType<typeof getV2rayNSubscription> => {
const { requestUserAgent } = params
const requestUserAgent = this.determineRequestUserAgent(
params.requestUserAgent,
)
const nodeList = await getV2rayNSubscription({
url: this.url,
skipCertVerify: this.skipCertVerify,
tls13: this.tls13,
udpRelay: this.udpRelay,
isCompatibleMode: this.compatibleMode,
requestUserAgent: requestUserAgent || this.config.requestUserAgent,
requestUserAgent: requestUserAgent,
})

if (this.config.hooks?.afterFetchNodeList) {
Expand Down
Loading

0 comments on commit ceaecb4

Please sign in to comment.