From bedff827094fdc68e106967be4237ed22afbcf5e Mon Sep 17 00:00:00 2001 From: Roy Li Date: Thu, 15 Jun 2023 23:24:49 +0200 Subject: [PATCH] feat: support Surge tuic-v5 format --- package.json | 1 + pnpm-lock.yaml | 7 ++++ src/__tests__/__snapshots__/index.test.ts.md | 10 +++++- .../__snapshots__/index.test.ts.snap | Bin 1457 -> 1655 bytes src/generator/__tests__/artifact.test.ts | 2 +- src/provider/__tests__/ClashProvider.test.ts | 2 +- .../__snapshots__/remote-snippet.test.ts.snap | Bin 2031 -> 2032 bytes src/utils/__tests__/surge.test.ts | 2 +- src/utils/surge.ts | 34 ++++++++++++++++-- test/cli.cli-test.ts | 2 +- tsconfig.json | 1 - 11 files changed, 52 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 8a64b21b4..88e86a73a 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,7 @@ "@types/chai": "^4.3.5", "@types/debug": "^4.1.8", "@types/fs-extra": "^11.0.1", + "@types/ini": "^1.3.31", "@types/inquirer": "^8.2.6", "@types/ioredis-mock": "^8.2.2", "@types/lodash": "^4.14.195", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a9796b7e..be66787ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -168,6 +168,9 @@ devDependencies: '@types/fs-extra': specifier: ^11.0.1 version: 11.0.1 + '@types/ini': + specifier: ^1.3.31 + version: 1.3.31 '@types/inquirer': specifier: ^8.2.6 version: 8.2.6 @@ -1562,6 +1565,10 @@ packages: /@types/http-cache-semantics@4.0.1: resolution: {integrity: sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==} + /@types/ini@1.3.31: + resolution: {integrity: sha512-8ecxxaG4AlVEM1k9+BsziMw8UsX0qy3jYI1ad/71RrDZ+rdL6aZB0wLfAuflQiDhkD5o4yJ0uPK3OSUic3fG0w==} + dev: true + /@types/inquirer@8.2.6: resolution: {integrity: sha512-3uT88kxg8lNzY8ay2ZjP44DKcRaTGztqeIvN2zHvhzIBH/uAPaL75aBtdNRKbA7xXoMbBt5kX0M00VKAnfOYlA==} dependencies: diff --git a/src/__tests__/__snapshots__/index.test.ts.md b/src/__tests__/__snapshots__/index.test.ts.md index c1434e09c..a1c274fb5 100644 --- a/src/__tests__/__snapshots__/index.test.ts.md +++ b/src/__tests__/__snapshots__/index.test.ts.md @@ -20,8 +20,16 @@ Generated by [AVA](https://avajs.dev). SURFBOARD: 'Surfboard', SURGE: 'Surge', }, - defineProvider: Function defineProvider {}, + defineBlackSSLProvider: Function defineBlackSSLProvider {}, + defineClashProvider: Function defineClashProvider {}, + defineCustomProvider: Function defineCustomProvider {}, + defineShadowsocksJsonSubscribeProvider: Function defineShadowsocksJsonSubscribeProvider {}, + defineShadowsocksSubscribeProvider: Function defineShadowsocksSubscribeProvider {}, + defineShadowsocksrSubscribeProvider: Function defineShadowsocksrSubscribeProvider {}, + defineSsdProvider: Function defineSsdProvider {}, defineSurgioConfig: Function defineSurgioConfig {}, + defineTrojanProvider: Function defineTrojanProvider {}, + defineV2rayNSubscribeProvider: Function defineV2rayNSubscribeProvider {}, utils: { SortFilterWithSortedFilters: Function SortFilterWithSortedFilters {}, SortFilterWithSortedKeywords: Function SortFilterWithSortedKeywords {}, diff --git a/src/__tests__/__snapshots__/index.test.ts.snap b/src/__tests__/__snapshots__/index.test.ts.snap index 56225fc0d9758b303fdf2fe3783d622353e54fb1..6ebda458b58fa81ab01e8ba8840beb815f7c76af 100644 GIT binary patch literal 1655 zcmV--28j7VRzV00000000AxmrHCLRTPG2nwKAOY$t9?pQLG<@+i;;;a#D`Bx%4n#ZH<& z7G-ST*tZ$aSTlFx8i7PnL8Vd=8zfdpMXiK{1dA?^5ULO?3d^o4#G)$d3Km785@J!6 zFXePIziglTk_j$!R@8aU(ySV{4Xt9AOktn9TqtNl zJF#g;m(r)y|EU2rauWW38}u&d3g}nRJRN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qnN|v5YG$q|_p=v&w<|0*qMBsqO$N%ZTm#|Eo?e1ZDpL LJ)f2wq7wiB3OKd{ diff --git a/src/generator/__tests__/artifact.test.ts b/src/generator/__tests__/artifact.test.ts index 1365b1afd..bc9249db4 100644 --- a/src/generator/__tests__/artifact.test.ts +++ b/src/generator/__tests__/artifact.test.ts @@ -4,7 +4,7 @@ import { loadConfig } from '../../config' import { Artifact } from '../artifact' import { getEngine } from '../template' -const resolve = (p) => join(__dirname, '../../../test/fixture/', p) +const resolve = (p: string) => join(__dirname, '../../../test/fixture/', p) test('new Artifact()', async (t) => { const fixture = resolve('plain') diff --git a/src/provider/__tests__/ClashProvider.test.ts b/src/provider/__tests__/ClashProvider.test.ts index b294b4414..067d0a449 100644 --- a/src/provider/__tests__/ClashProvider.test.ts +++ b/src/provider/__tests__/ClashProvider.test.ts @@ -655,7 +655,7 @@ test.serial( test('ClashProvider with hooks', async (t) => { const afterNodeListResponse = sinon.spy((nodeList) => { - nodeList.forEach((node) => { + nodeList.forEach((node: any) => { node.nodeName = 'override' }) }) diff --git a/src/utils/__tests__/__snapshots__/remote-snippet.test.ts.snap b/src/utils/__tests__/__snapshots__/remote-snippet.test.ts.snap index 47571d329aaf9edeb4b3fa516f9094066c39eb8b..eadcce454d0a331523d17295fed7e4a09e571ef2 100644 GIT binary patch delta 487 zcmVHaAFr^6|cG#o1-D<4#7xNgw0VskFVMqY9oyJwn8&76M$8hiOCX-xcdUgr6v zu5=X1hwj6p7=<`3>-zpzfBEun|9t<4e-w&15MM&_lu}Khi!D|QOg!aflHWtI3wch# zqhyFmp=frA6WcFGlWb6z3rMu?#?Dg(a#@<_9H+$tgbyf=xs0Y>;}Yvr3xSoRu}Jd7 zIH8xSJK>^}vi^U-d}C9nIf!nk9Y~<>`(b3d?sYxYjry?{c(32E>xDl4uZv^%SJ(7~ zHMNPPg!(GTT6EPUYXGnH;O5I`mg{Q%ZoW9aW!+r-u3#7i$dvrk>BXzX>F;F^rix})sv(LBqZHU*tE8q)>hNn dYFb+wLetu6T3bzP>;Kr=`VWra-xF;$004A${saI3 delta 487 zcmVSCoIjZOJRa-;N#KPGKwyF5dd)x7P zOp}R)HV~+KKRF=xQxnmNa25^5sO^+bkdxiB!liWgOjD_uGeCc1FaIQsiJ#8PJfGB+ zjw1QceRve35T|8b-~Z|_U;gc%@Bi?RLJrw0MB<0mU(w(bQ{PVtr~MuyQmONuC%d z^ip*vTy#>_ADDk{Yzj38(G9f&3G{tGj7-G;I$syeEH0BUCrOk7st1(n~UES45I*j@`AkK51H8O>65bmR&ThE!C0Uw6>F~2O=jmt*yHWo7Ps-+G<)` dO>0X-Xj)rMYpZE({U2Lf{{fER-xF;$004;y>U{tJ diff --git a/src/utils/__tests__/surge.test.ts b/src/utils/__tests__/surge.test.ts index 26dc1e23f..51cc4bdef 100644 --- a/src/utils/__tests__/surge.test.ts +++ b/src/utils/__tests__/surge.test.ts @@ -536,7 +536,7 @@ test('getSurgeNodes - Tuic', (t) => { '测试 Tuic = tuic, example.com, 443, token=token, server-cert-fingerprint-sha256=sha256', '测试 Tuic = tuic, example.com, 443, token=token, alpn=h3', '测试 Tuic = tuic, example.com, 443, token=token, sni=sni.example.com, skip-cert-verify=true, alpn=h3', - '测试 Tuic = tuic, example.com, 443, password=password, uuid=uuid, sni=sni.example.com, skip-cert-verify=true, version=5, alpn=h3', + '测试 Tuic = tuic-v5, example.com, 443, password=password, uuid=uuid, sni=sni.example.com, skip-cert-verify=true, alpn=h3', ].join('\n'), ) }) diff --git a/src/utils/surge.ts b/src/utils/surge.ts index 567014ff4..0bd5ddc3d 100644 --- a/src/utils/surge.ts +++ b/src/utils/surge.ts @@ -475,6 +475,37 @@ function nodeListMapper( } case NodeTypeEnum.Tuic: { + if ('version' in nodeConfig && Number(nodeConfig.version) === 5) { + const result = [ + 'tuic-v5', + nodeConfig.hostname, + nodeConfig.port, + ...pickAndFormatStringList( + nodeConfig, + [ + 'password', + 'uuid', + 'sni', + 'underlyingProxy', + 'testUrl', + 'skipCertVerify', + 'serverCertFingerprintSha256', + ], + { + keyFormat: 'kebabCase', + }, + ), + ...(Array.isArray(nodeConfig.alpn) + ? [`alpn=${nodeConfig.alpn.join(',')}`] + : []), + ] + + return [ + nodeConfig.nodeName, + [nodeConfig.nodeName, result.join(', ')].join(' = '), + ] + } + const result = [ 'tuic', nodeConfig.hostname, @@ -483,14 +514,11 @@ function nodeListMapper( nodeConfig, [ 'token', - 'password', - 'uuid', 'sni', 'underlyingProxy', 'testUrl', 'skipCertVerify', 'serverCertFingerprintSha256', - 'version', ], { keyFormat: 'kebabCase', diff --git a/test/cli.cli-test.ts b/test/cli.cli-test.ts index ba06137ca..cbfdec76f 100644 --- a/test/cli.cli-test.ts +++ b/test/cli.cli-test.ts @@ -5,7 +5,7 @@ import ini from 'ini' import { test, expect } from '@oclif/test' const fixture = join(__dirname, './fixture') -const resolve = (p) => join(fixture, p) +const resolve = (p: string) => join(fixture, p) afterEach(async () => { process.env.ENV_SURGIO_PROJECT_DIR = undefined diff --git a/tsconfig.json b/tsconfig.json index 19ddac9f9..8089d8733 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "rootDir": "./src", // https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping "target": "ES2022", "module": "Node16",