ekfa/docs/heartbeat.md

53 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UDP 心跳
`efka` 通过独立的 `efka_iot_heartbeat` 进程向 `iot` 发送 UDP 心跳。该心跳只表示主机存活,不依赖 TLS control channel 是否在线。
## 配置
TLS 和 UDP 共用同一个 `iot_server.host`
```erlang
{iot_server, [
{host, "localhost"},
{tls_port, 443},
{udp_port, 18080}
]},
{heartbeat, [
{interval, 5000}
]},
{auth, [
{uuid, "qbxmjyzrkpntfgswaevodhluicqzxplkm"},
{token, "zpxlkvmqwnbghytrujsdieofazxcvbnm"}
]}
```
- `tls_port` 用于 `efka_iot_client` 建立 TLS 连接。
- `udp_port` 用于 `efka_iot_heartbeat` 发送 UDP 心跳。
- `heartbeat.interval` 是发送间隔,单位毫秒,默认 5000。
- UDP 心跳和 TLS 鉴权复用 `auth.uuid``auth.token`
## 包格式
```erlang
<<
Version:8,
UuidLen:16,
UUID:UuidLen/binary,
Timestamp:64/unsigned-big,
Nonce:16/binary,
Mac:32/binary
>>
```
`Mac` 使用 HMAC-SHA256
```erlang
HeartbeatSecret = crypto:hash(sha256, Token),
Payload = <<Version:8, UuidLen:16, UUID:UuidLen/binary, Timestamp:64/unsigned-big, Nonce:16/binary>>,
Mac = crypto:mac(hmac, sha256, HeartbeatSecret, Payload)
```
其中 `Token``auth.token``iot` 侧注册 efka client 时保存同样的 `SHA256(Token)` 派生值,并用它校验心跳。