2019-04-25 04:52:08 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-04-05 06:57:47 +07:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2022-06-14 14:20:13 +07:00
|
|
|
"fmt"
|
2020-07-16 13:36:37 +07:00
|
|
|
"net"
|
2019-04-25 04:52:08 +07:00
|
|
|
"os"
|
2020-06-21 20:40:37 +07:00
|
|
|
"strconv"
|
2022-08-09 04:50:12 +07:00
|
|
|
"strings"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-04-06 04:57:23 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2022-06-14 14:20:13 +07:00
|
|
|
// CreateDefaultServer Create Authelia's internal webserver with the given configuration and providers.
|
|
|
|
func CreateDefaultServer(config schema.Configuration, providers middlewares.Providers) (server *fasthttp.Server, listener net.Listener, err error) {
|
|
|
|
server = &fasthttp.Server{
|
2022-06-12 06:26:28 +07:00
|
|
|
ErrorHandler: handleError(),
|
|
|
|
Handler: handleRouter(config, providers),
|
2020-04-30 10:16:41 +07:00
|
|
|
NoDefaultServerHeader: true,
|
2022-08-09 04:50:12 +07:00
|
|
|
ReadBufferSize: config.Server.Buffers.Read,
|
|
|
|
WriteBufferSize: config.Server.Buffers.Write,
|
|
|
|
ReadTimeout: config.Server.Timeouts.Read,
|
|
|
|
WriteTimeout: config.Server.Timeouts.Write,
|
|
|
|
IdleTimeout: config.Server.Timeouts.Idle,
|
2020-04-11 11:59:58 +07:00
|
|
|
}
|
2022-04-07 07:58:51 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
address := net.JoinHostPort(config.Server.Host, strconv.Itoa(config.Server.Port))
|
2020-07-16 13:36:37 +07:00
|
|
|
|
2022-04-05 06:57:47 +07:00
|
|
|
var (
|
|
|
|
connectionType string
|
|
|
|
connectionScheme string
|
|
|
|
)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
if config.Server.TLS.Certificate != "" && config.Server.TLS.Key != "" {
|
2022-08-09 04:50:12 +07:00
|
|
|
connectionType, connectionScheme = connTLS, schemeHTTPS
|
2022-04-05 06:57:47 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
if err = server.AppendCert(config.Server.TLS.Certificate, config.Server.TLS.Key); err != nil {
|
2022-06-14 14:20:13 +07:00
|
|
|
return nil, nil, fmt.Errorf("unable to load tls server certificate '%s' or private key '%s': %w", config.Server.TLS.Certificate, config.Server.TLS.Key, err)
|
2021-08-05 11:02:07 +07:00
|
|
|
}
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
if len(config.Server.TLS.ClientCertificates) > 0 {
|
2022-04-05 06:57:47 +07:00
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
2022-06-14 14:20:13 +07:00
|
|
|
var cert []byte
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
for _, path := range config.Server.TLS.ClientCertificates {
|
2022-06-14 14:20:13 +07:00
|
|
|
if cert, err = os.ReadFile(path); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to load tls client certificate '%s': %w", path, err)
|
2022-04-05 06:57:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool.AppendCertsFromPEM(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientCAs should never be nil, otherwise the system cert pool is used for client authentication
|
|
|
|
// but we don't want everybody on the Internet to be able to authenticate.
|
|
|
|
server.TLSConfig.ClientCAs = caCertPool
|
|
|
|
server.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
2021-08-10 07:31:08 +07:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:58:51 +07:00
|
|
|
if listener, err = tls.Listen("tcp", address, server.TLSConfig.Clone()); err != nil {
|
2022-06-14 14:20:13 +07:00
|
|
|
return nil, nil, fmt.Errorf("unable to initialize tcp listener: %w", err)
|
2022-04-05 06:57:47 +07:00
|
|
|
}
|
2020-03-03 14:18:25 +07:00
|
|
|
} else {
|
2022-08-09 04:50:12 +07:00
|
|
|
connectionType, connectionScheme = connNonTLS, schemeHTTP
|
2022-04-07 07:58:51 +07:00
|
|
|
|
|
|
|
if listener, err = net.Listen("tcp", address); err != nil {
|
2022-06-14 14:20:13 +07:00
|
|
|
return nil, nil, fmt.Errorf("unable to initialize tcp listener: %w", err)
|
2021-08-05 11:02:07 +07:00
|
|
|
}
|
2022-04-05 06:57:47 +07:00
|
|
|
}
|
2021-08-05 11:02:07 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
if err = writeHealthCheckEnv(config.Server.DisableHealthcheck, connectionScheme, config.Server.Host,
|
|
|
|
config.Server.Path, config.Server.Port); err != nil {
|
2022-06-14 14:20:13 +07:00
|
|
|
return nil, nil, fmt.Errorf("unable to configure healthcheck: %w", err)
|
2020-03-03 14:18:25 +07:00
|
|
|
}
|
2022-04-05 06:57:47 +07:00
|
|
|
|
2022-08-09 04:50:12 +07:00
|
|
|
paths := []string{"/"}
|
2022-06-14 14:20:13 +07:00
|
|
|
|
2022-08-09 04:50:12 +07:00
|
|
|
if config.Server.Path != "" {
|
|
|
|
paths = append(paths, config.Server.Path)
|
2022-04-05 06:57:47 +07:00
|
|
|
}
|
|
|
|
|
2022-08-09 04:50:12 +07:00
|
|
|
logging.Logger().Infof(fmtLogServerInit, "server", connectionType, listener.Addr().String(), strings.Join(paths, "' and '"))
|
|
|
|
|
2022-06-14 14:20:13 +07:00
|
|
|
return server, listener, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMetricsServer creates a metrics server.
|
|
|
|
func CreateMetricsServer(config schema.TelemetryMetricsConfig) (server *fasthttp.Server, listener net.Listener, err error) {
|
|
|
|
if listener, err = config.Address.Listener(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server = &fasthttp.Server{
|
|
|
|
ErrorHandler: handleError(),
|
|
|
|
NoDefaultServerHeader: true,
|
|
|
|
Handler: handleMetrics(),
|
2022-08-09 04:50:12 +07:00
|
|
|
ReadBufferSize: config.Buffers.Read,
|
|
|
|
WriteBufferSize: config.Buffers.Write,
|
|
|
|
ReadTimeout: config.Timeouts.Read,
|
|
|
|
WriteTimeout: config.Timeouts.Write,
|
|
|
|
IdleTimeout: config.Timeouts.Idle,
|
2022-06-14 14:20:13 +07:00
|
|
|
}
|
|
|
|
|
2022-08-09 04:50:12 +07:00
|
|
|
logging.Logger().Infof(fmtLogServerInit, "server (metrics)", connNonTLS, listener.Addr().String(), "/metrics")
|
|
|
|
|
2022-06-14 14:20:13 +07:00
|
|
|
return server, listener, nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|