Skip to content

GatewayUse

WireGuard

Let Chan Desktop reach the proxy nodes through WireGuard, with keys your self-hosted gateway enrolls at sign-in.

WireGuard enrollment

A gateway you host can carry Chan Desktop's connections to its proxy nodes inside WireGuard. The gateway enrolls the desktop's key at sign-in, and the desktop runs the tunnels in its own process: no key to hand over, no VPN client to install. It is optional, with three parts:

PartWhereDoes
A WireGuard interface and the agentEach proxy nodeThe node terminates WireGuard itself, with a key of its own. The agent keeps the interface's peers equal to the gateway's enrolled keys.
The list of nodesidentity.envTells desktops each node's key and endpoint.
An address poolprofile.envGives each enrolled key its tunnel address.

No node forwards for another: a desktop runs one tunnel per node, carrying only that node's names. Sign-in at gw.example.com never needs a tunnel.

Turn it on

  1. Bring up the interface on the node

    Install the agent package, which brings in wireguard-tools, and bring the interface up:

    $sudo apt install ./chan-gateway-wireguard-agent_*.deb sudo install -d -m 700 /etc/wireguard sudo sh -c 'umask 077; wg genkey > /etc/wireguard/chan-tunnel.key' sudo ip link add wg0 type wireguard sudo wg set wg0 listen-port 51820 private-key /etc/wireguard/chan-tunnel.key sudo ip address add 10.7.0.1/24 dev wg0 sudo ip link set wg0 up sudo wg show wg0 public-key

    The last command prints the interface's public key, for the next step. Open UDP port 51820 to the desktops. The interface lasts until a reboot: make it part of the server's network configuration.

  2. Set the pool and list the nodes

    Add the pool to /etc/chan-gateway/profile.env:

    WIREGUARD_POOL_V4=10.7.0.0/24

    Add the list to /etc/chan-gateway/identity.env, with the key from step 1:

    WIREGUARD_INGRESSES=p1=<public key>@p1.proxy.example.com:51820 WIREGUARD_INGRESS_ADDRESSES=10.7.0.1 WIREGUARD_ALLOWED_IPS=10.7.0.1/32
    VariableHolds
    WIREGUARD_POOL_V4The pool enrolled desktops take their tunnel addresses from. Each key takes one address for the whole gateway, whatever the number of nodes, so a /24 holds 253 keys.
    WIREGUARD_INGRESSESOne id=public_key@endpoint entry per node, with ; between entries, written without quotes. The id is the node id you gave the setup script, p1. The endpoint is the name or address, and the UDP port, where desktops reach the interface.
    WIREGUARD_INGRESS_ADDRESSESThe tunnel address, where a node answers inside the tunnel. Every node uses the same one, each with a key of its own.
    WIREGUARD_ALLOWED_IPSWhat a desktop sends into a tunnel: the tunnel address alone.
  3. Point the agent at identity

    Fill in /etc/chan-gateway/wireguard-agent.env, with the token copied from identity.env:

    IDENTITY_URL=http://127.0.0.1:7004 WIREGUARD_AGENT_TOKEN=<WIREGUARD_AGENT_TOKEN from identity.env> WIREGUARD_AGENT_HOST_ID=p1 WIREGUARD_INTERFACE=wg0

    Restart profile and identity so they read the new lines, and start the agent:

    $sudo systemctl restart chan-gateway-profile chan-gateway-identity sudo systemctl enable --now chan-gateway-wireguard-agent
  4. Check it

    The discovery document, https://gw.example.com/.well-known/chan-gateway, has a wireguard entry: that is how desktops learn they can enroll. Once a desktop enrolls, as in Connect to a gateway, sudo wg show wg0 lists it as a peer within seconds.

Details and limits
  • p1.proxy.example.com needs a DNS record of its own, since the wildcard record does not cover the name itself.
  • The agent logs to its journal: journalctl -u chan-gateway-wireguard-agent -f.
  • After a reboot, once the interface is back, the agent puts the enrolled desktops back on it.
  • identity works out each node's names from its id: the node's name and every name under it, which are the addresses of the devservers on that node. The id must be the node's real id.
  • The three identity lines go together: identity does not start with only some of them.
  • identity also does not start while WIREGUARD_HOST_ID, WIREGUARD_ENDPOINT, WIREGUARD_PUBLIC_KEY or WIREGUARD_HOSTS is set. The list replaces all four.
  • The tunnel address must not be one the pool hands out. profile never hands out a pool's first two addresses, here 10.7.0.0 and 10.7.0.1; list any other tunnel address in WIREGUARD_POOL_RESERVED. Nothing checks this for you.
  • WIREGUARD_PERSISTENT_KEEPALIVE in identity.env is optional, in seconds: 25 by default, 0 for none. Set without the three lines, it stops identity from starting.
  • In a file a shell sources, put the list in single quotes: a shell ends an assignment at the first ;.
  • An IPv6 pool is WIREGUARD_POOL_V6 in profile.env, with the IPv6 tunnel address and its /128 added to the two identity lines.
  • The edge configuration listens on every address, the tunnel address included. An edge that names its listen addresses needs this one added.

With the setup script

On a new server, install the agent package and bring the interface up before you run configure.sh, then answer:

QuestionAnswer
Kernel WireGuard interface for the agentEnter, for wg0
WireGuard ingressesThe list: p1=<public key>@p1.proxy.example.com:51820
WireGuard address poolEnter, for 10.7.0.0/24

The script takes the pool's first address, 10.7.0.1, as the tunnel address, and writes the four lines above and the agent's file.

Details and limits
  • Run over a gateway that enrolls, the script first shows the list the old files hold, as a line to paste at WireGuard ingresses. An empty answer there turns enrollment off.
  • WireGuard address pool offers the pool the old files hold; Enter keeps it. With another pool, enrolled desktops keep their old addresses, and every node's interface needs the new one.
  • It names each WireGuard setting it does not carry over, such as an IPv6 pool or the keepalive. Those stay in the backup it keeps of each file.
  • The script asks no IPv6 question: an IPv6 pool stays a hand edit.

When a key leaves

On your self-hosted gateway, an enrolled key lives as long as the sign-in token that enrolled it. When the token is revoked or expires, or the account is blocked, the key leaves every node at its agent's next check, every five seconds by default, and the desktop's tunnels stop carrying traffic.

Details and limits
  • identity refuses an agent whose WIREGUARD_AGENT_HOST_ID is not an id in its list. That agent keeps the keys it has, revoked ones included, until the id is in the list or the interface is taken down.
  • Both sides log a warning about it: the agent in its own journal, and identity, naming the id, in journalctl -u chan-gateway-identity.
  • profile keeps a denied key's address for five minutes by default, WIREGUARD_RELEASE_GRACE_MINUTES in profile.env, before handing it out again.
  • The agent changes only the peers it added. A peer you add to the interface by hand stays.

More nodes

Give each proxy node an interface and an agent of its own:

SettingOn each node
Private keyIts own
Tunnel addressThe same, 10.7.0.1
WIREGUARD_AGENT_HOST_IDThe node's own id
WIREGUARD_AGENT_TOKENThe same, from identity.env
IDENTITY_URLidentity's internal listener, as that node's proxy reaches it

Add each node's entry to WIREGUARD_INGRESSES and restart identity; profile does not change. A desktop follows the list at its next connect, with no new sign-in. To retire a node, remove its entry, stop its agent, and take its interface down.

Details and limits
  • Every node accepts every enrolled key.
  • A WireGuard file written by hand, as in the gateway guide, remains the way for a gateway that does not enroll, and for a devserver's own tunnel.
  • An enrolled desktop keeps its key in one file of its own and writes no WireGuard file; its tunnels come from the gateway's list.

Next