wol

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wol 实现网络唤醒模块:向局域网发送魔术包唤醒设备,并可通过 TCP 端口探测设备是否在线; 远程关机有三种方式:经 Web 终端的 SSH 连接执行命令、由设备上运行的客户端执行(客户端主动连接, 还支持重启与睡眠)、向设备或其他服务发送 HTTP 请求;并可接入巴法云,用小爱同学、小度等语音助手开关设备。

Index

Constants

View Source
const (
	ActionShutdown = "shutdown"
	ActionReboot   = "reboot"
	ActionSleep    = "sleep"
)

电源操作:关机三种方式都支持;重启与睡眠只有客户端方式支持(其他方式只配置了一条关机命令或请求)

View Source
const (
	ModeSSH   = "ssh"
	ModeAgent = "agent"
	ModeHTTP  = "http"
)

远程关机方式

View Source
const ID = "wol"

ID 是模块标识。

Variables

This section is empty.

Functions

func MagicPacket

func MagicPacket(mac string) ([]byte, error)

MagicPacket 生成魔术包:6 字节 0xFF 后跟 16 次 MAC。

func Wake

func Wake(d Device) (int, error)

Wake 发送魔术包,返回实际发送的目标数量。

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 是一台可唤醒的设备。

func (*Device) Validate

func (d *Device) Validate() error

Validate 校验设备配置。

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 New

func New(deps ...Deps) *Module

New 创建模块。deps 可省略(测试或未编译 Web 终端时)。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) ImportLucky

func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)

ImportLucky 实现 luckyimport.Importer。

func (*Module) Init

func (m *Module) Init(env module.Env) error

Init 实现 module.Module。

func (*Module) PublicAuth added in v0.6.6

func (m *Module) PublicAuth(r *http.Request) bool

PublicAuth 实现 module.PublicRouter:请求带有某台设备的有效客户端凭据。

func (*Module) PublicRoutes added in v0.6.6

func (m *Module) PublicRoutes(r module.Router)

PublicRoutes 实现 module.PublicRouter:客户端轮询、确认与脚本下载。

func (*Module) Routes

func (m *Module) Routes(r module.Router)

Routes 实现 module.Module。

func (*Module) Start

func (m *Module) Start(ctx context.Context) error

Start 实现 module.Module:启动巴法云连接(未开启时等待设置变化)。

func (*Module) Stop

func (m *Module) Stop() error

Stop 实现 module.Module。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type SSHTarget added in v0.6.6

type SSHTarget struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	Host string `json:"host"`
	// Trusted 表示已记录主机密钥,可以无人值守地执行命令
	Trusted bool `json:"trusted"`
}

SSHTarget 是可用于远程关机的 Web 终端 SSH 连接。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL