Skip to content

Deploying on a server behind a Cloudflare Tunnel

Version 0.2, 2026-10-05
Audience The owner, with the developer for the Android part
Result BankSync running in Docker on the server, reachable by the phone app through a Cloudflare Tunnel, with no router ports open

This uses a pattern that suits small apps on a server: the stock .NET runtime image with the published app mounted into it (nothing to build), a promote-and-roll-back entrypoint, and cloudflared on a fixed address.

1. What you are deploying

flowchart LR
    Phone["Android app<br/>(Keystore keys)"] -- "HTTPS + signed request<br/>sealed answers" --> CF["Cloudflare edge<br/>ends TLS"]
    Browser["Your browser<br/>(home network)"] -- "http, password + code" --> App
    CF -- "tunnel (outbound only)" --> CD["cloudflared<br/>172.29.0.3"]
    CD -- "http" --> App["banksync<br/>:8080"]
    App --> DB[("encrypted<br/>SQLite")]
    App -- "RS256 JWT" --> EB["Enable Banking"]
    EB --> Nordea["Nordea + MitID"]

What protects what:

Layer Protects Mechanism
Cloudflare edge The internet side TLS 1.2/1.3, DDoS protection, optional Access gate
Tunnel The path to the server cloudflared dials out; nothing is listening on your router
Surface list What a stranger can even ask for Through the tunnel only /api/, /callback and /healthz exist
Signed requests Who may use the API Each request carries a token signed by a key in the phone's secure hardware, valid for one request
Sealed answers What Cloudflare can read The phone's answers are encrypted to a key only the phone holds, so Cloudflare carries ciphertext
Password + one-time code The web pages PBKDF2 (600k iterations), time-based code, lockout
Encrypted database A copied disk or backup SQLCipher (AES-256), key kept apart from the data

Why the sealed answers matter

A Cloudflare Tunnel ends TLS at Cloudflare. Without the sealing, Cloudflare could read every response that passes, including your transactions. With it, the phone's data crosses Cloudflare as ciphertext. The web pages are different: if you serve them through the tunnel, Cloudflare can read those. The compose file therefore keeps them off the tunnel by default and uses them on the home network only.

2. Before you start

You need:

  • The production application at Enable Banking, with your Nordea accounts linked, following the owner setup checklist. Its redirect URL must be https://<your hostname>/callback.
  • A hostname in a domain you control on Cloudflare, for example bank.<your-domain>. Use a hostname of its own, not one that another app already uses.
  • A server running Docker, and a share on it where Docker projects live. A Docker web interface is convenient but not required.
  • On your PC: the .NET 10 SDK and an ssh client.
  • An authenticator app on your phone (any app that supports a time-based setup key).

3. Create the secrets

On your PC, in a folder outside the repository. Nothing in this step is ever committed.

The Enable Banking private key. Register the matching certificate in the control panel (checklist, step 3).

$env:Path += ";C:\Program Files\Git\usr\bin"
openssl genrsa -out eb_private_key.pem 4096
openssl req -new -x509 -days 3650 -key eb_private_key.pem -out eb_public.crt -subj "/CN=banksync"

The database key. Random, and kept apart from the data.

dotnet run --project samples/BankSync.DemoApp -- --new-key

Save the output to database.key (one line, no spaces). If this key is lost the database cannot be opened. The bank data can be fetched again after a new consent, but your categories and notes would be gone. Keep a copy in your password manager.

Your password hash and the one-time-code key.

dotnet run --project samples/BankSync.DemoApp -- --hash-password
dotnet run --project samples/BankSync.DemoApp -- --new-totp owner

The first asks for a password of at least 12 characters, twice, and prints a line for APP_PASSWORD_HASH. The second prints a key for APP_TOTP_SECRET; add it to the authenticator app with "enter a setup key" (time based, six digits). Test the code in the app before you continue.

4. Set up Cloudflare

  1. Zero Trust → Networks → Tunnels → Create a tunnel (Cloudflared). Name it banksync. Copy the token.
  2. Add a public hostname: bank.<your-domain>, service HTTP, URL banksync:8080. The name resolves because cloudflared and the app share a Docker network.
  3. Check the setting that bites: SSL/TLS mode is irrelevant for a tunnel, but make sure Always Use HTTPS is on.
  4. Cache: add a Cache Rule for the hostname with Bypass cache. Everything the app returns is private.
  5. Configuration Rule for the hostname: switch off Rocket Loader, Auto Minify, Email Obfuscation, Mirage. They rewrite HTML and the sign-in page is plain HTML, so this matters only if you serve the pages through the tunnel, but it costs nothing.
  6. Optional second gate, recommended: a Zero Trust Access application on the hostname with a Service Auth policy for a service token named banksync-phone, plus Bypass policies for the exact paths /callback and /healthz (the bank's redirect and the health check cannot present a token). The phone sends the token's two headers on every request; the simulator and the client library support this. This puts Cloudflare's check in front of the app's own, so an unauthenticated stranger is stopped before reaching it.

Bypass policies are exceptions, keep them narrow

Only /callback and /healthz, and only those exact paths. Never a bypass on /api/: that is what the service token is for. /callback is safe to leave open because it only does anything for a one-time code the bank just issued, tied to a random state the app created.

5. Put the files on the server

Create the project folder /srv/docker/banksync (the share path on your PC is something like \\server\docker\banksync), and inside it:

secrets/eb_private_key.pem      from step 3
secrets/database.key            from step 3
.env                            from deploy/.env.example, filled in

In .env, PUBLIC_BASE_URL is https://bank.<your-domain> and the tunnel token is the one from step 4. Restrict the permissions of .env and secrets/ to your admin account.

Then create a project in your Docker manager: name banksync (exactly this, the deploy script relies on it), path /srv/docker/banksync, source "use the existing docker-compose.yml". Do not start it yet. Without a Docker manager, the deploy script's SSH step does the same from the command line.

6. Deploy

From the repository on your PC:

.\deploy\Deploy-ToServer.ps1 -ServerPath \\server\docker\banksync -BaseUrl http://<server-address>:8082 -ServerHost <server-address> -ServerUser <account on the server>

It publishes for Linux, copies the build and the compose file (never .env, secrets/ or data/), recreates the container over SSH after asking, and waits for /healthz. It warns if .env or a secret file is missing.

If the container does not become healthy it rolls back to the previous build by itself. The first deploy has no previous build, so a missing secret shows up as a clear message in the container log instead.

7. Check it

Check Expected
curl -I https://bank.<your-domain>/healthz 200 (this is the moment HTTPS works)
curl -I https://bank.<your-domain>/ 404, because the pages are not on the tunnel
curl -I https://bank.<your-domain>/api/status 401 (or Cloudflare's own refusal if you set the Access gate)
curl -sI https://bank.<your-domain>/healthz \| findstr /i cf-cache-status BYPASS or DYNAMIC, never HIT
http://<server-address>:8082/ in your browser the sign-in page, asking for a code
The banksync container log Healthy. and a warning that AllowInsecureLan is on
  1. Open http://<server-address>:8082/ on your home network, sign in with password and code.
  2. Connect / renew with MitID. Approve in the MitID app. The bank sends the browser to https://bank.<your-domain>/callback, which answers "Bank connected".
  3. Open the overview again. The accounts appear and the history loads in the background. The first hour is the window in which the bank gives the full history, so do this when you can leave it alone.

9. Pair a phone

Until the Android app exists, use the simulator to prove the whole path, including Cloudflare:

  1. On the overview page choose Phones → Pair a phone. A code like 95VR-8FMF appears for ten minutes.
  2. On your PC:
dotnet run --project samples/BankSync.PhoneSimulator -- pair https://bank.<your-domain> 95VR-8FMF "My PC"
dotnet run --project samples/BankSync.PhoneSimulator -- status
dotnet run --project samples/BankSync.PhoneSimulator -- get /api/transactions take=5

If you set the Access gate, first set CF_ACCESS_CLIENT_ID and CF_ACCESS_CLIENT_SECRET. The answers print as readable JSON on your PC, while Cloudflare only ever carried the sealed form.

The Android app does the same with keys in the Keystore: see the Android API.

10. Everyday operation

Task How
Update Run the deploy script again. The previous build is kept for rollback.
Renew the bank consent (every ≤180 days) The overview shows a banner 14 days ahead. Connect / renew, approve with MitID.
Lost or stolen phone Phones → Revoke. It stops at once; the key on the phone is useless without the server accepting it.
Back up Copy data/ (encrypted) and, separately, secrets/. A backup of data/ alone is unreadable by design.
Change a password or the code key New hash or key into .env, recreate the container. Existing sessions end.
Rotate the tunnel token Cloudflare → tunnel → refresh token, update .env, recreate.
Rotate the Enable Banking key Register a new certificate in the control panel, replace eb_private_key.pem, recreate.
Stop everything Stop the project in your Docker manager. Or Disconnect on the overview to revoke the bank consent too.

11. When it does not work

Symptom Likely cause
The container exits at once The log names the missing piece: a secret file, App__Auth__*, the second-factor key or App__Proxy__*. The app refuses to run half-secured.
curl to the hostname gives 1033 or 502 The tunnel is not connected, or the public hostname points at the wrong service name. It must be banksync:8080.
Sign-in page loads but nothing happens You are on http:// without App__AllowInsecureLan, so the browser drops the cookie.
Every visitor appears as the tunnel's address App__Proxy__KnownProxies__0 does not match the cloudflared address (172.29.0.3). Check the log for "Ignoring CF-Connecting-IP".
Pairing says the code is wrong Codes last ten minutes, work once, and are destroyed by five wrong tries. Ask for a new one.
The phone gets 401 Revoked, locked out by repeated failures (15 minutes), the Access gate refusing, or a phone clock more than a minute off.
After a deploy the old version is still running The container was restarted, not recreated. Recreate the project: stop and build it again in your Docker manager, or use the script's SSH step.
Bank consent fails with an address error The redirect URL at Enable Banking differs from PUBLIC_BASE_URL/callback by even one character.

12. Settings reference

Setting Default Meaning
App__Auth__Username, PasswordHash, TotpSecret required The app refuses to start without all three
App__Proxy__KnownProxies__0 none Exact address of the proxy container. Never a subnet
App__Proxy__PublicPaths__n all Path prefixes reachable through the proxy
App__Proxy__ClientIpHeader CF-Connecting-IP Where the proxy puts the real client address
App__AllowInsecureLan false Lets the web pages work over plain http on the home network
App__Api__SealResponses true Encrypt answers to phones end to end
App__DeviceAudience banksync The audience phone tokens must carry
BankSync__DatabaseKeyPath required File holding the database key

The library's own settings are in the library guide.