From 6adc607e995024dca0e657978fb4c8e992d37d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=BA=B8=E5=BF=98=E5=BF=A7?= Date: Sun, 16 Jul 2023 18:39:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AE=20bun=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +++- pnpm-lock.yaml | 7 +++++++ service/server.ts | 11 ++--------- service/tsconfig.json | 5 +++++ 4 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 service/tsconfig.json diff --git a/package.json b/package.json index 7534282b..558f4724 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "author": "一纸忘忧 ", "type": "module", "scripts": { + "build": "vue-tsc && vite build", "dev": "vite", - "build": "vue-tsc && vite build" + "start": "bun --watch service/server.ts" }, "dependencies": { "@arco-design/web-vue": "^2.48.1", @@ -31,6 +32,7 @@ "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "@vitejs/plugin-vue": "^4.2.3", + "bun-types": "^0.6.14", "eslint": "^8.44.0", "eslint-config-prettier": "^8.8.0", "eslint-plugin-vue": "^9.15.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 07d4a84e..ff2a3a70 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -66,6 +66,9 @@ importers: '@vitejs/plugin-vue': specifier: ^4.2.3 version: 4.2.3(vite@4.4.4)(vue@3.3.4) + bun-types: + specifier: ^0.6.14 + version: 0.6.14 eslint: specifier: ^8.44.0 version: 8.44.0 @@ -1658,6 +1661,10 @@ packages: resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} dev: true + /bun-types@0.6.14: + resolution: {integrity: sha512-sRdvu+t59+H/TVOe7FSGFWYITbqkhiCx9NxVUHt2+JOXM9gUOe5uMPvVvcr/hGngnh+/yb5a7uPE4JaS6uxujg==} + dev: true + /bundle-name@3.0.0: resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==} engines: {node: '>=12'} diff --git a/service/server.ts b/service/server.ts index 0bc6e76b..d1328f63 100644 --- a/service/server.ts +++ b/service/server.ts @@ -2,14 +2,7 @@ import { createServer } from 'http' import { platform } from 'os' import { execFile } from 'child_process' -type Body = { - port: string - host: string - app: string - protocol: '4' | '5' | '6' -} - -const getResult = (body: Body) => { +const getResult = body => { const { port, host, app, protocol } = body return new Promise(resolve => { @@ -35,7 +28,7 @@ const getResult = (body: Body) => { const server = createServer(async (req, res) => { const url = new URL(req.url, `http://${req.headers.host}`) - const body = Object.fromEntries(url.searchParams) as Body + const body = Object.fromEntries(url.searchParams) if (url.pathname === '/check') { res.end(JSON.stringify(await getResult(body))) diff --git a/service/tsconfig.json b/service/tsconfig.json new file mode 100644 index 00000000..b4ecf7e0 --- /dev/null +++ b/service/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "types": ["bun-types"] + } +} From 47f8a6dbf781fec9d113cbf7ab0c3b77f43e6b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=BA=B8=E5=BF=98=E5=BF=A7?= Date: Sun, 16 Jul 2023 22:25:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20kms=20?= =?UTF-8?q?=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/server.ts | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/service/server.ts b/service/server.ts index d1328f63..8a9fa0f6 100644 --- a/service/server.ts +++ b/service/server.ts @@ -1,40 +1,31 @@ -import { createServer } from 'http' import { platform } from 'os' import { execFile } from 'child_process' -const getResult = body => { - const { port, host, app, protocol } = body - +const fetchData = params => { return new Promise(resolve => { + const { host, port, app, protocol } = params execFile( - `./vlmcs/vlmcs-${platform()}`, - [ - `-${['4', '5', '6'].includes(protocol) ? protocol : '6'}`, - `${host}:${port}`, - `${app === '' ? '' : '-l ' + app}` - ], - { timeout: 5000 }, - (err, std) => { - if (err) { - resolve({ msg: 'error', result: std.toString() }) - } else { - resolve({ msg: 'success', result: std.toString() }) - } + `./service/vlmcs/vlmcs-${platform()}`, + [`-l ${app}`, `-${protocol}`, `${host}:${port}`], + { timeout: 10 * 1000 }, + function (error, stdout) { + resolve({ result: error ? 'error' : 'success', stdout: stdout.toString() }) } ) }) } -const server = createServer(async (req, res) => { - const url = new URL(req.url, `http://${req.headers.host}`) - - const body = Object.fromEntries(url.searchParams) - - if (url.pathname === '/check') { - res.end(JSON.stringify(await getResult(body))) - } else { - res.end('KMS Tools') +const server = Bun.serve({ + async fetch(req) { + const url = new URL(req.url) + if (url.pathname === '/api/kms-check') { + const params = Object.fromEntries(url.searchParams) + const result = await fetchData(params) + return new Response(JSON.stringify(result)) + } else { + return new Response(`404!`) + } } }) -server.listen(3000, () => console.log('Server running at http://localhost:3000/')) +console.log(`Server running at http://localhost:${server.port}`)