diff --git a/src/actions/channel.ts b/src/actions/channel.ts index 3771278..cbe0611 100644 --- a/src/actions/channel.ts +++ b/src/actions/channel.ts @@ -37,6 +37,19 @@ export async function createChannels(params: { return callPublic('/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('/api/channel/create/v2', params) +} + export async function createChannelsV3(params: { resource_no: string protocol: number diff --git a/src/app/(api)/proxies/route.ts b/src/app/(api)/proxies/route.ts index 8e3336d..61eda1e 100644 --- a/src/app/(api)/proxies/route.ts +++ b/src/app/(api)/proxies/route.ts @@ -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({ - 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, - }) - } + const result = await createChannelsV3({ + resource_no: resourceParam, + auth_type: Number(auth_type), + protocol: Number(protocol), + count: Number(count), + area_id: Number(area_id), + isp: Number(isp), + host_format: hostFormat === 'domain' ? 1 : 2, + }) if (!result.success) { throw new Error(result.message) } diff --git a/src/app/(api)/proxies/v2/route.ts b/src/app/(api)/proxies/v2/route.ts new file mode 100644 index 0000000..e423912 --- /dev/null +++ b/src/app/(api)/proxies/v2/route.ts @@ -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}) + } +}