Part 3 of the fifth-protocol series. The earlier readers post goes deep on four brands. This one is the map: every kind of terminal the connector handles after the site visit, on one page, and where the new one fits.
- Found by
- TCP knock on 5005, then its hello must answer
- Proves it is the right terminal by
- no serial on the wire: the network ID and address stand in
- What is read
- count, log blocks, a name per staff number
- Never sent
- four commands exist in the package; anything else is refused before the wire
One contract
Every terminal the PC asks is behind one small Go interface:
type Reader interface {
Info(ctx context.Context) (Info, error) // alive; how many punches; no log walk
Punches(ctx context.Context, since, until time.Time) ([]Punch, error) // (since, until]
Users(ctx context.Context) ([]User, error)
Close() error
}
A registry maps a family name to a constructor. The runner does not know which protocol it is talking to: it opens a reader for a terminal, reads from where it last stopped, and closes it. Optional abilities, such as reading or setting a clock, are separate interfaces found with a type check, so a family that cannot do them simply does not have them.
Adding the FK family was one new package that registers itself:
func init() {
machine.Register("fk", func(t machine.Target) (machine.Reader, error) { return New(t), nil })
}
and one line on the platform's side allowing the family name, so a terminal of that kind goes on its PC's reading list.
The six
| Family | On the wire | Who talks first | How it is found | How it proves which terminal it is |
|---|---|---|---|---|
| ZKTeco | binary protocol, TCP 4370 | the PC | a TCP knock on 4370, then the MAC's vendor prefix | says its serial before any read |
| ZKTeco push | ADMS over HTTP, to the PC | the terminal | it calls in with its serial | its serial in every request |
| Hikvision | ISAPI, HTTP with digest | the PC | SADP multicast probe | its serial from deviceInfo |
| Dahua | CGI, HTTP with digest | the PC | DHIP multicast probe | its serial from magicBox |
| Sintech | 32-byte UDP datagrams | the terminal | its keypad menu points it at the PC | its serial in every datagram |
| FK family | binary protocol, TCP 5005 | the PC | a TCP knock on 5005, then its hello must answer | none on the wire: network ID and address |
The two that talk first are not readers. The connector relays them: it receives what they send, keeps it on disk, and passes it on. It does not instruct them.
Finding a terminal that does not announce itself
Hikvision and Dahua answer a multicast question on the local network. The binary protocols answer nothing until spoken to, so the search knocks: it opens a TCP connection to each likely port on each address and closes it, sending nothing.
A port that accepts a connection is not proof of a terminal. Port 5005 in particular is a common developer port. So for the FK family the knock is followed by the protocol's hello, a read, and an address is listed as a terminal only if the hello answers in that protocol. A terminal with a communication password answers the hello with "no", which is still an answer in the protocol: it is listed, and marked as having a password.
When nothing answers a probe, the network ID can still help. The first half of a MAC address names the maker of the network chip. That is a hint, never an identity: several terminal brands use the same chip makers, and this terminal used a locally administered MAC that names no maker at all. Finding machines that do not announce themselves has the full search.
Proving it is the right terminal
Reading the wrong terminal is worse than reading none: punches land on the wrong office's attendance. So every reader starts by checking the terminal is the one on the platform's list. Four families say their serial; the connector compares it before reading anything.
The FK family has no serial request in its protocol. It is known instead by its network ID, read from the PC's address table, and by the address it was set up at. When the terminal moves, the search accepts it at a new address only if its network ID or its newest punch matches the one on record.
Read-only by construction
The FK package can send four requests: hello, record count, a block of the log, and a name for a staff number. There is no constant in the package for anything else, and the function that sends requests refuses any command not on that list before it reaches the network:
var sendable = map[byte]bool{cmdPing: true, cmdCount: true, cmdBlock: true, cmdName: true}
func (r *Reader) request(ctx context.Context, cmd byte, args [10]byte, more int) (...) {
if !sendable[cmd] {
return 0, nil, fmt.Errorf("command %#x is not one this connector sends", cmd)
}
...
}
The ZKTeco package is built the same way. In both families there are commands that clear the attendance log, and a reader that cannot express them cannot send them by mistake: not through a bug, not through a typo, not through a well-meaning "try this" on site. A test checks that only those requests ever reach the test terminal.
The HTTP families can set a clock, and only when someone asks. Nothing else is written to any terminal.
What each one costs to read
| Family | A read asks for | Filter by time on the terminal? |
|---|---|---|
| ZKTeco | the whole log | no |
| Hikvision | events in a time window, page by page | yes |
| Dahua | records in a time window | yes |
| FK family | the whole log, in 1,024-byte blocks | no |
The two binary families send everything every time and the connector filters. For the FK terminal on the day, that was about 16,000 records of 20 bytes, roughly 330 KB, in about twenty seconds over the LAN. Fine once a minute for one terminal; the code notes the upgrade path, reading from the end backwards, for when a full terminal proves slow.
The rest of the connector
Everything after the reader is shared by all six:
- A spool on disk. Punches are written before they are sent, and removed only when the platform has them. An outage on either side is a delay, not a loss.
- A reading position per terminal. The first read of a terminal starts from now; older punches are an explicit request, uploaded day by day.
- A machine list from the platform, every few minutes: which terminals to read, at which address, port and device number.
- Outbound HTTPS only. Nothing on the office network is opened to the internet.
The architecture post has all of it. The point of this map is smaller: a sixth kind of terminal was one new package, one name on the platform's list, and the same rule as the others. It cannot write.