better link generator

This commit is contained in:
Alireza Ahmadi
2024-07-23 16:24:26 +02:00
parent 39022c1b2d
commit eef7e200ba
4 changed files with 56 additions and 59 deletions
+18 -17
View File
@@ -1,6 +1,7 @@
import { Hysteria, Hysteria2, InTypes, Inbound, Naive, Shadowsocks, TUIC, Trojan, VLESS, VMess } from "@/types/inbounds" 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 { HTTP, WebSocket, gRPC, HTTPUpgrade, Transport, TrspTypes } from "@/types/transport"
import RandomUtil from "./randomUtil" import RandomUtil from "./randomUtil"
import { Client } from "@/types/clients"
export interface Link { export interface Link {
type: "local" | "external" | "sub" type: "local" | "external" | "sub"
@@ -14,7 +15,7 @@ function utf8ToBase64(utf8String: string): string {
} }
export namespace LinkUtil { 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){ switch(inbound.type){
case InTypes.Shadowsocks: case InTypes.Shadowsocks:
return shadowsocksLink(user,<Shadowsocks>inbound, addrs) return shadowsocksLink(user,<Shadowsocks>inbound, addrs)
@@ -36,8 +37,8 @@ export namespace LinkUtil {
return [] return []
} }
function shadowsocksLink(user: string, inbound: Shadowsocks, addrs: any[]): string[] { function shadowsocksLink(user: Client, inbound: Shadowsocks, addrs: any[]): string[] {
const userPass = inbound.users?.find(i => i.name == user)?.password const userPass = user.config.shadowsocks?.password
const password = [userPass] const password = [userPass]
if (inbound.method.startsWith('2022')) password.push(inbound.password) if (inbound.method.startsWith('2022')) password.push(inbound.password)
const params = { const params = {
@@ -70,8 +71,8 @@ export namespace LinkUtil {
return links return links
} }
function hysteriaLink(user: string, inbound: Hysteria, addrs: any[], tlsClient: any): string[] { function hysteriaLink(user: Client, inbound: Hysteria, addrs: any[], tlsClient: any): string[] {
const auth = inbound.users.find(i => i.name == user)?.auth_str const auth = user.config.hysteria.auth_str
const params = { const params = {
upmbps: inbound.up_mbps?? null, upmbps: inbound.up_mbps?? null,
downmbps: inbound.down_mbps?? null, downmbps: inbound.down_mbps?? null,
@@ -118,8 +119,8 @@ export namespace LinkUtil {
return links return links
} }
function hysteria2Link(user: string, inbound: Hysteria2, addrs: any[], tlsClient: any): string[] { function hysteria2Link(user: Client, inbound: Hysteria2, addrs: any[], tlsClient: any): string[] {
const password = inbound.users.find(i => i.name == user)?.password const password = user.config.hysteria2.password
const params = { const params = {
upmbps: inbound.up_mbps?? null, upmbps: inbound.up_mbps?? null,
downmbps: inbound.down_mbps?? null, downmbps: inbound.down_mbps?? null,
@@ -166,8 +167,8 @@ export namespace LinkUtil {
return links return links
} }
function naiveLink(user: string, inbound: Naive, addrs: any[], tlsClient: any): string[] { function naiveLink(user: Client, inbound: Naive, addrs: any[], tlsClient: any): string[] {
const password = inbound.users.find(i => i.username == user)?.password const password = user.config.naive.password
let links = <string[]>[] let links = <string[]>[]
if (addrs.length == 0) { if (addrs.length == 0) {
@@ -208,8 +209,8 @@ export namespace LinkUtil {
return links return links
} }
function tuicLink(user: string, inbound: TUIC, addrs: any[], tlsClient: any): string[] { function tuicLink(user: Client, inbound: TUIC, addrs: any[], tlsClient: any): string[] {
const u = inbound.users.find(i => i.name == user) const u = user.config.tuic
const params = { const params = {
sni: inbound.tls.server_name?? null, sni: inbound.tls.server_name?? null,
alpn: inbound.tls.alpn?.join(',')?? null, alpn: inbound.tls.alpn?.join(',')?? null,
@@ -286,8 +287,8 @@ export namespace LinkUtil {
return params return params
} }
function vlessLink(user: string, inbound: VLESS, addrs: any[], tlsClient: any): string[] { function vlessLink(user: Client, inbound: VLESS, addrs: any[], tlsClient: any): string[] {
const u = inbound.users.find(i => i.name == user) const u = user.config.vless
const transport = <Transport>inbound.transport const transport = <Transport>inbound.transport
const tParams = getTransportParams(transport) const tParams = getTransportParams(transport)
@@ -348,8 +349,8 @@ export namespace LinkUtil {
return links return links
} }
function trojanLink(user: string, inbound: Trojan, addrs: any[], tlsClient: any): string[] { function trojanLink(user: Client, inbound: Trojan, addrs: any[], tlsClient: any): string[] {
const u = inbound.users.find(i => i.name == user) const u = user.config.trojan
const transport = <Transport>inbound.transport const transport = <Transport>inbound.transport
const tParams = getTransportParams(transport) const tParams = getTransportParams(transport)
@@ -410,8 +411,8 @@ export namespace LinkUtil {
return links return links
} }
function vmessLink(user: string, inbound: VMess, addrs: any[], tlsClient: any): string[] { function vmessLink(user: Client, inbound: VMess, addrs: any[], tlsClient: any): string[] {
const u = inbound.users.find(i => i.name == user) const u = user.config.vmess
const transport = <Transport>inbound.transport const transport = <Transport>inbound.transport
const tParams = getTransportParams(transport) const tParams = getTransportParams(transport)
+1 -1
View File
@@ -299,7 +299,7 @@ const updateLinks = (c:Client):Link[] => {
const tlsConfig = <any>Data().tlsConfigs?.findLast((t:any) => t.inbounds.includes(i.tag)) const tlsConfig = <any>Data().tlsConfigs?.findLast((t:any) => t.inbounds.includes(i.tag))
const cData = <any>Data().inData?.findLast((d:any) => d.tag == i.tag) const cData = <any>Data().inData?.findLast((d:any) => d.tag == i.tag)
const addrs = cData ? <any[]>cData.addrs : [] const addrs = cData ? <any[]>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){ if (uris.length>0){
uris.forEach(uri => { uris.forEach(uri => {
newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri }) newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri })
+7 -9
View File
@@ -254,28 +254,26 @@ const saveModal = (data:Inbound, stats: boolean, tls_id: number, cData: any) =>
modal.value.visible = false modal.value.visible = false
} }
const updateLinks = (i: any) => { const updateLinks = (i: any) => {
if(i.users && i.users.length>0){ if(i.users){
i.users.forEach((u:any) => { const uClients = clients.value.filter(c => c.inbounds.includes(i.tag))
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) uClients.forEach((u:Client) => {
if (client){ const clientInbounds = <Inbound[]>inbounds.value.filter(inb => u.inbounds.includes(inb.tag))
const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.includes(inb.tag))
const newLinks = <Link[]>[] const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{ clientInbounds.forEach(i =>{
const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? {} const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? {}
const cData = <any>Data().inData?.findLast((d:any) => d.tag == i.tag) const cData = <any>Data().inData?.findLast((d:any) => d.tag == i.tag)
const addrs = cData ? <any[]>cData.addrs : [] const addrs = cData ? <any[]>cData.addrs : []
const uris = LinkUtil.linkGenerator(client.name,i, tlsClient, addrs) const uris = LinkUtil.linkGenerator(u,i, tlsClient, addrs)
if (uris.length>0){ if (uris.length>0){
uris.forEach(uri => { uris.forEach(uri => {
newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri }) newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri })
}) })
} }
}) })
let links = client.links && client.links.length>0? client.links : <Link[]>[] let links = u.links && u.links.length>0? u.links : <Link[]>[]
links = [...newLinks, ...links.filter(l => l.type != 'local')] links = [...newLinks, ...links.filter(l => l.type != 'local')]
client.links = links u.links = links
}
}) })
} }
} }
+3 -5
View File
@@ -150,10 +150,9 @@ const delTls = (index: number) => {
} }
const updateLinks = (i:any,tlsClient:any) => { const updateLinks = (i:any,tlsClient:any) => {
if(i.users && i.users.length>0){ if(i.users){
i.users.forEach((u:any) => { const uClients = clients.value.filter(c => c.inbounds.includes(i.tag))
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) uClients.forEach((client:any) => {
if (client){
const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.includes(inb.tag)) const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.includes(inb.tag))
const newLinks = <Link[]>[] const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{ clientInbounds.forEach(i =>{
@@ -170,7 +169,6 @@ const updateLinks = (i:any,tlsClient:any) => {
links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')] links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')]
client.links = links client.links = links
}
}) })
} }
} }