Skip to content

Commit b681397

Browse files
committed
server: add option to specify TLS key log file
This allows developers to analyze the traffic using Wireshark, even when it is encrypted.
1 parent 70f8e5a commit b681397

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

server/server.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,24 @@ func Main[T any](flagsGenerator FlagsGenerator[T], sh StreamHandler[T]) {
4444
addr string
4545
tlsCert string
4646
tlsKey string
47+
keyLog string
4748
baton T
4849
)
4950

50-
flag.StringVar(&addr, "listen",
51-
"127.0.0.1:853", "UDP address to listen on.")
52-
flag.StringVar(&tlsCert, "cert",
53-
"cert.pem", "TLS certificate path.")
54-
flag.StringVar(&tlsKey, "key",
55-
"key.pem", "TLS key path.")
51+
flag.StringVar(&addr, "listen", "127.0.0.1:853",
52+
"UDP address to listen on.")
53+
flag.StringVar(&tlsCert, "cert", "cert.pem",
54+
"TLS certificate path.")
55+
flag.StringVar(&tlsKey, "key", "key.pem",
56+
"TLS key path.")
57+
flag.StringVar(&keyLog, "keylog", "",
58+
"TLS key log file (e.g. for Wireshark analysis) - none if empty")
5659
if flagsGenerator != nil {
5760
flagsGenerator(&baton)
5861
}
5962
flag.Parse()
6063

61-
return loop(l, ctx, sh, addr, tlsCert, tlsKey, baton)
64+
return loop(l, ctx, sh, addr, tlsCert, tlsKey, keyLog, baton)
6265
}, func(error) {
6366
cancel()
6467
})
@@ -87,7 +90,7 @@ func Main[T any](flagsGenerator FlagsGenerator[T], sh StreamHandler[T]) {
8790
}
8891

8992
func loop[T any](l log.Logger, ctx context.Context, sh StreamHandler[T],
90-
addr string, tlsCert string, tlsKey string,
93+
addr string, tlsCert string, tlsKey string, keyLog string,
9194
baton T) error {
9295

9396
cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
@@ -101,6 +104,16 @@ func loop[T any](l log.Logger, ctx context.Context, sh StreamHandler[T],
101104
MinVersion: tls.VersionTLS13,
102105
}
103106

107+
if keyLog != "" {
108+
keyLogFile, err := os.OpenFile(keyLog, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0755)
109+
if err != nil {
110+
return fmt.Errorf("open keylog file: %w", err)
111+
}
112+
defer keyLogFile.Close()
113+
tls.KeyLogWriter = keyLogFile
114+
}
115+
116+
104117
quic_conf := quic.Config{
105118
MaxIdleTimeout: 10 * time.Second,
106119
Allow0RTT: true,

0 commit comments

Comments
 (0)