# ArmLink Agent (Go)

The on-site connector that links **ALNET NetStation** virtual outputs to the
**ArmLink cloud**. A single self-contained `.exe` — no PowerShell, no .NET, no
dependencies. Runs as a Windows service, auto-starts on boot, restarts itself if
it ever crashes.

## How it works

- **Outbound only.** The agent only makes HTTPS requests to the cloud. No inbound
  ports are opened on the site — nothing to forward, nothing to expose.
- **Heartbeat** (every 30s): reads the state of every virtual output from ALNET
  and reports armed/disarmed to the cloud → the dashboard shows live status.
- **Poll** (every 3s): fetches pending arm/disarm commands, applies them via
  ALNET `SetOutput`, verifies the new state, and acks back to the cloud.
- Site keys are `site0..siteN` (= ALNET output number), matching the cloud.

## Local dashboard (LAN)

The agent also serves an on-site dashboard at **`http://<server>:8787`** (Hebrew,
RTL), reachable from the LAN. **Viewing status is open**; **arm/disarm requires the
admin password** you set during install (`--local-pass`). Status auto-refreshes
every 5s and actions go straight to ALNET (the cloud picks up the change on the
next heartbeat).

- Bind to LAN (default): `--local-bind 0.0.0.0`
- Localhost only: `--local-bind 127.0.0.1`
- Disable entirely: `--no-local`
- The password is stored only as a SHA-256 hash. Note: the LAN page is plain HTTP,
  so use it on a trusted network.

## Install (the easy way)

1. Unzip anywhere on the NetStation server (e.g. `C:\ArmLink`).
2. In the cloud UI open the **"סוכנים"** tab and copy the agent **token**.
3. **Double-click `install.bat`** → approve the admin prompt → paste the token,
   enter the ALNET user/password, press Enter for the host.
   It tests the connection and installs the 24/7 service.

That's it. The service is named **ArmLink Agent** (`ArmLinkAgent`).

> **ALNET user:** use a NetStation user that can see **all** outputs. A restricted
> user (like `ABC`) may only expose one output. Recommended: create a dedicated
> NetStation user with access to all outputs and nothing else.

## Manual commands

```
armlink-agent.exe config --token agt_xxx [--alnet-user admin] [--alnet-pass ...]
                         [--alnet-host 127.0.0.1] [--alnet-port 9003] [--url https://...]
armlink-agent.exe once        One heartbeat+poll cycle, printed to the console (test).
armlink-agent.exe install     Install + start the service (auto-start on boot).
armlink-agent.exe uninstall   Stop + remove the service.
armlink-agent.exe start|stop  Control the installed service.
armlink-agent.exe status      Show config + service state.
armlink-agent.exe run         Run in the foreground (debug).
```

## Files & locations

- Config: `C:\ProgramData\ArmLink\armlink-agent.json` (token + ALNET login; chmod 600)
- Log:    `C:\ProgramData\ArmLink\agent.log`

## Uninstall

Double-click `uninstall.bat` (or `armlink-agent.exe uninstall`).

## Build from source

Requires Go ≥ 1.22 on Windows:

```
go build -ldflags "-s -w" -o armlink-agent.exe .
```
