cmd/hello: refactor to use tsweb.Server.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2022-02-03 14:17:32 -08:00
parent f13e5e38b2
commit 19b1c31e60

View File

@@ -18,10 +18,10 @@ import (
"net/http"
"os"
"strings"
"time"
"tailscale.com/client/tailscale"
"tailscale.com/client/tailscale/apitype"
"tailscale.com/tsweb"
)
var (
@@ -62,20 +62,18 @@ func main() {
http.HandleFunc("/", root)
log.Printf("Starting hello server.")
errc := make(chan error, 1)
if *httpAddr != "" {
log.Printf("running HTTP server on %s", *httpAddr)
go func() {
errc <- http.ListenAndServe(*httpAddr, nil)
}()
mainAddr := *httpsAddr
if mainAddr == "" {
mainAddr = *httpAddr
}
if *httpsAddr != "" {
log.Printf("running HTTPS server on %s", *httpsAddr)
go func() {
hs := &http.Server{
Addr: *httpsAddr,
TLSConfig: &tls.Config{
GetCertificate: func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
httpCfg := tsweb.ServerConfig{
Name: "hello",
Addr: mainAddr,
Handler: http.DefaultServeMux,
}
server := tsweb.NewServer(httpCfg)
if server.HTTPS != nil {
server.HTTPS.TLSConfig.GetCertificate = func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
switch hi.ServerName {
case "hello.ts.net":
return tailscale.GetCertificate(hi)
@@ -90,16 +88,11 @@ func main() {
return &c, nil
}
return nil, errors.New("invalid SNI name")
},
},
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 20 * time.Second,
MaxHeaderBytes: 10 << 10,
}
errc <- hs.ListenAndServeTLS("", "")
}()
}
log.Fatal(<-errc)
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func devMode() bool { return *httpsAddr == "" && *httpAddr != "" }