chore codes

Kim committed Oct 21, 2025 at 14:59 UTC f5b21b4c825c8af5175d4efe5185629a4b1a78e7
3 files changed +69 -65
cmd/server/main.go
+17 -17
@@ -25,7 +25,7 @@ var (
25 httpAddr string // unified admin + HTTP proxy (e.g. :8080)
26 protocol string
27 topic string
28 - tcpAddr string // optional raw TCP ingress (e.g. :2222 for SSH)
28 + tcpAddr string // optional raw TCP ingress (e.g. :2222 for SSH)
29 )
30
31 func init() {
@@ -35,7 +35,7 @@ func init() {
35 flags.StringVar(&httpAddr, "http", ":8080", "Unified admin UI and HTTP proxy listen address")
36 flags.StringVar(&protocol, "protocol", "/relaydns/http/1.0", "libp2p protocol id for streams (must match clients)")
37 flags.StringVar(&topic, "topic", "relaydns.backends", "pubsub topic for backend adverts")
38 - flags.StringVar(&tcpAddr, "tcp", "", "Optional raw TCP ingress (e.g. :2222 for SSH). Empty to disable")
38 + flags.StringVar(&tcpAddr, "tcp", "", "Optional raw TCP ingress (e.g. :2222 for SSH). Empty to disable")
39 }
40
41 func main() {
@@ -100,18 +100,18 @@ func serveTCPIngress(ctx context.Context, addr string, d *relaydns.Director) {
100 }
101 continue
102 }
103 - go func(c net.Conn) {
104 - hosts := d.Hosts()
105 - if len(hosts) == 0 {
106 - log.Warn().Msg("tcp ingress: no backend peers available")
107 - _ = c.Close()
108 - return
109 - }
110 - // pick most recent (Hosts() sorted by last seen)
111 - peerID := hosts[0].Info.Peer
112 - if err := d.ProxyTCP(c, peerID); err != nil {
113 - log.Warn().Err(err).Msgf("tcp ingress proxy failed to %s", peerID)
114 - }
115 - }(conn)
116 - }
117 - }
103 + go func(c net.Conn) {
104 + hosts := d.Hosts()
105 + if len(hosts) == 0 {
106 + log.Warn().Msg("tcp ingress: no backend peers available")
107 + _ = c.Close()
108 + return
109 + }
110 + // pick most recent (Hosts() sorted by last seen)
111 + peerID := hosts[0].Info.Peer
112 + if err := d.ProxyTCP(c, peerID); err != nil {
113 + log.Warn().Err(err).Msgf("tcp ingress proxy failed to %s", peerID)
114 + }
115 + }(conn)
116 + }
117 +}
cmd/server/view.go
+39 -35
@@ -1,13 +1,13 @@
1 package main
2
3 import (
4 - "context"
5 - "encoding/json"
6 - "fmt"
7 - "html/template"
8 - "net/http"
9 - "strings"
10 - "time"
4 + "context"
5 + "encoding/json"
6 + "fmt"
7 + "html/template"
8 + "net/http"
9 + "strings"
10 + "time"
11
12 "github.com/gosuda/relaydns/relaydns"
13 "github.com/libp2p/go-libp2p/core/host"
@@ -27,26 +27,30 @@ func serveHTTP(ctx context.Context, addr string, d *relaydns.Director, h host.Ho
27 http.NotFound(w, r)
28 return
29 }
30 - rows := make([]row, 0)
31 - for _, v := range d.Hosts() {
32 - ttl := ""
33 - if v.Info.TTL > 0 {
34 - ttl = fmt.Sprintf("%ds", v.Info.TTL)
35 - }
36 - kind := "TCP"
37 - if strings.Contains(v.Info.Proto, "/http/") { kind = "HTTP" }
38 - if strings.Contains(v.Info.Proto, "/ssh/") { kind = "SSH" }
39 - rows = append(rows, row{
40 - Peer: v.Info.Peer,
41 - Name: v.Info.Name,
42 - DNS: v.Info.DNS,
43 - LastSeen: time.Since(v.LastSeen).Round(time.Second).String() + " ago",
44 - Link: "/peer/" + v.Info.Peer + "/",
45 - TTL: ttl,
46 - Connected: v.Connected,
47 - Kind: kind,
48 - })
49 - }
30 + rows := make([]row, 0)
31 + for _, v := range d.Hosts() {
32 + ttl := ""
33 + if v.Info.TTL > 0 {
34 + ttl = fmt.Sprintf("%ds", v.Info.TTL)
35 + }
36 + kind := "TCP"
37 + if strings.Contains(v.Info.Proto, "/http/") {
38 + kind = "HTTP"
39 + }
40 + if strings.Contains(v.Info.Proto, "/ssh/") {
41 + kind = "SSH"
42 + }
43 + rows = append(rows, row{
44 + Peer: v.Info.Peer,
45 + Name: v.Info.Name,
46 + DNS: v.Info.DNS,
47 + LastSeen: time.Since(v.LastSeen).Round(time.Second).String() + " ago",
48 + Link: "/peer/" + v.Info.Peer + "/",
49 + TTL: ttl,
50 + Connected: v.Connected,
51 + Kind: kind,
52 + })
53 + }
54 w.Header().Set("Content-Type", "text/html; charset=utf-8")
55 log.Debug().Int("clients", len(rows)).Msg("render admin index")
56 _ = adminIndexTmpl.Execute(w, page{
@@ -97,14 +101,14 @@ func serveHTTP(ctx context.Context, addr string, d *relaydns.Director, h host.Ho
101
102 // view model types used by template rendering
103 type row struct {
100 - Peer string
101 - Name string
102 - DNS string
103 - LastSeen string
104 - Link string
105 - TTL string
106 - Connected bool
107 - Kind string
104 + Peer string
105 + Name string
106 + DNS string
107 + LastSeen string
108 + Link string
109 + TTL string
110 + Connected bool
111 + Kind string
112 }
113
114 type page struct {
relaydns/types.go
+13 -13
@@ -8,22 +8,22 @@ import (
8 )
9
10 type Advertise struct {
11 - Peer string `json:"peer"`
12 - Name string `json:"name,omitempty"`
13 - DNS string `json:"dns,omitempty"`
14 - Addrs []string `json:"addrs"`
15 - Ready bool `json:"ready"`
16 - Load float64 `json:"load"`
17 - TS time.Time `json:"ts"`
18 - TTL int `json:"ttl,omitempty"` // seconds
19 - Proto string `json:"proto,omitempty"`
11 + Peer string `json:"peer"`
12 + Name string `json:"name,omitempty"`
13 + DNS string `json:"dns,omitempty"`
14 + Addrs []string `json:"addrs"`
15 + Ready bool `json:"ready"`
16 + Load float64 `json:"load"`
17 + TS time.Time `json:"ts"`
18 + TTL int `json:"ttl,omitempty"` // seconds
19 + Proto string `json:"proto,omitempty"`
20 }
21
22 type HostEntry struct {
23 - Info Advertise
24 - AddrInfo *peer.AddrInfo
25 - LastSeen time.Time
26 - Connected bool
23 + Info Advertise
24 + AddrInfo *peer.AddrInfo
25 + LastSeen time.Time
26 + Connected bool
27 }
28
29 // Removed Picker: selection is explicit via /peer/{peerID}/...