新增 v2 提取接口
This commit is contained in:
@@ -37,6 +37,19 @@ export async function createChannels(params: {
|
||||
return callPublic<CreateChannelsResp[]>('/api/channel/create', params)
|
||||
}
|
||||
|
||||
export async function createChannelsV2(params: {
|
||||
resource_no: string
|
||||
protocol: number
|
||||
auth_type: number
|
||||
count: number
|
||||
prov?: string
|
||||
city?: string
|
||||
isp?: number
|
||||
host_format?: number
|
||||
}) {
|
||||
return callPublic<CreateChannelsResp[]>('/api/channel/create/v2', params)
|
||||
}
|
||||
|
||||
export async function createChannelsV3(params: {
|
||||
resource_no: string
|
||||
protocol: number
|
||||
|
||||
@@ -22,50 +22,19 @@ export async function GET(req: NextRequest) {
|
||||
if (!count) {
|
||||
throw new Error('需要指定通道创建数量')
|
||||
}
|
||||
// const prov = params.get('a') || undefined
|
||||
const area_id = params.get('b') || undefined
|
||||
const isp = params.get('s') || undefined
|
||||
const hostFormat = params.get('rh') || 'domain'
|
||||
const isNumeric = /^\d+$/.test(resourceParam)
|
||||
|
||||
let result
|
||||
if (!isNumeric) {
|
||||
console.log(area_id, 'area_id', params.get('b'), 'params.get')
|
||||
|
||||
result = await createChannelsV3({
|
||||
const result = await createChannelsV3({
|
||||
resource_no: resourceParam,
|
||||
auth_type: Number(auth_type),
|
||||
protocol: Number(protocol),
|
||||
count: Number(count),
|
||||
// prov,
|
||||
area_id: Number(area_id),
|
||||
isp: Number(isp),
|
||||
host_format: hostFormat === 'domain' ? 1 : 2,
|
||||
})
|
||||
console.log({
|
||||
resource_no: resourceParam,
|
||||
auth_type: Number(auth_type),
|
||||
protocol: Number(protocol),
|
||||
count: Number(count),
|
||||
// prov,
|
||||
area_id: Number(area_id),
|
||||
isp: Number(isp),
|
||||
host_format: hostFormat === 'domain' ? 1 : 2,
|
||||
})
|
||||
}
|
||||
else {
|
||||
result = await createChannels({
|
||||
resource_id: Number(resourceParam),
|
||||
auth_type: Number(auth_type),
|
||||
protocol: Number(protocol),
|
||||
count: Number(count),
|
||||
// prov,
|
||||
area_id: Number(area_id),
|
||||
isp: Number(isp),
|
||||
host_format: hostFormat === 'domain' ? 1 : 2,
|
||||
})
|
||||
}
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message)
|
||||
}
|
||||
|
||||
93
src/app/(api)/proxies/v2/route.ts
Normal file
93
src/app/(api)/proxies/v2/route.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import {NextRequest, NextResponse} from 'next/server'
|
||||
import {createChannels, createChannelsV2, createChannelsV3} from '@/actions/channel'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const params = req.nextUrl.searchParams
|
||||
|
||||
try {
|
||||
const resourceParam = params.get('i')
|
||||
|
||||
if (!resourceParam) {
|
||||
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 hostFormat = params.get('rh') || 'domain'
|
||||
|
||||
const result = await createChannelsV2({
|
||||
resource_no: resourceParam,
|
||||
auth_type: Number(auth_type),
|
||||
protocol: Number(protocol),
|
||||
count: Number(count),
|
||||
prov,
|
||||
city,
|
||||
isp: Number(isp),
|
||||
host_format: hostFormat === 'domain' ? 1 : 2,
|
||||
})
|
||||
|
||||
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':
|
||||
if (hostFormat === 'domain') {
|
||||
const domainFormatData = result.data.map(item => ({
|
||||
host: item.host,
|
||||
port: item.port,
|
||||
...(item.username && item.password ? {username: item.username, password: item.password} : {}),
|
||||
}))
|
||||
return NextResponse.json(domainFormatData)
|
||||
}
|
||||
else {
|
||||
const ipFormatData = result.data.map(item => ({
|
||||
ip: item.ip,
|
||||
port: item.port,
|
||||
...(item.username && item.password ? {username: item.username, password: item.password} : {}),
|
||||
}))
|
||||
return NextResponse.json(ipFormatData)
|
||||
}
|
||||
case 'text':
|
||||
const text = result.data.map((item) => {
|
||||
let hostValue: string
|
||||
if (hostFormat === 'domain') {
|
||||
hostValue = item.host
|
||||
}
|
||||
else {
|
||||
hostValue = item.ip
|
||||
}
|
||||
const list = [hostValue, String(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 as Error).message})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user