cmd/tailscale: make tailscale cert --serve-demo accept optional listen argument

Change-Id: I48f2f4f74c9996b9ed4bee02c61f125d42154a34
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2022-10-17 13:07:19 -07:00 committed by Brad Fitzpatrick
parent 630bcb5b67
commit 45b7e8c23c

View File

@ -8,6 +8,7 @@
"bytes" "bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -44,6 +45,7 @@
func runCert(ctx context.Context, args []string) error { func runCert(ctx context.Context, args []string) error {
if certArgs.serve { if certArgs.serve {
s := &http.Server{ s := &http.Server{
Addr: ":443",
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
GetCertificate: localClient.GetCertificate, GetCertificate: localClient.GetCertificate,
}, },
@ -57,7 +59,16 @@ func runCert(ctx context.Context, args []string) error {
fmt.Fprintf(w, "<h1>Hello from Tailscale</h1>It works.") fmt.Fprintf(w, "<h1>Hello from Tailscale</h1>It works.")
}), }),
} }
log.Printf("running TLS server on :443 ...") switch len(args) {
case 0:
// Nothing.
case 1:
s.Addr = args[0]
default:
return errors.New("too many arguments; max 1 allowed with --serve-demo (the listen address)")
}
log.Printf("running TLS server on %s ...", s.Addr)
return s.ListenAndServeTLS("", "") return s.ListenAndServeTLS("", "")
} }