fix coderabbit issue
Kim committed
Mar 3, 2026 at 12:17 UTC
eb89fbf5fc363c4276cdcf6e44630dbb80ab6e38
4 files changed
+46
-7
cmd/portal-tunnel/main.go
+26
-3
@@ -6,6 +6,7 @@ import (
6
"fmt"
7
"io"
8
"net"
9
+ "net/url"
10
"os"
11
"os/signal"
12
"strings"
@@ -183,11 +184,16 @@ var bufferPool = sync.Pool{
184
func proxyConnection(ctx context.Context, localAddr string, relayConn net.Conn, tlsEnabled bool) error {
185
defer relayConn.Close()
186
187
+ targetAddr, err := normalizeTargetAddr(localAddr)
188
+ if err != nil {
189
+ return fmt.Errorf("invalid --host value %q: %w", localAddr, err)
190
+ }
191
+
192
dialer := &net.Dialer{Timeout: 5 * time.Second}
187
- localConn, err := dialer.DialContext(ctx, "tcp", localAddr)
193
+ localConn, err := dialer.DialContext(ctx, "tcp", targetAddr)
194
if err != nil {
195
log.Debug().
190
- Str("addr", localAddr).
196
+ Str("addr", targetAddr).
197
Err(err).
198
Msg("Local service unavailable")
199
if tlsEnabled {
@@ -197,7 +203,7 @@ func proxyConnection(ctx context.Context, localAddr string, relayConn net.Conn,
203
}
204
defer localConn.Close()
205
200
- log.Info().Str("addr", localAddr).Msg("Connected to local service")
206
+ log.Info().Str("addr", targetAddr).Msg("Connected to local service")
207
208
errCh := make(chan error, 2)
209
stopCh := make(chan struct{})
@@ -275,3 +281,20 @@ func splitCSV(raw string) []string {
281
}
282
return out
283
}
284
+
285
+func normalizeTargetAddr(raw string) (string, error) {
286
+ raw = strings.TrimSpace(raw)
287
+ if raw == "" {
288
+ return "", fmt.Errorf("empty host")
289
+ }
290
+
291
+ u, err := url.Parse(raw)
292
+ if err == nil && u.Scheme != "" {
293
+ if strings.TrimSpace(u.Host) == "" {
294
+ return "", fmt.Errorf("missing host in URL")
295
+ }
296
+ return u.Host, nil
297
+ }
298
+
299
+ return raw, nil
300
+}
cmd/relay-server/main.go
+14
-2
@@ -114,7 +114,13 @@ func runServer(cfg relayServerConfig) error {
114
Str("sni", route.SNI).
115
Msg("[SNI] Lease not active; dropping connection and unregistering route")
116
serv.GetSNIRouter().UnregisterRouteByLeaseID(route.LeaseID)
117
- clientConn.Close()
117
+ if err := clientConn.Close(); err != nil {
118
+ log.Debug().
119
+ Err(err).
120
+ Str("lease_id", route.LeaseID).
121
+ Str("sni", route.SNI).
122
+ Msg("[SNI] failed to close client connection")
123
+ }
124
return
125
}
126
@@ -128,7 +134,13 @@ func runServer(cfg relayServerConfig) error {
134
Str("lease_id", route.LeaseID).
135
Str("sni", route.SNI).
136
Msg("[SNI] Reverse tunnel unavailable")
131
- clientConn.Close()
137
+ if err := clientConn.Close(); err != nil {
138
+ log.Debug().
139
+ Err(err).
140
+ Str("lease_id", route.LeaseID).
141
+ Str("sni", route.SNI).
142
+ Msg("[SNI] failed to close client connection")
143
+ }
144
return
145
}
146
cmd/relay-server/serve.go
+1
-1
@@ -271,7 +271,7 @@ func redirectToHTTPS(w http.ResponseWriter, r *http.Request, sniListenAddr strin
271
if r.URL.RawQuery != "" {
272
target += "?" + r.URL.RawQuery
273
}
274
- http.Redirect(w, r, target, http.StatusMovedPermanently)
274
+ http.Redirect(w, r, target, http.StatusPermanentRedirect)
275
}
276
277
func handleKeylessSign(w http.ResponseWriter, r *http.Request, signer *keyless.Signer) {
cmd/relay-server/utils.go
+5
-1
@@ -418,7 +418,11 @@ func (r *leaseRow) fromLeaseEntry(entry *portal.LeaseEntry, admin *Admin, portal
418
r.LastSeenISO = entry.LastSeen.UTC().Format(time.RFC3339)
419
r.FirstSeenISO = entry.FirstSeen.UTC().Format(time.RFC3339)
420
r.TTL = r.formatDuration(time.Until(entry.Expires))
421
- r.Link = fmt.Sprintf("//%s.%s/", lease.Name, portalHostPort(portalURL))
421
+ linkLabel := strings.TrimSpace(lease.Name)
422
+ if linkLabel == "" {
423
+ linkLabel = identityID
424
+ }
425
+ r.Link = fmt.Sprintf("//%s.%s/", linkLabel, portalHostPort(portalURL))
426
r.StaleRed = !connected && since >= 15*time.Second
427
r.Hide = entry.ParsedMetadata != nil && entry.ParsedMetadata.Hide
428
r.Metadata = metadataStr