From 9cbc95a6db3b933f22b79cdc6cc9413b31af7ed0 Mon Sep 17 00:00:00 2001 From: Maddie Zhan Date: Sat, 9 Jul 2022 01:03:33 +0800 Subject: [PATCH] Fix Windows build Fixes #44, closes #45 --- web/listener.go | 42 ++++++++++++++++++++++++++++++ web/listener_linux.go | 60 +++++++++++++++++++++++++++++++++++++++++++ web/web.go | 46 +-------------------------------- 3 files changed, 103 insertions(+), 45 deletions(-) create mode 100644 web/listener.go create mode 100644 web/listener_linux.go diff --git a/web/listener.go b/web/listener.go new file mode 100644 index 0000000..ae66f0c --- /dev/null +++ b/web/listener.go @@ -0,0 +1,42 @@ +//go:build !linux +// +build !linux + +package web + +import ( + "crypto/tls" + "github.com/go-chi/chi/v5" + "github.com/librespeed/speedtest/config" + log "github.com/sirupsen/logrus" + "net" + "net/http" +) + +func startListener(conf *config.Config, r *chi.Mux) error { + var s error + + addr := net.JoinHostPort(conf.BindAddress, conf.Port) + log.Infof("Starting backend server on %s", addr) + + // TLS and HTTP/2. + if conf.EnableTLS { + log.Info("Use TLS connection.") + if !(conf.EnableHTTP2) { + srv := &http.Server{ + Addr: addr, + Handler: r, + TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), + } + s = srv.ListenAndServeTLS(conf.TLSCertFile, conf.TLSKeyFile) + } else { + s = http.ListenAndServeTLS(addr, conf.TLSCertFile, conf.TLSKeyFile, r) + } + } else { + if conf.EnableHTTP2 { + log.Errorf("TLS is mandatory for HTTP/2. Ignore settings that enable HTTP/2.") + } + s = http.ListenAndServe(addr, r) + } + + return s +} diff --git a/web/listener_linux.go b/web/listener_linux.go new file mode 100644 index 0000000..56cc7b3 --- /dev/null +++ b/web/listener_linux.go @@ -0,0 +1,60 @@ +//go:build linux +// +build linux + +package web + +import ( + "crypto/tls" + "github.com/coreos/go-systemd/activation" + "github.com/go-chi/chi/v5" + "github.com/librespeed/speedtest/config" + log "github.com/sirupsen/logrus" + "net" + "net/http" +) + +func startListener(conf *config.Config, r *chi.Mux) error { + // See if systemd socket activation has been used when starting our process + listeners, err := activation.Listeners() + if err != nil { + log.Fatalf("Error whilst checking for systemd socket activation %s", err) + } + + var s error + + switch len(listeners) { + case 0: + addr := net.JoinHostPort(conf.BindAddress, conf.Port) + log.Infof("Starting backend server on %s", addr) + + // TLS and HTTP/2. + if conf.EnableTLS { + log.Info("Use TLS connection.") + if !(conf.EnableHTTP2) { + srv := &http.Server{ + Addr: addr, + Handler: r, + TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), + } + s = srv.ListenAndServeTLS(conf.TLSCertFile, conf.TLSKeyFile) + } else { + s = http.ListenAndServeTLS(addr, conf.TLSCertFile, conf.TLSKeyFile, r) + } + } else { + if conf.EnableHTTP2 { + log.Errorf("TLS is mandatory for HTTP/2. Ignore settings that enable HTTP/2.") + } + s = http.ListenAndServe(addr, r) + } + case 1: + log.Info("Starting backend server on inherited file descriptor via systemd socket activation") + if conf.BindAddress != "" || conf.Port != "" { + log.Errorf("Both an address/port (%s:%s) has been specificed in the config AND externally configured socket activation has been detected", conf.BindAddress, conf.Port) + log.Fatal(`Please deconfigure socket activation (e.g. in systemd unit files), or set both 'bind_address' and 'listen_port' to ''`) + } + s = http.Serve(listeners[0], r) + default: + log.Fatalf("Asked to listen on %s sockets via systemd activation. Sorry we currently only support listening on 1 socket.", len(listeners)) + } + return s +} diff --git a/web/web.go b/web/web.go index b82bc07..fb7fda9 100644 --- a/web/web.go +++ b/web/web.go @@ -1,7 +1,6 @@ package web import ( - "crypto/tls" "embed" "encoding/json" "io" @@ -14,7 +13,6 @@ import ( "strconv" "strings" - "github.com/coreos/go-systemd/activation" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/cors" @@ -96,49 +94,7 @@ func ListenAndServe(conf *config.Config) error { go listenProxyProtocol(conf, r) - // See if systemd socket activation has been used when starting our process - listeners, err := activation.Listeners() - if err != nil { - log.Fatalf("Error whilst checking for systemd socket activation %s", err) - } - - var s error - - switch len(listeners) { - case 0: - addr := net.JoinHostPort(conf.BindAddress, conf.Port) - log.Infof("Starting backend server on %s", addr) - - // TLS and HTTP/2. - if conf.EnableTLS { - log.Info("Use TLS connection.") - if !(conf.EnableHTTP2) { - srv := &http.Server{ - Addr: addr, - Handler: r, - TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), - } - s = srv.ListenAndServeTLS(conf.TLSCertFile, conf.TLSKeyFile) - } else { - s = http.ListenAndServeTLS(addr, conf.TLSCertFile, conf.TLSKeyFile, r) - } - } else { - if conf.EnableHTTP2 { - log.Errorf("TLS is mandatory for HTTP/2. Ignore settings that enable HTTP/2.") - } - s = http.ListenAndServe(addr, r) - } - case 1: - log.Info("Starting backend server on inherited file descriptor via systemd socket activation") - if conf.BindAddress != "" || conf.Port != "" { - log.Errorf("Both an address/port (%s:%s) has been specificed in the config AND externally configured socket activation has been detected", conf.BindAddress, conf.Port) - log.Fatal(`Please deconfigure socket activation (e.g. in systemd unit files), or set both 'bind_address' and 'listen_port' to ''`) - } - s = http.Serve(listeners[0], r) - default: - log.Fatalf("Asked to listen on %s sockets via systemd activation. Sorry we currently only support listening on 1 socket.", len(listeners)) - } - return s + return startListener(conf, r) } func listenProxyProtocol(conf *config.Config, r *chi.Mux) {