Skip to content

Destination audit

Destination audit lets a katana node block connections to hosts you do not want it to reach, such as BitTorrent trackers or a site your terms of service forbid. Each rule is a regular expression over domain names. katana tests it against the domain every flow asks for, or the one sniffed from the flow’s first bytes, and sends a matching flow to a blackhole. katana reports nothing about blocked flows to the panel.

Rules come from two places, and you can use both at once: the panel, except on helium, and a local file named by rule_list_path. Read this page if you run a node for a panel that manages audit rules, or if you want a fixed block list that does not depend on the panel.

This procedure adds a local block list to an existing node. It works with every panel type.

  1. Write the rules file. Put one regex on each line. Blank lines and lines that start with # are skipped.

    /etc/katana/rules.txt
    # katana destination audit rules: one regex per line.
    # Hosts named tracker.*, announce.* or open.tracker.*, a common naming
    # pattern for public BitTorrent trackers
    ^(tracker|announce|open\.tracker)\.
    # One site and every subdomain of it, with or without a trailing dot
    (^|\.)blocked\.example\.com\.?$
  2. Point the node at it. Set rule_list_path in the node’s [node.api] table:

    /etc/katana/config.toml
    [[node]]
    panel_type = "newV2board"
    [node.api]
    host = "https://panel.example.com"
    node_id = 1
    key = "replace-with-the-panel-key"
    node_type = "V2ray"
    rule_list_path = "/etc/katana/rules.txt"
  3. Check the config. katana --test -c /etc/katana/config.toml checks that the key is spelled right and holds a string. It does not open the rules file, so a wrong path or a bad regex still prints Configuration OK.

  4. Apply the change. If katana is already running, saving the config is enough: the hot reload loads the new path at once and keeps open connections. Otherwise start katana. The node reads the rules during its start-up, before it serves anyone, and again on every panel poll, so later edits to the file’s contents need no reload or restart: see When rules change.

  5. Check the log. A path katana cannot read and each regex it cannot compile produce a WARN line. The log lines table lists them.

Both keys belong to one [[node]] entry. Each node has its own rule set, so two nodes can use different files, or the same one.

Key Table Type Default Description
rule_list_path [node.api] path "" The local rules file. Empty means no local rules. katana reads the file on every rule refresh. A relative path is resolved against katana’s working directory, so use an absolute path.
disable_get_rule [node.controller] bool false true turns rule refreshes off entirely, for both the panel rules and the local file. Set at start-up, it means the node audits nothing.

Both tables reject unknown keys, so a typo such as disable_get_rules stops katana instead of silently turning audit off. The error starts with the line and column of the mistake:

configuration error: config parse error: TOML parse error at line 9, column 1

update_periodic in [node.controller] (default 60 seconds) sets how often the node polls the panel, and therefore how often rules are refreshed. The katana config file page describes it with the other controller keys.

On every refresh, katana builds one ordered list: the local rules first, in file order, then the panel’s rules. What the panel contributes depends on the panel type.

With panel_type = "newV2board" (or "v2board"), katana takes rules from the routes array of the node config the panel returns. It makes no extra request for them.

Part of the panel's node config response
"routes": [
{ "id": 3, "match": ["(^|\\.)blocked\\.example\\.com$", "^tracker\\."], "action": "block" },
{ "id": 7, "match": ["example.org"], "action": "dns", "action_value": "198.51.100.53" }
]
  • Only entries whose action is exactly block become audit rules. katana does not use the other actions.
  • The entries of one route’s match list are joined with | into a single regex. The first route above becomes (^|\.)blocked\.example\.com$|^tracker\..
  • The rule’s id is the route’s position in the routes array, counting from 0, not the panel’s id field. The first route above gets id 0. The id names the rule’s blackhole outbound; see How katana applies the rules.
  • If the joined regex does not compile, katana logs a warning and skips the whole route, not just the bad entry.
  • katana keeps the routes from the last node config it received. When the panel answers 304 Not Modified or the request fails, the previous routes stay in use.
  • When a hot reload gives the node a new panel client, after an edit to its [node.api] table, to enable_mtls or to [node.hysteria], the new client takes over these routes. The rules they produce stay in force even if the panel cannot be reached at that moment.

The local file contributes the same way for every panel type:

  • katana trims each line, then skips it if it is empty or starts with #. There are no inline comments: foo # bar is the regex foo # bar.
  • Every other line is compiled as one regex. A line that does not compile is logged and skipped, and the rest of the file still loads.
  • Every local rule gets the id -1.
  • If the file cannot be read, katana logs a warning and the refresh continues with no local rules. The panel’s rules still load.
Xboard / V2board Local file
Source routes entries with action = "block" rule_list_path
One rule per route (its match list joined with |) line
Rule id position in routes, from 0 -1

A helium node has the local file’s rules alone.

katana 6 has no SSPanel support, and with it went SSPanel’s detect rules and the detection-log reports katana 5 sent there; see Upgrading to katana 6.

katana turns the rules into routes at the top of the node’s route table, ahead of every [node.route] rule. All the rules that share an id become one route, and each id gets a blackhole outbound of its own, named audit#<id>, which is why no [[outbound]] tag may start with audit#. The routes keep the order in which the ids first appear in the list, so the local rules, all with id -1, come first.

A rule is tested against the flow’s domain name, without the port:

  • the domain the client asked for, for example www.blocked.example.com;
  • and, while sniffing is on (disable_sniffing = false, the default), the name sniffed from the TLS server name or HTTP Host of a TCP flow.

Both names are lowercased before the test. A flow addressed by IP with no sniffed name is never matched, so an audit rule cannot block an address; use a cidr or geoip rule that routes to block for that (Routing).

The match is an unanchored search with the Rust regex syntax. That means:

  • blocked\.example\.com also matches notblocked.example.com.evil.test. Anchor with ^ and $, and use (^|\.) to cover a domain and its subdomains.
  • katana does not strip a trailing dot, so a client that asks for blocked.example.com. does not match a pattern that ends in com$. End the pattern with \.?$ to cover both forms.
  • . matches any character. Escape it as \. in names.
  • Names are lowercased, so write patterns in lower case. A pattern with a capital letter never matches, unless it starts with (?i).
  • Look-around ((?=…), (?<!…)) and backreferences are not supported. Such a regex fails to compile and is skipped.
  • The port is not part of the string, so an audit rule cannot target a port. Use a routing rule for that.

Every flow any inbound opens goes through the same path: a TCP request, each multiplexed sub-flow, each Hysteria 2 stream, and each packet of a UDP association. katana first admits the user, then walks the route table, which starts with the audit rules.

flowchart TB
  A["New flow"] --> B{"User still registered?"}
  B -- no --> R1["Refused"]
  B -- yes --> E{"An audit rule matches the domain?"}
  E -- yes --> R3["Blackhole: the rule's audit outbound"]
  E -- no --> D{"Route picks block?"}
  D -- yes --> R2["Blackhole: block"]
  D -- no --> F["Dialled through the routed outbound"]
  • The first match wins. katana tests the rules in route order, local rules first, and stops at the first rule that matches.
  • An audited flow looks like a routing block. It goes to a blackhole, exactly like a flow routed to block: a TCP flow is answered as if connected and then ends without data, and a UDP packet is dropped. The client cannot tell the two apart, and katana writes no log line that names the rule or the user.
  • Audit comes before routing. No [node.route] rule can exempt a domain an audit rule matches: the audit rule is tested first and blocks the flow, even when a later rule would send it to direct.

A UDP association has no single destination, because every datagram carries its own address. katana therefore routes each outgoing packet on its own, audit rules first. A packet to a matching domain is dropped, and the association stays open for its other destinations.

Each poll runs these steps in order:

  1. Fetch the node config and the user list.
  2. Refresh the rules, unless disable_get_rule is true: read the local file and collect the panel’s rules as described above.
  3. Apply the node config, the users and the rules.
  4. Report traffic.

A new rule therefore takes effect within one update_periodic interval. At start-up, katana loads the rules before the node starts serving. When a hot reload changes a running node’s [node.api] table or a setting such as disable_sniffing, katana runs one poll at once instead of waiting for the timer, so that poll refreshes the rules too.

katana compares the new list with the one in force, by id and regex text, in order. If they are identical, it keeps the current set. Any difference, including a change of order, replaces the whole set at once. New rules apply to new flows, and no connection is closed: TCP flows that are already open are not re-checked, and each later UDP packet is checked against the new set.

A Hysteria 2 node on Xboard / V2board whose [node.hysteria].port is set is described from the config file, and katana never fetches the panel’s node config for it, so the node receives no panel routes. Only the local file applies.

To point a running node at another file, change rule_list_path and save the config. The hot reload builds the node a new panel client and runs one poll at once, and that poll’s refresh reads the new file. New flows are checked against the new rules right away, and the edit drops no open connections. Edits to the contents of the file the node already points at need no reload: the next refresh reads them.

katana logs rule problems at WARN and keeps running. 1 stands for the node’s node_id.

Log line Cause and fix
cannot read rule_list_path /etc/katana/rules.txt: No such file or directory (os error 2) The file is missing or unreadable by the katana user. The node runs with the panel’s rules only until a later refresh can read it.
invalid local rule "(foo": regex parse error: … One line of the local file does not compile. That line is skipped; fix it and it loads on the next refresh.
invalid block rule ["(foo", "bar"]: regex parse error: … An Xboard / V2board block route whose joined match list does not compile. The whole route is skipped.
audit rule 3 skipped: … The rules of one id could not be compiled together into a route. That id is left out of the route table.

A regex parse error spans several lines, with a caret under the problem:

invalid local rule "(foo": regex parse error:
(foo
^
error: unclosed group

A rule never matches.

  • The client may be connecting by IP address, with no name katana can sniff, or sniffing may be off. Audit rules match domain names only; use a routing cidr or geoip rule for addresses.
  • Check the case: names are lowercased before the test, so write the pattern in lower case.
  • Check the anchors against the exact host string: ^blocked\.example\.com$ matches neither www.blocked.example.com nor blocked.example.com..
  • Look for a WARN line about the rule, and make sure disable_get_rule is not true.

Too much is blocked.

  • Look for an overly broad pattern, such as an unanchored short pattern or a local rule such as . or .*.
  • Comment out local rules one at a time; each edit takes effect on the next refresh.