> ## Documentation Index
> Fetch the complete documentation index at: https://docs.plato.ae/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTPS And Mesh

> Caddy reverse proxy and PeerJS mesh signaling.

# HTTPS And Mesh

The PC printer driver can expose HTTPS through Caddy and PeerJS signaling through `/mesh`.

## HTTPS Conditions

HTTPS starts only when all of these are true:

* `features.httpsEnabled` is `true`.
* `caddy.enabled` is `true`.
* The runtime platform is Windows.
* Certificates exist in `assets/certs`.
* Caddy can be installed or reused.

If HTTPS setup fails, the driver logs the error and continues with HTTP.

## Certificate Files

Required files:

```txt theme={null}
assets/certs/ca.crt
assets/certs/server.crt
assets/certs/server.key
```

The driver copies them into the Caddy working folder:

```txt theme={null}
assets/caddy/ca.crt
assets/caddy/server.crt
assets/caddy/server.key
```

The CA certificate can be downloaded from:

```txt theme={null}
http://<driver-ip>:8080/ca.crt
```

## Caddy Install

Caddy setup uses this order:

1. Reuse `assets/caddy/bin/caddy.exe` if already installed with `.installed`.
2. Copy bundled `caddy.exe` from known local paths.
3. Download Caddy from GitHub release URL.

The configured Caddy version defaults to `2.6.4`.

## Generated Caddyfile

The driver writes a Caddyfile like:

```txt theme={null}
{
  auto_https disable_redirects
}

plato-printer.local {
  reverse_proxy localhost:8080

  handle /mesh/* {
    reverse_proxy localhost:8080 {
      header_up Connection {http.request.header.Connection}
      header_up Upgrade {http.request.header.Upgrade}
    }
  }

  tls "assets/caddy/server.crt" "assets/caddy/server.key"
}
```

If `caddy.httpsPort` is not `443`, the site address includes the port.

## Caddy Runtime

Caddy runs as a child process with:

* `XDG_DATA_HOME` set to `assets/caddy/data`
* `XDG_CONFIG_HOME` set to `assets/caddy/config`
* working directory set to `assets/caddy`

If Caddy exits unexpectedly, the driver retries up to 5 times with a `2000ms` delay.

## Mesh Signaling

When `features.meshEnabled` is true, the driver mounts PeerJS at:

```txt theme={null}
ws://<driver-host>:8080/mesh
wss://plato-printer.local/mesh
```

`GET /mesh-status` returns:

* whether mesh is enabled
* mesh path
* port
* connected peer IDs
* peer count

Mesh is signaling only. Print jobs still use the HTTP endpoints.

## Common HTTPS Problems

| Symptom                                        | Likely Cause                | Fix                                                 |
| ---------------------------------------------- | --------------------------- | --------------------------------------------------- |
| Browser blocks HTTPS driver URL                | CA certificate not trusted  | Install `ca.crt` on the POS device.                 |
| `https://plato-printer.local` does not resolve | DNS/hosts missing           | Add local DNS or hosts entry to driver IP.          |
| Port 443 unavailable                           | Another service is using it | Stop the other service or change `caddy.httpsPort`. |
| HTTPS skipped on macOS/Linux                   | Code is Windows-only        | Use HTTP in development or test Caddy separately.   |
| Windows 7 startup error                        | Caddy version not `2.6.4`   | Set `caddy.version` back to `2.6.4`.                |
