Documentation
¶
Index ¶
- Constants
- func ConfigureLogger(format, level string)
- func Must[T any](value T, err error) T
- func WithDebugOption() func(*ServerBuilder)
- func WithPortRangeOption(portRange string) func(*ServerBuilder)
- func WithTLSOption(certFile, keyFile string) func(*ServerBuilder)
- type Config
- type ConfigChain
- func (c *ConfigChain) Address(address string) *ConfigChain
- func (c *ConfigChain) Build() *ConfigResult
- func (c *ConfigChain) DenyPeers(peers []string) *ConfigChain
- func (c *ConfigChain) ExternalIP(ip string) *ConfigChain
- func (c *ConfigChain) ExternalSecret(secret string) *ConfigChain
- func (c *ConfigChain) Logging(format, level string) *ConfigChain
- func (c *ConfigChain) PortRange(portRange string) *ConfigChain
- func (c *ConfigChain) Start() *ServerResult
- func (c *ConfigChain) StartTLS() *ServerResult
- func (c *ConfigChain) TLS(certFile, keyFile string) *ConfigChain
- type ConfigResult
- type Entry
- type ExternalServer
- type Generator
- type InternalServer
- type RelayAddressGeneratorNone
- type RelayAddressGeneratorPortRange
- func (r *RelayAddressGeneratorPortRange) AllocateConn(network string, requestedPort int) (net.Conn, net.Addr, error)
- func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requestedPort int) (net.PacketConn, net.Addr, error)
- func (r *RelayAddressGeneratorPortRange) Validate() error
- type Result
- func AndThen[T, U any](r *Result[T], fn func(T) *Result[U]) *Result[U]
- func Err[T any](err error) *Result[T]
- func Map[T, U any](r *Result[T], fn func(T) U) *Result[U]
- func NewResult[T any](value T, err error) *Result[T]
- func Ok[T any](value T) *Result[T]
- func Try[T any](value T, err error) *Result[T]
- func (r *Result[T]) IsErr() bool
- func (r *Result[T]) IsOk() bool
- func (r *Result[T]) LogError(msg string, args ...any) *Result[T]
- func (r *Result[T]) LogSuccess(msg string, args ...any) *Result[T]
- func (r *Result[T]) MapErr(fn func(error) error) *Result[T]
- func (r *Result[T]) OrElse(fn func(error) *Result[T]) *Result[T]
- func (r *Result[T]) Unwrap() T
- func (r *Result[T]) UnwrapOr(defaultValue T) T
- func (r *Result[T]) UnwrapOrElse(fn func(error) T) T
- type ResultBuilder
- type Server
- type ServerBuilder
- func (b *ServerBuilder) Build() (Server, error)
- func (b *ServerBuilder) BuildTLS() (Server, error)
- func (b *ServerBuilder) EnableDebugLogging() *ServerBuilder
- func (b *ServerBuilder) EnableJSONLogging() *ServerBuilder
- func (b *ServerBuilder) GetConfig() (Config, error)
- func (b *ServerBuilder) WithAddress(address string) *ServerBuilder
- func (b *ServerBuilder) WithDenyPeers(peers []string) *ServerBuilder
- func (b *ServerBuilder) WithExternalAuth(secret string) *ServerBuilder
- func (b *ServerBuilder) WithExternalIP(ip string) *ServerBuilder
- func (b *ServerBuilder) WithExternalIPs(ips []string) *ServerBuilder
- func (b *ServerBuilder) WithIPProvider(provider ipdns.Provider) *ServerBuilder
- func (b *ServerBuilder) WithLogging(format, level string) *ServerBuilder
- func (b *ServerBuilder) WithPortRange(portRange string) *ServerBuilder
- func (b *ServerBuilder) WithSTUNProbe(stunServer string) *ServerBuilder
- func (b *ServerBuilder) WithTLS(certFile, keyFile string) *ServerBuilder
- type ServerManager
- func (m *ServerManager) CleanupInactiveUsers(inactiveTime time.Duration) int
- func (m *ServerManager) CreateUser(userID string, clientIP net.IP) (username, password string, err error)
- func (m *ServerManager) DisableUser(userID string) error
- func (m *ServerManager) EnableUser(userID string) error
- func (m *ServerManager) GetServer() Server
- func (m *ServerManager) GetStats() ServerStats
- func (m *ServerManager) GetUser(userID string) (UserInfo, error)
- func (m *ServerManager) ListUsers() []UserInfo
- func (m *ServerManager) StartCleanupRoutine(interval, inactiveTime time.Duration)
- func (m *ServerManager) Stop(ctx ...context.Context)
- type ServerResult
- type ServerStats
- type UserInfo
Constants ¶
const Realm = "zishuo.net"
Variables ¶
This section is empty.
Functions ¶
func ConfigureLogger ¶
func ConfigureLogger(format, level string)
ConfigureLogger 根据配置初始化日志 format: 日志格式,支持 "json", "text", "console" level: 日志级别,支持 "debug", "info", "warn", "error"
func WithDebugOption ¶ added in v0.2.0
func WithDebugOption() func(*ServerBuilder)
func WithPortRangeOption ¶ added in v0.2.0
func WithPortRangeOption(portRange string) func(*ServerBuilder)
func WithTLSOption ¶ added in v0.2.0
func WithTLSOption(certFile, keyFile string) func(*ServerBuilder)
常用选项函数
Types ¶
type Config ¶
type Config struct {
// TLS证书配置
TLSEnabled bool `split_words:"true"` // 是否启用TLS
TLSCertFile string `split_words:"true"` // TLS证书文件路径
TLSKeyFile string `split_words:"true"` // TLS私钥文件路径
// TURN服务器配置
TurnAddress string `default:":3478" required:"true" split_words:"true"` // TURN服务器监听地址
TurnPortRange string `split_words:"true"` // TURN端口范围,格式为"min:max"
// TURN外部IP配置
TurnExternalIP []string `split_words:"true"` // TURN服务器的外部IP地址列表
TurnExternalPort string `default:"3478" split_words:"true"` // TURN服务器的外部端口
TurnExternalSecret string `split_words:"true"` // 用于外部TURN认证的密钥
// 安全配置
TurnDenyPeers []string `default:"0.0.0.0/8,127.0.0.1/8,::/128,::1/128,fe80::/10" split_words:"true"` // 拒绝的对等方IP范围
TurnDenyPeersParsed []*net.IPNet `ignored:"true"` // 解析后的拒绝对等方IP网络
// 日志配置
LogFormat string `default:"text" split_words:"true"` // 日志格式:text, json, console
LogLevel string `default:"info" split_words:"true"` // 日志级别:debug, info, warn, error
// 内部使用字段
TurnExternal bool `ignored:"true"` // 是否使用外部TURN服务器
TurnIPProvider ipdns.Provider `ignored:"true"` // IP地址提供者
}
Config 代表TURN服务器配置
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig 返回一个具有合理默认值的TURN服务器配置 用户可以在此基础上修改特定的配置项
func DefaultConfigWithAuth ¶ added in v0.2.0
DefaultConfigWithAuth 返回一个包含外部认证的默认配置
func DefaultConfigWithIP ¶
DefaultConfigWithIP 返回一个包含指定外部IP的默认配置 这是一个快速启动方法,只需提供你的服务器公网IP即可
func (*Config) PrepareForStart ¶ added in v0.2.0
PrepareForStart 准备配置用于启动服务器
func (Config) ShouldUseExternalAuth ¶ added in v0.2.0
ShouldUseExternalAuth 判断是否应该使用外部认证
type ConfigChain ¶ added in v0.2.0
type ConfigChain struct {
// contains filtered or unexported fields
}
ConfigChain 提供链式配置API
func (*ConfigChain) Address ¶ added in v0.2.0
func (c *ConfigChain) Address(address string) *ConfigChain
Address 设置监听地址
func (*ConfigChain) DenyPeers ¶ added in v0.2.0
func (c *ConfigChain) DenyPeers(peers []string) *ConfigChain
DenyPeers 设置拒绝的对等方
func (*ConfigChain) ExternalIP ¶ added in v0.2.0
func (c *ConfigChain) ExternalIP(ip string) *ConfigChain
ExternalIP 设置外部IP
func (*ConfigChain) ExternalSecret ¶ added in v0.2.0
func (c *ConfigChain) ExternalSecret(secret string) *ConfigChain
ExternalSecret 设置外部认证密钥
func (*ConfigChain) Logging ¶ added in v0.2.0
func (c *ConfigChain) Logging(format, level string) *ConfigChain
Logging 设置日志
func (*ConfigChain) PortRange ¶ added in v0.2.0
func (c *ConfigChain) PortRange(portRange string) *ConfigChain
PortRange 设置端口范围
func (*ConfigChain) Start ¶ added in v0.2.0
func (c *ConfigChain) Start() *ServerResult
Start 直接启动服务器
func (*ConfigChain) StartTLS ¶ added in v0.2.0
func (c *ConfigChain) StartTLS() *ServerResult
StartTLS 直接启动TLS服务器
func (*ConfigChain) TLS ¶ added in v0.2.0
func (c *ConfigChain) TLS(certFile, keyFile string) *ConfigChain
TLS 启用TLS
type ConfigResult ¶ added in v0.2.0
ConfigResult 配置操作结果的类型别名
func ConfigErr ¶ added in v0.2.0
func ConfigErr(err error) *ConfigResult
func ConfigOk ¶ added in v0.2.0
func ConfigOk(config Config) *ConfigResult
type ExternalServer ¶
type ExternalServer struct {
// contains filtered or unexported fields
}
func (*ExternalServer) Authenticate ¶
func (*ExternalServer) Credentials ¶
func (*ExternalServer) Disallow ¶
func (a *ExternalServer) Disallow(username string)
type Generator ¶
type Generator struct {
turn.RelayAddressGenerator
IPProvider ipdns.Provider
}
func (*Generator) AllocatePacketConn ¶
type InternalServer ¶
type InternalServer struct {
// contains filtered or unexported fields
}
func (*InternalServer) Authenticate ¶
func (*InternalServer) Credentials ¶
func (*InternalServer) Disallow ¶
func (a *InternalServer) Disallow(username string)
type RelayAddressGeneratorNone ¶
type RelayAddressGeneratorNone struct{}
func (*RelayAddressGeneratorNone) AllocateConn ¶
func (*RelayAddressGeneratorNone) AllocatePacketConn ¶
func (r *RelayAddressGeneratorNone) AllocatePacketConn(network string, requestedPort int) (net.PacketConn, net.Addr, error)
func (*RelayAddressGeneratorNone) Validate ¶
func (r *RelayAddressGeneratorNone) Validate() error
type RelayAddressGeneratorPortRange ¶
type RelayAddressGeneratorPortRange struct {
MinPort uint16
MaxPort uint16
Rand randutil.MathRandomGenerator
}
func (*RelayAddressGeneratorPortRange) AllocateConn ¶
func (*RelayAddressGeneratorPortRange) AllocatePacketConn ¶
func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requestedPort int) (net.PacketConn, net.Addr, error)
func (*RelayAddressGeneratorPortRange) Validate ¶
func (r *RelayAddressGeneratorPortRange) Validate() error
type Result ¶ added in v0.2.0
type Result[T any] struct { // contains filtered or unexported fields }
Result 提供更优雅的错误处理和链式操作
func (*Result[T]) LogSuccess ¶ added in v0.2.0
LogSuccess 记录成功日志(如果成功)
func (*Result[T]) UnwrapOr ¶ added in v0.2.0
func (r *Result[T]) UnwrapOr(defaultValue T) T
UnwrapOr 解包结果,如果有错误则返回默认值
func (*Result[T]) UnwrapOrElse ¶ added in v0.2.0
UnwrapOrElse 解包结果,如果有错误则调用函数获取默认值
type ResultBuilder ¶ added in v0.2.0
type ResultBuilder struct {
// contains filtered or unexported fields
}
结果组合器
func NewResultBuilder ¶ added in v0.2.0
func NewResultBuilder() *ResultBuilder
func (*ResultBuilder) Add ¶ added in v0.2.0
func (rb *ResultBuilder) Add(r *Result[any]) *ResultBuilder
func (*ResultBuilder) AddError ¶ added in v0.2.0
func (rb *ResultBuilder) AddError(err error) *ResultBuilder
func (*ResultBuilder) Build ¶ added in v0.2.0
func (rb *ResultBuilder) Build() error
type Server ¶
type Server interface {
Stop(ctx ...context.Context) error
// contains filtered or unexported methods
}
Server 表示可控制生命周期的 TURN 服务器
func QuickStart ¶ added in v0.2.0
func QuickStart(externalIP string, options ...func(*ServerBuilder)) (Server, error)
QuickStart 提供最简单的启动方式
func QuickStartWithAuth ¶ added in v0.2.0
func QuickStartWithAuth(externalIP, secret string, options ...func(*ServerBuilder)) (Server, error)
QuickStartWithAuth 快速启动带认证的服务器
type ServerBuilder ¶ added in v0.2.0
type ServerBuilder struct {
// contains filtered or unexported fields
}
ServerBuilder 提供构建器模式来优雅地构建TURN服务器
func NewServerBuilder ¶ added in v0.2.0
func NewServerBuilder() *ServerBuilder
NewServerBuilder 创建一个新的服务器构建器
func (*ServerBuilder) Build ¶ added in v0.2.0
func (b *ServerBuilder) Build() (Server, error)
Build 构建并启动服务器
func (*ServerBuilder) BuildTLS ¶ added in v0.2.0
func (b *ServerBuilder) BuildTLS() (Server, error)
BuildTLS 构建并启动TLS服务器
func (*ServerBuilder) EnableDebugLogging ¶ added in v0.2.0
func (b *ServerBuilder) EnableDebugLogging() *ServerBuilder
EnableDebugLogging 启用调试日志
func (*ServerBuilder) EnableJSONLogging ¶ added in v0.2.0
func (b *ServerBuilder) EnableJSONLogging() *ServerBuilder
EnableJSONLogging 启用JSON格式日志
func (*ServerBuilder) GetConfig ¶ added in v0.2.0
func (b *ServerBuilder) GetConfig() (Config, error)
GetConfig 获取构建的配置(用于高级用途)
func (*ServerBuilder) WithAddress ¶ added in v0.2.0
func (b *ServerBuilder) WithAddress(address string) *ServerBuilder
WithAddress 设置监听地址
func (*ServerBuilder) WithDenyPeers ¶ added in v0.2.0
func (b *ServerBuilder) WithDenyPeers(peers []string) *ServerBuilder
WithDenyPeers 设置拒绝的对等方IP范围
func (*ServerBuilder) WithExternalAuth ¶ added in v0.2.0
func (b *ServerBuilder) WithExternalAuth(secret string) *ServerBuilder
WithExternalAuth 启用外部认证
func (*ServerBuilder) WithExternalIP ¶ added in v0.2.0
func (b *ServerBuilder) WithExternalIP(ip string) *ServerBuilder
WithExternalIP 设置外部IP地址
func (*ServerBuilder) WithExternalIPs ¶ added in v0.2.0
func (b *ServerBuilder) WithExternalIPs(ips []string) *ServerBuilder
WithExternalIPs 设置多个外部IP地址
func (*ServerBuilder) WithIPProvider ¶ added in v0.2.0
func (b *ServerBuilder) WithIPProvider(provider ipdns.Provider) *ServerBuilder
WithIPProvider 设置自定义IP提供者
func (*ServerBuilder) WithLogging ¶ added in v0.2.0
func (b *ServerBuilder) WithLogging(format, level string) *ServerBuilder
WithLogging 设置日志配置
func (*ServerBuilder) WithPortRange ¶ added in v0.2.0
func (b *ServerBuilder) WithPortRange(portRange string) *ServerBuilder
WithPortRange 设置端口范围
func (*ServerBuilder) WithSTUNProbe ¶ added in v0.2.0
func (b *ServerBuilder) WithSTUNProbe(stunServer string) *ServerBuilder
WithSTUNProbe 使用STUN探测获取外部IP
func (*ServerBuilder) WithTLS ¶ added in v0.2.0
func (b *ServerBuilder) WithTLS(certFile, keyFile string) *ServerBuilder
WithTLS 启用TLS支持
type ServerManager ¶ added in v0.2.0
type ServerManager struct {
// contains filtered or unexported fields
}
ServerManager 提供更高级的服务器管理功能
func NewServerManager ¶ added in v0.2.0
func NewServerManager(server Server, config Config) *ServerManager
NewServerManager 创建服务器管理器
func WithManager ¶ added in v0.2.0
func WithManager(server Server, config Config) *ServerManager
WithManager 为现有服务器添加管理功能
func (*ServerManager) CleanupInactiveUsers ¶ added in v0.2.0
func (m *ServerManager) CleanupInactiveUsers(inactiveTime time.Duration) int
CleanupInactiveUsers 清理不活跃用户
func (*ServerManager) CreateUser ¶ added in v0.2.0
func (m *ServerManager) CreateUser(userID string, clientIP net.IP) (username, password string, err error)
CreateUser 创建用户并返回凭证
func (*ServerManager) DisableUser ¶ added in v0.2.0
func (m *ServerManager) DisableUser(userID string) error
DisableUser 禁用用户
func (*ServerManager) EnableUser ¶ added in v0.2.0
func (m *ServerManager) EnableUser(userID string) error
EnableUser 重新启用用户
func (*ServerManager) GetServer ¶ added in v0.2.0
func (m *ServerManager) GetServer() Server
GetServer 获取底层服务器实例
func (*ServerManager) GetStats ¶ added in v0.2.0
func (m *ServerManager) GetStats() ServerStats
GetStats 获取服务器统计信息
func (*ServerManager) GetUser ¶ added in v0.2.0
func (m *ServerManager) GetUser(userID string) (UserInfo, error)
GetUser 获取用户信息
func (*ServerManager) ListUsers ¶ added in v0.2.0
func (m *ServerManager) ListUsers() []UserInfo
ListUsers 列出所有用户
func (*ServerManager) StartCleanupRoutine ¶ added in v0.2.0
func (m *ServerManager) StartCleanupRoutine(interval, inactiveTime time.Duration)
StartCleanupRoutine 启动定期清理例程
func (*ServerManager) Stop ¶ added in v0.2.0
func (m *ServerManager) Stop(ctx ...context.Context)
Stop 停止服务器管理器
type ServerResult ¶ added in v0.2.0
ServerResult 服务器操作结果的类型别名
func ServerErr ¶ added in v0.2.0
func ServerErr(err error) *ServerResult