Skip to content

Commit

Permalink
Merge pull request #19 from ikxin/feature
Browse files Browse the repository at this point in the history
refactor: 使用 bun 重构 kms 检测模块
  • Loading branch information
ikxin committed Jul 16, 2023
2 parents 6ae7c1b + 47f8a6d commit 27dd580
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"author": "一纸忘忧 <i@ikxin.com>",
"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",
Expand All @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 18 additions & 34 deletions service/server.ts
Original file line number Diff line number Diff line change
@@ -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}`)
5 changes: 5 additions & 0 deletions service/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"types": ["bun-types"]
}
}

1 comment on commit 27dd580

@vercel
Copy link

@vercel vercel bot commented on 27dd580 Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.