feat: add standalone controlplane support
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/cedar2025/xboard-node/internal/config"
|
||||
"github.com/cedar2025/xboard-node/internal/panel"
|
||||
)
|
||||
|
||||
func NodeSpecFromPanel(nc *panel.NodeConfig) *NodeSpec {
|
||||
if nc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var certCfg *config.CertConfig
|
||||
if nc.CertConfig != nil {
|
||||
certCfg = &config.CertConfig{
|
||||
CertMode: nc.CertConfig.CertMode,
|
||||
Domain: nc.CertConfig.Domain,
|
||||
Email: nc.CertConfig.Email,
|
||||
DNSProvider: nc.CertConfig.DNSProvider,
|
||||
DNSEnv: nc.CertConfig.DNSEnv,
|
||||
HTTPPort: nc.CertConfig.HTTPPort,
|
||||
CertFile: nc.CertConfig.CertFile,
|
||||
KeyFile: nc.CertConfig.KeyFile,
|
||||
CertContent: nc.CertConfig.CertContent,
|
||||
KeyContent: nc.CertConfig.KeyContent,
|
||||
}
|
||||
}
|
||||
|
||||
var multiplex *MultiplexConfig
|
||||
if nc.Multiplex != nil {
|
||||
multiplex = &MultiplexConfig{
|
||||
Enabled: nc.Multiplex.Enabled,
|
||||
Protocol: nc.Multiplex.Protocol,
|
||||
MaxConnections: nc.Multiplex.MaxConnections,
|
||||
MinStreams: nc.Multiplex.MinStreams,
|
||||
MaxStreams: nc.Multiplex.MaxStreams,
|
||||
Padding: nc.Multiplex.Padding,
|
||||
}
|
||||
if nc.Multiplex.Brutal != nil {
|
||||
multiplex.Brutal = &BrutalConfig{
|
||||
Enabled: nc.Multiplex.Brutal.Enabled,
|
||||
UpMbps: nc.Multiplex.Brutal.UpMbps,
|
||||
DownMbps: nc.Multiplex.Brutal.DownMbps,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routes := make([]RouteRule, 0, len(nc.Routes))
|
||||
for _, route := range nc.Routes {
|
||||
routes = append(routes, RouteRule{
|
||||
ID: route.ID,
|
||||
Match: cloneStringSlice(route.Match),
|
||||
Action: route.Action,
|
||||
ActionValue: route.ActionValue,
|
||||
})
|
||||
}
|
||||
|
||||
outbounds := make([]OutboundConfig, 0, len(nc.CustomOutbounds))
|
||||
for _, outbound := range nc.CustomOutbounds {
|
||||
outbounds = append(outbounds, OutboundConfig{
|
||||
Tag: outbound.Tag,
|
||||
Protocol: outbound.Protocol,
|
||||
Settings: cloneAnyMap(outbound.Settings),
|
||||
ProxyTag: outbound.ProxyTag,
|
||||
})
|
||||
}
|
||||
|
||||
return &NodeSpec{
|
||||
Protocol: nc.Protocol,
|
||||
ListenIP: nc.ListenIP,
|
||||
ServerPort: nc.ServerPort,
|
||||
Network: nc.Network,
|
||||
NetworkSettings: cloneAnyMap(nc.NetworkSettings),
|
||||
Routes: routes,
|
||||
KernelType: nc.KernelType,
|
||||
KernelLogLevel: nc.KernelLogLevel,
|
||||
CustomOutbounds: outbounds,
|
||||
CustomRoutes: cloneMapSlice(nc.CustomRoutes),
|
||||
CertConfig: certCfg,
|
||||
AutoTLS: nc.AutoTLS,
|
||||
Domain: nc.Domain,
|
||||
Cipher: nc.Cipher,
|
||||
Plugin: nc.Plugin,
|
||||
PluginOpt: nc.PluginOpt,
|
||||
ServerKey: nc.ServerKey,
|
||||
TLS: nc.TLS,
|
||||
Flow: nc.Flow,
|
||||
Decryption: nc.Decryption,
|
||||
TLSSettings: cloneAnyMap(nc.TLSSettings),
|
||||
Host: nc.Host,
|
||||
ServerName: nc.ServerName,
|
||||
Version: nc.Version,
|
||||
UpMbps: nc.UpMbps,
|
||||
DownMbps: nc.DownMbps,
|
||||
Obfs: nc.Obfs,
|
||||
ObfsPassword: nc.ObfsPassword,
|
||||
CongestionControl: nc.CongestionControl,
|
||||
PaddingScheme: string(nc.PaddingScheme),
|
||||
Transport: nc.Transport,
|
||||
TrafficPattern: nc.TrafficPattern,
|
||||
Multiplex: multiplex,
|
||||
AcceptProxyProtocol: nc.AcceptProxyProtocol,
|
||||
}
|
||||
}
|
||||
|
||||
func UserSpecsFromPanel(users []panel.User) []UserSpec {
|
||||
if users == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]UserSpec, 0, len(users))
|
||||
for _, user := range users {
|
||||
out = append(out, UserSpec{ID: user.ID, UUID: user.UUID, SpeedLimit: user.SpeedLimit, DeviceLimit: user.DeviceLimit})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (n *NodeSpec) ToPanel() *panel.NodeConfig {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var certCfg *panel.CertConfig
|
||||
if n.CertConfig != nil {
|
||||
certCfg = &panel.CertConfig{
|
||||
CertMode: n.CertConfig.CertMode,
|
||||
Domain: n.CertConfig.Domain,
|
||||
Email: n.CertConfig.Email,
|
||||
DNSProvider: n.CertConfig.DNSProvider,
|
||||
DNSEnv: n.CertConfig.DNSEnv,
|
||||
HTTPPort: n.CertConfig.HTTPPort,
|
||||
CertFile: n.CertConfig.CertFile,
|
||||
KeyFile: n.CertConfig.KeyFile,
|
||||
CertContent: n.CertConfig.CertContent,
|
||||
KeyContent: n.CertConfig.KeyContent,
|
||||
}
|
||||
}
|
||||
|
||||
var multiplex *panel.MultiplexConfig
|
||||
if n.Multiplex != nil {
|
||||
multiplex = &panel.MultiplexConfig{
|
||||
Enabled: n.Multiplex.Enabled,
|
||||
Protocol: n.Multiplex.Protocol,
|
||||
MaxConnections: n.Multiplex.MaxConnections,
|
||||
MinStreams: n.Multiplex.MinStreams,
|
||||
MaxStreams: n.Multiplex.MaxStreams,
|
||||
Padding: n.Multiplex.Padding,
|
||||
}
|
||||
if n.Multiplex.Brutal != nil {
|
||||
multiplex.Brutal = &panel.BrutalConfig{
|
||||
Enabled: n.Multiplex.Brutal.Enabled,
|
||||
UpMbps: n.Multiplex.Brutal.UpMbps,
|
||||
DownMbps: n.Multiplex.Brutal.DownMbps,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routes := make([]panel.RouteRule, 0, len(n.Routes))
|
||||
for _, route := range n.Routes {
|
||||
routes = append(routes, panel.RouteRule{
|
||||
ID: route.ID,
|
||||
Match: cloneStringSlice(route.Match),
|
||||
Action: route.Action,
|
||||
ActionValue: route.ActionValue,
|
||||
})
|
||||
}
|
||||
|
||||
outbounds := make([]panel.OutboundConfig, 0, len(n.CustomOutbounds))
|
||||
for _, outbound := range n.CustomOutbounds {
|
||||
outbounds = append(outbounds, panel.OutboundConfig{
|
||||
Tag: outbound.Tag,
|
||||
Protocol: outbound.Protocol,
|
||||
Settings: cloneAnyMap(outbound.Settings),
|
||||
ProxyTag: outbound.ProxyTag,
|
||||
})
|
||||
}
|
||||
|
||||
return &panel.NodeConfig{
|
||||
Protocol: n.Protocol,
|
||||
ListenIP: n.ListenIP,
|
||||
ServerPort: n.ServerPort,
|
||||
Network: n.Network,
|
||||
NetworkSettings: cloneAnyMap(n.NetworkSettings),
|
||||
Routes: routes,
|
||||
KernelType: n.KernelType,
|
||||
KernelLogLevel: n.KernelLogLevel,
|
||||
CustomOutbounds: outbounds,
|
||||
CustomRoutes: cloneMapSlice(n.CustomRoutes),
|
||||
CertConfig: certCfg,
|
||||
AutoTLS: n.AutoTLS,
|
||||
Domain: n.Domain,
|
||||
Cipher: n.Cipher,
|
||||
Plugin: n.Plugin,
|
||||
PluginOpt: n.PluginOpt,
|
||||
ServerKey: n.ServerKey,
|
||||
TLS: n.TLS,
|
||||
Flow: n.Flow,
|
||||
Decryption: n.Decryption,
|
||||
TLSSettings: cloneAnyMap(n.TLSSettings),
|
||||
Host: n.Host,
|
||||
ServerName: n.ServerName,
|
||||
Version: n.Version,
|
||||
UpMbps: n.UpMbps,
|
||||
DownMbps: n.DownMbps,
|
||||
Obfs: n.Obfs,
|
||||
ObfsPassword: n.ObfsPassword,
|
||||
CongestionControl: n.CongestionControl,
|
||||
PaddingScheme: panel.StringOrArray(n.PaddingScheme),
|
||||
Transport: n.Transport,
|
||||
TrafficPattern: n.TrafficPattern,
|
||||
Multiplex: multiplex,
|
||||
AcceptProxyProtocol: n.AcceptProxyProtocol,
|
||||
}
|
||||
}
|
||||
|
||||
func UserSpecsToPanel(users []UserSpec) []panel.User {
|
||||
if users == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]panel.User, 0, len(users))
|
||||
for _, user := range users {
|
||||
out = append(out, panel.User{ID: user.ID, UUID: user.UUID, SpeedLimit: user.SpeedLimit, DeviceLimit: user.DeviceLimit})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package model
|
||||
|
||||
import "github.com/cedar2025/xboard-node/internal/config"
|
||||
|
||||
func NodeSpecFromStandalone(cfg *config.Config) *NodeSpec {
|
||||
sc := cfg.Standalone
|
||||
if sc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
routes := make([]RouteRule, 0, len(sc.Node.Routes))
|
||||
for _, route := range sc.Node.Routes {
|
||||
routes = append(routes, RouteRule{
|
||||
ID: route.ID,
|
||||
Match: cloneStringSlice(route.Match),
|
||||
Action: route.Action,
|
||||
ActionValue: route.ActionValue,
|
||||
})
|
||||
}
|
||||
|
||||
var multiplex *MultiplexConfig
|
||||
if sc.Node.Multiplex != nil {
|
||||
multiplex = &MultiplexConfig{
|
||||
Enabled: sc.Node.Multiplex.Enabled,
|
||||
Protocol: sc.Node.Multiplex.Protocol,
|
||||
MaxConnections: sc.Node.Multiplex.MaxConnections,
|
||||
MinStreams: sc.Node.Multiplex.MinStreams,
|
||||
MaxStreams: sc.Node.Multiplex.MaxStreams,
|
||||
Padding: sc.Node.Multiplex.Padding,
|
||||
}
|
||||
if sc.Node.Multiplex.Brutal != nil {
|
||||
multiplex.Brutal = &BrutalConfig{
|
||||
Enabled: sc.Node.Multiplex.Brutal.Enabled,
|
||||
UpMbps: sc.Node.Multiplex.Brutal.UpMbps,
|
||||
DownMbps: sc.Node.Multiplex.Brutal.DownMbps,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &NodeSpec{
|
||||
Protocol: sc.Node.Protocol,
|
||||
ListenIP: sc.Node.ListenIP,
|
||||
ServerPort: sc.Node.ServerPort,
|
||||
Network: sc.Node.Network,
|
||||
NetworkSettings: cloneAnyMap(sc.Node.NetworkSettings),
|
||||
Routes: routes,
|
||||
KernelType: cfg.Kernel.Type,
|
||||
KernelLogLevel: cfg.Kernel.LogLevel,
|
||||
Cipher: sc.Node.Cipher,
|
||||
Plugin: sc.Node.Plugin,
|
||||
PluginOpt: sc.Node.PluginOpt,
|
||||
ServerKey: sc.Node.ServerKey,
|
||||
TLS: sc.Node.TLS,
|
||||
Flow: sc.Node.Flow,
|
||||
Decryption: sc.Node.Decryption,
|
||||
TLSSettings: cloneAnyMap(sc.Node.TLSSettings),
|
||||
Host: sc.Node.Host,
|
||||
ServerName: sc.Node.ServerName,
|
||||
Version: sc.Node.Version,
|
||||
UpMbps: sc.Node.UpMbps,
|
||||
DownMbps: sc.Node.DownMbps,
|
||||
Obfs: sc.Node.Obfs,
|
||||
ObfsPassword: sc.Node.ObfsPassword,
|
||||
CongestionControl: sc.Node.CongestionControl,
|
||||
PaddingScheme: sc.Node.PaddingScheme,
|
||||
Transport: sc.Node.Transport,
|
||||
TrafficPattern: sc.Node.TrafficPattern,
|
||||
Multiplex: multiplex,
|
||||
AcceptProxyProtocol: sc.Node.AcceptProxyProtocol,
|
||||
}
|
||||
}
|
||||
|
||||
func UserSpecsFromStandalone(cfg *config.Config) []UserSpec {
|
||||
if cfg.Standalone == nil {
|
||||
return nil
|
||||
}
|
||||
users := make([]UserSpec, 0, len(cfg.Standalone.Users))
|
||||
for _, user := range cfg.Standalone.Users {
|
||||
users = append(users, UserSpec{ID: user.ID, UUID: user.UUID, SpeedLimit: user.SpeedLimit, DeviceLimit: user.DeviceLimit})
|
||||
}
|
||||
return users
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cedar2025/xboard-node/internal/config"
|
||||
)
|
||||
|
||||
func TestNodeSpecFromStandalone(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Kernel: config.KernelConfig{Type: "xray", LogLevel: "info"},
|
||||
Standalone: &config.StandaloneConfig{
|
||||
Enabled: true,
|
||||
Node: config.StandaloneNodeConfig{
|
||||
Protocol: "vless",
|
||||
ListenIP: "::",
|
||||
ServerPort: 8443,
|
||||
Network: "ws",
|
||||
NetworkSettings: map[string]any{"path": "/ws"},
|
||||
TLS: 1,
|
||||
Host: "node.example.com",
|
||||
ServerName: "node.example.com",
|
||||
AcceptProxyProtocol: true,
|
||||
PaddingScheme: "stop=8\n0=30-30",
|
||||
Routes: []config.StandaloneRouteRule{{ID: 1, Match: []string{"example.com"}, Action: "direct"}},
|
||||
Multiplex: &config.StandaloneMultiplexConfig{Enabled: true, Protocol: "smux", MaxConnections: 4},
|
||||
},
|
||||
Users: []config.StandaloneUser{{ID: 7, UUID: "11111111-1111-1111-1111-111111111111", SpeedLimit: 50, DeviceLimit: 2}},
|
||||
},
|
||||
}
|
||||
|
||||
nc := NodeSpecFromStandalone(cfg)
|
||||
users := UserSpecsFromStandalone(cfg)
|
||||
if nc.Protocol != "vless" || nc.ServerPort != 8443 {
|
||||
t.Fatalf("unexpected node spec: %#v", nc)
|
||||
}
|
||||
if nc.NetworkSettings["path"] != "/ws" {
|
||||
t.Fatalf("network_settings.path: got %v", nc.NetworkSettings["path"])
|
||||
}
|
||||
if nc.PaddingScheme != "stop=8\n0=30-30" {
|
||||
t.Fatalf("padding_scheme: got %q", nc.PaddingScheme)
|
||||
}
|
||||
if nc.Multiplex == nil || !nc.Multiplex.Enabled {
|
||||
t.Fatal("expected multiplex config to be copied")
|
||||
}
|
||||
if len(users) != 1 || users[0].DeviceLimit != 2 {
|
||||
t.Fatalf("users: %#v", users)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package model
|
||||
|
||||
import "github.com/cedar2025/xboard-node/internal/config"
|
||||
|
||||
type NodeSpec struct {
|
||||
Protocol string
|
||||
ListenIP string
|
||||
ServerPort int
|
||||
Network string
|
||||
NetworkSettings map[string]any
|
||||
Routes []RouteRule
|
||||
|
||||
KernelType string
|
||||
KernelLogLevel string
|
||||
CustomOutbounds []OutboundConfig
|
||||
CustomRoutes []map[string]any
|
||||
CertConfig *config.CertConfig
|
||||
AutoTLS bool
|
||||
Domain string
|
||||
|
||||
Cipher string
|
||||
Plugin string
|
||||
PluginOpt string
|
||||
ServerKey string
|
||||
|
||||
TLS int
|
||||
Flow string
|
||||
Decryption string
|
||||
TLSSettings map[string]any
|
||||
|
||||
Host string
|
||||
ServerName string
|
||||
|
||||
Version int
|
||||
UpMbps int
|
||||
DownMbps int
|
||||
Obfs string
|
||||
ObfsPassword string
|
||||
|
||||
CongestionControl string
|
||||
PaddingScheme string
|
||||
Transport string
|
||||
TrafficPattern string
|
||||
|
||||
Multiplex *MultiplexConfig
|
||||
AcceptProxyProtocol bool
|
||||
}
|
||||
|
||||
type OutboundConfig struct {
|
||||
Tag string
|
||||
Protocol string
|
||||
Settings map[string]any
|
||||
ProxyTag string
|
||||
}
|
||||
|
||||
type RouteRule struct {
|
||||
ID int
|
||||
Match []string
|
||||
Action string
|
||||
ActionValue string
|
||||
}
|
||||
|
||||
type MultiplexConfig struct {
|
||||
Enabled bool
|
||||
Protocol string
|
||||
MaxConnections int
|
||||
MinStreams int
|
||||
MaxStreams int
|
||||
Padding bool
|
||||
Brutal *BrutalConfig
|
||||
}
|
||||
|
||||
type BrutalConfig struct {
|
||||
Enabled bool
|
||||
UpMbps int
|
||||
DownMbps int
|
||||
}
|
||||
|
||||
type UserSpec struct {
|
||||
ID int
|
||||
UUID string
|
||||
SpeedLimit int
|
||||
DeviceLimit int
|
||||
}
|
||||
|
||||
func (n *NodeSpec) GetProxyProtocol() bool {
|
||||
if n == nil {
|
||||
return false
|
||||
}
|
||||
if n.AcceptProxyProtocol {
|
||||
return true
|
||||
}
|
||||
if n.NetworkSettings != nil {
|
||||
if v, ok := n.NetworkSettings["acceptProxyProtocol"]; ok {
|
||||
if b, ok := v.(bool); ok {
|
||||
return b
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cloneAnyMap(src map[string]any) map[string]any {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(src))
|
||||
for k, v := range src {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneMapSlice(src []map[string]any) []map[string]any {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]map[string]any, 0, len(src))
|
||||
for _, item := range src {
|
||||
out = append(out, cloneAnyMap(item))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneStringSlice(src []string) []string {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, len(src))
|
||||
copy(out, src)
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user