Files
Mickael Kerjean 054c92c71f fix (plg_starter_https): broken https plugin
the https plugin was broken. Instead of trying to do everything, we've
refactor the plugin to only do self signed certificate.

let's encrypt is causing a lot of weird issues as it won't work in a range of vps
provider so we considered it out of scope for this plugin. If we're to
integrate with lets encrypt in the future, this should be done as
another starter plugin
2022-04-30 12:35:04 +10:00

74 lines
1.7 KiB
Go

package plg_starter_https
import (
"crypto/tls"
"fmt"
"github.com/gorilla/mux"
. "github.com/mickael-kerjean/filestash/server/common"
"github.com/mickael-kerjean/filestash/server/common/ssl"
"net/http"
"os"
"path/filepath"
"time"
)
var SSL_PATH string = filepath.Join(GetCurrentDir(), CERT_PATH, "ssl")
func init() {
os.MkdirAll(SSL_PATH, os.ModePerm)
domain := Config.Get("general.host").String()
port := Config.Get("general.port").Int()
Hooks.Register.Starter(func(r *mux.Router) {
Log.Info("[https] starting ...%s", domain)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: r,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
TLSConfig: &DefaultTLSConfig,
ErrorLog: NewNilLogger(),
}
TLSCert, roots, err := ssl.GenerateSelfSigned()
if err != nil {
return
}
srv.TLSConfig.Certificates = []tls.Certificate{TLSCert}
HTTPClient.Transport.(*TransformedTransport).Orig.(*http.Transport).TLSClientConfig = &tls.Config{
RootCAs: roots,
}
HTTP.Transport.(*TransformedTransport).Orig.(*http.Transport).TLSClientConfig = &tls.Config{
RootCAs: roots,
}
go ensureAppHasBooted(fmt.Sprintf("https://127.0.0.1:%d/about", port), fmt.Sprintf("[https] listening on :%d", port))
if err := srv.ListenAndServeTLS("", ""); err != nil {
Log.Error("[https]: listen_serve %v", err)
return
}
})
}
func ensureAppHasBooted(address string, message string) {
i := 0
for {
if i > 10 {
Log.Warning("[http] didn't boot")
break
}
time.Sleep(250 * time.Millisecond)
res, err := HTTPClient.Get(address)
if err != nil {
i += 1
continue
}
res.Body.Close()
if res.StatusCode != http.StatusOK {
i += 1
continue
}
Log.Info(message)
break
}
}