WinMx4u Web Chat — hosting kit

I updated Web Chat after a temporary gateway address expired. The local
gateway was still running, but the website could no longer reach it.
This kit lets the gateway computer connect outward to your HTTPS site.
The browser also gives brief gateway failures time to recover.

There may still be bugs. If you run into one, email log@rapsmx.com with
the time, browser, operating system and what you were trying to do.
Please leave out passwords, bridge keys and private messages.

Setup

1. Your website needs HTTPS, PHP 8.1 or newer with PDO SQLite, and a
   directory that PHP can write to outside the public document root.
   Upload the contents of webchat to a public /webchat/ folder.
2. Beside the document root, create a private winmx-gateway-private
   directory. In it, create bridge-origin.txt containing your website's
   final HTTPS origin (for example https://example.com, with no trailing
   slash). Use the origin after any www or other redirects.
3. Generate a different private 32-byte key for each site and save it
   as 64 lowercase hex characters in bridge.key. On Linux you can use
   openssl rand -hex 32. On Windows PowerShell you can generate it with:

   $b=New-Object byte[] 32
   $rng=[Security.Cryptography.RandomNumberGenerator]::Create()
   $rng.GetBytes($b); $rng.Dispose()
   [BitConverter]::ToString($b).Replace('-','').ToLowerInvariant() | Set-Content bridge.key -Encoding ascii

   Copy bridge.key into winmx-gateway-private and keep the local copy
   beside the bridge executable. Keep both copies out of web folders.
   Give PHP read access to the key and write access to the directory.
4. On an always-on Windows x64 or Linux x86-64/ARM64 computer, open
   the matching folder and start wpn-gateway.exe (Windows) or
   ./wpn-gateway (Linux). If your ZIP extractor removed the executable
   permissions, first run chmod +x wpn-gateway wpn-webchat-bridge.
   The default local address is 127.0.0.1:8080.
   Check http://127.0.0.1:8080/ before connecting the bridge.
5. Start a bridge for this website. Run the command from the folder
   containing your gateway, bridge and private bridge.key:

   .\wpn-webchat-bridge.exe -relay https://example.com/webchat/gateway-bridge.php
   ./wpn-webchat-bridge -relay https://example.com/webchat/gateway-bridge.php

   If the local gateway uses a different port, pass its loopback
   address with -gateway. Keep both programs running. No inbound
   router forwarding is needed for this bridge.
6. Visit https://example.com/webchat/gateway-bridge.php. "ready" means
   the website can reach the local gateway. Open /webchat/ from another
   device and check that it can load channels, join and leave a room.

Direct HTTPS hosting

If your gateway is already reachable through a stable HTTPS reverse
proxy, you can use the separate static site download instead. Set its
chat-gateway meta tag to the reachable gateway base URL and allow the
site's exact origin on WPN Gateway with -site-origin. The hosting kit
above is for sites whose gateway can only make outbound connections.
