Skip to content

Commit

Permalink
feat: support surfboard rule format
Browse files Browse the repository at this point in the history
  • Loading branch information
KotaHv committed Oct 2, 2022
1 parent 642d1b7 commit b7abfb2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/constant/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ export const MELLOW_UNSUPPORTED_RULE: ReadonlyArray<string> = [
'RULE-SET',
];

// @see https://getsurfboard.com/docs/profile-format/rule/
export const SURFBOARD_SUPPORTED_RULE: ReadonlyArray<string> = [
'DOMAIN-SUFFIX',
'DOMAIN',
'DOMAIN-KEYWORD',
'IP-CIDR',
'IP-CIDR6',
'GEOIP',
'FINAL',
'PROCESS-NAME',
'RULE-SET',
'DOMAIN-SET',
];

export const CATEGORIES = {
SNIPPET: 'Snippet',
SURGE: 'Surge',
Expand All @@ -71,6 +85,7 @@ export const CATEGORIES = {
QUANTUMULT_X_REWRITE: 'Quantumult X Rewrite',
CLASH: 'Clash',
LOON: 'Loon',
SURFBOARD: 'Surfboard',
};

export const RELAY_SERVICE = 'https://surgio-cors.herokuapp.com/';
Expand Down
69 changes: 69 additions & 0 deletions lib/generator/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,75 @@ test('loon filter 3', (t) => {
t.is(result, '');
});

test('surfboard filter 1', (t) => {
const body = `{{ str | surfboard }}`;
const str = `# Comment`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, '# Comment');
});

test('surfboard filter 2', (t) => {
const body = `{{ str | surfboard }}`;
const str = [
'IP-CIDR,67.198.55.0/24,Proxy,no-resolve // test rule',
'DOMAIN,example.com,Proxy,force-remote-dns',
].join('\n');
const result = templateEngine.renderString(body, {
str,
});

t.is(
result,
[
'IP-CIDR,67.198.55.0/24,Proxy,no-resolve // test rule',
'DOMAIN,example.com,Proxy,force-remote-dns',
].join('\n'),
);
});

test('surfboard filter 3', (t) => {
const body = `{{ str | surfboard }}`;
const str = `IP-CIDR6,xxxxxxxxxxxx`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, 'IP-CIDR6,xxxxxxxxxxxx');
});

test('surfboard filter 4', (t) => {
const body = `{{ str | surfboard }}`;
const str = `PROCESS-NAME,Telegram,Proxy,no-resolve // test rule`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, 'PROCESS-NAME,Telegram,Proxy,no-resolve // test rule');
});

test('surfboard filter 5', (t) => {
const body = `{{ str | surfboard }}`;
const str = `RULE-SET,http://example.com/rule-set,DIRECT`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, 'RULE-SET,http://example.com/rule-set,DIRECT');
});

test('surfboard filter 6', (t) => {
const body = `{{ str | surfboard }}`;
const str = `URL-REGEX,xxxxxxxxxxxx`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, '');
});

test('spaces in string', (t) => {
const str = ` `;

Expand Down
31 changes: 31 additions & 0 deletions lib/generator/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
QUANTUMULT_X_SUPPORTED_RULE,
CLASH_SUPPORTED_RULE,
LOON_SUPPORTED_RULE,
SURFBOARD_SUPPORTED_RULE,
} from '../constant';
import { addProxyToSurgeRuleSet } from '../utils/remote-snippet';

Expand Down Expand Up @@ -162,6 +163,36 @@ export function getEngine(templateDir: string): nunjucks.Environment {
.join('\n');
});

engine.addFilter('surfboard', (str?: string): string => {
// istanbul ignore next
if (!str) {
return '';
}

const array = str.split('\n');

return array
.map((item) => {
const testString: string =
!!item && item.trim() !== '' ? item.toUpperCase() : '';

if (testString.startsWith('#') || testString === '') {
return item;
}

const matched = testString.match(/^([\w-]+),/);

if (matched && SURFBOARD_SUPPORTED_RULE.some((s) => matched[1] === s)) {
// 过滤出支持的规则类型
return item.trim();
}

return null;
})
.filter((item) => !!item)
.join('\n');
});

// yaml
engine.addFilter('yaml', (obj: JsonObject) => YAML.stringify(obj));

Expand Down

0 comments on commit b7abfb2

Please sign in to comment.