Publish Zonghengjia Windows 0.4.0 client source

This commit is contained in:
2026-11-29 23:48:07 +08:00
parent 308d71021e
commit 7a00f97910
37 changed files with 1722 additions and 511 deletions
+68 -39
View File
@@ -616,33 +616,38 @@ def node_sync(request):
return JsonResponse({"error": "node authentication failed"}, status=401)
now = timezone.now()
Node.objects.filter(pk=node.pk).update(
last_seen_at=now,
status=Node.Status.ONLINE,
agent_version=request.headers.get("X-Agent-Version", "")[:64],
)
desired = list(desired_subscriptions_for_node(node))
agent_version = request.headers.get("X-Agent-Version", "")[:64]
if request.method == "GET":
NodeDeployment.objects.bulk_create(
[NodeDeployment(node=node, subscription=subscription) for subscription in desired],
ignore_conflicts=True,
)
return JsonResponse(
{
"node": node.slug,
"generated_at": now.isoformat(),
"clients": [
{
"subscription_id": subscription.pk,
"email": f"sub-{subscription.pk}",
"uuid": str(subscription.credential_uuid),
"flow": "xtls-rprx-vision",
}
for subscription in desired
],
}
)
# Keep the eligibility snapshot, deployment rows and serialized response
# in the same row-lock transaction. Finalize therefore orders strictly
# before or after this generation; it cannot interleave after the snapshot.
with transaction.atomic():
node = Node.objects.select_for_update(**non_key_lock_options()).get(pk=node.pk)
if not node.active:
return JsonResponse({"error": "node retired"}, status=409)
node.last_seen_at = now
node.agent_version = agent_version
node.save(update_fields=["last_seen_at", "agent_version", "updated_at"])
desired = list(desired_subscriptions_for_node(node))
NodeDeployment.objects.bulk_create(
[NodeDeployment(node=node, subscription=subscription) for subscription in desired],
ignore_conflicts=True,
)
return JsonResponse(
{
"node": node.slug,
"generated_at": now.isoformat(),
"clients": [
{
"subscription_id": subscription.pk,
"email": f"sub-{subscription.pk}",
"uuid": str(subscription.credential_uuid),
"flow": "xtls-rprx-vision",
}
for subscription in desired
],
}
)
try:
payload = json.loads(request.body or b"{}")
@@ -656,8 +661,6 @@ def node_sync(request):
except (KeyError, TypeError, ValueError, json.JSONDecodeError):
return JsonResponse({"error": "invalid report payload"}, status=400)
desired_ids = {subscription.pk for subscription in desired}
normalized_usage = []
try:
for item in usage:
@@ -672,18 +675,37 @@ def node_sync(request):
except (KeyError, TypeError, ValueError):
return JsonResponse({"error": "invalid usage item"}, status=400)
known_deployment_ids = set(
NodeDeployment.objects.filter(node=node).values_list("subscription_id", flat=True)
) | desired_ids
if not applied_ids.issubset(known_deployment_ids):
return JsonResponse({"error": "applied subscription is not associated with this node"}, status=400)
if any(subscription_id not in known_deployment_ids for subscription_id, _, _ in normalized_usage):
return JsonResponse({"error": "usage item is not associated with this node"}, status=400)
with transaction.atomic():
# Serialize first reports as well as existing deployment rows. Otherwise
# two first reports can both try to create the same NodeDeployment.
Node.objects.select_for_update(**non_key_lock_options()).get(pk=node.pk)
# Re-read every mutable eligibility field under the one row lock used by
# drain/finalize. A request authenticated before finalization must never
# restore deployments or acknowledge an obsolete authorization generation.
node = Node.objects.select_for_update(**non_key_lock_options()).get(pk=node.pk)
if not node.active or node.status == Node.Status.OFFLINE:
return JsonResponse({"error": "node retired"}, status=409)
node.last_seen_at = now
node.agent_version = agent_version
node.save(update_fields=["last_seen_at", "agent_version", "updated_at"])
desired = list(desired_subscriptions_for_node(node))
desired_ids = {subscription.pk for subscription in desired}
known_deployment_ids = set(
NodeDeployment.objects.filter(node=node).values_list("subscription_id", flat=True)
) | desired_ids
if not applied_ids.issubset(known_deployment_ids):
return JsonResponse(
{"error": "applied subscription is not associated with this node"},
status=400,
)
if any(
subscription_id not in known_deployment_ids
for subscription_id, _, _ in normalized_usage
):
return JsonResponse(
{"error": "usage item is not associated with this node"},
status=400,
)
# Serialize first reports as well as existing deployment rows. Otherwise
# two first reports can both try to create the same NodeDeployment.
report, created = NodeReport.objects.get_or_create(node=node, report_id=report_id)
if not created:
return JsonResponse({"status": "already_processed"})
@@ -714,6 +736,13 @@ def node_sync(request):
deployment.revoked_at = now
deployment.last_error = ""
deployment.save()
if (
not desired_ids
and not applied_ids
and not node.subscription_enabled
and node.retirement_drain_started_at is not None
):
Node.objects.filter(pk=node.pk).update(retirement_empty_ack_at=now)
else:
for subscription_id in desired_ids:
deployment = deployments[subscription_id]