stun

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 31 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 stun 实现 STUN 内网穿透模块:在 NAT1(全锥形)网络下获取公网映射端口, 并通过保活与内置转发把外部访问引到内网服务。

Index

Constants

View Source
const (
	ProtoTCP4 = "tcp4"
	ProtoUDP4 = "udp4"
)

协议取值

View Source
const (
	BindIP        = "ip"
	BindInterface = "interface"
)

通道绑定方式

View Source
const (
	StatusStopped    = "stopped"
	StatusConnecting = "connecting"
	StatusOK         = "ok"
	StatusRetrying   = "retrying"
	StatusFailed     = "failed"
)

规则状态取值

View Source
const ID = "stun"

ID 是模块标识。

Variables

This section is empty.

Functions

This section is empty.

Types

type AddrChange

type AddrChange struct {
	Time time.Time `json:"time"`
	Addr string    `json:"addr"`
}

AddrChange 记录一次公网地址变化。

type Bind

type Bind struct {
	Mode string `json:"mode"`
	// IP 为空表示所有地址
	IP        string `json:"ip"`
	Interface string `json:"interface"`
	// Regex 选择网卡上的地址,兼容 Lucky 写法:留空取第 1 个、数字 n 取第 n 个、24* 前缀、*24 后缀,其余按正则
	Regex string `json:"regex"`
}

Bind 指定穿透通道使用的本地地址,多拨或多线路时用来选择出口。

type Module

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

Module 是 STUN 穿透模块。

func New

func New() *Module

New 创建模块实例。

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) 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"`
	Enable   bool   `json:"enable"`
	Protocol string `json:"protocol"`
	Bind     Bind   `json:"bind"`
	// LocalPort 为穿透通道本地端口,0 表示随机
	LocalPort        int      `json:"localPort"`
	UseGlobalServers bool     `json:"useGlobalServers"`
	StunServers      []string `json:"stunServers"`
	// KeepAliveServers 仅 TCP 使用:保持 NAT 映射的长连接目标,格式 host:port,端口缺省为 80
	KeepAliveServers []string `json:"keepAliveServers"`

	// DisableForward 为 true 时不使用内置转发,需在路由器上把通道端口映射到目标
	DisableForward  bool     `json:"disableForward"`
	TargetAddresses []string `json:"targetAddresses"`
	TargetPort      int      `json:"targetPort"`

	UPnP bool `json:"upnp"`
	// UPnPGateway 为网关 IP,留空自动发现
	UPnPGateway string `json:"upnpGateway"`
	// UPnPLocalHost 覆盖映射的内部地址(Docker 桥接网络时填宿主机局域网 IP)
	UPnPLocalHost string `json:"upnpLocalHost"`
	// UPnPControlURL 直接指定 IGD 控制地址,跳过发现
	UPnPControlURL string `json:"upnpControlUrl"`
	// UPnPInternalPort 非 0 时把路由器端口直接映射到本机该端口(绕过内置转发)
	UPnPInternalPort int    `json:"upnpInternalPort"`
	NATPMP           bool   `json:"natpmp"`
	NATPMPGateway    string `json:"natpmpGateway"`

	// 以下时间单位为毫秒,与 Lucky 一致
	HeartbeatInterval int  `json:"heartbeatInterval"`
	StunTimeout       int  `json:"stunTimeout"`
	RetryInterval     int  `json:"retryInterval"`
	AutoRetry         bool `json:"autoRetry"`
	// DisableCheck 关闭通道有效性检测(连接自身公网地址验证可达)
	DisableCheck bool `json:"disableCheck"`

	UseGlobalWebhook bool           `json:"useGlobalWebhook"`
	Webhook          webhook.Config `json:"webhook"`
	// WebhookOnlyOnChange 为 true 时只在公网地址变化时触发通知
	WebhookOnlyOnChange bool `json:"webhookOnlyOnChange"`
	ScriptEnable        bool `json:"scriptEnable"`
	// Script 以 sh -c 执行,变量通过环境变量传入:ruleName time ip port ipAddr
	Script string `json:"script"`

	MaxConnections    int    `json:"maxConnections"`
	UDPSessionTimeout int    `json:"udpSessionTimeout"`
	Remark            string `json:"remark"`
}

Rule 是一条 STUN 穿透规则。

func (*Rule) Validate

func (r *Rule) Validate() error

Validate 校验并补全默认值。

type RuleState

type RuleState struct {
	Status     string       `json:"status"`
	PublicAddr string       `json:"publicAddr,omitempty"`
	LocalAddr  string       `json:"localAddr,omitempty"`
	StunServer string       `json:"stunServer,omitempty"`
	Since      time.Time    `json:"since,omitzero"`
	Error      string       `json:"error,omitempty"`
	Mapping    string       `json:"mapping,omitempty"`
	History    []AddrChange `json:"history,omitempty"`
	Stats      Stats        `json:"stats"`
}

RuleState 是规则运行状态。

type RuleView

type RuleView struct {
	Rule
	State RuleState `json:"state"`
}

RuleView 是带运行状态的规则。

type Settings

type Settings struct {
	Enable           bool           `json:"enable"`
	StunServers      []string       `json:"stunServers"`
	KeepAliveServers []string       `json:"keepAliveServers"`
	Webhook          webhook.Config `json:"webhook"`
	// AddrCheckInterval 为重新查询公网地址的周期(秒)
	AddrCheckInterval int `json:"addrCheckInterval"`
}

Settings 是模块级配置。

type Stats

type Stats struct {
	ActiveConns int64 `json:"activeConns"`
	TotalConns  int64 `json:"totalConns"`
	BytesIn     int64 `json:"bytesIn"`
	BytesOut    int64 `json:"bytesOut"`
}

Stats 是转发统计。

Jump to

Keyboard shortcuts

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