Skip to content

Commit

Permalink
feat: 统一 UA
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Aug 16, 2020
1 parent 6c406fd commit 83264f8
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 99 deletions.
2 changes: 1 addition & 1 deletion lib/command/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getProvider } from '../provider';
import { errorHandler } from '../utils/error-helper';

class CheckCommand extends Command {
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio check [provider]';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type OnComplete = Parameters<typeof check>[1];
type CheckInfo = Parameters<OnComplete>[1];

class DoctorCommand extends Command {ssss
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio doctor';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { errorHandler } from '../utils/error-helper';
import { checkAndFix } from '../utils/linter';

class GenerateCommand extends Command {
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio generate';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Command from 'common-bin';
import { check, checkAndFix } from '../utils/linter';

class LintCommand extends Command {
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio lint [--fix]';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultTemplates = join(__dirname, '../../hygen-template');
const logger = createLogger({ service: 'surgio:NewCommand' })

class NewCommand extends Command {
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio new [provider|template|artifact]';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type PossibleProviderType = BlackSSLProvider|ShadowsocksJsonSubscribeProvider|Sh
class SubscriptionsCommand extends Command {
private config: CommandConfig;

constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio subscriptions';
this.options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { errorHandler } from '../utils/error-helper';
class GenerateCommand extends Command {
private readonly spinner: Ora;

constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);
this.usage = '使用方法: surgio upload';
this.spinner = ora();
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import path from 'path';
import updateNotifier from 'update-notifier';
import { transports } from '@surgio/logger';

import { isHeroku, isNow } from './utils';
import { isGitHubActions, isGitLabCI, isHeroku, isNow } from './utils';
import * as filter from './utils/filter';
import { errorHandler } from './utils/error-helper';
import { CATEGORIES } from './utils/constant';

// istanbul ignore next
if (!isNow() && !isHeroku()) {
if (!isNow() && !isHeroku() && !isGitHubActions() && !isGitLabCI()) {
// Global proxy
bootstrap();
}

const envPath = path.resolve(process.cwd(), './.env');

export class SurgioCommand extends Command {
constructor(rawArgv) {
constructor(rawArgv?: string[]) {
super(rawArgv);

// istanbul ignore next
Expand Down
72 changes: 68 additions & 4 deletions lib/provider/BlackSSLProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// istanbul ignore file

import Joi from '@hapi/joi';
import { BlackSSLProviderConfig } from '../types';
import { getBlackSSLConfig } from '../utils';
import assert from 'assert';

import {
BlackSSLProviderConfig,
HttpsNodeConfig,
NodeTypeEnum,
SubscriptionUserinfo
} from '../types';
import { ConfigCache } from '../utils/cache';
import httpClient from '../utils/http-client';
import Provider from './Provider';

export default class BlackSSLProvider extends Provider {
Expand Down Expand Up @@ -31,9 +39,65 @@ export default class BlackSSLProvider extends Provider {

this.username = config.username;
this.password = config.password;
this.supportGetSubscriptionUserInfo = true;
}

public async getSubscriptionUserInfo(): Promise<SubscriptionUserinfo|undefined> {
const { subscriptionUserinfo } = await this.getBlackSSLConfig(this.username, this.password);

if (subscriptionUserinfo) {
return subscriptionUserinfo;
}
return void 0;
}

public async getNodeList(): Promise<ReadonlyArray<HttpsNodeConfig>> {
const { nodeList } = await this.getBlackSSLConfig(this.username, this.password);
return nodeList;
}

public getNodeList(): ReturnType<typeof getBlackSSLConfig> {
return getBlackSSLConfig(this.username, this.password);
// istanbul ignore next
private async getBlackSSLConfig(username: string, password: string): Promise<{
readonly nodeList: ReadonlyArray<HttpsNodeConfig>;
readonly subscriptionUserinfo?: SubscriptionUserinfo;
}> {
assert(username, '未指定 BlackSSL username.');
assert(password, '未指定 BlackSSL password.');

const key = `blackssl_${username}`;

const response = ConfigCache.has(key) ? JSON.parse(ConfigCache.get(key) as string) : await (async () => {
const res = await httpClient
.get('https://api.darkssl.com/v1/service/ssl_info', {
searchParams: {
username,
password,
},
headers: {
'user-agent': 'GoAgentX/774 CFNetwork/901.1 Darwin/17.6.0 (x86_64)',
},
});

ConfigCache.set(key, res.body);

return JSON.parse(res.body);
})();

return {
nodeList: (response.ssl_nodes as readonly any[]).map<HttpsNodeConfig>(item => ({
nodeName: item.name,
type: NodeTypeEnum.HTTPS,
hostname: item.server,
port: item.port,
username,
password,
})),
subscriptionUserinfo: {
upload: 0,
download: response.transfer_used,
total: response.transfer_enable,
expire: response.expired_at,
},
};
}
}
10 changes: 4 additions & 6 deletions lib/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Joi from '@hapi/joi';
import assert from 'assert';
import got from 'got';
import yaml from 'yaml';
import _ from 'lodash';
import { createLogger } from '@surgio/logger';
Expand All @@ -18,9 +17,10 @@ import {
VmessNodeConfig,
} from '../types';
import { lowercaseHeaderKeys } from '../utils';
import httpClient, { getUserAgent } from '../utils/http-client';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_CLASH_UA, NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { NETWORK_CLASH_UA, RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

type SupportConfigTypes = ShadowsocksNodeConfig|VmessNodeConfig|HttpsNodeConfig|HttpNodeConfig|ShadowsocksrNodeConfig|SnellNodeConfig|TrojanNodeConfig;
Expand Down Expand Up @@ -101,12 +101,10 @@ export const getClashSubscription = async (
? SubscriptionCache.get(url) as SubsciptionCacheItem
: await (
async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
const res = await httpClient.get(url, {
responseType: 'text',
headers: {
'User-Agent': NETWORK_CLASH_UA,
'user-agent': getUserAgent(NETWORK_CLASH_UA),
},
});
const subsciptionCacheItem: SubsciptionCacheItem = {
Expand Down
10 changes: 3 additions & 7 deletions lib/provider/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Joi from '@hapi/joi';
import assert from 'assert';
import got from 'got';
import { default as legacyUrl } from 'url';
import Debug from 'debug';
import { createLogger } from '@surgio/logger';
Expand All @@ -12,9 +11,10 @@ import {
SubscriptionUserinfo,
} from '../types';
import { decodeStringList, fromBase64, fromUrlSafeBase64 } from '../utils';
import httpClient from '../utils/http-client';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { RELAY_SERVICE } from '../utils/constant';
import Provider from './Provider';

const logger = createLogger({
Expand Down Expand Up @@ -94,11 +94,7 @@ export const getShadowsocksSubscription = async (
? SubscriptionCache.get(url) as SubsciptionCacheItem
: await (
async () => {
const res = await got.get(url, {
retry: NETWORK_RETRY,
responseType: 'text',
timeout: NETWORK_TIMEOUT,
});
const res = await httpClient.get(url);
const subsciptionCacheItem: SubsciptionCacheItem = {
body: res.body,
};
Expand Down
10 changes: 3 additions & 7 deletions lib/provider/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Joi from '@hapi/joi';
import { createLogger } from '@surgio/logger';
import assert from 'assert';
import got from 'got';

import { ShadowsocksrNodeConfig, ShadowsocksrSubscribeProviderConfig, SubscriptionUserinfo } from '../types';
import { fromBase64 } from '../utils';
import httpClient from '../utils/http-client';
import { parseSubscriptionNode, parseSubscriptionUserInfo } from '../utils/subscription';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { RELAY_SERVICE } from '../utils/constant';
import { parseSSRUri } from '../utils/ssr';
import Provider from './Provider';

Expand Down Expand Up @@ -82,11 +82,7 @@ export const getShadowsocksrSubscription = async (
? SubscriptionCache.get(url) as SubsciptionCacheItem
: await (
async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
responseType: 'text',
});
const res = await httpClient.get(url);
const subsciptionCacheItem: SubsciptionCacheItem = {
body: res.body,
};
Expand Down
10 changes: 3 additions & 7 deletions lib/provider/SsdProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Joi from '@hapi/joi';
import { createLogger } from '@surgio/logger';
import assert from "assert";
import bytes from 'bytes';
import got from 'got';

import { NodeTypeEnum, ShadowsocksNodeConfig, SsdProviderConfig, SubscriptionUserinfo } from '../types';
import { decodeStringList, fromBase64 } from '../utils';
import { SubsciptionCacheItem, SubscriptionCache } from '../utils/cache';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { RELAY_SERVICE } from '../utils/constant';
import httpClient from '../utils/http-client';
import { parseSubscriptionUserInfo } from '../utils/subscription';
import Provider from './Provider';

Expand Down Expand Up @@ -85,11 +85,7 @@ export const getSsdSubscription = async (
? SubscriptionCache.get(url) as SubsciptionCacheItem
: await (
async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
responseType: 'text',
retry: NETWORK_RETRY,
});
const res = await httpClient.get(url);
const subsciptionCacheItem: SubsciptionCacheItem = {
body: res.body,
};
Expand Down
9 changes: 3 additions & 6 deletions lib/provider/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Joi from '@hapi/joi';
import { logger } from '@surgio/logger';
import assert from "assert";
import got from 'got';

import { NodeTypeEnum, V2rayNSubscribeProviderConfig, VmessNodeConfig } from '../types';
import { fromBase64 } from '../utils';
import { ConfigCache } from '../utils/cache';
import { NETWORK_RETRY, NETWORK_TIMEOUT, RELAY_SERVICE } from '../utils/constant';
import { RELAY_SERVICE } from '../utils/constant';
import httpClient from '../utils/http-client';
import Provider from './Provider';

export default class V2rayNSubscribeProvider extends Provider {
Expand Down Expand Up @@ -79,10 +79,7 @@ export const getV2rayNSubscription = async (

async function requestConfigFromRemote(): Promise<ReadonlyArray<VmessNodeConfig>> {
const response = ConfigCache.has(url) ? ConfigCache.get(url) as string : await (async () => {
const res = await got.get(url, {
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
});
const res = await httpClient.get(url);

ConfigCache.set(url, res.body);

Expand Down
2 changes: 0 additions & 2 deletions lib/utils/__tests__/subscription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ test('parseSubscriptionNode', t => {
});

test('formatSubscriptionUserInfo', t => {
const result = parseSubscriptionUserInfo('upload=0; download=42211676245; total=216256217222; expire=1584563470;');

t.deepEqual(
parseSubscriptionUserInfo('upload=0; download=42211676245; total=216256217222; expire=1584563470;'),
{
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const NETWORK_RETRY = process.env.SURGIO_NETWORK_RETRY ? Number(process.e

export const NETWORK_CLASH_UA = process.env.SURGIO_NETWORK_CLASH_UA ?? 'clash';

export const NETWORK_SURGIO_UA = 'surgio';

export const OBFS_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1';

export const PROXY_TEST_URL = 'http://cp.cloudflare.com/generate_204';
Expand Down
10 changes: 9 additions & 1 deletion lib/utils/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import got from 'got';
import HttpAgent, { HttpsAgent } from 'agentkeepalive';

import { NETWORK_TIMEOUT } from './constant';
import { NETWORK_TIMEOUT, NETWORK_SURGIO_UA, NETWORK_RETRY } from './constant';

const pkg = require('../../package.json');

export const getUserAgent = (str?: string): string => `${str ? str + ' ' : ''}${NETWORK_SURGIO_UA}/${pkg.version}`;

const httpClient = got.extend({
timeout: NETWORK_TIMEOUT,
retry: NETWORK_RETRY,
headers: {
'user-agent': getUserAgent(),
},
agent: {
http: new HttpAgent(),
https: new HttpsAgent()
Expand Down
Loading

0 comments on commit 83264f8

Please sign in to comment.