render

package
v0.7.16 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package render 把 m-ui 数据模型(线路/上游/用户)渲染成一份 sing-box 配置。

一条线路 = 一个入站 + 一条 "inbound→outbound" 路由规则(带分流规则时前面再加几条,见 route_rules.go)。支持的入站协议: hysteria2 / anytls / tuic / trojan / vless / vmess / shadowsocks / socks / http / mixed。 TLS 三种模式:cert(节点证书)、reality、none;vless/vmess/trojan 可选 ws/grpc/httpupgrade/http 传输。 该渲染由 Hub 按节点执行,产物整份下发给对应 Agent。

Index

Constants

View Source
const (
	RouteDirect = 0
	RouteReject = -1
)
View Source
const GroupType = "urltest"

GroupType 上游组:几条上游放在一起,内核定时测,用延迟最低的那条能通的,挂了自动换。 就是 sing-box 的 urltest 出站;成员写的是上游名(也就是出站标签),改名时一起改(见 web)。

View Source
const PlaceholderName = "m-ui#nobody"

PlaceholderName 占位用户在数据面里的名字(见 placeholderUser)。它的凭据谁都不知道,不会有连接用这个名字认证进来。

Variables

View Source
var Protocols = map[string]struct {
	TLSRequired  bool   // 没有 TLS 就无法工作
	TLSDefault   string // Tls 为空时的默认模式
	Transport    bool   // 支持 ws/grpc/httpupgrade/http 传输
	HotUsers     bool   // 支持不断线热换用户表
	CredKey      string // 凭据键(shadowsocks 按算法另算)
	SingleSecret bool   // 单口令入站(用户级凭据不下发)
}{
	"hysteria2":   {TLSRequired: true, TLSDefault: "cert", HotUsers: true, CredKey: "hysteria2"},
	"anytls":      {TLSRequired: true, TLSDefault: "cert", HotUsers: true, CredKey: "anytls"},
	"tuic":        {TLSRequired: true, TLSDefault: "cert", HotUsers: true, CredKey: "tuic"},
	"trojan":      {TLSDefault: "cert", Transport: true, HotUsers: true, CredKey: "trojan"},
	"vless":       {TLSDefault: "reality", Transport: true, HotUsers: true, CredKey: "vless"},
	"vmess":       {TLSDefault: "none", Transport: true, HotUsers: true, CredKey: "vmess"},
	"shadowsocks": {TLSDefault: "none", HotUsers: true, CredKey: "shadowsocks"},
	"socks":       {TLSDefault: "none", CredKey: "socks"},
	"http":        {TLSDefault: "cert", CredKey: "http"},
	"mixed":       {TLSDefault: "none", CredKey: "socks"},
}

Protocols 是面板支持的入站协议及其特性。

View Source
var RouteRuleTypes = map[string]bool{"domain_suffix": true, "domain": true, "domain_keyword": true, "ip_cidr": true, "port": true}

RouteRuleTypes 支持的匹配方式。

Functions

func AllowPrivate added in v0.5.0

func AllowPrivate(db *gorm.DB) bool

AllowPrivate 设置 allowPrivate=true 时不加私网屏蔽规则(有人确实要经代理访问内网时打开)。

func BuildConfig

func BuildConfig(db *gorm.DB, cert NodeCert) ([]byte, error)

BuildConfig 从数据库读取本机应部署的线路/全部上游/用户,渲染成 sing-box 配置字节。

func CredKey

func CredKey(line model.Line) string

CredKey 返回线路对应的用户凭据键。

func EffectiveLines added in v0.6.12

func EffectiveLines(db *gorm.DB, userID uint) ([]model.UserLine, []model.UserLineNode, error)

EffectiveLines 读用户的线路分配(user_lines + user_line_nodes),代理名下的用户再与代理当前的授权取交集: 授权收回的线路不算;授权收窄到部分服务器时,只留这些服务器(用户原来是"全部"的,就是授权的那几台)。 库里的分配原样保留 —— 重新授权即恢复。数据面、订阅、落地页、下发给副机的快照都经过这里,口径一致。

副机不再求交集:它的授权表不由旧版主机下发,主机发来的快照里已经是交集后的分配。 userID 为 0 读全部用户。返回按 (user, line[, node]) 排好序,快照修订号据此稳定。

func ExpandGroups added in v0.7.16

func ExpandGroups(used map[uint]bool, all []model.Upstream)

ExpandGroups used 里的上游组,把成员也算上:线路只写了组,成员的出站同样要渲染、要巡检。

func ExpandIDs added in v0.7.16

func ExpandIDs(ids []uint, all []model.Upstream) []uint

ExpandIDs 同 ExpandGroups,给只有一串 id 的地方用。

func GroupMembers added in v0.7.16

func GroupMembers(u model.Upstream) []string

GroupMembers 上游组的成员名。

func HasRouteRules added in v0.6.14

func HasRouteRules(l model.Line) bool

HasRouteRules 线路是否用了分流规则。

func HasTransport

func HasTransport(line model.Line) bool

HasTransport 报告线路是否配置了非 TCP 传输。

func InboundJSON

func InboundJSON(line model.Line, cert NodeCert) (json.RawMessage, error)

InboundJSON 渲染单个线路的入站(含 TLS/传输,不含用户),供保存前校验与整体渲染共用。

func IsGroup added in v0.7.16

func IsGroup(u model.Upstream) bool

IsGroup 这条上游是不是上游组。

func LineOnNode

func LineOnNode(line model.Line, selfID uint) bool

LineOnNode 报告线路是否部署在某台服务器上:NodeIds 为空 = 全部;selfID 为 0(尚无本机记录)视为全部。

func LineUpstreams added in v0.6.14

func LineUpstreams(l model.Line) []uint

LineUpstreams 线路用到的上游 id:自己选的上游 + 分流规则指向的上游(去重,不含直连 / 拦截)。 删上游的引用检查、巡检和"未使用"判断都按它算。

func LocalNodeID

func LocalNodeID(db *gorm.DB) uint

LocalNodeID 返回本机在 nodes 表中的 id(没有则 0)。

func OutboundJSON

func OutboundJSON(u model.Upstream) (json.RawMessage, error)

OutboundJSON 渲染单个上游为 sing-box 出站(整体渲染与保存前隔离校验共用)。

func PureIPv6 added in v0.7.12

func PureIPv6(db *gorm.DB) bool

PureIPv6 本机只有 IPv6:探测到的公网地址(设置 publicIp,有 IPv4 时一定是 IPv4)本身是 IPv6。

func VisionEnabled

func VisionEnabled(line model.Line) bool

VisionEnabled 报告 vless 线路是否启用 xtls-rprx-vision(仅在有 TLS 且无传输时有效)。

Types

type NodeCert

type NodeCert struct {
	ServerName string
	CertPath   string
	KeyPath    string
	// Named 这台服务器的证书库:线路单独选了证书时按名字找;这台没有那张就用上面的默认证书,不让整份配置起不来。
	Named map[string]NodeCert
}

NodeCert 节点的服务端 TLS 材料(证书各机各签,路径固定)。

type RouteRule added in v0.6.14

type RouteRule struct {
	Name   string   `json:"name,omitempty"` // 可选的名字(查应用域名填的应用名),只用于显示,不影响分流
	Type   string   `json:"type"`           // 见 RouteRuleTypes
	Values []string `json:"values"`         // 任一命中即算命中
	To     int      `json:"to"`             // >0 上游 id;RouteDirect 直连;RouteReject 拦截
}

RouteRule 一条分流规则。

func ParseRouteRules added in v0.6.14

func ParseRouteRules(raw json.RawMessage) ([]RouteRule, error)

ParseRouteRules 读出并整理分流规则:去掉空白和重复,域名转小写,IP 写成网段,端口区间写成 a:b。 内容不合法时返回错误(说清是第几条的哪个值)。空 / null / [] 返回 nil, nil。

type TLSConfig

type TLSConfig struct {
	Mode        string `json:"mode"`           // cert | reality | none
	Cert        string `json:"cert,omitempty"` // cert 模式下单独选的证书(证书库里的名字);空 = 服务器的默认证书
	Fingerprint string `json:"fingerprint,omitempty"`
	Reality     struct {
		PrivateKey      string   `json:"private_key"`
		PublicKey       string   `json:"public_key"`
		ShortIDs        []string `json:"short_ids"`
		HandshakeServer string   `json:"handshake_server"`
		HandshakePort   int      `json:"handshake_port"`
	} `json:"reality"`
}

TLSConfig 是 Line.Tls 的结构。mode 为空时按协议取默认。

func ParseTLS

func ParseTLS(line model.Line) TLSConfig

ParseTLS 解析线路的 TLS 配置并补默认模式。

Jump to

Keyboard shortcuts

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