Help us shape the future of Go! Take the 2026 Go Developer Survey

node

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package node holds the network module's server and node entities: the servers with the protocols they offer, stored as JSON and normalized for storage and distribution, the nodes that expose them, the per-server node configuration overrides, and the Redis keys of the node-facing caches.

Index

Constants

View Source
const (
	Expiry                                = 300 * time.Second              // Cache expiry time in seconds
	StatusCacheKey                        = "node:status:%d"               // Node status cache key format (Server ID and protocol) Example: node:status:1:shadowsocks
	OnlineUserCacheKeyWithSubscribe       = "node:online:subscribe:%d:%s"  // Online user subscribe cache key format (Server ID and protocol) Example: node:online:subscribe:1:shadowsocks
	OnlineUserSubscribeCacheKeyWithGlobal = "node:online:subscribe:global" // Online user global subscribe cache key
)
View Source
const (
	// ServerCacheTTL bounds the node-facing response caches, the server
	// configs and the user lists.
	ServerCacheTTL = 5 * time.Minute

	// ServerUserListCacheKey prefixes the cached user lists of a server.
	ServerUserListCacheKey = "server:user:"

	// ServerConfigCacheKey prefixes the cached configs of a server.
	ServerConfigCacheKey = "server:config:"

	// ServerCacheIndexKey tracks the exact response-cache keys generated for a
	// server so invalidation does not need to scan the entire Redis keyspace.
	ServerCacheIndexKey = "server:cache:index:%d"

	// ServerCacheGenerationKey fences response-cache fills that started before
	// a server configuration mutation completed.
	ServerCacheGenerationKey = "server:cache:generation:%d"
)

Variables

This section is empty.

Functions

func NormalizeCertPinSHA256 added in v1.15.3

func NormalizeCertPinSHA256(raw string) string

NormalizeCertPinSHA256 canonicalizes a node-reported TLS certificate fingerprint to lowercase hex. It returns "" when the value is not a SHA256 hex digest, so callers can treat invalid input as "not reported".

Types

type FilterNodeParams

type FilterNodeParams struct {
	Page     int      // Page Number
	Size     int      // Page Size
	NodeId   []int64  // Node IDs
	ServerId []int64  // Server IDs
	Tag      []string // Tags
	Search   string   // Search Address or Name
	Protocol string   // Protocol
	Preload  bool     // Preload Server
	Enabled  *bool    // Enabled
}

FilterNodeParams selects nodes, a page of them for the admin list.

type FilterParams

type FilterParams struct {
	Page   int
	Size   int
	Ids    []int64 // Server IDs
	Search string
}

FilterParams selects a page of servers.

type Node

type Node struct {
	Id        int64     `gorm:"primary_key"`
	Name      string    `gorm:"type:varchar(100);not null;default:'';comment:Node Name"`
	Tags      string    `gorm:"type:varchar(255);not null;default:'';comment:Tags"`
	Port      uint16    `gorm:"not null;default:0;comment:Connect Port"`
	Address   string    `gorm:"type:varchar(255);not null;default:'';comment:Connect Address"`
	ServerId  int64     `gorm:"not null;default:0;comment:Server ID"`
	Server    *Server   `gorm:"foreignKey:ServerId;references:Id"`
	Protocol  string    `gorm:"type:varchar(100);not null;default:'';comment:Protocol"`
	Enabled   *bool     `gorm:"type:boolean;not null;default:true;comment:Enabled"`
	Sort      int       `gorm:"uniqueIndex;not null;default:0;comment:Sort"`
	CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
	UpdatedAt time.Time `gorm:"comment:Update Time"`
}

Node is an entry point to a server's protocol: the address and port clients connect to, with the tags plans select nodes by.

func (*Node) BeforeCreate

func (n *Node) BeforeCreate(tx *gorm.DB) error

func (*Node) BeforeDelete

func (n *Node) BeforeDelete(tx *gorm.DB) error

func (*Node) BeforeUpdate

func (n *Node) BeforeUpdate(tx *gorm.DB) error

func (*Node) TableName

func (n *Node) TableName() string

type OnlineUserSubscribe

type OnlineUserSubscribe map[int64][]string

OnlineUserSubscribe maps the subscriptions online on a server to their IPs.

type Protocol

type Protocol struct {
	// Common: the protocol type, such as shadowsocks, vless, vmess, hysteria2, tuic or nowhere.
	Type string `json:"type"`
	// Common: the inbound listening port.
	Port uint16 `json:"port"`
	// Version: Snell accepts 5 or 6, TUIC 5 and Nowhere 1; 0 means the protocol's default.
	Version int `json:"version,omitempty"`
	// Snell only: the Snell v6 mode; other protocols leave it unset.
	Mode string `json:"mode,omitempty"`
	// Common: whether the inbound protocol is enabled.
	Enable bool `json:"enable"`
	// TLS/REALITY protocols: none, tls or reality; each protocol limits the values it accepts.
	Security string `json:"security,omitempty"`
	// Listening network: tcp, udp or both; Nowhere normalizes it to mix, tcp or udp, and the other protocols check it against what each supports.
	Network string `json:"network,omitempty"`
	// TLS protocols: the certificate domain and the TLS server name; REALITY also uses it as the server name.
	SNI string `json:"sni,omitempty"`
	// TLS/HTTP/QUIC protocols: the TLS ALPN list; Nowhere takes exactly one value, now/1 by default.
	ALPN []string `json:"alpn,omitempty"`
	// TLS client compatibility: allows skipping certificate verification; inbound configurations usually ignore it, and it is not a server certificate setting.
	AllowInsecure bool `json:"allow_insecure,omitempty"`
	// TLS client compatibility: the uTLS fingerprint; node inbounds currently ignore it, it is kept only for outbound and legacy configurations.
	Fingerprint string `json:"fingerprint,omitempty"`
	// VLESS/VMess REALITY only: the address the REALITY handshake is forwarded to.
	RealityServerAddr string `json:"reality_server_addr,omitempty"`
	// VLESS/VMess REALITY only: the port the REALITY handshake is forwarded to.
	RealityServerPort int `json:"reality_server_port,omitempty"`
	// VLESS/VMess REALITY, server side only: the server's X25519 private key.
	RealityPrivateKey string `json:"reality_private_key,omitempty"`
	// VLESS/VMess REALITY client information: the public key of the private key, mainly for the subscription output.
	RealityPublicKey string `json:"reality_public_key,omitempty"`
	// VLESS/VMess REALITY only: the short ID clients may use.
	RealityShortId string `json:"reality_short_id,omitempty"`
	// VLESS/VMess/Trojan transport: tcp, ws, httpupgrade, grpc or xhttp.
	Transport string `json:"transport,omitempty"`
	// VLESS/VMess/Trojan transport: the Host of WebSocket, HTTPUpgrade or XHTTP.
	Host string `json:"host,omitempty"`
	// VLESS/VMess/Trojan transport: the request path of WebSocket, HTTPUpgrade or XHTTP.
	Path string `json:"path,omitempty"`
	// VLESS/VMess/Trojan gRPC transport only: the gRPC service name.
	ServiceName string `json:"service_name,omitempty"`
	// Shadowsocks/SSR: the Shadowsocks method or the SSR cipher.
	Cipher string `json:"cipher,omitempty"`
	// Key-based protocols: the Shadowsocks 2022 server key, the SSR password or the Snell PSK.
	ServerKey string `json:"server_key,omitempty"`
	// Shadowsocks (AEAD/2022) only: the inbound plugin, such as obfs, v2ray-plugin, shadow-tls or restls.
	Plugin string `json:"plugin,omitempty"`
	// Shadowsocks (AEAD/2022) only: the structured options of the selected inbound plugin.
	PluginOptions any `json:"plugin_opts,omitempty"`
	// VLESS only: the XTLS Vision flow control; xtls-rprx-vision is currently the only valid value.
	Flow string `json:"flow,omitempty"`
	// Protocol-independent capability: UDP over TCP, for every protocol that supports UoT rather than one in particular.
	UoT bool `json:"uot,omitempty"`
	// Protocol-independent capability: the UoT version, currently 1 or 2; 0 means the default version.
	UoTVersion int `json:"uot_version,omitempty"`
	// Listener compatibility: whether to accept the PROXY protocol; node inbounds do not all enable it yet.
	AcceptProxyProtocol bool `json:"accept_proxy_protocol,omitempty"`
	// Hysteria2/TUIC-style QUIC protocols: the port hopping range; node inbounds do not enable port hopping yet.
	HopPorts string `json:"hop_ports,omitempty"`
	// Hysteria2/TUIC-style QUIC protocols: the port hopping interval; node inbounds do not enable port hopping yet.
	HopInterval int `json:"hop_interval,omitempty"`
	// Hysteria2 only: the Salamander obfuscation password, used only with obfs=salamander.
	ObfsPassword string `json:"obfs_password,omitempty"`
	// TLS client compatibility: disables SNI; server inbounds currently ignore it.
	DisableSNI bool `json:"disable_sni,omitempty"`
	// TUIC only: enables QUIC 0-RTT to save round trips on the first handshake.
	ReduceRtt bool `json:"reduce_rtt,omitempty"`
	// TUIC only: the connection heartbeat interval in seconds; 0 uses the node's default.
	Heartbeat int `json:"heartbeat,omitempty"`
	// TUIC/Hysteria compatibility: the UDP relay mode of the former implementation; node inbounds currently ignore it.
	UDPRelayMode string `json:"udp_relay_mode,omitempty"`
	// QUIC protocols: TUIC's congestion control field, and the legacy alias of Naive's.
	CongestionController string `json:"congestion_controller,omitempty"`
	// QUIC protocols: Naive's congestion control field, and the compatibility alias of TUIC's.
	QUICCongestionControl string `json:"quic_congestion_control,omitempty"`
	// Protocol-independent capability: the multiplexing level (off, low, medium or high), for the stream protocols that support mux.
	Multiplex string `json:"multiplex,omitempty"`
	// AnyTLS only: the TLS record padding scheme.
	PaddingScheme string `json:"padding_scheme,omitempty"`
	// Mieru only: the traffic pattern (packet length distribution) settings.
	TrafficPattern string `json:"traffic_pattern,omitempty"`
	// Mieru only: whether clients must send a user hint that identifies the user.
	UserHintIsMandatory bool `json:"user_hint_is_mandatory,omitempty"`
	// Hysteria2 only: the server's upload bandwidth in Mbps.
	UpMbps int `json:"up_mbps,omitempty"`
	// Hysteria2 only: the server's download bandwidth in Mbps.
	DownMbps int `json:"down_mbps,omitempty"`
	// Obfuscating protocols: Hysteria2's Salamander, Snell v5's obfs or SSR's obfs method.
	Obfs string `json:"obfs,omitempty"`
	// SSR only: the SSR protocol method, under the JSON name protocol, which is not the top-level type.
	SSRProtocol string `json:"protocol,omitempty"`
	// SSR only: the SSR protocol_param.
	ProtocolParam string `json:"protocol_param,omitempty"`
	// SSR only: the SSR obfs_param.
	ObfsParam string `json:"obfs_param,omitempty"`
	// Legacy obfuscation compatibility: the obfuscation Host; node inbounds currently ignore it, and Shadowsocks plugins take plugin_opts instead.
	ObfsHost string `json:"obfs_host,omitempty"`
	// Legacy obfuscation compatibility: the obfuscation request path; node inbounds currently ignore it, and Shadowsocks plugins take plugin_opts instead.
	ObfsPath string `json:"obfs_path,omitempty"`
	// VLESS/VMess/Trojan XHTTP transport only: the XHTTP mode, such as auto, packet-up or stream-up.
	XhttpMode string `json:"xhttp_mode,omitempty"`
	// VLESS/VMess/Trojan XHTTP transport only: the XHTTP extra path and parameters.
	XhttpExtra string `json:"xhttp_extra,omitempty"`
	// VLESS Encryption only: the cipher suite, such as none or mlkem768x25519plus.
	Encryption string `json:"encryption,omitempty"`
	// VLESS Encryption only: the key encapsulation mode, such as native, xorpub or random.
	EncryptionMode string `json:"encryption_mode,omitempty"`
	// VLESS Encryption only: the handshake round-trip mode, 0rtt or 1rtt.
	EncryptionRtt string `json:"encryption_rtt,omitempty"`
	// VLESS Encryption, server side only: the 0-RTT ticket.
	EncryptionTicket string `json:"encryption_ticket,omitempty"`
	// VLESS Encryption, server side only: the padding rules of the server direction.
	EncryptionServerPadding string `json:"encryption_server_padding,omitempty"`
	// VLESS Encryption, server side only: the ML-KEM/X25519 private key material.
	EncryptionPrivateKey string `json:"encryption_private_key,omitempty"`
	// VLESS Encryption client information: the padding rules of the client direction, for the subscription output.
	EncryptionClientPadding string `json:"encryption_client_padding,omitempty"`
	// VLESS Encryption client information: the 1-RTT (derived) authentication password, for the subscription output.
	EncryptionPassword string `json:"encryption_password,omitempty"`
	// Subscription clients: whether Encrypted ClientHello is enabled; left out of the configuration distributed to the nodes.
	EchEnable bool `json:"ech_enable,omitempty"`
	// Subscription clients: the outer ECH server name; left out of the configuration distributed to the nodes.
	EchServerName string `json:"ech_server_name,omitempty"`
	// Panel: the traffic billing ratio, 1 by default; not part of the protocol handshake.
	Ratio float64 `json:"ratio,omitempty"`
	// TLS protocols: the certificate source, file, self, http or dns; none configures no certificate.
	CertMode string `json:"cert_mode,omitempty"`
	// TLS protocols: the DNS provider used with cert_mode=dns.
	CertDNSProvider string `json:"cert_dns_provider,omitempty"`
	// TLS protocols: the environment variables (credentials) passed to the DNS provider with cert_mode=dns.
	CertDNSEnv string `json:"cert_dns_env"`
	// TLS protocols: the SHA256 fingerprint (lowercase hex) of the TLS leaf
	// certificate the node reports, kept only with cert_mode=self, for the
	// subscription clients to pin the certificate. The admin API neither reads
	// nor writes it: partial updates keep it by merging the stored fields back.
	CertPinSHA256 string `json:"cert_pin_sha256,omitempty"`
}

Protocol is one protocol a server offers, as stored in the server's protocols JSON.

func NormalizeProtocolForStorage

func NormalizeProtocolForStorage(protocol Protocol) (Protocol, error)

func SanitizeProtocolsForNodeDistribution

func SanitizeProtocolsForNodeDistribution(protocols []Protocol) []Protocol

func (*Protocol) Marshal

func (m *Protocol) Marshal() ([]byte, error)

Marshal returns the protocol's JSON form.

func (*Protocol) Unmarshal

func (m *Protocol) Unmarshal(data []byte) error

Unmarshal fills the protocol from its JSON form.

type Server

type Server struct {
	Id             int64      `gorm:"primary_key"`
	Name           string     `gorm:"type:varchar(100);not null;default:'';comment:Server Name"`
	Country        string     `gorm:"type:varchar(128);not null;default:'';comment:Country"`
	City           string     `gorm:"type:varchar(128);not null;default:'';comment:City"`
	Address        string     `gorm:"type:varchar(100);not null;default:'';comment:Server Address"`
	Sort           int        `gorm:"type:int;not null;default:0;comment:Sort"`
	Protocols      string     `gorm:"type:text;default:null;comment:Protocol"`
	LastReportedAt *time.Time `gorm:"comment:Last Reported Time"`
	CreatedAt      time.Time  `gorm:"<-:create;comment:Creation Time"`
	UpdatedAt      time.Time  `gorm:"comment:Update Time"`
}

Server is a proxy server: its address and location, and the protocols it offers, stored as JSON.

func (*Server) ApplyReportedCertPin added in v1.15.3

func (m *Server) ApplyReportedCertPin(protocolType, fingerprint string) (bool, error)

ApplyReportedCertPin records the certificate fingerprint a node reported for its protocol type. Only self-signed entries (cert_mode=self) accept the report; the node computes the fingerprint solely for certificates it generated itself, so a pin against any other cert_mode is stale by definition. It returns true when the stored protocols changed.

func (*Server) BeforeCreate

func (m *Server) BeforeCreate(tx *gorm.DB) error

func (*Server) BeforeDelete

func (m *Server) BeforeDelete(tx *gorm.DB) error

func (*Server) BeforeUpdate

func (m *Server) BeforeUpdate(tx *gorm.DB) error

func (*Server) MarshalProtocols

func (m *Server) MarshalProtocols(list []Protocol) error

MarshalProtocols stores list as the server's protocols. Every protocol needs a type, and no type may appear twice.

func (*Server) TableName

func (*Server) TableName() string

func (*Server) UnmarshalProtocols

func (m *Server) UnmarshalProtocols() ([]Protocol, error)

UnmarshalProtocols returns the server's stored protocols; an empty value holds none.

type ServerConfigOverride

type ServerConfigOverride struct {
	Id         int64     `gorm:"primary_key"`
	ServerId   int64     `gorm:"uniqueIndex;not null;comment:Server ID"`
	IPStrategy *string   `gorm:"type:varchar(32);comment:IP strategy override, NULL means inherit"`
	DNS        *string   `gorm:"type:text;comment:DNS override, NULL means inherit"`
	Block      *string   `gorm:"type:text;comment:Block override, NULL means inherit"`
	Outbound   *string   `gorm:"type:text;comment:Outbound override, NULL means inherit"`
	CreatedAt  time.Time `gorm:"<-:create;comment:Creation Time"`
	UpdatedAt  time.Time `gorm:"comment:Update Time"`
}

func (*ServerConfigOverride) TableName

func (*ServerConfigOverride) TableName() string

type SortItem

type SortItem struct {
	Id   int64
	Sort int64
}

SortItem is a list entry's position.

type Status

type Status struct {
	Cpu       float64 `json:"cpu"`
	Mem       float64 `json:"mem"`
	Disk      float64 `json:"disk"`
	UpdatedAt int64   `json:"updated_at"`
}

Status is the resource usage a server last reported.

func (*Status) Marshal

func (s *Status) Marshal() string

Marshal returns the status's JSON form.

func (*Status) Unmarshal

func (s *Status) Unmarshal(data string) error

Unmarshal fills the status from its JSON form.

Jump to

Keyboard shortcuts

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