mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-29 13:16:51 +08:00
9672249edb
* feat(clients): add calendar weekly renewal and schedule previews Expose fixed-day, calendar-weekly, calendar-monthly, and disabled renewal through one shared selector in individual and bulk client forms. Store the weekly weekday separately (Monday 1 through Sunday 7) and use panel-local calendar dates rather than a fixed 168-hour duration. Resolve skipped or repeated midnights to the first valid instant of the selected date, and skip an entirely nonexistent calendar date rather than changing the weekday. Reuse the existing renewal writer and share its boundary alignment and per-period catch-up calculation with an authenticated, read-only preview. Keep monthly precedence for legacy records, fixed-day interval semantics, maximum renewal allowances, first-use durations, and operator-disabled settings unchanged. Selecting a mode does not rewrite an existing cutoff; an unset calendar cutoff requires an explicit action to choose the first. The last-valid-second preview uses the stored exclusive expiry, even when the billing calculation aligns a legacy last-second cutoff up to midnight. Carry weekly schedules through client persistence, paging, enable toggles, inbound settings, and node traffic reconciliation. Migrate missing or nullable weekday columns to disabled by default without altering existing limits, and include the new isolated-schema PostgreSQL regression in the live CI gate. Regenerate API contracts and reference documentation, add lifecycle and form regressions, and document timezone, quota-reset, and upgrade considerations. All participating nodes must be upgraded before weekly mode is enabled; older binaries ignore the new field. Independent periodic traffic resets and the optional month-end subscription-header display are not changed. * fix(clients): validate renewal schedules across inbound write paths Reject conflicting weekly/interval/monthly schedules and out-of-range weekdays on inbound creation and edits, legacy one-client apply paths, record/link synchronization, and traffic metadata writes. Validate imported traffic snapshots as well, before any inbound or client is persisted, so an inbound API cannot create a client that the clients page cannot toggle. Merge a weekly-related schedule as one timestamp-selected tuple rather than filling its zero fields from another renewal mode. Preserve empty migration snapshots and the existing non-weekly monthly/interval merge semantics. Renewal caps, counters, credentials, and deadlines are unchanged. Add regressions for nine write paths, unchanged records and runtime calls after rejection, valid inbound clients remaining editable, and duplicate record merges between individually valid renewal modes. * docs(clients): clarify depleted-client deletion risks on downgrade Explain in English and Chinese that older versions not only stop weekly renewal: their depleted-client cleanup can delete a weekly-only client once its expiry or quota is exhausted. This is conditional on cleanup, not an automatic deletion caused by downgrade itself. Recommend backing up and converting weekly schedules to a mode supported by every participating version before rollback, and avoiding cleanup while mixed versions or unconverted clients remain. Merely disabling weekly renewal does not restore the old binary's missing purge protection. * fix(clients): bound weekly renewal date searches Limit the search for a valid weekly calendar date to eight candidates so an unusual timezone cannot monopolize the single traffic writer. Exhaustion returns the original instant, allowing the existing catch-up forward-progress guard to stop without advancing expiry, consuming an allowance, resetting traffic, or falling back to a fixed-duration schedule that can drift. Reject a non-future calendar suggestion in the read-only preview instead of offering an immediately expired initial cutoff. Also report failed weekly catch-up as a search error when allowances remain, not as cap exhaustion. Existing preview errors use the form's current warning; no API schema or locale changes are needed. Exercise exhaustion with a synthetic valid TZif containing twelve skipped Sundays. This fault-injection case was red without the bound; it is not a claim that a production IANA timezone was observed hanging. Keep the Havana and Apia regressions for real skipped/repeated midnights and absent dates. * fix(tests): isolate weekly renewal preview timezone Stop the weekly search regression from replacing process-global time.Local. CI caught that assignment and its cleanup racing with background timer reads through time.Now, even though the top-level tests do not use t.Parallel. Pass the timezone and current instant into the unchanged preview calculation. The public service still validates the request and resolves the panel timezone; API responses, renewal accounting, and persisted client data are unchanged. Use fixed dates for both suggestion and catch-up exhaustion, removing the test's dependency on today's date and its unnecessary database setup. Keep a bounded-lifetime background clock reader to expose future global-timezone mutations under the existing race gate rather than disabling that check. * ci: retrigger PR checks Create an empty commit to request a fresh pull-request CI run after release dependency downloads failed with network errors. No source, dependency, or workflow changes are included. Retry the existing checks without bypassing them. * ci: retry PR checks and record deferred download hardening Request another CI run after the amd64 release job compiled successfully but failed during dependency fetching with exit code 4 (network failure). Record possible follow-up improvements for the Linux release fetch helper: - Print each download URL and destination, and preserve error details. - Reuse the existing curl configuration with up to five retries; add connection and per-attempt timeouts and a bounded retry window. - Download to a temporary file and promote it to the final filename only after a successful, non-empty transfer. Keep the job failing if downloads ultimately fail. - Validate successful downloads, recovery after a temporary failure, and correct failure after persistent errors before shipping such a change. These improvements are intentionally deferred, not implemented or tested by this commit. This commit is empty: renewal logic, dependencies, workflow configuration, check requirements, and TLS verification remain unchanged. --------- Co-authored-by: JacktheRanger <219502738+JacktheRanger@users.noreply.github.com>
250 lines
7.3 KiB
Go
250 lines
7.3 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ExportAll returns every client in the same {client, inboundIds} shape that
|
|
// /add and /bulkCreate accept, so an exported file round-trips straight back
|
|
// through Import. Clients with no inbound attachment are included with an empty
|
|
// inboundIds list so an export taken before DeleteOrphans can restore them.
|
|
func (s *ClientService) ExportAll() ([]ClientCreatePayload, error) {
|
|
db := database.GetDB()
|
|
var rows []model.ClientRecord
|
|
if err := db.Order("id ASC").Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]ClientCreatePayload, 0, len(rows))
|
|
if len(rows) == 0 {
|
|
return out, nil
|
|
}
|
|
|
|
ids := make([]int, 0, len(rows))
|
|
for i := range rows {
|
|
ids = append(ids, rows[i].Id)
|
|
}
|
|
|
|
attachments := make(map[int][]int, len(rows))
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
var links []model.ClientInbound
|
|
if err := db.Where("client_id IN ?", batch).Order("inbound_id ASC").Find(&links).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
for _, l := range links {
|
|
attachments[l.ClientId] = append(attachments[l.ClientId], l.InboundId)
|
|
}
|
|
}
|
|
|
|
for i := range rows {
|
|
client := rows[i].ToClient()
|
|
// The per-inbound flow_override is the reliable flow for multi-inbound
|
|
// clients; the canonical column can be left stale by SyncInbound (#4792).
|
|
if flow, err := s.EffectiveFlow(db, rows[i].Id); err == nil && flow != "" {
|
|
client.Flow = flow
|
|
}
|
|
out = append(out, ClientCreatePayload{
|
|
Client: *client,
|
|
InboundIds: attachments[rows[i].Id],
|
|
LimitHwid: rows[i].LimitHwid,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ImportClients recreates clients from an exported list. Items that carry
|
|
// inboundIds go through the normal BulkCreate path (added to every inbound and
|
|
// pushed to xray); items with no inboundIds are restored as bare records so an
|
|
// orphan-inclusive export round-trips. Existing emails are never overwritten —
|
|
// they are reported in Skipped. The boolean reports whether xray needs a restart.
|
|
func (s *ClientService) ImportClients(inboundSvc *InboundService, items []ClientCreatePayload) (BulkCreateResult, bool, error) {
|
|
result := BulkCreateResult{}
|
|
if len(items) == 0 {
|
|
return result, false, nil
|
|
}
|
|
|
|
attached := make([]ClientCreatePayload, 0, len(items))
|
|
orphans := make([]ClientCreatePayload, 0)
|
|
for i := range items {
|
|
if len(items[i].InboundIds) > 0 {
|
|
attached = append(attached, items[i])
|
|
} else {
|
|
orphans = append(orphans, items[i])
|
|
}
|
|
}
|
|
|
|
skip := func(email, reason string) {
|
|
if strings.TrimSpace(email) == "" {
|
|
email = "(missing email)"
|
|
}
|
|
result.Skipped = append(result.Skipped, BulkCreateReport{Email: email, Reason: reason})
|
|
}
|
|
|
|
needRestart := false
|
|
if len(attached) > 0 {
|
|
sub, nr, err := s.BulkCreate(inboundSvc, attached)
|
|
if err != nil {
|
|
return result, needRestart, err
|
|
}
|
|
needRestart = needRestart || nr
|
|
result.Created += sub.Created
|
|
result.Skipped = append(result.Skipped, sub.Skipped...)
|
|
}
|
|
|
|
db := database.GetDB()
|
|
for i := range orphans {
|
|
client := orphans[i].Client
|
|
email := strings.TrimSpace(client.Email)
|
|
if email == "" {
|
|
skip("", "client email is required")
|
|
continue
|
|
}
|
|
if verr := validateClientEmail(email); verr != nil {
|
|
skip(email, verr.Error())
|
|
continue
|
|
}
|
|
if verr := validateClientSubID(client.SubID); verr != nil {
|
|
skip(email, verr.Error())
|
|
continue
|
|
}
|
|
if verr := validateClientRenewal(client); verr != nil {
|
|
skip(email, verr.Error())
|
|
continue
|
|
}
|
|
if verr := validateClientResetMax(client.ResetMax); verr != nil {
|
|
skip(email, verr.Error())
|
|
continue
|
|
}
|
|
if verr := validateClientTrafficReset(client.TrafficReset, client.TrafficResetDay); verr != nil {
|
|
skip(email, verr.Error())
|
|
continue
|
|
}
|
|
|
|
// An existing record (in the DB or just created from the attached set
|
|
// above) always wins — import never clobbers a live client.
|
|
var taken int64
|
|
if err := db.Model(&model.ClientRecord{}).Where("email = ?", email).Count(&taken).Error; err != nil {
|
|
return result, needRestart, err
|
|
}
|
|
if taken > 0 {
|
|
skip(email, "email already in use: "+email)
|
|
continue
|
|
}
|
|
|
|
client.Email = email
|
|
if client.SubID == "" {
|
|
client.SubID = uuid.NewString()
|
|
}
|
|
if client.SubID != "" {
|
|
var subTaken int64
|
|
if err := db.Model(&model.ClientRecord{}).
|
|
Where("sub_id = ? AND email <> ?", client.SubID, email).
|
|
Count(&subTaken).Error; err != nil {
|
|
return result, needRestart, err
|
|
}
|
|
if subTaken > 0 {
|
|
skip(email, "subId already in use: "+client.SubID)
|
|
continue
|
|
}
|
|
}
|
|
// Preserve exported enable so a disabled orphan stays disabled (#6478).
|
|
now := time.Now().UnixMilli()
|
|
if client.CreatedAt == 0 {
|
|
client.CreatedAt = now
|
|
}
|
|
client.UpdatedAt = now
|
|
|
|
rec := client.ToRecord()
|
|
rec.LimitHwid = orphans[i].LimitHwid
|
|
if err := db.Create(rec).Error; err != nil {
|
|
skip(email, err.Error())
|
|
continue
|
|
}
|
|
// gorm default:true drops enable=false on Create — restate (#6478).
|
|
if !client.Enable {
|
|
if err := db.Model(&model.ClientRecord{}).Where("id = ?", rec.Id).
|
|
UpdateColumn("enable", false).Error; err != nil {
|
|
return result, needRestart, err
|
|
}
|
|
}
|
|
result.Created++
|
|
}
|
|
|
|
return result, needRestart, nil
|
|
}
|
|
|
|
// DeleteOrphans removes every client that is not attached to any inbound,
|
|
// together with its traffic rows, IP log, and external links. It mirrors the
|
|
// cleanup the single-client Delete performs, batched into one transaction.
|
|
// Returns the number of clients deleted.
|
|
func (s *ClientService) DeleteOrphans() (int, error) {
|
|
db := database.GetDB()
|
|
sub := database.GetDB().Table("client_inbounds").Select("client_id")
|
|
var rows []model.ClientRecord
|
|
if err := db.Where("id NOT IN (?)", sub).Order("id ASC").Find(&rows).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
ids := make([]int, 0, len(rows))
|
|
emails := make([]string, 0, len(rows))
|
|
subIDs := make([]string, 0, len(rows))
|
|
for i := range rows {
|
|
ids = append(ids, rows[i].Id)
|
|
if rows[i].Email != "" {
|
|
emails = append(emails, rows[i].Email)
|
|
}
|
|
subIDs = append(subIDs, rows[i].SubID)
|
|
}
|
|
tombstoneClientEmails(emails)
|
|
|
|
if err := runSerializedTx(func(tx *gorm.DB) error {
|
|
if e := adjustGroupBaselinesForRemovedTraffic(tx, emails); e != nil {
|
|
return e
|
|
}
|
|
if e := clearClientHwidsBySubIDTx(tx, subIDs...); e != nil {
|
|
return e
|
|
}
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientInbound{}).Error; e != nil {
|
|
return e
|
|
}
|
|
if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientExternalLink{}).Error; e != nil {
|
|
return e
|
|
}
|
|
}
|
|
if len(emails) > 0 {
|
|
for _, batch := range chunkStrings(emails, sqlInChunk) {
|
|
if e := tx.Where("email IN ?", batch).Delete(&xray.ClientTraffic{}).Error; e != nil {
|
|
return e
|
|
}
|
|
if e := tx.Where("client_email IN ?", batch).Delete(&model.InboundClientIps{}).Error; e != nil {
|
|
return e
|
|
}
|
|
}
|
|
if e := clearGlobalTraffic(tx, emails...); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
if e := tx.Where("id IN ?", batch).Delete(&model.ClientRecord{}).Error; e != nil {
|
|
return e
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return 0, err
|
|
}
|
|
return len(ids), nil
|
|
}
|