traefik/server/server.go

758 lines
23 KiB
Go
Raw Normal View History

package server
import (
2016-08-16 15:26:10 +00:00
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
2017-10-10 12:50:03 +00:00
"fmt"
2017-11-18 00:10:03 +00:00
stdlog "log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
2017-08-25 19:32:03 +00:00
"github.com/armon/go-proxyproto"
"github.com/containous/traefik/cluster"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/config"
"github.com/containous/traefik/config/static"
2018-05-28 09:46:03 +00:00
"github.com/containous/traefik/h2c"
"github.com/containous/traefik/ip"
"github.com/containous/traefik/log"
"github.com/containous/traefik/metrics"
"github.com/containous/traefik/middlewares"
"github.com/containous/traefik/middlewares/accesslog"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/middlewares/requestdecorator"
"github.com/containous/traefik/old/configuration"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/safe"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/server/middleware"
traefiktls "github.com/containous/traefik/tls"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/tracing"
"github.com/containous/traefik/tracing/datadog"
"github.com/containous/traefik/tracing/jaeger"
"github.com/containous/traefik/tracing/zipkin"
"github.com/containous/traefik/types"
2018-01-22 11:16:03 +00:00
"github.com/sirupsen/logrus"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/acme"
)
func newHijackConnectionTracker() *hijackConnectionTracker {
return &hijackConnectionTracker{
conns: make(map[net.Conn]struct{}),
}
}
type hijackConnectionTracker struct {
conns map[net.Conn]struct{}
lock sync.RWMutex
}
// AddHijackedConnection add a connection in the tracked connections list
func (h *hijackConnectionTracker) AddHijackedConnection(conn net.Conn) {
h.lock.Lock()
defer h.lock.Unlock()
h.conns[conn] = struct{}{}
}
// RemoveHijackedConnection remove a connection from the tracked connections list
func (h *hijackConnectionTracker) RemoveHijackedConnection(conn net.Conn) {
h.lock.Lock()
defer h.lock.Unlock()
delete(h.conns, conn)
}
// Shutdown wait for the connection closing
func (h *hijackConnectionTracker) Shutdown(ctx context.Context) error {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
h.lock.RLock()
if len(h.conns) == 0 {
return nil
}
h.lock.RUnlock()
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}
// Close close all the connections in the tracked connections list
func (h *hijackConnectionTracker) Close() {
for conn := range h.conns {
if err := conn.Close(); err != nil {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Errorf("Error while closing Hijacked connection: %v", err)
}
delete(h.conns, conn)
}
}
// Server is the reverse-proxy/load-balancer engine
type Server struct {
2018-11-14 09:18:03 +00:00
serverEntryPoints serverEntryPoints
configurationChan chan config.Message
configurationValidatedChan chan config.Message
signals chan os.Signal
stopChan chan bool
currentConfigurations safe.Safe
providerConfigUpdateMap map[string]chan config.Message
globalConfiguration configuration.GlobalConfiguration
accessLoggerMiddleware *accesslog.Handler
tracer *tracing.Tracing
routinesPool *safe.Pool
leadership *cluster.Leadership
defaultRoundTripper http.RoundTripper
metricsRegistry metrics.Registry
provider provider.Provider
configurationListeners []func(config.Configuration)
entryPoints map[string]EntryPoint
requestDecorator *requestdecorator.RequestDecorator
}
// RouteAppenderFactory the route appender factory interface
type RouteAppenderFactory interface {
NewAppender(ctx context.Context, middlewaresBuilder *middleware.Builder, currentConfigurations *safe.Safe) types.RouteAppender
}
// EntryPoint entryPoint information (configuration + internalRouter)
type EntryPoint struct {
2018-11-14 09:18:03 +00:00
RouteAppenderFactory RouteAppenderFactory
Configuration *configuration.EntryPoint
OnDemandListener func(string) (*tls.Certificate, error)
TLSALPNGetter func(string) (*tls.Certificate, error)
CertificateStore *traefiktls.CertificateStore
}
type serverEntryPoints map[string]*serverEntryPoint
type serverEntryPoint struct {
httpServer *h2c.Server
listener net.Listener
httpRouter *middlewares.HandlerSwitcher
certs *traefiktls.CertificateStore
onDemandListener func(string) (*tls.Certificate, error)
tlsALPNGetter func(string) (*tls.Certificate, error)
hijackConnectionTracker *hijackConnectionTracker
}
func (s serverEntryPoint) Shutdown(ctx context.Context) {
var wg sync.WaitGroup
2018-09-17 14:26:03 +00:00
if s.httpServer != nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.httpServer.Shutdown(ctx); err != nil {
if ctx.Err() == context.DeadlineExceeded {
2018-11-14 09:18:03 +00:00
logger := log.FromContext(ctx)
logger.Debugf("Wait server shutdown is over due to: %s", err)
2018-09-17 14:26:03 +00:00
err = s.httpServer.Close()
if err != nil {
2018-11-14 09:18:03 +00:00
logger.Error(err)
2018-09-17 14:26:03 +00:00
}
}
}
2018-09-17 14:26:03 +00:00
}()
}
if s.hijackConnectionTracker != nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.hijackConnectionTracker.Shutdown(ctx); err != nil {
if ctx.Err() == context.DeadlineExceeded {
2018-11-14 09:18:03 +00:00
logger := log.FromContext(ctx)
logger.Debugf("Wait hijack connection is over due to: %s", err)
2018-09-17 14:26:03 +00:00
s.hijackConnectionTracker.Close()
}
}
2018-09-17 14:26:03 +00:00
}()
}
wg.Wait()
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
2018-11-14 09:18:03 +00:00
if err = tc.SetKeepAlive(true); err != nil {
return nil, err
}
if err = tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
return nil, err
}
return tc, nil
}
2018-11-14 09:18:03 +00:00
func setupTracing(conf *static.Tracing) tracing.TrackingBackend {
switch conf.Backend {
case jaeger.Name:
return conf.Jaeger
case zipkin.Name:
return conf.Zipkin
case datadog.Name:
return conf.DataDog
default:
log.WithoutContext().Warnf("Could not initialize tracing: unknown tracer %q", conf.Backend)
return nil
}
}
// NewServer returns an initialized Server.
func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider, entrypoints map[string]EntryPoint) *Server {
2018-06-11 09:36:03 +00:00
server := &Server{}
server.entryPoints = entrypoints
server.provider = provider
2018-06-11 09:36:03 +00:00
server.globalConfiguration = globalConfiguration
server.serverEntryPoints = make(map[string]*serverEntryPoint)
2018-11-14 09:18:03 +00:00
server.configurationChan = make(chan config.Message, 100)
server.configurationValidatedChan = make(chan config.Message, 100)
server.signals = make(chan os.Signal, 1)
server.stopChan = make(chan bool, 1)
server.configureSignals()
2018-11-14 09:18:03 +00:00
currentConfigurations := make(config.Configurations)
server.currentConfigurations.Set(currentConfigurations)
2018-11-14 09:18:03 +00:00
server.providerConfigUpdateMap = make(map[string]chan config.Message)
transport, err := createHTTPTransport(globalConfiguration)
if err != nil {
log.WithoutContext().Error(err)
server.defaultRoundTripper = http.DefaultTransport
} else {
server.defaultRoundTripper = transport
}
2018-06-11 09:36:03 +00:00
if server.globalConfiguration.API != nil {
server.globalConfiguration.API.CurrentConfigurations = &server.currentConfigurations
}
server.routinesPool = safe.NewPool(context.Background())
2018-06-11 09:36:03 +00:00
2018-11-14 09:18:03 +00:00
if globalConfiguration.Tracing != nil {
trackingBackend := setupTracing(static.ConvertTracing(globalConfiguration.Tracing))
var err error
server.tracer, err = tracing.NewTracing(globalConfiguration.Tracing.ServiceName, globalConfiguration.Tracing.SpanNameLimit, trackingBackend)
if err != nil {
log.WithoutContext().Warnf("Unable to create tracer: %v", err)
}
2018-06-11 09:36:03 +00:00
}
2018-11-14 09:18:03 +00:00
server.requestDecorator = requestdecorator.New(static.ConvertHostResolverConfig(globalConfiguration.HostResolver))
2018-11-14 09:18:03 +00:00
server.metricsRegistry = registerMetricClients(static.ConvertMetrics(globalConfiguration.Metrics))
if globalConfiguration.Cluster != nil {
// leadership creation if cluster mode
server.leadership = cluster.NewLeadership(server.routinesPool.Ctx(), globalConfiguration.Cluster)
}
2017-05-25 11:25:53 +00:00
if globalConfiguration.AccessLog != nil {
var err error
2018-11-14 09:18:03 +00:00
server.accessLoggerMiddleware, err = accesslog.NewHandler(static.ConvertAccessLog(globalConfiguration.AccessLog))
2017-05-25 11:25:53 +00:00
if err != nil {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Warnf("Unable to create access logger : %v", err)
2017-05-25 11:25:53 +00:00
}
2017-05-22 19:39:29 +00:00
}
return server
}
// Start starts the server.
2017-11-24 18:18:03 +00:00
func (s *Server) Start() {
s.startHTTPServers()
s.startLeadership()
s.routinesPool.Go(func(stop chan bool) {
s.listenProviders(stop)
})
2017-11-24 18:18:03 +00:00
s.routinesPool.Go(func(stop chan bool) {
s.listenConfigurations(stop)
})
s.startProvider()
2018-09-06 12:24:03 +00:00
s.routinesPool.Go(func(stop chan bool) {
s.listenSignals(stop)
})
}
2018-03-14 12:14:03 +00:00
// StartWithContext starts the server and Stop/Close it when context is Done
func (s *Server) StartWithContext(ctx context.Context) {
go func() {
defer s.Close()
<-ctx.Done()
2018-11-14 09:18:03 +00:00
logger := log.FromContext(ctx)
logger.Info("I have to go...")
2018-03-14 12:14:03 +00:00
reqAcceptGraceTimeOut := time.Duration(s.globalConfiguration.LifeCycle.RequestAcceptGraceTimeout)
if reqAcceptGraceTimeOut > 0 {
2018-11-14 09:18:03 +00:00
logger.Infof("Waiting %s for incoming requests to cease", reqAcceptGraceTimeOut)
2018-03-14 12:14:03 +00:00
time.Sleep(reqAcceptGraceTimeOut)
}
2018-11-14 09:18:03 +00:00
logger.Info("Stopping server gracefully")
2018-03-14 12:14:03 +00:00
s.Stop()
}()
s.Start()
}
// Wait blocks until server is shutted down.
2017-11-24 18:18:03 +00:00
func (s *Server) Wait() {
<-s.stopChan
}
// Stop stops the server
2017-11-24 18:18:03 +00:00
func (s *Server) Stop() {
2018-11-14 09:18:03 +00:00
defer log.WithoutContext().Info("Server stopped")
var wg sync.WaitGroup
2017-11-24 18:18:03 +00:00
for sepn, sep := range s.serverEntryPoints {
wg.Add(1)
go func(serverEntryPointName string, serverEntryPoint *serverEntryPoint) {
defer wg.Done()
2018-11-14 09:18:03 +00:00
logger := log.WithoutContext().WithField(log.EntryPointName, serverEntryPointName)
2017-11-24 18:18:03 +00:00
graceTimeOut := time.Duration(s.globalConfiguration.LifeCycle.GraceTimeOut)
ctx, cancel := context.WithTimeout(context.Background(), graceTimeOut)
2018-11-14 09:18:03 +00:00
logger.Debugf("Waiting %s seconds before killing connections on entrypoint %s...", graceTimeOut, serverEntryPointName)
serverEntryPoint.Shutdown(ctx)
cancel()
2018-11-14 09:18:03 +00:00
logger.Debugf("Entry point %s closed", serverEntryPointName)
}(sepn, sep)
}
wg.Wait()
2017-11-24 18:18:03 +00:00
s.stopChan <- true
}
// Close destroys the server
2017-11-24 18:18:03 +00:00
func (s *Server) Close() {
2018-03-14 12:14:03 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
go func(ctx context.Context) {
<-ctx.Done()
if ctx.Err() == context.Canceled {
return
} else if ctx.Err() == context.DeadlineExceeded {
2018-03-14 12:14:03 +00:00
panic("Timeout while stopping traefik, killing instance ✝")
}
}(ctx)
2018-11-14 09:18:03 +00:00
stopMetricsClients()
2017-11-24 18:18:03 +00:00
s.stopLeadership()
s.routinesPool.Cleanup()
close(s.configurationChan)
close(s.configurationValidatedChan)
signal.Stop(s.signals)
close(s.signals)
close(s.stopChan)
2018-11-14 09:18:03 +00:00
2017-11-24 18:18:03 +00:00
if s.accessLoggerMiddleware != nil {
if err := s.accessLoggerMiddleware.Close(); err != nil {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Errorf("Could not close the access log file: %s", err)
2017-05-22 19:39:29 +00:00
}
}
2018-11-14 09:18:03 +00:00
if s.tracer != nil {
s.tracer.Close()
}
cancel()
}
2017-11-24 18:18:03 +00:00
func (s *Server) startLeadership() {
if s.leadership != nil {
s.leadership.Participate(s.routinesPool)
}
}
2017-11-24 18:18:03 +00:00
func (s *Server) stopLeadership() {
if s.leadership != nil {
s.leadership.Stop()
}
}
2017-11-24 18:18:03 +00:00
func (s *Server) startHTTPServers() {
2018-06-11 09:36:03 +00:00
s.serverEntryPoints = s.buildServerEntryPoints()
2017-01-12 13:34:54 +00:00
2017-11-24 18:18:03 +00:00
for newServerEntryPointName, newServerEntryPoint := range s.serverEntryPoints {
2018-11-14 09:18:03 +00:00
ctx := log.With(context.Background(), log.Str(log.EntryPointName, newServerEntryPointName))
serverEntryPoint := s.setupServerEntryPoint(ctx, newServerEntryPointName, newServerEntryPoint)
go s.startServer(ctx, serverEntryPoint)
}
}
2017-11-24 18:18:03 +00:00
func (s *Server) listenProviders(stop chan bool) {
for {
select {
case <-stop:
return
2017-11-24 18:18:03 +00:00
case configMsg, ok := <-s.configurationChan:
if !ok || configMsg.Configuration == nil {
return
}
2017-11-24 18:18:03 +00:00
s.preLoadConfiguration(configMsg)
}
}
}
2018-03-05 19:54:04 +00:00
// AddListener adds a new listener function used when new configuration is provided
2018-11-14 09:18:03 +00:00
func (s *Server) AddListener(listener func(config.Configuration)) {
2018-03-05 19:54:04 +00:00
if s.configurationListeners == nil {
2018-11-14 09:18:03 +00:00
s.configurationListeners = make([]func(config.Configuration), 0)
2018-03-05 19:54:04 +00:00
}
s.configurationListeners = append(s.configurationListeners, listener)
}
2018-08-06 18:00:03 +00:00
// getCertificate allows to customize tlsConfig.GetCertificate behavior to get the certificates inserted dynamically
func (s *serverEntryPoint) getCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
2018-07-06 08:30:03 +00:00
domainToCheck := types.CanonicalDomain(clientHello.ServerName)
2018-07-03 10:44:04 +00:00
if s.tlsALPNGetter != nil {
cert, err := s.tlsALPNGetter(domainToCheck)
if err != nil {
return nil, err
}
if cert != nil {
return cert, nil
}
}
bestCertificate := s.certs.GetBestCertificate(clientHello)
if bestCertificate != nil {
return bestCertificate, nil
}
2018-07-06 08:30:03 +00:00
if s.onDemandListener != nil && len(domainToCheck) > 0 {
// Only check for an onDemandCert if there is a domain name
2018-03-05 19:54:04 +00:00
return s.onDemandListener(domainToCheck)
}
2018-07-03 10:44:04 +00:00
2018-07-06 08:30:03 +00:00
if s.certs.SniStrict {
return nil, fmt.Errorf("strict SNI enabled - No certificate found for domain: %q, closing connection", domainToCheck)
}
2018-11-14 09:18:03 +00:00
log.WithoutContext().Debugf("Serving default certificate for request: %q", domainToCheck)
2018-07-06 08:30:03 +00:00
return s.certs.DefaultCertificate, nil
}
func (s *Server) startProvider() {
jsonConf, err := json.Marshal(s.provider)
if err != nil {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Debugf("Unable to marshal provider configuration %T: %v", s.provider, err)
}
2018-11-14 09:18:03 +00:00
log.WithoutContext().Infof("Starting provider %T %s", s.provider, jsonConf)
currentProvider := s.provider
2018-11-14 09:18:03 +00:00
safe.Go(func() {
err := currentProvider.Provide(s.configurationChan, s.routinesPool)
if err != nil {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Errorf("Error starting provider %T: %s", s.provider, err)
}
})
}
// creates a TLS config that allows terminating HTTPS for multiple domains using SNI
func (s *Server) createTLSConfig(entryPointName string, tlsOption *traefiktls.TLS, router *middlewares.HandlerSwitcher) (*tls.Config, error) {
if tlsOption == nil {
return nil, nil
}
2018-11-14 09:18:03 +00:00
conf, err := tlsOption.Certificates.CreateTLSConfig(entryPointName)
if err != nil {
return nil, err
}
2018-07-06 08:30:03 +00:00
s.serverEntryPoints[entryPointName].certs.DynamicCerts.Set(make(map[string]*tls.Certificate))
2018-07-03 10:44:04 +00:00
2016-11-09 16:56:41 +00:00
// ensure http2 enabled
2018-11-14 09:18:03 +00:00
conf.NextProtos = []string{"h2", "http/1.1", acme.ACMETLS1Protocol}
2016-11-09 16:56:41 +00:00
2017-11-10 09:30:04 +00:00
if len(tlsOption.ClientCA.Files) > 0 {
pool := x509.NewCertPool()
2017-11-10 09:30:04 +00:00
for _, caFile := range tlsOption.ClientCA.Files {
2018-08-29 09:36:03 +00:00
data, err := caFile.Read()
if err != nil {
return nil, err
}
ok := pool.AppendCertsFromPEM(data)
if !ok {
2018-06-11 09:36:03 +00:00
return nil, fmt.Errorf("invalid certificate(s) in %s", caFile)
}
}
2018-11-14 09:18:03 +00:00
conf.ClientCAs = pool
2017-11-10 09:30:04 +00:00
if tlsOption.ClientCA.Optional {
2018-11-14 09:18:03 +00:00
conf.ClientAuth = tls.VerifyClientCertIfGiven
2017-11-10 09:30:04 +00:00
} else {
2018-11-14 09:18:03 +00:00
conf.ClientAuth = tls.RequireAndVerifyClientCert
2017-11-10 09:30:04 +00:00
}
}
2018-11-14 09:18:03 +00:00
// FIXME onDemand
if s.globalConfiguration.ACME != nil {
// if entryPointName == s.globalConfiguration.ACME.EntryPoint {
// checkOnDemandDomain := func(domain string) bool {
// routeMatch := &mux.RouteMatch{}
// match := router.GetHandler().Match(&http.Request{URL: &url.URL{}, Host: domain}, routeMatch)
// if match && routeMatch.Route != nil {
// return true
// }
// return false
// }
//
// err := s.globalConfiguration.ACME.CreateClusterConfig(s.leadership, config, s.serverEntryPoints[entryPointName].certs.DynamicCerts, checkOnDemandDomain)
// if err != nil {
// return nil, err
// }
// }
} else {
2018-11-14 09:18:03 +00:00
conf.GetCertificate = s.serverEntryPoints[entryPointName].getCertificate
}
2018-03-05 19:54:04 +00:00
2018-11-14 09:18:03 +00:00
if len(conf.Certificates) != 0 {
certMap := s.buildNameOrIPToCertificate(conf.Certificates)
2018-03-05 19:54:04 +00:00
2018-11-14 09:18:03 +00:00
if s.entryPoints[entryPointName].CertificateStore != nil {
s.entryPoints[entryPointName].CertificateStore.StaticCerts.Set(certMap)
}
}
2018-07-06 08:30:03 +00:00
2018-11-14 09:18:03 +00:00
// Remove certs from the TLS config object
conf.Certificates = []tls.Certificate{}
2018-03-05 19:54:04 +00:00
// Set the minimum TLS version if set in the config TOML
2018-11-14 09:18:03 +00:00
if minConst, exists := traefiktls.MinVersion[tlsOption.MinVersion]; exists {
conf.PreferServerCipherSuites = true
conf.MinVersion = minConst
}
2018-03-05 19:54:04 +00:00
// Set the list of CipherSuites if set in the config TOML
2018-11-14 09:18:03 +00:00
if tlsOption.CipherSuites != nil {
// if our list of CipherSuites is defined in the entryPoint config, we can re-initialize the suites list as empty
conf.CipherSuites = make([]uint16, 0)
for _, cipher := range tlsOption.CipherSuites {
if cipherConst, exists := traefiktls.CipherSuites[cipher]; exists {
2018-11-14 09:18:03 +00:00
conf.CipherSuites = append(conf.CipherSuites, cipherConst)
} else {
2018-03-05 19:54:04 +00:00
// CipherSuite listed in the toml does not exist in our listed
2018-06-11 09:36:03 +00:00
return nil, fmt.Errorf("invalid CipherSuite: %s", cipher)
}
}
}
2018-06-11 09:36:03 +00:00
2018-11-14 09:18:03 +00:00
return conf, nil
}
2018-11-14 09:18:03 +00:00
func (s *Server) startServer(ctx context.Context, serverEntryPoint *serverEntryPoint) {
logger := log.FromContext(ctx)
logger.Infof("Starting server on %s", serverEntryPoint.httpServer.Addr)
2018-06-11 09:36:03 +00:00
var err error
2017-08-25 19:32:03 +00:00
if serverEntryPoint.httpServer.TLSConfig != nil {
err = serverEntryPoint.httpServer.ServeTLS(serverEntryPoint.listener, "", "")
} else {
2017-08-25 19:32:03 +00:00
err = serverEntryPoint.httpServer.Serve(serverEntryPoint.listener)
}
2018-06-11 09:36:03 +00:00
if err != http.ErrServerClosed {
2018-11-14 09:18:03 +00:00
logger.Error("Cannot create server: %v", err)
}
}
2018-11-14 09:18:03 +00:00
func (s *Server) setupServerEntryPoint(ctx context.Context, newServerEntryPointName string, newServerEntryPoint *serverEntryPoint) *serverEntryPoint {
newSrv, listener, err := s.prepareServer(ctx, newServerEntryPointName, s.entryPoints[newServerEntryPointName].Configuration, newServerEntryPoint.httpRouter)
2018-06-11 09:36:03 +00:00
if err != nil {
2018-11-14 09:18:03 +00:00
log.FromContext(ctx).Fatalf("Error preparing server: %v", err)
2018-06-11 09:36:03 +00:00
}
serverEntryPoint := s.serverEntryPoints[newServerEntryPointName]
serverEntryPoint.httpServer = newSrv
serverEntryPoint.listener = listener
serverEntryPoint.hijackConnectionTracker = newHijackConnectionTracker()
serverEntryPoint.httpServer.ConnState = func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateHijacked:
serverEntryPoint.hijackConnectionTracker.AddHijackedConnection(conn)
case http.StateClosed:
serverEntryPoint.hijackConnectionTracker.RemoveHijackedConnection(conn)
}
}
2018-06-11 09:36:03 +00:00
return serverEntryPoint
}
2018-11-14 09:18:03 +00:00
func (s *Server) prepareServer(ctx context.Context, entryPointName string, entryPoint *configuration.EntryPoint, router *middlewares.HandlerSwitcher) (*h2c.Server, net.Listener, error) {
logger := log.FromContext(ctx)
2018-11-14 09:18:03 +00:00
readTimeout, writeTimeout, idleTimeout := buildServerTimeouts(s.globalConfiguration)
logger.
WithField("readTimeout", readTimeout).
WithField("writeTimeout", writeTimeout).
WithField("idleTimeout", idleTimeout).
Infof("Preparing server %+v", entryPoint)
2017-11-24 18:18:03 +00:00
tlsConfig, err := s.createTLSConfig(entryPointName, entryPoint.TLS, router)
if err != nil {
2018-06-11 09:36:03 +00:00
return nil, nil, fmt.Errorf("error creating TLS config: %v", err)
2017-08-25 19:32:03 +00:00
}
listener, err := net.Listen("tcp", entryPoint.Address)
if err != nil {
2018-06-11 09:36:03 +00:00
return nil, nil, fmt.Errorf("error opening listener: %v", err)
2017-08-25 19:32:03 +00:00
}
listener = tcpKeepAliveListener{listener.(*net.TCPListener)}
2017-10-10 12:50:03 +00:00
if entryPoint.ProxyProtocol != nil {
2018-11-14 09:18:03 +00:00
listener, err = buildProxyProtocolListener(ctx, entryPoint, listener)
2017-10-10 12:50:03 +00:00
if err != nil {
return nil, nil, fmt.Errorf("error creating proxy protocol listener: %v", err)
2017-10-10 12:50:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
httpServerLogger := stdlog.New(logger.WriterLevel(logrus.DebugLevel), "", 0)
2018-05-28 09:46:03 +00:00
return &h2c.Server{
Server: &http.Server{
Addr: entryPoint.Address,
2018-11-14 09:18:03 +00:00
Handler: router,
2018-05-28 09:46:03 +00:00
TLSConfig: tlsConfig,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
ErrorLog: httpServerLogger,
},
2017-08-25 19:32:03 +00:00
},
listener,
nil
}
2018-11-14 09:18:03 +00:00
func buildProxyProtocolListener(ctx context.Context, entryPoint *configuration.EntryPoint, listener net.Listener) (net.Listener, error) {
var sourceCheck func(addr net.Addr) (bool, error)
if entryPoint.ProxyProtocol.Insecure {
sourceCheck = func(_ net.Addr) (bool, error) {
return true, nil
}
} else {
checker, err := ip.NewChecker(entryPoint.ProxyProtocol.TrustedIPs)
if err != nil {
return nil, err
}
2018-06-11 09:36:03 +00:00
sourceCheck = func(addr net.Addr) (bool, error) {
ipAddr, ok := addr.(*net.TCPAddr)
2018-06-11 09:36:03 +00:00
if !ok {
return false, fmt.Errorf("type error %v", addr)
}
return checker.ContainsIP(ipAddr.IP), nil
}
}
2018-11-14 09:18:03 +00:00
log.FromContext(ctx).Infof("Enabling ProxyProtocol for trusted IPs %v", entryPoint.ProxyProtocol.TrustedIPs)
return &proxyproto.Listener{
Listener: listener,
SourceCheck: sourceCheck,
2018-06-11 09:36:03 +00:00
}, nil
}
func buildServerTimeouts(globalConfig configuration.GlobalConfiguration) (readTimeout, writeTimeout, idleTimeout time.Duration) {
readTimeout = time.Duration(0)
writeTimeout = time.Duration(0)
if globalConfig.RespondingTimeouts != nil {
readTimeout = time.Duration(globalConfig.RespondingTimeouts.ReadTimeout)
writeTimeout = time.Duration(globalConfig.RespondingTimeouts.WriteTimeout)
}
2018-07-31 17:28:03 +00:00
if globalConfig.RespondingTimeouts != nil {
2017-09-20 16:14:03 +00:00
idleTimeout = time.Duration(globalConfig.RespondingTimeouts.IdleTimeout)
} else {
2017-12-18 08:14:03 +00:00
idleTimeout = configuration.DefaultIdleTimeout
}
return readTimeout, writeTimeout, idleTimeout
}
func registerMetricClients(metricsConfig *types.Metrics) metrics.Registry {
if metricsConfig == nil {
return metrics.NewVoidRegistry()
}
2018-01-31 18:10:04 +00:00
var registries []metrics.Registry
2018-11-14 09:18:03 +00:00
if metricsConfig.Prometheus != nil {
2018-11-14 09:18:03 +00:00
ctx := log.With(context.Background(), log.Str(log.MetricsProviderName, "prometheus"))
prometheusRegister := metrics.RegisterPrometheus(ctx, metricsConfig.Prometheus)
2018-08-06 12:58:03 +00:00
if prometheusRegister != nil {
registries = append(registries, prometheusRegister)
2018-11-14 09:18:03 +00:00
log.FromContext(ctx).Debug("Configured Prometheus metrics")
2018-08-06 12:58:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
if metricsConfig.Datadog != nil {
2018-11-14 09:18:03 +00:00
ctx := log.With(context.Background(), log.Str(log.MetricsProviderName, "datadog"))
registries = append(registries, metrics.RegisterDatadog(ctx, metricsConfig.Datadog))
log.FromContext(ctx).Debugf("Configured DataDog metrics: pushing to %s once every %s",
metricsConfig.Datadog.Address, metricsConfig.Datadog.PushInterval)
}
2018-11-14 09:18:03 +00:00
if metricsConfig.StatsD != nil {
2018-11-14 09:18:03 +00:00
ctx := log.With(context.Background(), log.Str(log.MetricsProviderName, "statsd"))
registries = append(registries, metrics.RegisterStatsd(ctx, metricsConfig.StatsD))
log.FromContext(ctx).Debugf("Configured StatsD metrics: pushing to %s once every %s",
metricsConfig.StatsD.Address, metricsConfig.StatsD.PushInterval)
2017-04-18 06:22:06 +00:00
}
2018-11-14 09:18:03 +00:00
if metricsConfig.InfluxDB != nil {
2018-11-14 09:18:03 +00:00
ctx := log.With(context.Background(), log.Str(log.MetricsProviderName, "influxdb"))
registries = append(registries, metrics.RegisterInfluxDB(ctx, metricsConfig.InfluxDB))
log.FromContext(ctx).Debugf("Configured InfluxDB metrics: pushing to %s once every %s",
metricsConfig.InfluxDB.Address, metricsConfig.InfluxDB.PushInterval)
}
2017-04-18 06:22:06 +00:00
return metrics.NewMultiRegistry(registries)
}
func stopMetricsClients() {
metrics.StopDatadog()
metrics.StopStatsd()
metrics.StopInfluxDB()
}
2018-07-06 08:30:03 +00:00
func (s *Server) buildNameOrIPToCertificate(certs []tls.Certificate) map[string]*tls.Certificate {
certMap := make(map[string]*tls.Certificate)
for i := range certs {
cert := &certs[i]
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
continue
}
if len(x509Cert.Subject.CommonName) > 0 {
certMap[x509Cert.Subject.CommonName] = cert
}
for _, san := range x509Cert.DNSNames {
certMap[san] = cert
}
for _, ipSan := range x509Cert.IPAddresses {
certMap[ipSan.String()] = cert
}
}
return certMap
}