Feature/fix external subscription client expiry (#6333)
* fix(sub): honor client expiry for external links * fix(ui): show client expiry on external links * fix(sub): address external expiry review
This commit is contained in:
1 parent
13e87a18c8
commit
2ddcf53020
16 files changed
+332
-61
No files matched your search
@@ -39,6 +39,7 @@ func (s *SubClashService) GetClash(subId string, host string) (string, string, e
|
||||
}
|
||||
|
||||
var proxies []map[string]any
|
||||
var hasInactiveExternal bool
|
||||
|
||||
seenEmails := make(map[string]struct{})
|
||||
for _, inbound := range inbounds {
|
||||
@@ -56,6 +57,11 @@ func (s *SubClashService) GetClash(subId string, host string) (string, string, e
|
||||
}
|
||||
}
|
||||
for _, ext := range externalLinks {
|
||||
if !ext.Active {
|
||||
seenEmails[ext.Email] = struct{}{}
|
||||
hasInactiveExternal = true
|
||||
continue
|
||||
}
|
||||
for _, el := range expandEntry(ext) {
|
||||
name := el.Name
|
||||
if name == "" {
|
||||
@@ -68,17 +74,21 @@ func (s *SubClashService) GetClash(subId string, host string) (string, string, e
|
||||
}
|
||||
}
|
||||
|
||||
if len(proxies) == 0 {
|
||||
if len(proxies) == 0 && !hasInactiveExternal {
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
ensureUniqueProxyNames(proxies)
|
||||
|
||||
emails := make([]string, 0, len(seenEmails))
|
||||
for e := range seenEmails {
|
||||
emails = append(emails, e)
|
||||
}
|
||||
traffic, _ := subReq.AggregateTrafficByEmails(emails)
|
||||
header := fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
|
||||
if len(proxies) == 0 {
|
||||
return "", header, nil
|
||||
}
|
||||
|
||||
ensureUniqueProxyNames(proxies)
|
||||
|
||||
proxyNames := make([]string, 0, len(proxies)+1)
|
||||
for _, proxy := range proxies {
|
||||
@@ -116,7 +126,6 @@ func (s *SubClashService) GetClash(subId string, host string) (string, string, e
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
header := fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
|
||||
return string(finalYAML), header, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ func (a *SUBController) buildSubPageData(c *gin.Context) (PageData, bool) {
|
||||
subReq := a.subService.ForRequest(host)
|
||||
subReq.subscriptionBody = false
|
||||
subs, emails, lastOnline, traffic, err := subReq.getSubs(subId)
|
||||
if err != nil || len(subs) == 0 {
|
||||
if err != nil || subs == nil {
|
||||
writeSubError(c, err)
|
||||
return PageData{}, false
|
||||
}
|
||||
@@ -413,7 +413,7 @@ func (a *SUBController) subs(c *gin.Context) {
|
||||
subReq := a.subService.ForRequest(host)
|
||||
subReq.subscriptionBody = true
|
||||
subs, _, _, traffic, err := subReq.getSubs(subId)
|
||||
if err != nil || len(subs) == 0 {
|
||||
if err != nil || subs == nil {
|
||||
writeSubError(c, err)
|
||||
} else {
|
||||
var result strings.Builder
|
||||
@@ -742,7 +742,7 @@ func (a *SUBController) serveJsonBody(c *gin.Context, alwaysReturnArray bool, co
|
||||
writeSubError(c, err)
|
||||
return true
|
||||
}
|
||||
if len(jsonSub) == 0 {
|
||||
if len(jsonSub) == 0 && header == "" {
|
||||
return false
|
||||
}
|
||||
profileURL := fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
|
||||
@@ -793,7 +793,7 @@ func (a *SUBController) serveClashBody(c *gin.Context, rawDownload bool) bool {
|
||||
writeSubError(c, err)
|
||||
return true
|
||||
}
|
||||
if len(clashSub) == 0 {
|
||||
if len(clashSub) == 0 && header == "" {
|
||||
return false
|
||||
}
|
||||
profileURL := fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/link"
|
||||
)
|
||||
|
||||
// externalLinkEntry is one client × external-link row, resolved for a
|
||||
// subscription request. Email/Enable come from the owning client.
|
||||
// externalLinkEntry is one client × external-link row resolved for a request.
|
||||
// Active applies the owning client's enabled and expiry state.
|
||||
type externalLinkEntry struct {
|
||||
Kind string
|
||||
Value string
|
||||
@@ -23,6 +23,7 @@ type externalLinkEntry struct {
|
||||
NamePrefix string
|
||||
Email string
|
||||
Enable bool
|
||||
Active bool
|
||||
}
|
||||
|
||||
// expandedLink is a single share link contributed by an entry, with the display
|
||||
@@ -32,9 +33,8 @@ type expandedLink struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// getClientExternalLinksBySubId returns every external-link row attached to a
|
||||
// client that carries the given subId, in stable order. Stays inside
|
||||
// internal/sub + database + util/link — no dependency on the panel service layer.
|
||||
// getClientExternalLinksBySubId returns active rows with owner state attached.
|
||||
// Consumers keep inactive owners as metadata but omit their link values.
|
||||
func (s *SubService) getClientExternalLinksBySubId(subId string) ([]externalLinkEntry, error) {
|
||||
db := database.GetDB()
|
||||
var recs []model.ClientRecord
|
||||
@@ -74,6 +74,7 @@ func (s *SubService) getClientExternalLinksBySubId(subId string) ([]externalLink
|
||||
NamePrefix: r.NamePrefix,
|
||||
Email: rec.Email,
|
||||
Enable: rec.Enable,
|
||||
Active: rec.Enable && (rec.ExpiryTime <= 0 || rec.ExpiryTime > now),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
|
||||
Loaded 3 of 16 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user