Add relay node management and pin manager header layout

- server: add, delete and set-default node endpoints writing cluster.json with validation

- deploy: grant ads-server write access to /etc/ads

- flutter: relay node page with admin token plumbing, stop reconnecting devices removed from device list

- flutter: fixed header heights and strut so tab switching never shifts refresh/notice
This commit is contained in:
tongtong committed 2026-11-16 16:44:58 +08:00
1 parent e12cb76d68
commit 0a04d5990f
9 files changed
+606 -75

No files matched your search

+192 -11
View File
@@ -27,6 +27,7 @@ type NodeConfig struct {
URL string `json:"url"`
ConnectAddress string `json:"connect_address,omitempty"`
Priority int `json:"priority"`
Default bool `json:"default,omitempty"`
}
type clusterConfig struct {
Nodes []NodeConfig `json:"nodes"`
@@ -41,6 +42,8 @@ type nodeHealth struct {
type cluster struct {
config clusterConfig
secret string
path string
allowInsecure bool
mu sync.Mutex
health map[string]nodeHealth
signalingBytes atomic.Int64
@@ -52,6 +55,19 @@ type route struct {
Ticket string `json:"ticket"`
}
func validateNode(node NodeConfig, allowInsecure bool) error {
u, err := url.Parse(node.URL)
if err != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Scheme != "wss" && !(allowInsecure && u.Scheme == "ws")) || !validID(node.ID) {
return errors.New("invalid relay node id or URL")
}
if node.ConnectAddress != "" {
if _, _, err = net.SplitHostPort(node.ConnectAddress); err != nil {
return errors.New("invalid node connect_address")
}
}
return nil
}
func loadCluster(path, secret string, allowInsecure bool) (*cluster, error) {
if len(secret) < 32 {
return nil, errors.New("cluster mode requires ADS_RELAY_SECRET (32+ characters)")
@@ -77,24 +93,25 @@ func loadCluster(path, secret string, allowInsecure bool) (*cluster, error) {
return nil, errors.New("p2p_timeout_ms must be 1000–30000")
}
seen := map[string]bool{}
defaults := 0
for _, node := range config.Nodes {
u, e := url.Parse(node.URL)
if e != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Scheme != "wss" && !(allowInsecure && u.Scheme == "ws")) || !validID(node.ID) || seen[node.ID] {
if err = validateNode(node, allowInsecure); err != nil || seen[node.ID] {
return nil, errors.New("invalid or duplicate relay node")
}
if node.ConnectAddress != "" {
if _, _, e = net.SplitHostPort(node.ConnectAddress); e != nil {
return nil, errors.New("invalid node connect_address")
}
if node.Default {
defaults++
}
seen[node.ID] = true
}
if defaults > 1 {
return nil, errors.New("at most one default relay node")
}
for _, ice := range config.ICEServers {
if !strings.HasPrefix(ice, "stun:") {
return nil, errors.New("only STUN URLs are accepted; fallback uses WSS relay nodes")
}
}
return &cluster{config: config, secret: secret, health: map[string]nodeHealth{}}, nil
return &cluster{config: config, secret: secret, path: path, allowInsecure: allowInsecure, health: map[string]nodeHealth{}}, nil
}
func nodeHTTP(node NodeConfig, path string) (*http.Client, string) {
@@ -114,10 +131,20 @@ func nodeHTTP(node NodeConfig, path string) (*http.Client, string) {
return &http.Client{Transport: transport, Timeout: 3 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, u.String()
}
func (c *cluster) snapshot() clusterConfig {
c.mu.Lock()
defer c.mu.Unlock()
config := c.config
config.Nodes = append([]NodeConfig{}, c.config.Nodes...)
config.ICEServers = append([]string{}, c.config.ICEServers...)
return config
}
func (c *cluster) monitor(ctx context.Context) {
probe := func() {
nodes := c.snapshot().Nodes
var wg sync.WaitGroup
for _, node := range c.config.Nodes {
for _, node := range nodes {
wg.Add(1)
go func(node NodeConfig) {
defer wg.Done()
@@ -158,13 +185,16 @@ func (c *cluster) monitor(ctx context.Context) {
}
func (c *cluster) routes(session, lease, side string) []route {
nodes := append([]NodeConfig(nil), c.config.Nodes...)
nodes := c.snapshot().Nodes
c.mu.Lock()
sort.SliceStable(nodes, func(i, j int) bool {
a, b := c.health[nodes[i].ID], c.health[nodes[j].ID]
if a.Healthy != b.Healthy {
return a.Healthy
}
if nodes[i].Default != nodes[j].Default {
return nodes[i].Default
}
if nodes[i].Priority != nodes[j].Priority {
return nodes[i].Priority < nodes[j].Priority
}
@@ -185,7 +215,7 @@ func (c *cluster) revoke(lease string) {
}
body, _ := json.Marshal(map[string]string{"lease": lease})
var wg sync.WaitGroup
for _, node := range c.config.Nodes {
for _, node := range c.snapshot().Nodes {
wg.Add(1)
go func(node NodeConfig) {
defer wg.Done()
@@ -202,6 +232,156 @@ func (c *cluster) revoke(lease string) {
wg.Wait()
}
var errNodeNotFound = errors.New("relay node not found")
// mutate applies fn to a copy of the configuration and persists it before publishing.
func (c *cluster) mutate(fn func(*clusterConfig) error) error {
c.mu.Lock()
defer c.mu.Unlock()
next := c.config
next.Nodes = append([]NodeConfig{}, c.config.Nodes...)
next.ICEServers = append([]string{}, c.config.ICEServers...)
if err := fn(&next); err != nil {
return err
}
data, err := json.MarshalIndent(next, "", " ")
if err != nil {
return err
}
tmp := c.path + ".tmp"
if err = os.WriteFile(tmp, append(data, '\n'), 0o644); err != nil {
return err
}
if err = os.Rename(tmp, c.path); err != nil {
return err
}
c.config = next
for id := range c.health {
kept := false
for _, node := range next.Nodes {
if node.ID == id {
kept = true
break
}
}
if !kept {
delete(c.health, id)
}
}
return nil
}
func (b *broker) addNodeEndpoint(w http.ResponseWriter, r *http.Request) {
if b.cluster == nil {
http.Error(w, "cluster not configured", 404)
return
}
var node NodeConfig
if err := json.NewDecoder(io.LimitReader(r.Body, 8192)).Decode(&node); err != nil {
http.Error(w, "invalid JSON body", 400)
return
}
node.ID = strings.TrimSpace(node.ID)
node.URL = strings.TrimSpace(node.URL)
node.ConnectAddress = strings.TrimSpace(node.ConnectAddress)
if node.Priority <= 0 {
node.Priority = 100
}
err := b.cluster.mutate(func(config *clusterConfig) error {
if err := validateNode(node, b.cluster.allowInsecure); err != nil {
return err
}
for _, existing := range config.Nodes {
if existing.ID == node.ID {
return errors.New("relay node id already exists")
}
}
if len(config.Nodes) >= 32 {
return errors.New("at most 32 relay nodes")
}
if node.Default {
for i := range config.Nodes {
config.Nodes[i].Default = false
}
}
config.Nodes = append(config.Nodes, node)
return nil
})
if err != nil {
http.Error(w, err.Error(), 400)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (b *broker) deleteNodeEndpoint(w http.ResponseWriter, r *http.Request) {
if b.cluster == nil {
http.Error(w, "cluster not configured", 404)
return
}
id := r.PathValue("id")
err := b.cluster.mutate(func(config *clusterConfig) error {
index := -1
for i := range config.Nodes {
if config.Nodes[i].ID == id {
index = i
break
}
}
if index < 0 {
return errNodeNotFound
}
if len(config.Nodes) <= 1 {
return errors.New("keep at least one relay node")
}
config.Nodes = append(config.Nodes[:index], config.Nodes[index+1:]...)
return nil
})
if err != nil {
status := 400
if errors.Is(err, errNodeNotFound) {
status = 404
}
http.Error(w, err.Error(), status)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (b *broker) defaultNodeEndpoint(w http.ResponseWriter, r *http.Request) {
if b.cluster == nil {
http.Error(w, "cluster not configured", 404)
return
}
var body struct {
ID string `json:"id"`
}
if err := json.NewDecoder(io.LimitReader(r.Body, 4096)).Decode(&body); err != nil {
http.Error(w, "invalid JSON body", 400)
return
}
err := b.cluster.mutate(func(config *clusterConfig) error {
found := false
for i := range config.Nodes {
config.Nodes[i].Default = config.Nodes[i].ID == body.ID
found = found || config.Nodes[i].Default
}
if !found {
return errNodeNotFound
}
return nil
})
if err != nil {
status := 400
if errors.Is(err, errNodeNotFound) {
status = 404
}
http.Error(w, err.Error(), status)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (b *broker) nodesEndpoint(w http.ResponseWriter, r *http.Request) {
if b.cluster == nil {
http.Error(w, "cluster not configured", 404)
@@ -256,9 +436,10 @@ func (b *broker) clusterConnectEndpoint(w http.ResponseWriter, r *http.Request)
}
defer client.CloseNow()
publicOnly := r.URL.Query().Get("public_only") == "1"
config := b.cluster.snapshot()
manifest := func(side string) map[string]any {
return map[string]any{"type": "session", "protocol": 2, "session": s.id,
"ice_servers": b.cluster.config.ICEServers, "p2p_timeout_ms": b.cluster.config.P2PTimeoutMS, "public_only": publicOnly, "routes": b.cluster.routes(s.id, l.id, side)}
"ice_servers": config.ICEServers, "p2p_timeout_ms": config.P2PTimeoutMS, "public_only": publicOnly, "routes": b.cluster.routes(s.id, l.id, side)}
}
if writeJSON(ctx, client, manifest("client")) != nil {
return
+3
View File
@@ -386,6 +386,9 @@ func (b *broker) handler() http.Handler {
mux.HandleFunc("GET /v1/lease", b.auth(b.clientToken, b.leaseEndpoint))
mux.HandleFunc("GET /v1/connect", b.auth(b.clientToken, b.connectEndpoint))
mux.HandleFunc("GET /v2/nodes", b.auth(b.clientToken, b.nodesEndpoint))
mux.HandleFunc("POST /v2/nodes", b.managementAuth(b.addNodeEndpoint))
mux.HandleFunc("DELETE /v2/nodes/{id}", b.managementAuth(b.deleteNodeEndpoint))
mux.HandleFunc("POST /v2/nodes/default", b.managementAuth(b.defaultNodeEndpoint))
mux.HandleFunc("GET /v2/connect", b.auth(b.clientToken, b.clusterConnectEndpoint))
mux.HandleFunc("GET /v2/signal", b.auth(b.agentToken, b.signalEndpoint))
return mux