knock

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 33 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 knock 实现端口敲门模块:接收与 fwknop 兼容的单包授权(SPA)报文,认证通过后在限定时间内 为来源地址放行受保护的端口。

放行有两种方式,可同时使用:

  • 系统防火墙:规则开启“防火墙拦截”时,受保护端口在 nftables(内核没有 nf_tables 时用 iptables + ipset) 中默认丢弃,敲门成功的地址按 地址 + 协议 + 端口 写入带超时的集合元素。用于保护宿主机自己的 sshd 等服务;
  • Gately 自身:协议分流的路由、端口转发规则可以选择“需要敲门”,只转发持有该规则有效授权的来源地址 (见 internal/knockgate),不依赖防火墙权限。

报文经本模块监听的 UDP 端口(fwknop 默认 62201)发来,也可以经协议分流的 TCP 端口发来(fwknop 的 -P tcp)。 已建立的连接在授权到期后照常保持,与 fwknop 的语义一致。

Index

Constants

View Source
const (
	ReasonOS          = "os"
	ReasonMissing     = "missing"
	ReasonPermission  = "permission"
	ReasonUnsupported = "unsupported"
	ReasonError       = "error"
)

防火墙不可用的原因代码,由前端翻译

View Source
const (
	BackendNFT = "nftables"
	BackendIPT = "iptables"
)

防火墙后端

View Source
const ID = "knock"

ID 是模块标识。

Variables

This section is empty.

Functions

This section is empty.

Types

type Counters

type Counters struct {
	// Accepted 为认证通过并放行的报文数
	Accepted uint64 `json:"accepted"`
	// Rejected 为格式像敲门报文、但认证失败或被规则拒绝的报文数
	Rejected uint64 `json:"rejected"`
	// Ignored 为不是敲门报文、来源已被封禁或超出处理速率而直接丢弃的报文数
	Ignored uint64 `json:"ignored"`
}

Counters 是全部报文的处理结果统计。

type FirewallStatus

type FirewallStatus struct {
	// Needed 表示有启用的规则开启了防火墙拦截
	Needed bool `json:"needed"`
	// Active 表示拦截规则已写入系统防火墙
	Active bool `json:"active"`
	// Reason 为不可用原因代码,Error 为具体错误
	Reason  string `json:"reason,omitempty"`
	Error   string `json:"error,omitempty"`
	Backend string `json:"backend,omitempty"`
	Version string `json:"version,omitempty"`
	// NoIPv6 表示后端只拦截 IPv4(iptables 后端没有可用的 ip6tables),受保护端口经 IPv6 仍可直接访问
	NoIPv6 bool `json:"noIPv6,omitempty"`
	// Ports 为受保护端口
	Ports []string `json:"ports"`
	// Dropped 为被拦截的连接数,Allowed 为敲门放行后建立的连接数(规则重建后累计)
	Dropped   uint64    `json:"dropped"`
	Allowed   uint64    `json:"allowed"`
	CheckedAt time.Time `json:"checkedAt,omitzero"`
}

FirewallStatus 是防火墙拦截状态。

type GrantView

type GrantView struct {
	// Addr 为放行的地址;网页登录按设置放行整个 IPv6 网段时带前缀长度(如 2001:db8:1:2::/64)
	Addr     string    `json:"addr"`
	Rule     string    `json:"rule"`
	RuleName string    `json:"ruleName"`
	Until    time.Time `json:"until"`
	// Ports 为写入防火墙的放行端口;只用于协议分流、端口转发、Web 站点的授权为空
	Ports []string `json:"ports"`
	// Source 为授权来源:spa(敲门报文)、web(在 Web 站点的放行入口登录)
	Source string `json:"source"`
	// Identity 为敲门报文授权的身份名称(用共享密钥敲门时为空)
	Identity string `json:"identity,omitempty"`
	// User、Site、SiteName 为网页登录放行的用户与入口站点
	User     string `json:"user,omitempty"`
	Site     string `json:"site,omitempty"`
	SiteName string `json:"siteName,omitempty"`
}

GrantView 是一个有效授权。

type Identity added in v0.6.9

type Identity struct {
	// ID 为身份在规则内的标识,留空时自动分配
	ID     string `json:"id"`
	Name   string `json:"name"`
	Enable bool   `json:"enable"`
	// EncKey、HMACKey 含义与规则的同名字段相同,留空时自动生成
	EncKey  string `json:"encKey"`
	HMACKey string `json:"hmacKey"`
	Remark  string `json:"remark,omitempty"`
}

Identity 是规则中的一个身份(一个人或一台设备)及其密钥。

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module 是端口敲门模块。

func New

func New() *Module

New 创建模块。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) Init

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

Init 实现 module.Module。

func (*Module) Metrics

func (m *Module) Metrics() []module.Sample

Metrics 实现 module.Metricer。

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) Summary

func (m *Module) Summary() []module.SummaryItem

Summary 实现 module.Summarizer:启用的规则数、当前授权数,以及防火墙拦截与监听是否正常。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type Rule

type Rule struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	// Group 为列表中的分组名称,只影响展示
	Group  string `json:"group,omitempty"`
	Enable bool   `json:"enable"`
	// Ports 为敲门可以请求放行的端口(如 tcp/22),客户端 -A 参数中的端口必须全部在此列表中。
	// 留空时规则不接受敲门报文,只能在 Web 站点的放行入口登录取得授权(不开启防火墙拦截时才能留空)
	Ports []string `json:"ports"`
	// Firewall 为 true 时在系统防火墙中拦截这些端口,只放行敲门成功的地址;
	// 为 false 时不改动防火墙,只供协议分流路由、端口转发规则按敲门结果放行
	Firewall bool `json:"firewall"`
	// EncKey、HMACKey 为 base64 编码的加密密钥与 HMAC 密钥(对应 fwknop 的 KEY_BASE64、HMAC_KEY_BASE64),留空时自动生成
	EncKey  string `json:"encKey"`
	HMACKey string `json:"hmacKey"`
	// HMACType 为 HMAC 摘要算法:sha256(默认)、sha384、sha512
	HMACType string `json:"hmacType"`
	// Timeout 为放行时长(秒)
	Timeout int `json:"timeout"`
	// MaxTimeout 为客户端用 -f 请求的放行时长上限(秒),0 表示忽略客户端请求的时长
	MaxTimeout int `json:"maxTimeout"`
	// User 非空时只接受报文中用户名为该值的敲门(fwknop 默认发送本机登录名,可用 -U 指定)
	User string `json:"user"`
	// RequireAddr 为 true 时拒绝不带地址的报文(fwknop -s),客户端必须用 -a 或 -R 写明要放行的地址,
	// 防止中途截获报文的人抢先从自己的地址重放
	RequireAddr bool `json:"requireAddr"`
	// Identities 为规则的身份:每个身份有自己的一组密钥,敲门时按密钥识别是谁,日志、事件与授权列表显示身份名称;
	// 停用、删除身份或更换其密钥只撤销该身份的放行,其他人不受影响。身份使用规则的 HMAC 算法、端口与放行时长
	Identities []Identity `json:"identities,omitempty"`
	// SharedKeyOff 为 true 时规则自身的密钥(共享密钥)不再接受敲门,只接受各身份的密钥
	SharedKeyOff bool `json:"sharedKeyOff,omitempty"`
	// SingleAddr 为 true 时每个身份(共享密钥算一个)同时只放行一个地址:从新地址敲门成功后,该身份此前经敲门报文
	// 放行的其他地址立即撤销,防止同一份密钥被多人同时使用。已建立的连接不受影响
	SingleAddr bool   `json:"singleAddr,omitempty"`
	Remark     string `json:"remark"`
}

Rule 是一条敲门规则:一组共享密钥及其可以放行的端口。

func (*Rule) Validate

func (r *Rule) Validate() error

Validate 校验并规范化规则;密钥留空时生成新密钥。

type RuleView

type RuleView struct {
	Rule
	Stats ruleStats `json:"stats"`
	// IdentityStats 为各身份的运行统计:身份 ID -> 统计,没有记录的身份不出现
	IdentityStats map[string]ruleStats `json:"identityStats"`
	// UsedBy 为引用该规则的端口转发、协议分流、STUN 规则与路由
	UsedBy []string `json:"usedBy"`
}

RuleView 是规则的接口输出:密钥脱敏,附加运行统计。

type Settings

type Settings struct {
	// Port 为接收敲门报文的 UDP 端口(fwknop 客户端默认 62201),0 表示不监听 UDP,只接收经协议分流发来的报文
	Port int `json:"port"`
	// AutoFirewall 为 true 时在系统防火墙(OpenWrt fw4、firewalld 等)放行敲门端口;
	// 防火墙拦截生效后也放行受保护端口,使是否可访问只由敲门决定
	AutoFirewall bool `json:"autoFirewall"`
	// AllowLAN 为 true 时内网、回环地址与本机网卡所在的公网 IPv6 网段无需敲门即可访问受保护端口
	AllowLAN bool `json:"allowLan"`
	// AllowList 为无需敲门即可访问受保护端口的 IP 或网段
	AllowList []string `json:"allowList"`
	// MaxAge 为报文时间戳与本机时间允许的最大偏差(秒),超出视为过期或重放
	MaxAge int `json:"maxAge"`
}

Settings 是模块全局设置。

func (*Settings) Validate

func (s *Settings) Validate() error

Validate 校验并规范化设置。

type Status

type Status struct {
	// Listening 为是否正在监听 UDP 敲门端口,ListenError 为监听失败的原因
	Listening   bool   `json:"listening"`
	Port        int    `json:"port"`
	ListenError string `json:"listenError,omitempty"`
	// OpenStatus 为自动放行敲门端口的结果说明
	OpenStatus string         `json:"openStatus,omitempty"`
	Firewall   FirewallStatus `json:"firewall"`
	Grants     []GrantView    `json:"grants"`
	Counters   Counters       `json:"counters"`
	OS         string         `json:"os"`
	// Now 为服务器当前时间,界面据此计算授权剩余时间(避免浏览器时钟偏差)
	Now time.Time `json:"now"`
}

Status 是模块运行状态。

Jump to

Keyboard shortcuts

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