完善通道创建功能,添加参数验证与格式化输出

This commit is contained in:
2025-04-14 16:00:46 +08:00
parent e928b5a270
commit 4315c8eba9
5 changed files with 233 additions and 36 deletions

View File

@@ -0,0 +1,68 @@
import {NextRequest, NextResponse} from 'next/server'
import {createChannels} from '@/actions/channel'
export async function GET(req: NextRequest) {
try {
const params = req.nextUrl.searchParams
const resource_id = params.get('i')
if (!resource_id) {
throw new Error('需要指定资源ID')
}
let protocol = params.get('x')
if (!protocol) {
protocol = '1'
}
const auth_type = params.get('t')
if (!auth_type) {
throw new Error('需要指定认证类型')
}
const count = params.get('n')
if (!count) {
throw new Error('需要指定通道创建数量')
}
const prov = params.get('a') || undefined
const city = params.get('b') || undefined
const isp = params.get('s') || undefined
const result = await createChannels({
resource_id: Number(resource_id),
auth_type: Number(auth_type),
protocol: Number(protocol),
count: Number(count),
prov,
city,
isp,
})
if (!result.success) {
throw new Error(result.message)
}
const format = params.get('rt')
const rBreaker = params.get('rb') || '13,10'
const rSeparator = params.get('rs') || '124'
const breaker = rBreaker.split(',').map(code => String.fromCharCode(parseInt(code))).join('')
const separator = rSeparator.split(',').map(code => String.fromCharCode(parseInt(code))).join('')
switch (format) {
case 'json':
const body = JSON.stringify(params)
return NextResponse.json(body)
case 'text':
const text = result.data.map(item => {
const list = [item.host, item.port]
if (item.username && item.password) {
list.push(item.username)
list.push(item.password)
}
return list.join(separator)
}).join(breaker)
return new NextResponse(text)
}
}
catch (error) {
console.error('Error creating channels:', error)
return NextResponse.json({error: error})
}
}