mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-20 18:11:00 +00:00
feat(clients): give each client its own traffic reset cycle (#6240)
* feat(clients): give each client its own traffic reset cycle Traffic reset is configured on the inbound, so every client sharing an inbound resets together. An operator running a monthly 1000GB plan and a weekly 200GB plan side by side has to press Reset Traffic by hand. Clients now carry the same trafficReset / trafficResetDay pair the inbound already has, with the same vocabulary and the same monthly due-day rule, and PeriodicTrafficResetJob makes a second pass over the clients whose own cycle matches the period it is running for. A client that leaves the field at never behaves exactly as before: only its inbound's schedule can reset it. The fields live on ClientRecord as well as in the inbound settings JSON, so an ordinary edit does not write the cycle back as empty, and an unknown period is rejected rather than coerced, since a coerced value would read as configured while no job would ever select the client. Cron expressions and a custom post-reset quota from the issue are left out: both are separate decisions, and neither has an inbound-level counterpart to stay consistent with. * fix(clients): make the per-client reset cycle editable and safe to run Review found three things wrong with the first cut, one of them mine and worse than the bug it replaced. The cycle could only be set at creation. ClientService.Update writes the columns directly only for a client with no inbounds; the normal path goes through SyncInbound and applyClientRecordMerge, which this change had not extended, so an edit updated the settings JSON while the clients column kept the old value and the job kept applying the old cycle. The earlier test passed because it asserted the value survived an unrelated edit, which it did precisely because nothing ever wrote it. Replaced with a test that changes the cycle and switches it off again. Avoiding the re-enable that ResetTrafficByEmail performs was wrong. Depletion disables clients.enable and the settings JSON as well as client_traffics.enable, so lifting only the quota gate left a depleted client out of the generated config with zeroed counters, which no longer match the depleted predicate: locked out permanently. The rule is now about cause, not state — a client the quota switched off is restored, one disabled below its quota was switched off by hand and is skipped. The bulk path also bypassed node propagation and the MTProto sidecar quota that ResetTrafficByEmail handles, so it silently did nothing on node-backed inbounds. Dropped in favour of the integrated path, whose needRestart is now collected and turned into a single SetToNeedRestart. Also adds the AutoMigrate NULL backfill, guards the merge so a stale node snapshot cannot erase a configured cycle, validates the bulk-create and import paths, normalizes the day the way the inbound path does, marks the fields omitempty so existing clients match the published contract, and shares one TRAFFIC_RESETS tuple between the three forms. * fix(clients): validate renew fields on the bulk and import paths too BulkCreate and ImportClients insert client records without going through Create, so the resetDay/resetMax checks added with the calendar renewal (#6239) and the renew cap (#6238) never ran there. An API caller could store resetDay 45 or a negative resetMax, values the renewal query then mishandles silently. Mirror Create's validation on both batch paths, next to the trafficReset check they already carry. --------- Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
@@ -266,6 +266,8 @@ export const EXAMPLES: Record<string, unknown> = {
|
||||
"subId": "",
|
||||
"tgId": 0,
|
||||
"totalGB": 0,
|
||||
"trafficReset": "never",
|
||||
"trafficResetDay": 1,
|
||||
"updated_at": 0
|
||||
},
|
||||
"ClientInbound": {
|
||||
@@ -302,6 +304,8 @@ export const EXAMPLES: Record<string, unknown> = {
|
||||
"subId": "",
|
||||
"tgId": 0,
|
||||
"totalGB": 0,
|
||||
"trafficReset": "",
|
||||
"trafficResetDay": 0,
|
||||
"updatedAt": 0,
|
||||
"uuid": ""
|
||||
},
|
||||
|
||||
@@ -1131,6 +1131,22 @@ export const SCHEMAS: Record<string, unknown> = {
|
||||
"format": "int64",
|
||||
"type": "integer"
|
||||
},
|
||||
"trafficReset": {
|
||||
"description": "Per-client traffic reset cycle, independent of the inbound's own (#5497).",
|
||||
"enum": [
|
||||
"never",
|
||||
"hourly",
|
||||
"daily",
|
||||
"weekly",
|
||||
"monthly"
|
||||
],
|
||||
"type": "string"
|
||||
},
|
||||
"trafficResetDay": {
|
||||
"maximum": 31,
|
||||
"minimum": 1,
|
||||
"type": "integer"
|
||||
},
|
||||
"updated_at": {
|
||||
"description": "Last update timestamp",
|
||||
"format": "int64",
|
||||
@@ -1262,6 +1278,12 @@ export const SCHEMAS: Record<string, unknown> = {
|
||||
"format": "int64",
|
||||
"type": "integer"
|
||||
},
|
||||
"trafficReset": {
|
||||
"type": "string"
|
||||
},
|
||||
"trafficResetDay": {
|
||||
"type": "integer"
|
||||
},
|
||||
"updatedAt": {
|
||||
"format": "int64",
|
||||
"type": "integer"
|
||||
@@ -1298,6 +1320,8 @@ export const SCHEMAS: Record<string, unknown> = {
|
||||
"subId",
|
||||
"tgId",
|
||||
"totalGB",
|
||||
"trafficReset",
|
||||
"trafficResetDay",
|
||||
"updatedAt",
|
||||
"uuid"
|
||||
],
|
||||
|
||||
@@ -276,6 +276,8 @@ export interface Client {
|
||||
subId: string;
|
||||
tgId: number;
|
||||
totalGB: number;
|
||||
trafficReset?: string;
|
||||
trafficResetDay?: number;
|
||||
updated_at?: number;
|
||||
}
|
||||
|
||||
@@ -314,6 +316,8 @@ export interface ClientRecord {
|
||||
subId: string;
|
||||
tgId: number;
|
||||
totalGB: number;
|
||||
trafficReset: string;
|
||||
trafficResetDay: number;
|
||||
updatedAt: number;
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
@@ -296,6 +296,8 @@ export const ClientSchema = z.object({
|
||||
subId: z.string(),
|
||||
tgId: z.number().int(),
|
||||
totalGB: z.number().int(),
|
||||
trafficReset: z.enum(['never', 'hourly', 'daily', 'weekly', 'monthly']).optional(),
|
||||
trafficResetDay: z.number().int().min(1).max(31).optional(),
|
||||
updated_at: z.number().int().optional(),
|
||||
});
|
||||
export type Client = z.infer<typeof ClientSchema>;
|
||||
@@ -336,6 +338,8 @@ export const ClientRecordSchema = z.object({
|
||||
subId: z.string(),
|
||||
tgId: z.number().int(),
|
||||
totalGB: z.number().int(),
|
||||
trafficReset: z.string(),
|
||||
trafficResetDay: z.number().int(),
|
||||
updatedAt: z.number().int(),
|
||||
uuid: z.string(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user