A LICENSE => LICENSE +9 -0
@@ 0,0 1,9 @@
+MIT License
+
+Copyright (c) 2021 Vladimir Magyar
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
M go.mod => go.mod +6 -2
@@ 1,3 1,7 @@
-module gitlab.com/microo8/server
+module git.sr.ht/~ghost08/server
-go 1.14
+go 1.17
+
+require github.com/fsnotify/fsnotify v1.5.1
+
+require golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
A go.sum => go.sum +4 -0
@@ 0,0 1,4 @@
+github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
+github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
A keypair_reloader.go => keypair_reloader.go +81 -0
@@ 0,0 1,81 @@
+package server
+
+import (
+ "crypto/tls"
+ "log"
+ "net/http"
+ "path/filepath"
+ "sync"
+
+ "github.com/fsnotify/fsnotify"
+)
+
+type keypairReloader struct {
+ certMu sync.RWMutex
+ cert *tls.Certificate
+ certPath string
+ keyPath string
+}
+
+func NewKeypairReloader(certPath, keyPath string) (*keypairReloader, error) {
+ result := &keypairReloader{
+ certPath: certPath,
+ keyPath: keyPath,
+ }
+ cert, err := tls.LoadX509KeyPair(certPath, keyPath)
+ if err != nil {
+ return nil, err
+ }
+ result.cert = &cert
+ watcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer watcher.Close()
+ go func() {
+ for {
+ select {
+ case event, ok := <-watcher.Events:
+ if !ok {
+ return
+ }
+ log.Printf("INFO: File modified %s, reloading TLS certificate and key from %q and %q", event.Name, certPath, keyPath)
+ if err := result.maybeReload(); err != nil {
+ log.Printf("Keeping old TLS certificate because the new one could not be loaded: %v", err)
+ }
+ }
+ }
+ }()
+ watcher.Add(filepath.Dir(certPath))
+ watcher.Add(filepath.Dir(keyPath))
+ return result, nil
+}
+
+func (kpr *keypairReloader) maybeReload() error {
+ newCert, err := tls.LoadX509KeyPair(kpr.certPath, kpr.keyPath)
+ if err != nil {
+ return err
+ }
+ kpr.certMu.Lock()
+ defer kpr.certMu.Unlock()
+ kpr.cert = &newCert
+ return nil
+}
+
+func (kpr *keypairReloader) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
+ return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
+ kpr.certMu.RLock()
+ defer kpr.certMu.RUnlock()
+ return kpr.cert, nil
+ }
+}
+
+func WithKeyPairReloader(certPath, keyPath string) func(*http.Server) {
+ return func(srv *http.Server) {
+ kpr, err := NewKeypairReloader(certPath, keyPath)
+ if err != nil {
+ log.Fatal(err)
+ }
+ srv.TLSConfig.GetCertificate = kpr.GetCertificateFunc()
+ }
+}
M server.go => server.go +6 -1
@@ 6,7 6,9 @@ import (
"time"
)
-func New(handler http.Handler, serverAddress string) *http.Server {
+type Option func(*http.Server)
+
+func New(handler http.Handler, serverAddress string, options ...Option) *http.Server {
// See https://blog.cloudflare.com/exposing-go-on-the-internet/ for details
// about these settings
tlsConfig := &tls.Config{
@@ 38,5 40,8 @@ func New(handler http.Handler, serverAddress string) *http.Server {
TLSConfig: tlsConfig,
Handler: handler,
}
+ for _, o := range options {
+ o(srv)
+ }
return srv
}