Skip to content

feat(subscription): server groups in client templates and per-rule templates - #1073

Draft
Rerowros wants to merge 14 commits into
PasarGuard:devfrom
Rerowros:feat/template-host-injection
Draft

Rerowros wants to merge 14 commits into
PasarGuard:devfrom
Rerowros:feat/template-host-injection

Conversation

@Rerowros

Copy link
Copy Markdown
Contributor

Summary

Lets client templates decide where the user's servers go: into balancers, url-test / fallback groups, or a single config. It also lets a subscription rule pick the template. This brings template routing to the level of Remnawave's host injection directives and covers #517.

  • New pasarguard key in client templates; it is stripped before the config is sent. The selector {inbound_tags, inbound_tag_regex, remark_regex, host_ids} works the same in all formats: conditions are ANDed, and {} means all of the user's servers. A tags key is reserved for host tags (feat(hosts): reusable color-coded host tags #667).
  • Xray JSON: "pasarguard": {"mode": "per_host" | "single", "inject": [{"select": {...}, "tag_prefix": "proxy", "tag_from": "index" | "remark"}]}.
    • single returns one config in which every selected server becomes proxy-1..N, ready for a leastPing balancer.
    • Fragment/noise dialers of injected servers are named dialer-<tag>, so a ["proxy-"] selector never picks a direct dialer.
  • sing-box groups and Mihomo/Clash proxy-groups: "pasarguard": {"include_proxies": true, "select": {...}} fills a group with only the selected servers.
    • A group that ends up empty falls back to all of the user's servers, the same as a group without the directive, so traffic never silently leaves the VPN.
  • Per-rule template: subscription rules get an optional template_id, so one client app can get its own template without changing the default.
    • Precedence: host-level template > rule > default.
    • Manual format links (/sub/{token}/{format}) keep using the default template.
  • Dashboard: a template picker in the rule editor, a badge on templates that use server groups, and a help sheet with copy-paste examples.

Templates without the directive produce the same output as before.

Part of splitting #759 into focused PRs.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Documentation
  • Tests / CI

Checklist

  • I tested the change locally or explained why it cannot be tested.
  • I added or updated tests for behavior changes.
  • I updated documentation, translations, or examples if needed.
  • I checked database migrations when models or schema changed.
  • I did not include secrets, tokens, private keys, or unrelated changes.

Testing

uv run pytest tests/api/test_template_routing.py tests/test_subscription_template_directives.py -q   # 62 passed
cd dashboard && bun test src/features/templates/forms/template-directives.test.ts src/features/subscriptions/components/subscription-settings-schema.test.ts
bun run build

End to end on a local panel: for a user matched by the rule, an Xray template in single mode returned one config with a balancer over proxy-1..3. Another user got the usual per-host configs.

Screenshots

Will add in a comment.

Notes for reviewers

  • No migration: template_id lives inside the subscription settings JSON.
  • A Clash template that uses the directive is re-dumped as YAML, so its comments are lost. Templates without the directive are passed through untouched.
  • Regexes in selectors are treated as trusted input from the panel owner.
  • Full directive docs with examples are in the in-app help sheet. I can also move them to the docs repo.

…groups and balancers

A "pasarguard" directive in a client template selects the user's hosts by
inbound tag, inbound tag regex, remark regex or host id:
- Xray JSON: per_host mode appends the selected other hosts as extra
  outbounds (<prefix>-1..n or remark tags); single mode returns one config
  with only the injected outbounds, so a balancer can span all servers.
- Sing-box: a selector/urltest with the directive gets only the proxies it
  selects, after the entries written in the template.
- Clash/Mihomo: a proxy group with the directive gets the selected proxy
  names appended without duplicates.

The key is always stripped. Templates without it render byte for byte as
before. Invalid directives are rejected when an Xray, Sing-box or Clash
template is saved.
SubRule gets an optional template_id. When a rule matches and has one, that
template replaces the default for the rule's format (xray, sing_box, clash,
clash_meta); a host-level Xray template still wins. Resolution lives in
resolve_client_templates (host > rule > default) and the config cache key
includes the template, so two clients of one user do not share output.

Saving settings checks that the template exists and fits the rule target;
deleting a template a rule uses is refused with 409. Single-mode Xray
configs are named after the subscription profile title.
… server groups

Subscription rules for xray, sing_box, clash and clash_meta get a Template
select in the rule's advanced sheet (default = null) and a badge with the
template name on the row; changing the target to another template type
resets it. template_id is now kept when settings are loaded and saved.

Client templates that use the pasarguard directive get a Server groups
badge, and the editor for subscription templates has a help panel with
examples and an Insert example button.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a2dde715-572e-4be6-8b8b-a10ee335a657

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…wn direct outbound

When the selector of a single mode template matched none of the user's servers, the config held only the template's outbounds and Xray sent all traffic to the first one (often direct). Fall back to all of the user's servers, as empty sing-box and Clash groups already do.

Also: skip a repeated single template before parsing it, check reserved tag prefixes once as a tuple instead of growing the set with every proxy-N (same output, less work for per_host), and drop the unused single_remarks constructor argument.
…templates that have one

A remark or SNI containing "pasarguard" no longer triggers a YAML parse of the whole output. Selector regexes are limited to 256 characters, and tags is rejected for any value except null.
…cannot be deleted

The rule patterns are subscription settings that an admin with client_templates.delete may not be allowed to read. Template validation uses the same libyaml loader.
…e one Xray template cache

A set profile title that formats to an empty string stays empty as before; only an unset one becomes "Subscription". subscription_xray_templates now reads the per-type cache instead of keeping a second copy.
…precedence and sing-box/Clash rule templates
…ples on lines of their own

Insert example no longer glues the Clash YAML example to the line at the cursor, and adds the Xray block in single mode; a per_host template with an empty selector shows a size warning. The rule sheet skips the template list without client_templates.read_simple, the template badge is a real button, the directive badge ignores comments and string values, and the sheet text no longer promises a template picker for formats without one.
@Rerowros
Rerowros marked this pull request as draft October 25, 2026 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant