Reintroduce TLS support for device listener

preserve client IP visibility

Previously, TLS termination was moved to nginx for better separation of concerns.
However, this introduced an issue with the new device IP display feature:

- With nginx reverse proxying, all device connections appear to originate from 127.0.0.1
- The actual device IP addresses were masked by the proxy layer
- This prevented accurate IP-based device identification and reporting

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-08-01 12:16:17 +08:00
parent 25dbcc966b
commit c90e14a1ac
4 changed files with 56 additions and 1 deletions
+8
View File
@@ -47,6 +47,10 @@ type Config struct {
AllowOrigins bool AllowOrigins bool
PprofAddr string PprofAddr string
SslCert string
SslKey string
CaCert string
} }
func (cfg *Config) Parse(c *cli.Command) error { func (cfg *Config) Parse(c *cli.Command) error {
@@ -78,6 +82,10 @@ func (cfg *Config) Parse(c *cli.Command) error {
"allow-origins": &cfg.AllowOrigins, "allow-origins": &cfg.AllowOrigins,
"pprof": &cfg.PprofAddr, "pprof": &cfg.PprofAddr,
"sslcert": &cfg.SslCert,
"sslkey": &cfg.SslKey,
"cacert": &cfg.CaCert,
} }
for name, opt := range fields { for name, opt := range fields {
+31 -1
View File
@@ -28,11 +28,14 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"crypto/tls"
"crypto/x509"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
"os"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -154,7 +157,34 @@ func (srv *RttyServer) ListenDevices() {
} }
defer ln.Close() defer ln.Close()
log.Info().Msgf("Listen devices on: %s", ln.Addr().(*net.TCPAddr)) if cfg.SslCert != "" && cfg.SslKey != "" {
cert, err := tls.LoadX509KeyPair(cfg.SslCert, cfg.SslKey)
if err != nil {
log.Fatal().Msg(err.Error())
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
if cfg.CaCert != "" {
caCert, err := os.ReadFile(cfg.CaCert)
if err != nil {
log.Fatal().Msg(err.Error())
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
config.ClientCAs = caCertPool
config.ClientAuth = tls.RequireAndVerifyClientCert
}
ln = tls.NewListener(ln, config)
log.Info().Msgf("Listen devices on: %s SSL on", ln.Addr().(*net.TCPAddr))
} else {
log.Info().Msgf("Listen devices on: %s SSL off", ln.Addr().(*net.TCPAddr))
}
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
+12
View File
@@ -118,6 +118,18 @@ func main() {
Aliases: []string{"V"}, Aliases: []string{"V"},
Usage: "more detailed log output", Usage: "more detailed log output",
}, },
&cli.StringFlag{
Name: "sslcert",
Usage: "SSL/TLS certificate for device",
},
&cli.StringFlag{
Name: "sslkey",
Usage: "SSL/TLS private key for device",
},
&cli.StringFlag{
Name: "cacert",
Usage: "CA certificate to verify devices (mTLS)",
},
}, },
Action: cmdAction, Action: cmdAction,
} }
+5
View File
@@ -38,3 +38,8 @@
# CORS support (allow all origins for cross-domain requests) # CORS support (allow all origins for cross-domain requests)
#allow-origins: false #allow-origins: false
# SSL/TLS for listen device
#sslcert:
#sslkey:
#cacert: