From eef7e200badb05bb5dedafb8bcd5f3ce602ef19b Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Tue, 23 Jul 2024 16:24:26 +0200 Subject: [PATCH] better link generator --- frontend/src/plugins/link.ts | 35 +++++++++++++++-------------- frontend/src/views/Clients.vue | 2 +- frontend/src/views/Inbounds.vue | 40 ++++++++++++++++----------------- frontend/src/views/Tls.vue | 38 +++++++++++++++---------------- 4 files changed, 56 insertions(+), 59 deletions(-) diff --git a/frontend/src/plugins/link.ts b/frontend/src/plugins/link.ts index e79608b..1649313 100644 --- a/frontend/src/plugins/link.ts +++ b/frontend/src/plugins/link.ts @@ -1,6 +1,7 @@ import { Hysteria, Hysteria2, InTypes, Inbound, Naive, Shadowsocks, TUIC, Trojan, VLESS, VMess } from "@/types/inbounds" import { HTTP, WebSocket, gRPC, HTTPUpgrade, Transport, TrspTypes } from "@/types/transport" import RandomUtil from "./randomUtil" +import { Client } from "@/types/clients" export interface Link { type: "local" | "external" | "sub" @@ -14,7 +15,7 @@ function utf8ToBase64(utf8String: string): string { } export namespace LinkUtil { - export function linkGenerator(user: string, inbound: Inbound, tlsClient: any = {}, addrs: any[] = []): string[] { + export function linkGenerator(user: Client, inbound: Inbound, tlsClient: any = {}, addrs: any[] = []): string[] { switch(inbound.type){ case InTypes.Shadowsocks: return shadowsocksLink(user,inbound, addrs) @@ -36,8 +37,8 @@ export namespace LinkUtil { return [] } - function shadowsocksLink(user: string, inbound: Shadowsocks, addrs: any[]): string[] { - const userPass = inbound.users?.find(i => i.name == user)?.password + function shadowsocksLink(user: Client, inbound: Shadowsocks, addrs: any[]): string[] { + const userPass = user.config.shadowsocks?.password const password = [userPass] if (inbound.method.startsWith('2022')) password.push(inbound.password) const params = { @@ -70,8 +71,8 @@ export namespace LinkUtil { return links } - function hysteriaLink(user: string, inbound: Hysteria, addrs: any[], tlsClient: any): string[] { - const auth = inbound.users.find(i => i.name == user)?.auth_str + function hysteriaLink(user: Client, inbound: Hysteria, addrs: any[], tlsClient: any): string[] { + const auth = user.config.hysteria.auth_str const params = { upmbps: inbound.up_mbps?? null, downmbps: inbound.down_mbps?? null, @@ -118,8 +119,8 @@ export namespace LinkUtil { return links } - function hysteria2Link(user: string, inbound: Hysteria2, addrs: any[], tlsClient: any): string[] { - const password = inbound.users.find(i => i.name == user)?.password + function hysteria2Link(user: Client, inbound: Hysteria2, addrs: any[], tlsClient: any): string[] { + const password = user.config.hysteria2.password const params = { upmbps: inbound.up_mbps?? null, downmbps: inbound.down_mbps?? null, @@ -166,8 +167,8 @@ export namespace LinkUtil { return links } - function naiveLink(user: string, inbound: Naive, addrs: any[], tlsClient: any): string[] { - const password = inbound.users.find(i => i.username == user)?.password + function naiveLink(user: Client, inbound: Naive, addrs: any[], tlsClient: any): string[] { + const password = user.config.naive.password let links = [] if (addrs.length == 0) { @@ -208,8 +209,8 @@ export namespace LinkUtil { return links } - function tuicLink(user: string, inbound: TUIC, addrs: any[], tlsClient: any): string[] { - const u = inbound.users.find(i => i.name == user) + function tuicLink(user: Client, inbound: TUIC, addrs: any[], tlsClient: any): string[] { + const u = user.config.tuic const params = { sni: inbound.tls.server_name?? null, alpn: inbound.tls.alpn?.join(',')?? null, @@ -286,8 +287,8 @@ export namespace LinkUtil { return params } - function vlessLink(user: string, inbound: VLESS, addrs: any[], tlsClient: any): string[] { - const u = inbound.users.find(i => i.name == user) + function vlessLink(user: Client, inbound: VLESS, addrs: any[], tlsClient: any): string[] { + const u = user.config.vless const transport = inbound.transport const tParams = getTransportParams(transport) @@ -348,8 +349,8 @@ export namespace LinkUtil { return links } - function trojanLink(user: string, inbound: Trojan, addrs: any[], tlsClient: any): string[] { - const u = inbound.users.find(i => i.name == user) + function trojanLink(user: Client, inbound: Trojan, addrs: any[], tlsClient: any): string[] { + const u = user.config.trojan const transport = inbound.transport const tParams = getTransportParams(transport) @@ -410,8 +411,8 @@ export namespace LinkUtil { return links } - function vmessLink(user: string, inbound: VMess, addrs: any[], tlsClient: any): string[] { - const u = inbound.users.find(i => i.name == user) + function vmessLink(user: Client, inbound: VMess, addrs: any[], tlsClient: any): string[] { + const u = user.config.vmess const transport = inbound.transport const tParams = getTransportParams(transport) diff --git a/frontend/src/views/Clients.vue b/frontend/src/views/Clients.vue index 0d2bea8..089f652 100644 --- a/frontend/src/views/Clients.vue +++ b/frontend/src/views/Clients.vue @@ -299,7 +299,7 @@ const updateLinks = (c:Client):Link[] => { const tlsConfig = Data().tlsConfigs?.findLast((t:any) => t.inbounds.includes(i.tag)) const cData = Data().inData?.findLast((d:any) => d.tag == i.tag) const addrs = cData ? cData.addrs : [] - const uris = LinkUtil.linkGenerator(c.name,i, tlsConfig?.client?? {}, addrs) + const uris = LinkUtil.linkGenerator(c,i, tlsConfig?.client?? {}, addrs) if (uris.length>0){ uris.forEach(uri => { newLinks.push({ type: 'local', remark: i.tag, uri: uri }) diff --git a/frontend/src/views/Inbounds.vue b/frontend/src/views/Inbounds.vue index 26fa971..83d3774 100644 --- a/frontend/src/views/Inbounds.vue +++ b/frontend/src/views/Inbounds.vue @@ -254,28 +254,26 @@ const saveModal = (data:Inbound, stats: boolean, tls_id: number, cData: any) => modal.value.visible = false } const updateLinks = (i: any) => { - if(i.users && i.users.length>0){ - i.users.forEach((u:any) => { - const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) - if (client){ - const clientInbounds = inbounds.value.filter(inb => client?.inbounds.includes(inb.tag)) - const newLinks = [] - clientInbounds.forEach(i =>{ - const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? {} - const cData = Data().inData?.findLast((d:any) => d.tag == i.tag) - const addrs = cData ? cData.addrs : [] - const uris = LinkUtil.linkGenerator(client.name,i, tlsClient, addrs) - if (uris.length>0){ - uris.forEach(uri => { - newLinks.push({ type: 'local', remark: i.tag, uri: uri }) - }) - } - }) - let links = client.links && client.links.length>0? client.links : [] - links = [...newLinks, ...links.filter(l => l.type != 'local')] + if(i.users){ + const uClients = clients.value.filter(c => c.inbounds.includes(i.tag)) + uClients.forEach((u:Client) => { + const clientInbounds = inbounds.value.filter(inb => u.inbounds.includes(inb.tag)) + const newLinks = [] + clientInbounds.forEach(i =>{ + const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? {} + const cData = Data().inData?.findLast((d:any) => d.tag == i.tag) + const addrs = cData ? cData.addrs : [] + const uris = LinkUtil.linkGenerator(u,i, tlsClient, addrs) + if (uris.length>0){ + uris.forEach(uri => { + newLinks.push({ type: 'local', remark: i.tag, uri: uri }) + }) + } + }) + let links = u.links && u.links.length>0? u.links : [] + links = [...newLinks, ...links.filter(l => l.type != 'local')] - client.links = links - } + u.links = links }) } } diff --git a/frontend/src/views/Tls.vue b/frontend/src/views/Tls.vue index eeaa1e5..73f2de8 100644 --- a/frontend/src/views/Tls.vue +++ b/frontend/src/views/Tls.vue @@ -150,27 +150,25 @@ const delTls = (index: number) => { } const updateLinks = (i:any,tlsClient:any) => { - if(i.users && i.users.length>0){ - i.users.forEach((u:any) => { - const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) - if (client){ - const clientInbounds = inbounds.value.filter(inb => client?.inbounds.includes(inb.tag)) - const newLinks = [] - clientInbounds.forEach(i =>{ - const cData = Data().inData?.findLast((d:any) => d.tag == i.tag) - const addrs = cData ? cData.addrs : [] - const uris = LinkUtil.linkGenerator(client,i, tlsClient, addrs) - if (uris.length>0){ - uris.forEach(uri => { - newLinks.push({ type: 'local', remark: i.tag, uri: uri }) - }) - } - }) - let links = client.links && client.links.length>0? client.links : [] - links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')] + if(i.users){ + const uClients = clients.value.filter(c => c.inbounds.includes(i.tag)) + uClients.forEach((client:any) => { + const clientInbounds = inbounds.value.filter(inb => client?.inbounds.includes(inb.tag)) + const newLinks = [] + clientInbounds.forEach(i =>{ + const cData = Data().inData?.findLast((d:any) => d.tag == i.tag) + const addrs = cData ? cData.addrs : [] + const uris = LinkUtil.linkGenerator(client,i, tlsClient, addrs) + if (uris.length>0){ + uris.forEach(uri => { + newLinks.push({ type: 'local', remark: i.tag, uri: uri }) + }) + } + }) + let links = client.links && client.links.length>0? client.links : [] + links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')] - client.links = links - } + client.links = links }) } }