The last part of the fifth-protocol series.
These are the commands from the day, grouped by where they run. Everything in angle
brackets is a placeholder: <laptop-lan-ip>, <server>, <org>/<repo>. The real
values are an office's and an account's, and they are not mine to publish.
Just want the commands to copy? The command sheet has every network and PowerShell command from the day, one per block, each with a copy button.
Two rules apply to every section. Read before you write. And when a command can change something, write down how to undo it before you run it.
The whole list, one line each
| Command | Where | Why |
|---|---|---|
netstat -an | findstr | office PC | is anything else connected to the terminal right now |
Get-Process | office PC | which attendance program holds it |
taskkill /im <name>* | office PC | end the hidden setup window, keep the reading service |
curl.exe -o | office PC | fetch a build from the laptop; .exe, because curl is an alias |
Get-FileHash | office PC | the file is the one that was built |
.\c.exe setup | office PC | open the window; with no arguments it prints help |
ipconfig getifaddr, ping, nc -z | laptop | am I on the same network, and does port 5005 listen |
| probe program | laptop | talk to the terminal with three read requests only |
python3 -m http.server --bind | laptop | serve a build on the office network for minutes |
go vet, go test, gofmt -l | repository | the code builds, passes and is formatted, for Windows too |
revert check (cp, git show, cmp) | repository | the test fails without its fix |
devsim fk | repository | pretend terminals as separate programs, over real sockets |
go build with -ldflags -X | laptop | a Windows file identical in version, server and key |
git worktree add | repository | one folder per piece of work |
runner config.sh --ephemeral, run.sh | Mac | a runner for exactly one job |
gh workflow run, gh run watch, gh run cancel | release | start, follow and stop the signed build |
git merge-base --is-ancestor loop | release | build the moment the fix is on main, not before |
git log main..staging, gh pr create | release | see and open what goes to production; a person merges |
gh run download, shasum -c, grep -a, unzip -p | release | the artifact is complete, versioned and keyed |
comm -23 on base64 strings | release | find the public key inside an official build |
| back up, upload, read back | release | the downloads store gets the new files, and they are checked there |
gh run rerun (deploy) | release | the deploy copies the downloads store to the server |
curl + shasum per served file | release | live means the served bytes match the built ones |
gh pr checks, gh run view --log-failed, jobs API | CI | which step failed, and was it a failure or a cancel |
artifacts API, awk group, DELETE loop | CI | what fills the storage quota, and free it |
| a read-only script on the server | server | a question to production, with no shell |
rsvg-convert | site | a picture for the case study, at the exact size |
The rest of the post is each of these with its flags and the mistake it avoids.
On the office PC (Windows)
Typed, not pasted, in PowerShell opened as administrator.
Who is talking to the terminal right now?
netstat -an | findstr <terminal-ip>
Lists every TCP connection to the terminal's address. ESTABLISHED on port 5005
means a program on this PC holds a connection now; TIME_WAIT means one recently
finished. These terminals serve one session at a time, so a second program
connected, such as the vendor's own attendance software, can make reads fail. On the
day it showed only the connector's own finished connections.
Which program is it?
Get-Process | Where-Object Name -match "att|time|connector" | Select-Object Name, Path
Finds attendance software by name and shows where it lives. Read only.
Close the old setup window, keep the service.
taskkill /IM <ConnectorWindow>* /F
The connector runs as a service plus a setup window with a different file name. A new copy opened while the old window is open only brings that window forward, so it has to go first. The wildcard matches the window's name and not the service's.
Fetch a file from a laptop on the same LAN.
cd \
curl.exe -o c.exe http://<laptop-lan-ip>:8765/c.exe
curl.exe, with the extension: in Windows PowerShell plain curl is an alias for
a different command. The short name is because it was typed by hand.
Check the file before running it.
(Get-FileHash .\c.exe).Hash
SHA-256 of the file, to compare with the hash printed where it was built.
Open it.
.\c.exe setup
Started from a terminal with no arguments, the connector prints its help. setup
opens the window a double-click would.
On a laptop on the office LAN
Can I reach the terminal at all?
ipconfig getifaddr en0 # macOS: my address on this network
ping -c 2 <terminal-ip>
nc -z -G 3 <terminal-ip> 5005 && echo "port open"
nc -z opens a TCP connection and closes it without sending anything: the
smallest possible test that the port listens. -G 3 gives up after three seconds.
Talk to it, read only. A throwaway Go program that can only send three requests:
if cmd != 0x80 && cmd != 0xB4 && cmd != 0xA4 { // hello, count, block
panic("read requests only")
}
One variant per run, a fresh connection per try, the reply head printed in hex. When the terminal stops answering, wait: it serves one session at a time, and a burst of probes from the laptop competes with the office PC's own reads every minute. A small retry loop with a 20-second sleep found the gap.
Serve a build to the PC for a few minutes.
python3 -m http.server 8765 --bind <laptop-lan-ip>
curl --noproxy '*' -s -o /dev/null -w "%{http_code} %{size_download}\n" \
http://<laptop-lan-ip>:8765/c.exe
--bind keeps it on the office address only. The curl checks it from the laptop
itself; --noproxy because a local proxy on the laptop answered 403 to its own LAN
address. Stop the server as soon as the install is confirmed.
In the repository
Build and test.
cd connector
go vet ./... && go test ./...
GOOS=windows GOARCH=amd64 go vet ./internal/<reader>/ . # does it still build for Windows
gofmt -l ./internal # anything unformatted
Run go test from the module's own folder. From the repository root it finds no
module and fails in a way that looks like a test failure.
Check a test by taking its fix out.
cp internal/<reader>/reader.go /tmp/reader.go.fixed
git show origin/main:connector/internal/<reader>/reader.go > internal/<reader>/reader.go
go test ./internal/<reader>/ # must fail
cp /tmp/reader.go.fixed internal/<reader>/reader.go
cmp /tmp/reader.go.fixed internal/<reader>/reader.go && go test ./internal/<reader>/
Restore from a copy, not with git checkout: checkout would throw away the fix if
it is not committed yet. cmp proves the restore is byte for byte. When a fix has
several parts, take each out on its own; one run that removes everything shows only
that something mattered.
Run the pretend terminals as separate programs.
go build -o /tmp/devsim ./cmd/devsim
/tmp/devsim fk --wide --listen 127.0.0.1:5005 --device-no 1 --every 5s --for 3m
/tmp/devsim fk --wide --listen 127.0.0.1:5006 --device-no 2 --every 7s --for 3m
A real socket between the reader and the terminal catches what a function call cannot: partial reads, an unread tail, a reply that arrives in pieces.
Build one Windows file on a laptop, the same as the official one.
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath \
-ldflags "-s -w -X main.version=<x.y.z> -X main.DefaultServer=https://<api-host> \
-X <module-path>/update.PublicKeyBase64=<release-public-key>" -o c.exe .
shasum -a 256 c.exe
No C compiler is needed for Windows, so a Mac builds it. The three -X values are
what make it the same as the official build: the version, the server it talks to,
and the key every later update is checked against.
Work beside the main checkout.
git worktree add ../<repo>-wt-<topic> -b fix/<topic> origin/staging
One folder per piece of work, each on its own branch, so a probe program or a half-finished fix never sits in the checkout someone else builds from.
The release
Wake a runner for one job, and start the build.
TOKEN=$(gh api -X POST repos/<org>/<repo>/actions/runners/registration-token -q .token)
./config.sh --unattended --url https://github.com/<org>/<repo> --token "$TOKEN" \
--ephemeral --labels connector-once --replace
nohup ./run.sh > runner.log 2>&1 &
gh workflow run build-<app>.yml --ref main -f version=<x.y.z> -f environment=prod -f runner=this-mac
gh run watch <run-id> --exit-status --interval 20
--ephemeral makes the runner take one job and leave. gh run watch --exit-status
returns non-zero if the run fails, so a script waiting on it can stop.
Wait for a merge someone else does, then build.
until git fetch -q origin main && git merge-base --is-ancestor <fix-commit> origin/main; do sleep 30; done
pgrep -f Runner.Listener >/dev/null || ./run.sh & # the one-job runner is listening
gh workflow run build-<app>.yml --ref main -f version=<x.y.z> -f environment=prod -f runner=this-mac
Merging to main is a person's decision, not a script's. merge-base --is-ancestor
is true once the fix's commit is part of main, whichever way it got there, so the
build starts the moment it can and not before.
Open the promotion, don't merge it.
git log --oneline --merges origin/main..origin/staging # exactly what would go to production
gh pr create --base main --head staging --title "Promote staging: <what>" --body "<the PRs it carries>"
The first line is the whole review: every merge on staging that main does not have yet. If something else is in there, it goes in the description.
Cancel a build you no longer want.
gh run cancel <run-id>
Used when the hello fix turned out not to be the fix: no point signing a build that does not work.
Check the artifact.
gh run download <run-id> -n <app>-<x.y.z>-prod -D ./release
(cd release && shasum -a 256 -c SHA256SUMS)
LC_ALL=C grep -a -c '<x.y.z>' release/<connector>-windows-amd64.exe
unzip -p release/<connector>-mac.zip "<Connector>.app/Contents/Info.plist" | grep -A1 ShortVersion
grep -a searches a binary as if it were text; the count says the version string is
inside. LC_ALL=C stops grep from choking on bytes that are not valid text.
Find a public key inside a binary.
b64() { LC_ALL=C grep -a -o -E '[A-Za-z0-9+/]{43}=' "$1" | sort -u; }
b64 official.exe > a.txt; b64 keyless.exe > b.txt
comm -23 a.txt b.txt # strings only the official build has
printf '%s' "<candidate>" | base64 -d | wc -c # 32 bytes = an Ed25519 public key
An Ed25519 public key in base64 is 44 characters ending in =. Comparing an
official build with one built without the key leaves the one string that is the key.
It is public by design; this only avoids typing it wrong.
Deploy, then compare what is served.
The new files are first backed up, uploaded to the downloads store and read back from it (no command shown; it depends on the host). Then:
gh run rerun <production-deploy-run-id>
curl -s https://<server>/downloads/<app>/SHA256SUMS | diff - release/SHA256SUMS
The deploy copies the downloads store onto the server; the diff says whether the
server now serves exactly the build's checksums.
Why is that check red?
gh pr checks <pr> | grep -v -E "pass|skipping"
gh run view <run-id> --log-failed | grep -E "all shards:|##\[error\]"
gh api repos/<org>/<repo>/actions/jobs/<job-id> --jq '[.steps[] | select(.conclusion != "success" and .conclusion != "skipped") | .name + "=" + .conclusion]'
The first lists only what is not green. The second finds the test totals and the
real error line in a long log. The third says which step failed and how:
cancelled is not failure.
What is using artifact storage?
gh api "repos/<org>/<repo>/actions/artifacts?per_page=100" --paginate \
-q '.artifacts[] | "\(.size_in_bytes) \(.name)"' |
awk '{k=$2; sub(/[-_][0-9a-f]{6,}.*$/, "", k); s[k]+=$1; n[k]++} END {for (k in s) printf "%8.1f MB %4d %s\n", s[k]/1e6, n[k], k}' |
sort -rn | head
Groups artifacts by name and sums their size. On the day one name held 1.3 of the 1.5 GB.
Free it, keeping the newest.
gh api "repos/<org>/<repo>/actions/artifacts?name=<app>-dist&per_page=100" --paginate \
-q '.artifacts[] | "\(.created_at) \(.id)"' | sort -r | tail -n +4 | cut -d' ' -f2 |
while read id; do gh api -X DELETE repos/<org>/<repo>/actions/artifacts/$id; done
Sorted newest first, the first three kept, the rest deleted. Release builds and the small per-test timing files were left alone: one is evidence, the other makes the next run faster. The quota is recounted hours later, not at once.
Read-only checks on the server
Some questions only the server could answer: what the office PC last reported for the terminal, the newest punches and when each arrived, which instructions had been sent to the PC. There was no shell on production to ask them from. They were asked as a short read-only script, run once on the server through the hosting provider's remote-command service, with its output read back.
Three rules made that safe:
- The script only reads. No save, no update, no delete in it. The two writes of the day, setting the terminal's address and asking for ten days of older punches, were each asked for and confirmed first, and were separate.
- Check which account you are in before anything else. On the laptop used that day, the default credentials belonged to a different account, where a write would have succeeded silently.
- Look up names, don't remember them. The server and the downloads store were found from configuration each time, not typed from memory.
The same pattern answered the clock question in part 9: punch time against arrival time, for every live punch since the fix.
Pictures for the case study
rsvg-convert -w 1280 -h 800 cover.svg -o public/projects/<name>.png
Renders an SVG at the exact size the case-study card wants. No design tool needed.
The commands are the easy part to copy. The part that made the day work is the order they were run in: reach the terminal, read before writing, change one thing at a time, check every fix by taking it out, and call a release live only when the bytes people download are the bytes that were built.