Documentation
¶
Overview ¶
Package wol 实现网络唤醒模块:向局域网发送魔术包唤醒设备,并可通过 TCP 端口探测设备是否在线; 远程关机有三种方式:经 Web 终端的 SSH 连接执行命令、由设备上运行的客户端执行(客户端主动连接, 还支持重启与睡眠)、向设备或其他服务发送 HTTP 请求;并可接入巴法云,用小爱同学、小度等语音助手开关设备。
Index ¶
- Constants
- func MagicPacket(mac string) ([]byte, error)
- func Wake(d Device) (int, error)
- type AgentInfo
- type BemfaSettings
- type BemfaStatus
- type Deps
- type Device
- type HTTPAction
- type Module
- func (m *Module) ID() string
- func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
- func (m *Module) Init(env module.Env) error
- func (m *Module) PublicAuth(r *http.Request) bool
- func (m *Module) PublicRoutes(r module.Router)
- func (m *Module) Routes(r module.Router)
- func (m *Module) Start(ctx context.Context) error
- func (m *Module) Stop() error
- func (m *Module) Title() string
- type SSHTarget
Constants ¶
const ( ActionShutdown = "shutdown" ActionReboot = "reboot" ActionSleep = "sleep" )
电源操作:关机三种方式都支持;重启与睡眠只有客户端方式支持(其他方式只配置了一条关机命令或请求)
const ( ModeSSH = "ssh" ModeAgent = "agent" ModeHTTP = "http" )
远程关机方式
const ID = "wol"
ID 是模块标识。
Variables ¶
This section is empty.
Functions ¶
func MagicPacket ¶
MagicPacket 生成魔术包:6 字节 0xFF 后跟 16 次 MAC。
Types ¶
type AgentInfo ¶ added in v0.6.6
type AgentInfo struct {
Online bool `json:"online"`
Host string `json:"host"`
OS string `json:"os"`
Version string `json:"version"`
// Addr 为客户端的来源地址
Addr string `json:"addr"`
// Since 为本次上线时间,LastSeen 为最近一次轮询的时间(Unix 毫秒),未知时为 0
Since int64 `json:"since"`
LastSeen int64 `json:"lastSeen"`
}
AgentInfo 是客户端的连接信息,供管理界面展示。
type BemfaSettings ¶ added in v0.6.6
type BemfaSettings struct {
Enable bool `json:"enable"`
// UID 为巴法云控制台中的用户私钥,作为 MQTT 客户端 ID,等同于账号凭据
UID string `json:"uid"`
}
BemfaSettings 是巴法云接入设置。
func (*BemfaSettings) Validate ¶ added in v0.6.6
func (s *BemfaSettings) Validate() error
Validate 校验巴法云设置。
type BemfaStatus ¶ added in v0.6.6
type BemfaStatus struct {
Connected bool `json:"connected"`
// Topics 为已订阅的主题数
Topics int `json:"topics"`
// Error 为最近一次连接失败的原因,连接成功后清空
Error string `json:"error,omitempty"`
// Since 为进入当前状态的时间(Unix 秒)
Since int64 `json:"since,omitempty"`
}
BemfaStatus 是巴法云连接状态。
type Deps ¶ added in v0.6.6
type Deps struct {
// SSHExec 经 SSH 连接执行一条命令并返回输出
SSHExec func(ctx context.Context, conn, command string) (string, error)
// SSHTargets 列出可选的 SSH 连接
SSHTargets func() []SSHTarget
}
Deps 是模块依赖的其他模块能力,经注入提供;Web 终端模块未编译时均为 nil,关机不可用。
type Device ¶
type Device struct {
Key string `json:"key"`
Name string `json:"name"`
// Group 为列表中的分组名称,只影响管理界面的展示,留空为未分组
Group string `json:"group"`
MACs []string `json:"macs"`
// BroadcastIPs 为空时向 255.255.255.255 及各网卡广播地址发送
BroadcastIPs []string `json:"broadcastIps"`
Port int `json:"port"`
Repeat int `json:"repeat"`
// CheckAddr 为 host:port,用于探测设备是否在线(例如 192.168.1.10:3389),留空不探测
CheckAddr string `json:"checkAddr"`
// ShutdownMode 为远程关机方式:ssh 经 Web 终端的 SSH 连接执行命令,agent 由设备上运行的客户端执行,
// http 发送 HTTP 请求;留空表示不支持关机。旧配置没有该字段、只填了 ShutdownConn 时视为 ssh
ShutdownMode string `json:"shutdownMode"`
// ShutdownConn 为 ssh 方式使用的 Web 终端 SSH 连接 key
ShutdownConn string `json:"shutdownConn"`
// ShutdownCommand 为 ssh 方式的关机命令,例如 Linux 的 shutdown -h now、Windows 的 shutdown /s /t 0
ShutdownCommand string `json:"shutdownCommand"`
// AgentToken 为 agent 方式的客户端凭据,由服务端生成,接口输出时脱敏;换成其他方式时清空,旧客户端随即失效
AgentToken string `json:"agentToken"`
// ShutdownHTTP 为 http 方式发送的请求
ShutdownHTTP HTTPAction `json:"shutdownHttp"`
// BemfaTopic 为巴法云主题,留空不接入。收到 on 时唤醒,收到 off 时关机(需配置关机)
BemfaTopic string `json:"bemfaTopic"`
Remark string `json:"remark"`
// Enable 为了复用通用接口保留,网络唤醒设备始终可用
Enable bool `json:"enable"`
}
Device 是一台可唤醒的设备。
type HTTPAction ¶ added in v0.6.6
type HTTPAction struct {
// Method 为请求方法,留空时有请求体用 POST、否则用 GET
Method string `json:"method"`
// URL 为请求地址,只支持 http 与 https
URL string `json:"url"`
// Headers 为附加请求头,每项为“名称: 值”,凭据(如 Authorization)放在这里,只读账号看到的值会被脱敏
Headers []string `json:"headers"`
// Body 为请求体,可为空
Body string `json:"body"`
// Insecure 为 true 时不校验 HTTPS 证书,用于局域网内使用自签名证书的设备
Insecure bool `json:"insecure"`
}
HTTPAction 是 http 关机方式发送的请求,例如调用 Home Assistant、智能插座或设备上关机工具的接口。
func (*HTTPAction) Validate ¶ added in v0.6.6
func (h *HTTPAction) Validate() error
Validate 校验并规范化请求设置。
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是网络唤醒模块。
func (*Module) ImportLucky ¶
func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)
ImportLucky 实现 luckyimport.Importer。
func (*Module) PublicAuth ¶ added in v0.6.6
PublicAuth 实现 module.PublicRouter:请求带有某台设备的有效客户端凭据。
func (*Module) PublicRoutes ¶ added in v0.6.6
PublicRoutes 实现 module.PublicRouter:客户端轮询、确认与脚本下载。