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..8a9fa0f6 100644 --- a/service/server.ts +++ b/service/server.ts @@ -1,47 +1,31 @@ -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 { 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) as Body - - 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}`) 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"] + } +}