worldserver

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 41 Imported by: ? 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

Index

Constants

View Source
const (
	ControlTypeChallenge          = "challenge"
	ControlTypeChallengeResponse  = "challenge.response"
	ControlTypeNodeAnnounce       = "node.announce"
	ControlTypeDirectory          = "directory"
	ControlTypeDomainLookup       = "domain.lookup"
	ControlTypeDomainLookupResult = "domain.lookupResult"
	ControlTypeRelayAttached      = "relay.attached"
	ControlTypeRelayIncoming      = "relay.incoming"
	ControlTypePing               = "ping"
	ControlTypePong               = "pong"
	ControlTypeNodeRevoked        = "node.revoked"
)
View Source
const (
	ACLCapabilityMessage = "message"
	ACLCapabilityControl = "control"
)
View Source
const (
	DomainPendingOutbound = "pending_outbound"
	DomainPendingInbound  = "pending_inbound"
	DomainMutual          = "mutual"
	DomainRejected        = "rejected"
)
View Source
const ChannelFrameLimit = (16 << 20) + 8 + 16

ChannelFrameLimit 包含 16 MiB 明文、序号和 GCM 认证标签。

Variables

View Source
var (
	ErrDomainKeyConflict = errors.New("domain is already bound to a different public key")
	ErrDomainNotTrusted  = errors.New("domain is not trusted")
)

Functions

func ChallengeSignBytes

func ChallengeSignBytes(purpose ChallengePurpose, challenge []byte, nodeID string, timestamp int64) []byte

ChallengeSignBytes 将入口、挑战、节点标识和 Unix 秒时间戳绑定到证明。

func GetNodeIDFromSignPublic

func GetNodeIDFromSignPublic(public []byte) string

GetNodeIDFromSignPublic 从签名公钥摘要的前 80 位派生节点标识。

func HandleHealthz

func HandleHealthz(c *gin.Context)

HandleHealthz 为 World 控制面和独立 relay 返回空的成功响应。

func IsValidDomain

func IsValidDomain(domain string) bool

IsValidDomain 是否为小写主机名或带可选端口的主机地址?

func ParseJoinCode

func ParseJoinCode(code string) (string, error)

ParseJoinCode 读取入网码中的控制面地址。

func ParseWorldBaseURL

func ParseWorldBaseURL(base string, allowWebSocket bool) (*url.URL, error)

ParseWorldBaseURL 解析网页或 WebSocket 入口的基址,保留部署前缀。

func RegisterSignBytes

func RegisterSignBytes(request RegisterNodeRequest) []byte

RegisterSignBytes 构造节点注册签名的字节,绑定节点名、入网码及两把公钥。

Types

type ACLMatrix

type ACLMatrix struct {
	Nodes []ACLMatrixNode   `json:"nodes"`
	Cells [][]ACLMatrixCell `json:"cells"`
}

ACLMatrix 以相同的节点顺序排列发送行和接收列。

type ACLMatrixCell

type ACLMatrixCell struct {
	Allowed bool `json:"allowed"`
	// RuleIndex 指向草稿或已保存列表中的规则,默认规则为 -1。
	RuleIndex int `json:"rule_index"`
}

type ACLMatrixNode

type ACLMatrixNode struct {
	Name  string   `json:"name"`
	Owner string   `json:"owner"`
	Tags  []string `json:"tags"`
}

type ACLRule

type ACLRule struct {
	Priority     int      `json:"priority" yaml:"priority"`
	From         []string `json:"from" yaml:"from"`
	To           []string `json:"to" yaml:"to"`
	Action       string   `json:"action" yaml:"action"`
	Capabilities []string `json:"capabilities,omitempty" yaml:"capabilities,omitempty"`
}

ACLRule 按 Priority 从小到大匹配,同优先级按提交顺序匹配。

type ACLRuleRecord

type ACLRuleRecord struct {
	ID           uint     `gorm:"primaryKey;autoIncrement"`
	Priority     int      `gorm:"not null;index"`
	From         []string `gorm:"column:from;serializer:json;not null"`
	To           []string `gorm:"column:to;serializer:json;not null"`
	Action       string   `gorm:"not null"`
	Capabilities []string `gorm:"serializer:json"`
}

ACLRuleRecord 保存一条有序的访问规则。

func (ACLRuleRecord) TableName

func (ACLRuleRecord) TableName() string

TableName 返回权限规则表名。

type AddDomainRequest

type AddDomainRequest struct {
	Card string `json:"card"`
}

type AdminNode

type AdminNode struct {
	NodeRecord
	Domain  string             `json:"domain"`
	Online  bool               `json:"online"`
	Targets []AdvertisedTarget `json:"targets"`
}

AdminNode 将注册记录与当前在线公告合并。

type AdvertisedTarget

type AdvertisedTarget struct {
	Name        string `json:"name" yaml:"name"`
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
	Visibility  string `json:"visibility" yaml:"visibility"`
}

AdvertisedTarget 描述节点通告的一个目标。

type ChallengeMessage

type ChallengeMessage struct {
	Challenge []byte `json:"challenge"`
}

ChallengeMessage 由控制面在 WebSocket 握手成功后发送,带本次连接的随机挑战。

type ChallengePurpose

type ChallengePurpose string

ChallengePurpose 标识节点提交挑战应答的入口。

const (
	ChallengeConnect     ChallengePurpose = "openvaala-world-connect/v1"
	ChallengeRelayAttach ChallengePurpose = "openvaala-world-relay-attach/v1"
)

type ChallengeResponse

type ChallengeResponse struct {
	PublicKey []byte `json:"public_key"`
	NodeID    string `json:"node_id"`
	Timestamp int64  `json:"timestamp"`
	Signature []byte `json:"signature"`
}

ChallengeResponse 由节点收到挑战后发送,Timestamp 是 Unix 秒。

type ControlEnvelope

type ControlEnvelope struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload,omitempty"`
}

ControlEnvelope 是节点与控制面之间的 WebSocket 消息。

type CreateJoinTokenRequest

type CreateJoinTokenRequest struct {
	Owner string   `json:"owner"`
	Tags  []string `json:"tags"`
	// Reusable 入网码是否可供多个节点注册?
	Reusable  bool  `json:"reusable"`
	ExpiresAt int64 `json:"expires_at"`
}

CreateJoinTokenRequest 指定入网码归属和过期时间,ExpiresAt 使用 Unix 毫秒。

type CreateJoinTokenResponse

type CreateJoinTokenResponse struct {
	JoinTokenInfo
	Token string `json:"token"`
}

CreateJoinTokenResponse 在创建时一次性返回入网码明文。

type Directory

type Directory struct {
	Nodes   []DirectoryNode `json:"nodes"`
	Domains []PeerDomain    `json:"domains"`
}

Directory 由控制面在通讯录变更时推送给在线节点。

type DirectoryNode

type DirectoryNode struct {
	NodeID     string   `json:"node_id"`
	NodeName   string   `json:"node_name"`
	Domain     string   `json:"domain"`
	Owner      string   `json:"owner"`
	Tags       []string `json:"tags"`
	SignPublic []byte   `json:"sign_public"`
	// Online 节点是否有已认证连接?
	Online bool `json:"online"`
	// CanSend 接收本通讯录的节点是否有权主动向该节点发起消息?
	CanSend bool `json:"can_send"`
	// CanReceive 该节点是否有权向本通讯录的接收方发信?
	CanReceive bool `json:"can_receive"`
	// CanControl 接收本通讯录的节点是否有权控制该节点?
	CanControl bool `json:"can_control"`
	// AllowPeerControl 该节点是否有权控制本通讯录的接收方?
	AllowPeerControl bool               `json:"allow_peer_control"`
	Entries          PeerEntries        `json:"entries"`
	Targets          []AdvertisedTarget `json:"targets"`
}

DirectoryNode 是通讯录中一个节点的公开身份和可达信息。

type DomainChannels

type DomainChannels interface {
	SendTrust(context.Context, DomainIdentity, PeerDomain, DomainTrustMessage, DomainDirectory) error
	GetRelayAttachments() map[string]bool
	RunAttachments(ctx context.Context, local DomainIdentity, relays []string, localAcceptURLs <-chan string, directory DomainDirectory)
	ServeDirect(ctx context.Context, socket *websocket.Conn, local DomainIdentity, directory DomainDirectory) error
	Lookup(ctx context.Context, local DomainIdentity, peer PeerDomain, node string, directory DomainDirectory) (DomainMember, error)
}

DomainChannels 以本域身份挂靠、接受和发起域查询。

type DomainDirectory

type DomainDirectory interface {
	GetPendingTrustCard(context.Context, ed25519.PublicKey) (string, error)
	ReceiveTrust(context.Context, ed25519.PublicKey, DomainTrustMessage) (DomainTrustMessage, error)
	GetMember(ctx context.Context, caller ed25519.PublicKey, node string) (DomainMember, error)
}

DomainDirectory 按握手公钥授权域查询。

type DomainIdentity

type DomainIdentity struct {
	SignPublic, SignPrivate, BoxPublic, BoxPrivate []byte
}

DomainIdentity 是控制面持有的握手身份。

type DomainLookup

type DomainLookup struct {
	RequestID string `json:"request_id"`
	Domain    string `json:"domain"`
	Node      string `json:"node"`
}

DomainLookup 由已认证成员通过本域控制连接发起。

type DomainLookupResult

type DomainLookupResult struct {
	RequestID string        `json:"request_id"`
	DomainKey []byte        `json:"domain_key,omitempty"`
	Member    *DomainMember `json:"member,omitempty"`
	Code      string        `json:"code,omitempty"`
	Error     string        `json:"error,omitempty"`
}

DomainLookupResult 由控制面完成代查后回给原请求连接。

type DomainMember

type DomainMember struct {
	PublicKey []byte      `json:"public_key"`
	Owner     string      `json:"owner"`
	Entries   PeerEntries `json:"entries"`
}

DomainMember 是所属域确认的成员身份及进入方式。

type DomainTrustMessage

type DomainTrustMessage struct {
	Op       string `json:"op,omitempty"`
	Card     string `json:"card,omitempty"`
	Domain   string `json:"domain,omitempty"`
	Decision string `json:"decision,omitempty"`
}

DomainTrustMessage 在已验签的域通道上传递邀请或对邀请的决定。

type GetACLMatrixRequest

type GetACLMatrixRequest struct {
	Rules []ACLRule `json:"rules,omitzero"`
}

type InvalidAdminRequestError

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

InvalidAdminRequestError 表示管理请求的字段校验失败。

type JoinTokenInfo

type JoinTokenInfo struct {
	ID    string   `json:"id"`
	Owner string   `json:"owner"`
	Tags  []string `json:"tags"`
	// Reusable 入网码是否可供多个节点注册?
	Reusable  bool  `json:"reusable"`
	UsedCount int   `json:"used_count"`
	ExpiresAt int64 `json:"expires_at"`
	RevokedAt int64 `json:"revoked_at"`
	CreatedAt int64 `json:"created_at"`
}

JoinTokenInfo 返回入网码状态,时间字段使用 Unix 毫秒。

type JoinTokenRecord

type JoinTokenRecord struct {
	ID        string                      `gorm:"primaryKey"`
	TokenHash string                      `gorm:"uniqueIndex;not null"`
	Owner     string                      `gorm:"index;not null"`
	Tags      datatypes.JSONSlice[string] `gorm:"not null"`
	// Reusable 入网码是否可供多个节点注册?
	Reusable  bool  `gorm:"not null"`
	UsedCount int   `gorm:"not null"`
	ExpiresAt int64 `gorm:"index;not null"`
	RevokedAt int64 `gorm:"not null"`
	CreatedAt int64 `gorm:"autoCreateTime:milli"`
}

JoinTokenRecord 保存入网码哈希和使用状态。

func (JoinTokenRecord) TableName

func (JoinTokenRecord) TableName() string

TableName 返回入网码表名。

type ListDomainsResult

type ListDomainsResult struct {
	Domain   string       `json:"domain"`
	Domains  []PeerDomain `json:"domains"`
	SelfCard string       `json:"self_card"`
}

type ListUsersResult

type ListUsersResult struct {
	Users []WorldUser `json:"users"`
}

type NodeAnnounce

type NodeAnnounce struct {
	Entries PeerEntries        `json:"entries"`
	Targets []AdvertisedTarget `json:"targets"`
}

NodeAnnounce 由已认证节点在上报可用目标和直连地址时发送。

type NodeRecord

type NodeRecord struct {
	NodeID     string                      `gorm:"primaryKey" json:"node_id"`
	NodeName   string                      `gorm:"uniqueIndex;not null" json:"node_name"`
	Owner      string                      `gorm:"index;not null" json:"owner"`
	Tags       datatypes.JSONSlice[string] `gorm:"not null" json:"tags"`
	SignPublic []byte                      `gorm:"not null" json:"sign_public"`
	BoxPublic  []byte                      `gorm:"not null" json:"box_public"`
	RevokedAt  int64                       `gorm:"index;not null" json:"revoked_at"`
	CreatedAt  int64                       `gorm:"autoCreateTime:milli" json:"created_at"`
}

NodeRecord 保存已入网节点的身份、归属和吊销状态。

func (NodeRecord) TableName

func (NodeRecord) TableName() string

TableName 返回节点表名。

type NodeRevoked

type NodeRevoked struct {
	Code   string `json:"code,omitempty"`
	NodeID string `json:"node_id"`
}

NodeRevoked 由控制面在管理员吊销节点后向该节点发送。

type PeerDomain

type PeerDomain struct {
	Status    string      `json:"status,omitempty" gorm:"not null;default:mutual"`
	Domain    string      `json:"domain" gorm:"primaryKey"`
	PublicKey []byte      `json:"public_key" gorm:"not null;index"`
	Entries   PeerEntries `json:"entries" gorm:"serializer:json;not null"`
	AddedAt   int64       `json:"added_at,omitempty" gorm:"autoCreateTime:milli"`
}

PeerDomain 保存管理员导入的域名片和添加时间。

type PeerEntries

type PeerEntries struct {
	DirectURLs []string `json:"direct_urls"`
	Relays     []string `json:"relays"`
}

PeerEntries 保存按拨号顺序公布的直连与中转地址。

func (PeerEntries) IsValid

func (entries PeerEntries) IsValid() bool

IsValid 名片中是否至少有一项可用的拨号入口?

type PeerNotFoundError

type PeerNotFoundError string

PeerNotFoundError 携带所属域查不到的完整成员地址。

func (PeerNotFoundError) Error

func (e PeerNotFoundError) Error() string

type RegisterNodeRequest

type RegisterNodeRequest struct {
	NodeName   string `json:"node_name"`
	JoinToken  string `json:"join_token"`
	SignPublic []byte `json:"sign_public"`
	BoxPublic  []byte `json:"box_public"`
	Signature  []byte `json:"signature"`
}

RegisterNodeRequest 提交待注册节点的名称、公钥和入网证明。

type RegisterNodeResponse

type RegisterNodeResponse struct {
	NodeID   string   `json:"node_id"`
	NodeName string   `json:"node_name"`
	Domain   string   `json:"domain"`
	Owner    string   `json:"owner"`
	Tags     []string `json:"tags"`
}

RegisterNodeResponse 返回注册后的节点身份及归属。

type Relay

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

Relay 按公钥登记挂靠,为一次拨号配对两条二进制 WebSocket。

func NewRelay

func NewRelay(instances []string) *Relay

NewRelay 创建接受持钥者挂靠的 relay。

func (*Relay) RegisterRoutes

func (r *Relay) RegisterRoutes(router gin.IRouter)

RegisterRoutes 将三种已升级的连接交给运行中的 relay。

func (*Relay) Run

func (r *Relay) Run(ctx context.Context) error

Run 接收挂靠与配对连接,退出前等待它们的读写任务。

type RelayIncoming

type RelayIncoming struct {
	ConnectionID string `json:"connection_id"`
}

RelayIncoming 由 relay 收到拨号请求时发给目标节点。

func (RelayIncoming) GetAcceptURL

func (r RelayIncoming) GetAcceptURL(base string) string

GetAcceptURL 拼接本次接通通知对应的完整接受地址。

type RelayInstancesResult

type RelayInstancesResult struct {
	Instances []string `json:"instances"`
}

RelayInstancesResult 是公开 relay 返回的全部实例地址。

type ServerMetaRecord

type ServerMetaRecord struct {
	ID             uint   `gorm:"primaryKey"`
	Domain         string `gorm:"not null"`
	DomainIdentity `gorm:"embedded"`
	UpdatedAt      int64 `gorm:"autoUpdateTime:milli"`
}

ServerMetaRecord 保存本域的签名与密钥协商身份。

func (ServerMetaRecord) TableName

func (ServerMetaRecord) TableName() string

TableName 返回域身份表名。

type WorldServer

type WorldServer interface {
	// RegisterRoutes 将控制面路由挂载到调用方的路由器。
	RegisterRoutes(router gin.IRouter)
	DomainDirectory
	GetRelayAttachments() map[string]bool
	ServeDomainDirect(c *gin.Context)
	GetDomains(ctx context.Context) (ListDomainsResult, error)
	AddDomain(ctx context.Context, card string) (PeerDomain, bool, error)
	DecideDomain(ctx context.Context, domain string, accept bool) error
	DeleteDomain(ctx context.Context, domain string) error
	// ListUsers 汇总节点、有效入网码和规则提及的用户。
	ListUsers(ctx context.Context) ([]WorldUser, error)
	// GetACLMatrix 按已保存的规则或草稿计算节点之间的访问权限。
	GetACLMatrix(ctx context.Context, rules []ACLRule) (ACLMatrix, error)
	GetNodes(ctx context.Context) ([]AdminNode, error)
	RevokeNode(ctx context.Context, nodeID string) error
	RevokeOwner(ctx context.Context, owner string) error
	GetJoinTokens(ctx context.Context) ([]JoinTokenInfo, error)
	CreateJoinToken(ctx context.Context, req CreateJoinTokenRequest) (CreateJoinTokenResponse, error)
	RevokeJoinToken(ctx context.Context, id string) error
	GetACL(ctx context.Context) ([]ACLRule, error)
	ReplaceACL(ctx context.Context, rules []ACLRule) error
	// Run 运行控制面并在取消后等待所有连接退出,只能调用一次。
	Run(ctx context.Context) (err error)
}

WorldServer 提供域内联邦的注册、通讯录和接通通知。

func NewWorldServer

func NewWorldServer(opt *WorldServerOpt) (WorldServer, error)

NewWorldServer 校验配置并创建控制面实例。

type WorldServerOpt

type WorldServerOpt struct {
	Channels       DomainChannels
	Entries        PeerEntries
	LocalRelayURL  string
	PublicURL      string
	RelayInstances []string
	RelayMode      string
	Domain         string
	DBPath         string
	AdminToken     string
	StaticACL      []ACLRule
	Telemetry      *telemetry.Telemetry
}

WorldServerOpt 保存域名、数据库路径、管理凭据和初始 ACL。

func NewWorldServerOpt

func NewWorldServerOpt(domain, dbPath, adminToken string) *WorldServerOpt

NewWorldServerOpt 创建联邦控制面配置。

type WorldUser

type WorldUser struct {
	Owner       string `json:"owner"`
	NodeCount   int    `json:"node_count"`
	OnlineCount int    `json:"online_count"`
	TokenCount  int    `json:"token_count"`
	RuleCount   int    `json:"rule_count"`
}

WorldUser 汇总归属于同一个人的节点、有效入网码和规则。

Jump to

Keyboard shortcuts

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