cadd527680
- 新增 internal/dpcred 包,统一 DeriveHy2Password + DefaultFlow agentd 与 HTTP connect handler 共享同一实现 - 新增迁移 000011:nodes 表拆分 reality_prk 私钥 / reality_pbk 公钥 reality_short_id;修正 handler_grpc.go 使用私钥字段 - 新增迁移 000012:connect_credentials 持久化凭证 实现 CredentialsForNode 修复 agent 重连 resync 原先返回空的桩 - 扩展 NodeStore 接口:ListUp / EntitlementForUser / PersistCredential / DeleteCredential;同步 grpc_test.go mock - 新增 httpapi/nodes.go:GET /nodes、POST /nodes/id/connect Hub.Push + PersistCredential + 渲染完整 sing-box client 配置 JSON POST /nodes/id/disconnect - 新增 httpapi/account.go:GET /me、GET /plans、GET /notices - 新增 httpapi/clientconfig.go:BuildClientConfig 服务端渲染 - 重写 cmd/server/main.go:手写 chi public/protected 分组 nodes.Service/Hub 在 main 构造并共享;SMTPMailer/LogMailer go build ./... && go vet ./... && go test ./... 全部通过 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
156 lines
4.2 KiB
Go
156 lines
4.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/dpcred"
|
|
"github.com/wangjia/pangolin/server/internal/nodes"
|
|
)
|
|
|
|
// BuildClientConfig renders a complete sing-box CLIENT configuration JSON that
|
|
// the client app passes verbatim to the local tunnel kernel.
|
|
//
|
|
// Design rule (ARCHITECTURE.md §3.1): the Dart/Flutter client MUST NOT assemble
|
|
// or modify the config — it is rendered here, server-side, and returned raw.
|
|
//
|
|
// Parameters:
|
|
// - node: the target node row (provides endpoint, keys, ports)
|
|
// - dpUUID: the authenticated user's data-plane UUID (used as VLESS uuid)
|
|
// - deriveKey: shared HMAC key used by both server and agent to derive the
|
|
// Hysteria2 password from dp_uuid (must equal PANGOLIN_AGENT_DERIVE_KEY)
|
|
// - ttlSeconds: credential lifetime hint; not embedded in the config but can
|
|
// be used by callers to set a session timer
|
|
func BuildClientConfig(node *nodes.NodeRow, dpUUID, deriveKey string) ([]byte, error) {
|
|
// Parse host:port from endpoint; endpoint format is "host:port".
|
|
host, _ := splitHostPort(node.Endpoint)
|
|
if host == "" {
|
|
host = node.Endpoint
|
|
}
|
|
|
|
realityPublicKey := node.RealityPBK
|
|
realityShortID := node.RealityShortID
|
|
hy2Password := dpcred.DeriveHy2Password(dpUUID, deriveKey)
|
|
hy2Port := int32(443) // default
|
|
if node.Hy2Port.Valid {
|
|
hy2Port = node.Hy2Port.Int32
|
|
}
|
|
|
|
// REALITY outbound (VLESS + REALITY TLS, TCP 443).
|
|
realityOut := map[string]any{
|
|
"type": "vless",
|
|
"tag": "reality-out",
|
|
"server": host,
|
|
"server_port": 11443, // REALITY always uses port from endpoint
|
|
"uuid": dpUUID,
|
|
"flow": dpcred.DefaultFlow,
|
|
"tls": map[string]any{
|
|
"enabled": true,
|
|
"server_name": node.RealitySNI,
|
|
"utls": map[string]any{
|
|
"enabled": true,
|
|
"fingerprint": "chrome",
|
|
},
|
|
"reality": map[string]any{
|
|
"enabled": true,
|
|
"public_key": realityPublicKey,
|
|
"short_id": realityShortID,
|
|
},
|
|
},
|
|
}
|
|
|
|
// Parse the REALITY listen port from endpoint.
|
|
if _, portStr := splitHostPort(node.Endpoint); portStr != "" {
|
|
port := 0
|
|
for _, ch := range portStr {
|
|
if ch >= '0' && ch <= '9' {
|
|
port = port*10 + int(ch-'0')
|
|
}
|
|
}
|
|
if port > 0 {
|
|
realityOut["server_port"] = port
|
|
}
|
|
}
|
|
|
|
// Hysteria2 outbound (UDP 443).
|
|
hy2Out := map[string]any{
|
|
"type": "hysteria2",
|
|
"tag": "hy2-out",
|
|
"server": host,
|
|
"server_port": hy2Port,
|
|
"password": hy2Password,
|
|
"tls": map[string]any{
|
|
"enabled": true,
|
|
"alpn": []string{"h3"},
|
|
},
|
|
}
|
|
|
|
// TUN inbound with kill-switch (strict_route).
|
|
tunIn := map[string]any{
|
|
"type": "tun",
|
|
"tag": "tun-in",
|
|
"address": []string{"172.19.0.1/30"},
|
|
"mtu": 9000,
|
|
"auto_route": true,
|
|
"strict_route": true,
|
|
"stack": "system",
|
|
}
|
|
|
|
// urltest auto-select outbound.
|
|
autoBest := map[string]any{
|
|
"type": "urltest",
|
|
"tag": "auto",
|
|
"outbounds": []string{"reality-out", "hy2-out"},
|
|
"url": "https://www.gstatic.com/generate_204",
|
|
"interval": "3m",
|
|
"tolerance": 50,
|
|
}
|
|
|
|
// Route: LAN direct, everything else via auto.
|
|
route := map[string]any{
|
|
"rules": []any{
|
|
map[string]any{
|
|
"ip_cidr": []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8"},
|
|
"outbound": "direct",
|
|
},
|
|
},
|
|
"final": "auto",
|
|
"auto_detect_interface": true,
|
|
}
|
|
|
|
// DNS: remote over tunnel, local for domestic.
|
|
dns := map[string]any{
|
|
"servers": []any{
|
|
map[string]any{"tag": "remote", "address": "tls://8.8.8.8", "detour": "auto"},
|
|
map[string]any{"tag": "local", "address": "223.5.5.5", "detour": "direct"},
|
|
},
|
|
"final": "remote",
|
|
"strategy": "ipv4_only",
|
|
}
|
|
|
|
cfg := map[string]any{
|
|
"log": map[string]any{"level": "warn", "timestamp": true},
|
|
"inbounds": []any{tunIn},
|
|
"outbounds": []any{
|
|
realityOut,
|
|
hy2Out,
|
|
autoBest,
|
|
map[string]any{"type": "block", "tag": "block"},
|
|
map[string]any{"type": "direct", "tag": "direct"},
|
|
},
|
|
"route": route,
|
|
"dns": dns,
|
|
}
|
|
|
|
return json.Marshal(cfg)
|
|
}
|
|
|
|
// splitHostPort splits "host:port" into (host, port). Returns ("", "") on failure.
|
|
func splitHostPort(s string) (host, port string) {
|
|
i := strings.LastIndexByte(s, ':')
|
|
if i < 0 {
|
|
return s, ""
|
|
}
|
|
return s[:i], s[i+1:]
|
|
}
|