Skip to content

Commit

Permalink
feat: add SURGIO_GFW_FREE environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 21, 2022
1 parent 5bf51a6 commit 2aaf242
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/constant/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const ENV_SURGIO_REMOTE_SNIPPET_CACHE_MAXAGE =
'SURGIO_REMOTE_SNIPPET_CACHE_MAXAGE';

export const ENV_SURGIO_PROVIDER_CACHE_MAXAGE = 'SURGIO_PROVIDER_CACHE_MAXAGE';

export const ENV_SURGIO_GFW_FREE = 'SURGIO_GFW_FREE';
12 changes: 4 additions & 8 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { transports } from '@surgio/logger';
import {
isAWS,
isAWSLambda,
isFlyIO,
isGFWFree,
isGitHubActions,
isGitLabCI,
isHeroku,
Expand All @@ -29,14 +31,7 @@ import { CATEGORIES } from './constant';
import redis from './redis';

// istanbul ignore next
if (
!isNow() &&
!isHeroku() &&
!isGitHubActions() &&
!isGitLabCI() &&
!isRailway() &&
!isNetlify()
) {
if (!isGFWFree()) {
// Global proxy
bootstrap();
}
Expand Down Expand Up @@ -82,6 +77,7 @@ export const utils = {
isRailway,
isNetlify,
isAWS,
isFlyIO,
isAWSLambda,
};

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/clash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const getClashNodes = function (
);
return null;
}
if (!nodeConfig.alpn || !nodeConfig.alpn.length) {
if (nodeConfig.alpn && !nodeConfig.alpn.length) {
logger.warn(
`节点 ${nodeConfig.nodeName} 的 alpn 为空。Stash 客户端不支持 ALPN 为空,默认的 ALPN 为 h3。`,
);
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/env-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ms from 'ms';
import {
ENV_NETWORK_RESOLVE_TIMEOUT,
ENV_NETWORK_TIMEOUT_KEY,
ENV_SURGIO_GFW_FREE,
ENV_SURGIO_NETWORK_CLASH_UA,
ENV_SURGIO_NETWORK_CONCURRENCY,
ENV_SURGIO_NETWORK_RETRY,
Expand Down Expand Up @@ -42,3 +43,6 @@ export const getProviderCacheMaxage = (): number =>
process.env[ENV_SURGIO_PROVIDER_CACHE_MAXAGE]
? Number(process.env[ENV_SURGIO_PROVIDER_CACHE_MAXAGE])
: ms('10m');

export const getIsGFWFree = (): boolean =>
typeof process.env[ENV_SURGIO_GFW_FREE] !== 'undefined';
10 changes: 9 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
VmessNodeConfig,
} from '../types';
import { ERR_INVALID_FILTER, OBFS_UA } from '../constant';
import { getIsGFWFree } from './env-flag';
import { validateFilter, applyFilter } from './filter';
import { formatVmessUri } from './v2ray';

Expand Down Expand Up @@ -627,11 +628,18 @@ export const isAWS = (): boolean =>
typeof process.env.AWS_EXECUTION_ENV !== 'undefined' ||
typeof process.env.AWS_REGION !== 'undefined';

// istanbul ignore next
export const isFlyIO = (): boolean =>
typeof process.env.FLY_REGION !== 'undefined';

export const isGFWFree = (): boolean =>
getIsGFWFree() ||
isAWS() ||
isAWSLambda() ||
isVercel() ||
isHeroku() ||
isGitHubActions() ||
isGitLabCI() ||
isRailway() ||
isNetlify();
isNetlify() ||
isFlyIO();
1 change: 1 addition & 0 deletions lib/utils/patch-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// istanbul ignore file

import { createLogger } from '@surgio/logger';

import { isGFWFree } from './';

const logger = createLogger({ service: 'surgio:utils:patch-proxy' });
Expand Down

0 comments on commit 2aaf242

Please sign in to comment.