TLS HTTP setup

This commit is contained in:
Neil Alexander 2020-08-13 10:03:58 +01:00
parent 55df0bd374
commit 8382a9dcc2
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
14 changed files with 50 additions and 42 deletions

View file

@ -33,5 +33,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.AppServiceAPI.InternalAPI.Listen, base.Cfg.AppServiceAPI.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -46,5 +46,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.ClientAPI.InternalAPI.Listen, base.Cfg.ClientAPI.InternalAPI.Listen,
base.Cfg.ClientAPI.ExternalAPI.Listen, base.Cfg.ClientAPI.ExternalAPI.Listen,
nil, nil,
) )
} }

View file

@ -31,5 +31,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.CurrentStateServer.InternalAPI.Listen, base.Cfg.CurrentStateServer.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -36,5 +36,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.EDUServer.InternalAPI.Listen, base.Cfg.EDUServer.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -40,5 +40,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.FederationAPI.InternalAPI.Listen, base.Cfg.FederationAPI.InternalAPI.Listen,
base.Cfg.FederationAPI.ExternalAPI.Listen, base.Cfg.FederationAPI.ExternalAPI.Listen,
nil, nil,
) )
} }

View file

@ -38,5 +38,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.FederationSender.InternalAPI.Listen, base.Cfg.FederationSender.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -32,5 +32,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.KeyServer.InternalAPI.Listen, base.Cfg.KeyServer.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -33,5 +33,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.MediaAPI.InternalAPI.Listen, base.Cfg.MediaAPI.InternalAPI.Listen,
base.Cfg.MediaAPI.ExternalAPI.Listen, base.Cfg.MediaAPI.ExternalAPI.Listen,
nil, nil,
) )
} }

View file

@ -16,7 +16,6 @@ package main
import ( import (
"flag" "flag"
"fmt"
"os" "os"
"github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/appservice"
@ -148,42 +147,24 @@ func main() {
} }
monolith.AddAllPublicRoutes(base.PublicAPIMux) monolith.AddAllPublicRoutes(base.PublicAPIMux)
fmt.Printf("Public: %+v\n", base.PublicAPIMux)
fmt.Printf("Internal: %+v\n", base.InternalAPIMux)
/*
httputil.SetupHTTPAPI(
base.BaseMux,
base.PublicAPIMux,
base.InternalAPIMux,
&cfg.Global,
base.UseHTTPAPIs,
)
*/
// Expose the matrix APIs directly rather than putting them under a /api path. // Expose the matrix APIs directly rather than putting them under a /api path.
go func() { go func() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
config.HTTPAddress(httpAddr), // internal API config.HTTPAddress(httpAddr), // internal API
config.HTTPAddress(httpAddr), // external API config.HTTPAddress(httpAddr), // external API
nil, nil, // TLS settings
) )
}() }()
// Handle HTTPS if certificate and key are provided // Handle HTTPS if certificate and key are provided
_ = httpsAddr if *certFile != "" && *keyFile != "" {
/* go func() {
if *certFile != "" && *keyFile != "" { base.SetupAndServeHTTP(
go func() { config.HTTPAddress(httpsAddr), // internal API
serv := http.Server{ config.HTTPAddress(httpsAddr), // external API
Addr: config.HTTPAddress(httpsAddr)., certFile, keyFile, // TLS settings
WriteTimeout: setup.HTTPServerTimeout, )
Handler: base.BaseMux, }()
} }
logrus.Info("Listening on ", serv.Addr)
logrus.Fatal(serv.ListenAndServeTLS(*certFile, *keyFile))
}()
}
*/
// We want to block forever to let the HTTP and HTTPS handler serve the APIs // We want to block forever to let the HTTP and HTTPS handler serve the APIs
select {} select {}

View file

@ -36,5 +36,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.RoomServer.InternalAPI.Listen, base.Cfg.RoomServer.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -32,5 +32,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.ServerKeyAPI.InternalAPI.Listen, base.Cfg.ServerKeyAPI.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -36,5 +36,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.SyncAPI.InternalAPI.Listen, base.Cfg.SyncAPI.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -34,5 +34,6 @@ func main() {
base.SetupAndServeHTTP( base.SetupAndServeHTTP(
base.Cfg.UserAPI.InternalAPI.Listen, base.Cfg.UserAPI.InternalAPI.Listen,
setup.NoExternalListener, setup.NoExternalListener,
nil, nil,
) )
} }

View file

@ -264,7 +264,10 @@ func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationCli
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on // SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on
// ApiMux under /api/ and adds a prometheus handler under /metrics. // ApiMux under /api/ and adds a prometheus handler under /metrics.
func (b *BaseDendrite) SetupAndServeHTTP(internalHTTPAddr, externalHTTPAddr config.HTTPAddress) { func (b *BaseDendrite) SetupAndServeHTTP(
internalHTTPAddr, externalHTTPAddr config.HTTPAddress,
certFile, keyFile *string,
) {
block := make(chan struct{}) block := make(chan struct{})
internalAddr, _ := internalHTTPAddr.Address() internalAddr, _ := internalHTTPAddr.Address()
@ -294,21 +297,33 @@ func (b *BaseDendrite) SetupAndServeHTTP(internalHTTPAddr, externalHTTPAddr conf
go func() { go func() {
defer close(block) defer close(block)
logrus.Infof("Starting %s listener on %s", b.componentName, internalServ.Addr) logrus.Infof("Starting %s listener on %s", b.componentName, externalServ.Addr)
if err := internalServ.ListenAndServe(); err != nil { if certFile != nil && keyFile != nil {
logrus.WithError(err).Fatal("failed to serve HTTP") if err := externalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
} logrus.WithError(err).Fatal("failed to serve HTTPS")
logrus.Infof("Stopped %s listener on %s", b.componentName, internalServ.Addr) }
}() } else {
if externalAddr != "" && internalAddr != externalAddr {
go func() {
defer close(block)
logrus.Infof("Starting %s listener on %s", b.componentName, externalServ.Addr)
if err := externalServ.ListenAndServe(); err != nil { if err := externalServ.ListenAndServe(); err != nil {
logrus.WithError(err).Fatal("failed to serve HTTP") logrus.WithError(err).Fatal("failed to serve HTTP")
} }
logrus.Infof("Stopped %s listener on %s", b.componentName, externalServ.Addr) }
logrus.Infof("Stopped %s listener on %s", b.componentName, externalServ.Addr)
}()
if internalAddr != "" && internalAddr != externalAddr {
go func() {
defer close(block)
logrus.Infof("Starting %s listener on %s", b.componentName, internalServ.Addr)
if certFile != nil && keyFile != nil {
if err := internalServ.ListenAndServeTLS(*certFile, *keyFile); err != nil {
logrus.WithError(err).Fatal("failed to serve HTTPS")
}
} else {
if err := internalServ.ListenAndServe(); err != nil {
logrus.WithError(err).Fatal("failed to serve HTTP")
}
}
logrus.Infof("Stopped %s listener on %s", b.componentName, internalServ.Addr)
}() }()
} }