mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-17 02:25:02 +03:00
perf(sub): decode TUIC and AmneziaWG settings once per request
genTuicLink and genAmneziaWGLink, and their Clash and JSON siblings, parsed the inbound's whole settings, every client included, for each link they built, bypassing the per-request caches the other generators use. Building the links of n clients of one inbound, as the export of all links does, cost n full parses of an n-client document. Typed per-request caches next to settingsByInbound now hold the decoded TUIC instance, indexed by email, and the AmneziaWG server block, and every generator reads through them.
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/amneziawg"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/tuic"
|
||||
wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
|
||||
)
|
||||
|
||||
@@ -670,8 +669,7 @@ func (s *SubClashService) buildWireguardProxy(subReq *SubService, inbound *model
|
||||
return nil
|
||||
}
|
||||
|
||||
var inboundSettings map[string]any
|
||||
_ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
|
||||
inboundSettings := subReq.linkSettings(inbound)
|
||||
secretKey, _ := inboundSettings["secretKey"].(string)
|
||||
|
||||
proxy := map[string]any{
|
||||
@@ -723,21 +721,18 @@ func (s *SubClashService) buildWireguardProxy(subReq *SubService, inbound *model
|
||||
}
|
||||
|
||||
func (s *SubClashService) buildTuicProxy(subReq *SubService, inbound *model.Inbound, client model.Client, ep map[string]any) map[string]any {
|
||||
inst, ok := tuic.InstanceFromInbound(inbound)
|
||||
if !ok {
|
||||
inst := subReq.tuicInstanceForLink(inbound)
|
||||
if inst == nil {
|
||||
return nil
|
||||
}
|
||||
uuid := client.ID
|
||||
password := client.Password
|
||||
for _, c := range inst.Clients {
|
||||
if c.Email == client.Email {
|
||||
if uuid == "" {
|
||||
uuid = c.UUID
|
||||
}
|
||||
if password == "" {
|
||||
password = c.Password
|
||||
}
|
||||
break
|
||||
if c := inst.byEmail[client.Email]; c != nil {
|
||||
if uuid == "" {
|
||||
uuid = c.UUID
|
||||
}
|
||||
if password == "" {
|
||||
password = c.Password
|
||||
}
|
||||
}
|
||||
if uuid == "" || password == "" {
|
||||
@@ -779,15 +774,9 @@ func (s *SubClashService) buildTuicProxy(subReq *SubService, inbound *model.Inbo
|
||||
// amneziaWGClientAddresses prefers this inbound's own settings entry over the
|
||||
// shared clients.wg_allowed_ips column, which for an identity attached to both
|
||||
// a wireguard and an amneziawg inbound holds the other one's address.
|
||||
func amneziaWGClientAddresses(settingsClients []model.Client, client model.Client) []string {
|
||||
for i := range settingsClients {
|
||||
if !strings.EqualFold(settingsClients[i].Email, client.Email) {
|
||||
continue
|
||||
}
|
||||
if len(settingsClients[i].AllowedIPs) > 0 {
|
||||
return settingsClients[i].AllowedIPs
|
||||
}
|
||||
break
|
||||
func amneziaWGClientAddresses(subReq *SubService, inbound *model.Inbound, client model.Client) []string {
|
||||
if own, ok := subReq.clientForLink(inbound, client.Email); ok && len(own.AllowedIPs) > 0 {
|
||||
return own.AllowedIPs
|
||||
}
|
||||
return client.AllowedIPs
|
||||
}
|
||||
@@ -811,11 +800,10 @@ func (s *SubClashService) buildAmneziaWGProxy(subReq *SubService, inbound *model
|
||||
return nil
|
||||
}
|
||||
|
||||
var parsed amneziawg.InboundSettings
|
||||
if err := json.Unmarshal([]byte(inbound.Settings), &parsed); err != nil || parsed.Server == nil {
|
||||
server := subReq.amneziaWGServerForLink(inbound)
|
||||
if server == nil {
|
||||
return nil
|
||||
}
|
||||
server := parsed.Server
|
||||
|
||||
proxy := map[string]any{
|
||||
"name": subReq.endpointRemark(inbound, client.Email, ep, ""),
|
||||
@@ -836,7 +824,7 @@ func (s *SubClashService) buildAmneziaWGProxy(subReq *SubService, inbound *model
|
||||
proxy["persistent-keepalive"] = ka
|
||||
}
|
||||
|
||||
for _, addr := range amneziaWGClientAddresses(parsed.Clients, client) {
|
||||
for _, addr := range amneziaWGClientAddresses(subReq, inbound, client) {
|
||||
ip := stripCIDR(addr)
|
||||
if ip == "" {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user