Skip to content

Commit

Permalink
feat: 支持用户自定义 emoji 替换规则
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Sep 12, 2020
1 parent 7b363d6 commit bce9ec5
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export interface CommandConfig {
readonly v2ray?: string;
vmess?: string;
};
readonly flags?: {
[name: string]: ReadonlyArray<string|RegExp>|string|RegExp;
},
readonly surgeConfig?: {
readonly shadowsocksFormat?: 'ss'|'custom';
readonly v2ray?: 'native'|'external';
Expand Down
30 changes: 30 additions & 0 deletions lib/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DEP005, DEP006 } from '../misc/deprecation';

import { CommandConfig } from '../types';
import { PROXY_TEST_INTERVAL, PROXY_TEST_URL } from './constant';
import { addFlagMap } from './flag';
import { ensureConfigFolder } from './index';

const showDEP005 = deprecate(_.noop, DEP005, 'DEP005');
Expand All @@ -24,6 +25,22 @@ export const loadConfig = (cwd: string, configPath: string, override?: Partial<C

validateConfig(userConfig);

if (userConfig.flags) {
Object.keys(userConfig.flags).forEach(emoji => {
if (userConfig.flags) {
if (typeof userConfig.flags[emoji] === 'string') {
addFlagMap(userConfig.flags[emoji] as string, emoji);
} else if (_.isRegExp(userConfig.flags[emoji])) {
addFlagMap(userConfig.flags[emoji] as RegExp, emoji);
} else {
(userConfig.flags[emoji] as ReadonlyArray<string|RegExp>).forEach(name => {
addFlagMap(name, emoji);
});
}
}
});
}

if (override) {
return {
...normalizeConfig(cwd, userConfig),
Expand Down Expand Up @@ -127,6 +144,19 @@ export const validateConfig = (userConfig: Partial<CommandConfig>): void => {
v2ray: Joi.string().pattern(/^\//),
vmess: Joi.string().pattern(/^\//),
}),
flags: Joi
.object()
.pattern(
Joi.string(),
[
Joi.string(),
Joi.object().regex(),
Joi.array().items(
Joi.string(),
Joi.object().regex(),
)
]
),
surgeConfig: Joi.object({
shadowsocksFormat: Joi.string().valid('ss', 'custom'),
v2ray: Joi.string().valid('native', 'external'),
Expand Down
10 changes: 6 additions & 4 deletions lib/utils/flag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import EmojiRegex from 'emoji-regex'
import EmojiRegex from 'emoji-regex';
import _ from 'lodash';

import flag from '../misc/flag_cn';

const flagMap: Map<string|RegExp, string> = new Map();
Expand Down Expand Up @@ -28,12 +30,12 @@ export const prependFlag = (str: string, removeExistingEmoji = false): string =>
}

for (const [key, value] of flagMap.entries()) {
if (typeof key === 'string') {
if (str.toUpperCase().includes(key)) {
if (_.isRegExp(key)) {
if (key.test(str)) {
return `${value} ${str}`;
}
} else {
if (key.test(str)) {
if (str.toUpperCase().includes(key)) {
return `${value} ${str}`;
}
}
Expand Down
16 changes: 10 additions & 6 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ test.serial('doctor command', async t => {
});
});

test.serial('cli works', async t => {
await coffee.fork(cli, ['generate', '-h'], {
cwd: resolve('plain'),
})
.expect('code', 0)
.end();
test.serial('help command', async t => {
await t.notThrowsAsync(async () => {
await coffee.fork(cli, ['generate', '-h'], {
cwd: resolve('plain'),
})
.expect('code', 0)
.end();
});
});

test.serial.only('cli works', async t => {
await coffee.fork(cli, ['generate'], {
cwd: resolve('plain'),
execArgv: ['--require', require.resolve('./stub-axios.js')],
Expand Down
16 changes: 15 additions & 1 deletion test/fixture/plain/provider/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ module.exports = {
hostname: 'trojan.example.com',
port: '443',
password: 'password',
}
},
{
type: 'trojan',
nodeName: '火箭 trojan node',
hostname: 'trojan.example.com',
port: '443',
password: 'password',
},
{
type: 'trojan',
nodeName: 'foobar trojan node',
hostname: 'trojan.example.com',
port: '443',
password: 'password',
},
],
renameNode: name => {
if (name === 'rename to HTTPS') {
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/plain/surgio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ module.exports = {
shadowsocksr: '/usr/local/bin/ssr-local',
v2ray: '/usr/local/bin/v2ray',
},
flags: {
'🚀': '火箭',
'🎉': [/foobar/i],
},
gateway: {
accessToken: 'abcd',
},
Expand Down
48 changes: 48 additions & 0 deletions test/snapshots/cli.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,51 @@ Generated by [AVA](https://avajs.dev).
DOMAIN,example.com,Proxy␊
`

## cli works

> Snapshot 3
`getSurgeNodes␊
测试 1 = vmess, 1.1.1.1, 8080, username=1386f85e-657b-4d6e-9d56-78badb75e1fd, ws=true, ws-path=/, ws-headers="host:example.com|user-agent: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"␊
测试 2 = vmess, 1.1.1.1, 8080, username=1386f85e-657b-4d6e-9d56-78badb75e1fd␊
测试 tls = vmess, example.com, 443, username=1386f85e-657b-4d6e-9d56-78badb75e1fd, ws=true, ws-path=/, ws-headers="host:example.com|user-agent: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", tls=true, tls13=false, skip-cert-verify=false, sni=example.com␊
----␊
getNodeNames␊
测试 1, 测试 2, 测试 tls␊
----␊
getQuantumultXNodes␊
vmess=1.1.1.1:8080, method=chacha20-ietf-poly1305, password=1386f85e-657b-4d6e-9d56-78badb75e1fd, obfs=ws, obfs-uri=/, obfs-host=example.com, tag=测试 1␊
vmess=1.1.1.1:8080, method=chacha20-ietf-poly1305, password=1386f85e-657b-4d6e-9d56-78badb75e1fd, tag=测试 2␊
vmess=example.com:443, method=chacha20-ietf-poly1305, password=1386f85e-657b-4d6e-9d56-78badb75e1fd, obfs=wss, obfs-uri=/, obfs-host=example.com, tag=测试 tls␊
----␊
getSurgeNodes␊
----␊
getMellowNodes␊
测试 1, vmess1, vmess1://1386f85e-657b-4d6e-9d56-78badb75e1fd@1.1.1.1:8080/?network=ws&tls=false&ws.host=example.com␊
测试 2, vmess1, vmess1://1386f85e-657b-4d6e-9d56-78badb75e1fd@1.1.1.1:8080/?network=tcp&tls=false␊
测试 tls, vmess1, vmess1://1386f85e-657b-4d6e-9d56-78badb75e1fd@example.com:443/?network=ws&tls=true&ws.host=example.com␊
----␊
clashProxyConfig␊
----␊
proxyTestUrl␊
http://www.google.com/generate_204␊
----␊
downloadUrl␊
https://example.com/v2rayn.conf?access_token=abcd␊
---␊
foo␊
---␊
bar␊
---␊
value␊
---␊
DOMAIN,example.com,Proxy␊
---␊
DOMAIN,example.com,Proxy␊
`
Binary file modified test/snapshots/cli.test.ts.snap
Binary file not shown.
7 changes: 6 additions & 1 deletion test/utils/flag.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import test from 'ava';
import { prependFlag, removeFlag } from '../../lib/utils/flag';
import { addFlagMap, prependFlag, removeFlag } from '../../lib/utils/flag';

test.before(() => {
addFlagMap(/foobar/i, '🚀');
});

test('addFlag', t => {
t.is(prependFlag('美国'), '🇺🇸 美国');
Expand All @@ -13,6 +17,7 @@ test('addFlag', t => {
t.is(prependFlag('🇺🇸 jp', true), '🇯🇵 jp');
t.is(prependFlag('🇯🇵 🇺🇸 jp', true), '🇯🇵 jp');
t.is(prependFlag('🇺🇸 🇯🇵 US', true), '🇺🇸 US');
t.is(prependFlag('foobar 节点'), '🚀 foobar 节点');
});

test('removeFlag', t => {
Expand Down

0 comments on commit bce9ec5

Please sign in to comment.