Files
way-node/internal/model/validate.go
T
admin 860fb66442
Build way-node / Test, Build, Push (push) Failing after 14s
feat: way-node — 完整更名与 Gitea CI
- Go module: github.com/cedar2025/xboard-node → github.com/viaeon/way-node(307 处 import 同步)
- cmd/xboard-node/ → cmd/way-node/
- install.sh: APP_NAME / INSTALL_ROOT(/etc/way-node) / BINARY_PATH / SERVICE_NAME 全部更名,
  下载源与 Documentaion 指向 git.viaeon.com/admin/way-node
- Makefile / Dockerfile 产物与镜像路径更名
- README 全篇重写链接
- 新增 .gitea/workflows/build.yml:容器内跑测试 + 交叉编译 + 构建镜像推 git.viaeon.com/admin/way-node
  构建全程在 golang:1.26 容器里,不依赖 runner 宿主装 Go
- 禁用 .github/workflows/ci.yml(依赖 ghcr.io 与 GITHUB_TOKEN,Gitea 上跑不通)

保留:xbctl 工具名(不含旧品牌)、go.sum 里上游依赖哈希
2026-11-27 11:41:30 +08:00

101 lines
3.1 KiB
Go

package model
import (
"fmt"
"strings"
"github.com/viaeon/way-node/internal/config"
)
func ValidateNodeSpec(n *NodeSpec, kcfg config.KernelConfig) error {
if n == nil {
return nil
}
effectiveKernelType := strings.TrimSpace(kcfg.Type)
if effectiveKernelType == "" {
effectiveKernelType = strings.TrimSpace(n.KernelType)
}
kernelType, err := normalizeKernelType(effectiveKernelType)
if err != nil {
return fmt.Errorf("normalize kernel type: %w", err)
}
additionalOutboundSources, err := collectAdditionalOutboundTagSources(kcfg.CustomConfig, kcfg.CustomOutbound)
if err != nil {
return fmt.Errorf("collect additional outbound tags: %w", err)
}
if err := validateOutboundTagCollisions(n.CustomOutbounds, additionalOutboundSources); err != nil {
return fmt.Errorf("validate outbound tags: %w", err)
}
additionalTags := additionalTagNames(additionalOutboundSources)
availableTags := buildAvailableOutboundTags(n.CustomOutbounds, additionalTags)
if err := ValidateCustomOutboundsForKernel(n.CustomOutbounds, kernelType, additionalTags); err != nil {
return fmt.Errorf("validate custom outbounds: %w", err)
}
if err := ValidateCustomRouteRules(n.CustomRouteRules, kernelType, availableTags); err != nil {
return fmt.Errorf("validate custom route rules: %w", err)
}
if err := validateTransportKernel(n.Network, kernelType); err != nil {
return err
}
return nil
}
// singboxUnsupportedTransports lists transport types that sing-box does not support.
var singboxUnsupportedTransports = map[string]bool{
"xhttp": true,
"splithttp": true,
}
func validateTransportKernel(network, kernelType string) error {
net := strings.ToLower(strings.TrimSpace(network))
if kernelType == "singbox" && singboxUnsupportedTransports[net] {
return fmt.Errorf("transport %q is not supported by sing-box kernel; use xray kernel instead", net)
}
return nil
}
// ResolveKernelForTransport returns the kernel type required by the given
// transport. If the configured kernel cannot handle the transport, it returns
// the kernel that can. Otherwise it returns configuredKernel unchanged.
// This is used in machine mode to auto-switch kernel per node.
func ResolveKernelForTransport(network, configuredKernel string) string {
net := strings.ToLower(strings.TrimSpace(network))
if configuredKernel == "singbox" && singboxUnsupportedTransports[net] {
return "xray"
}
return configuredKernel
}
func normalizeKernelType(value string) (string, error) {
switch strings.ToLower(strings.TrimSpace(value)) {
case "singbox", "sing-box":
return "singbox", nil
case "xray":
return "xray", nil
default:
return "", fmt.Errorf("unsupported kernel type %q", value)
}
}
func buildAvailableOutboundTags(structured []OutboundConfig, rawTags []string) map[string]struct{} {
available := map[string]struct{}{
"direct": {},
"block": {},
}
for _, outbound := range structured {
tag := strings.ToLower(strings.TrimSpace(outbound.Tag))
if tag != "" {
available[tag] = struct{}{}
}
}
for _, tag := range rawTags {
tag = strings.ToLower(strings.TrimSpace(tag))
if tag != "" {
available[tag] = struct{}{}
}
}
return available
}