From ecee4e448268c58aea552f521c3166be03c5c236 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 22 May 2023 23:29:25 +0200 Subject: [PATCH] [feature] fallback link calculation #323 --- sub/subService.go | 28 ++++++++++++++++++++++++++++ web/html/xui/inbounds.html | 28 +++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/sub/subService.go b/sub/subService.go index 69000860..9e6ad158 100644 --- a/sub/subService.go +++ b/sub/subService.go @@ -38,6 +38,21 @@ func (s *SubService) GetSubs(subId string, host string) ([]string, []string, err if clients == nil { continue } + if len(inbound.Listen) > 0 && inbound.Listen[0] == '@' { + fallbackMaster, err := s.getFallbackMaster(inbound.Listen) + if err == nil { + inbound.Listen = fallbackMaster.Listen + inbound.Port = fallbackMaster.Port + var stream map[string]interface{} + json.Unmarshal([]byte(inbound.StreamSettings), &stream) + var masterStream map[string]interface{} + json.Unmarshal([]byte(fallbackMaster.StreamSettings), &masterStream) + stream["security"] = masterStream["security"] + stream["tlsSettings"] = masterStream["tlsSettings"] + modifiedStream, _ := json.MarshalIndent(stream, "", " ") + inbound.StreamSettings = string(modifiedStream) + } + } for _, client := range clients { if client.Enable && client.SubID == subId { link := s.getLink(inbound, client.Email) @@ -93,6 +108,19 @@ func (s *SubService) getClientTraffics(traffics []xray.ClientTraffic, email stri return xray.ClientTraffic{} } +func (s *SubService) getFallbackMaster(dest string) (*model.Inbound, error) { + db := database.GetDB() + var inbound *model.Inbound + err := db.Model(model.Inbound{}). + Where("JSON_TYPE(settings, '$.fallbacks') = 'array'"). + Where("EXISTS (SELECT * FROM json_each(settings, '$.fallbacks') WHERE json_extract(value, '$.dest') = ?)", dest). + Find(&inbound).Error + if err != nil { + return nil, err + } + return inbound, nil +} + func (s *SubService) getLink(inbound *model.Inbound, email string) string { switch inbound.Protocol { case "vmess": diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index a9fce618..08d0b112 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -760,11 +760,32 @@ default: return client.id; } }, + checkFallback(dbInbound) { + newDbInbound = new DBInbound(dbInbound); + if (dbInbound.listen.startsWith("@")){ + rootInbound = this.inbounds.find((i) => + i.tls && + ['trojan','vless'].includes(i.protocol) && + i.settings.fallbacks.find(f => f.dest === dbInbound.listen) + ); + if (rootInbound) { + newDbInbound.listen = rootInbound.listen; + newDbInbound.port = rootInbound.port; + newInbound = newDbInbound.toInbound(); + newInbound.stream.security = 'tls'; + newInbound.stream.tls = rootInbound.stream.tls; + newDbInbound.streamSettings = newInbound.stream.toString(); + } + } + return newDbInbound; + }, showQrcode(dbInbound, clientIndex) { - qrModal.show('{{ i18n "qrCode"}}', dbInbound, clientIndex); + newDbInbound = this.checkFallback(dbInbound); + qrModal.show('{{ i18n "qrCode"}}', newDbInbound, clientIndex); }, showInfo(dbInbound, index) { - infoModal.show(dbInbound, index); + newDbInbound = this.checkFallback(dbInbound); + infoModal.show(newDbInbound, index); }, switchEnable(dbInboundId) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); @@ -865,7 +886,8 @@ }, inboundLinks(dbInboundId) { dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); - txtModal.show('{{ i18n "pages.inbounds.export"}}', dbInbound.genInboundLinks, dbInbound.remark); + newDbInbound = this.checkFallback(dbInbound); + txtModal.show('{{ i18n "pages.inbounds.export"}}', newDbInbound.genInboundLinks, newDbInbound.remark); }, exportAllLinks() { let copyText = '';