2020-02-11 01:26:04 +01:00
package server
import (
"context"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/config/runtime"
"github.com/traefik/traefik/v2/pkg/config/static"
"github.com/traefik/traefik/v2/pkg/log"
2021-04-30 10:22:04 +02:00
"github.com/traefik/traefik/v2/pkg/metrics"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/server/middleware"
2022-03-17 11:02:08 -06:00
tcpmiddleware "github.com/traefik/traefik/v2/pkg/server/middleware/tcp"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/server/router"
2022-03-17 11:02:08 -06:00
tcprouter "github.com/traefik/traefik/v2/pkg/server/router/tcp"
udprouter "github.com/traefik/traefik/v2/pkg/server/router/udp"
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/server/service"
"github.com/traefik/traefik/v2/pkg/server/service/tcp"
"github.com/traefik/traefik/v2/pkg/server/service/udp"
"github.com/traefik/traefik/v2/pkg/tls"
2022-03-17 11:02:08 -06:00
udptypes "github.com/traefik/traefik/v2/pkg/udp"
2020-02-11 01:26:04 +01:00
)
// RouterFactory the factory of TCP/UDP routers.
type RouterFactory struct {
2024-09-13 15:54:04 +02:00
entryPointsTCP [ ] string
entryPointsUDP [ ] string
allowACMEByPass map [ string ] bool
managerFactory * service . ManagerFactory
2020-02-11 01:26:04 +01:00
2021-04-30 10:22:04 +02:00
metricsRegistry metrics . Registry
2020-02-11 01:26:04 +01:00
2020-09-07 13:58:03 +02:00
pluginBuilder middleware . PluginsBuilder
2024-09-13 15:54:04 +02:00
chainBuilder * middleware . ChainBuilder
tlsManager * tls . Manager
2020-02-11 01:26:04 +01:00
}
2020-05-11 12:06:07 +02:00
// NewRouterFactory creates a new RouterFactory.
2021-04-30 10:22:04 +02:00
func NewRouterFactory ( staticConfiguration static . Configuration , managerFactory * service . ManagerFactory , tlsManager * tls . Manager ,
2022-03-21 10:42:08 +01:00
chainBuilder * middleware . ChainBuilder , pluginBuilder middleware . PluginsBuilder , metricsRegistry metrics . Registry ,
) * RouterFactory {
2024-09-13 15:54:04 +02:00
handlesTLSChallenge := false
for _ , resolver := range staticConfiguration . CertificatesResolvers {
2024-09-19 11:50:04 +02:00
if resolver . ACME != nil && resolver . ACME . TLSChallenge != nil {
2024-09-13 15:54:04 +02:00
handlesTLSChallenge = true
break
}
}
allowACMEByPass := map [ string ] bool { }
2020-02-11 01:26:04 +01:00
var entryPointsTCP , entryPointsUDP [ ] string
2024-09-13 15:54:04 +02:00
for name , ep := range staticConfiguration . EntryPoints {
allowACMEByPass [ name ] = ep . AllowACMEByPass || ! handlesTLSChallenge
protocol , err := ep . GetProtocol ( )
2020-02-11 01:26:04 +01:00
if err != nil {
// Should never happen because Traefik should not start if protocol is invalid.
log . WithoutContext ( ) . Errorf ( "Invalid protocol: %v" , err )
}
if protocol == "udp" {
entryPointsUDP = append ( entryPointsUDP , name )
} else {
entryPointsTCP = append ( entryPointsTCP , name )
}
}
return & RouterFactory {
2021-04-30 10:22:04 +02:00
entryPointsTCP : entryPointsTCP ,
entryPointsUDP : entryPointsUDP ,
managerFactory : managerFactory ,
metricsRegistry : metricsRegistry ,
tlsManager : tlsManager ,
chainBuilder : chainBuilder ,
pluginBuilder : pluginBuilder ,
2024-09-13 15:54:04 +02:00
allowACMEByPass : allowACMEByPass ,
2020-02-11 01:26:04 +01:00
}
}
2020-05-11 12:06:07 +02:00
// CreateRouters creates new TCPRouters and UDPRouters.
2022-03-17 11:02:08 -06:00
func ( f * RouterFactory ) CreateRouters ( rtConf * runtime . Configuration ) ( map [ string ] * tcprouter . Router , map [ string ] udptypes . Handler ) {
2020-02-11 01:26:04 +01:00
ctx := context . Background ( )
// HTTP
serviceManager := f . managerFactory . Build ( rtConf )
2020-04-20 18:36:34 +02:00
middlewaresBuilder := middleware . NewBuilder ( rtConf . Middlewares , serviceManager , f . pluginBuilder )
2020-02-11 01:26:04 +01:00
2022-12-06 18:28:05 +01:00
routerManager := router . NewManager ( rtConf , serviceManager , middlewaresBuilder , f . chainBuilder , f . metricsRegistry , f . tlsManager )
2020-02-11 01:26:04 +01:00
handlersNonTLS := routerManager . BuildHandlers ( ctx , f . entryPointsTCP , false )
handlersTLS := routerManager . BuildHandlers ( ctx , f . entryPointsTCP , true )
2020-02-29 00:13:44 +01:00
serviceManager . LaunchHealthCheck ( )
2020-02-11 01:26:04 +01:00
// TCP
svcTCPManager := tcp . NewManager ( rtConf )
2022-03-17 11:02:08 -06:00
middlewaresTCPBuilder := tcpmiddleware . NewBuilder ( rtConf . TCPMiddlewares )
2021-06-11 15:30:05 +02:00
2022-03-17 11:02:08 -06:00
rtTCPManager := tcprouter . NewManager ( rtConf , svcTCPManager , middlewaresTCPBuilder , handlersNonTLS , handlersTLS , f . tlsManager )
2020-02-11 01:26:04 +01:00
routersTCP := rtTCPManager . BuildHandlers ( ctx , f . entryPointsTCP )
2024-09-13 15:54:04 +02:00
for ep , r := range routersTCP {
if allowACMEByPass , ok := f . allowACMEByPass [ ep ] ; ok && allowACMEByPass {
r . EnableACMETLSPassthrough ( )
}
}
2020-02-11 01:26:04 +01:00
// UDP
svcUDPManager := udp . NewManager ( rtConf )
2022-03-17 11:02:08 -06:00
rtUDPManager := udprouter . NewManager ( rtConf , svcUDPManager )
2020-02-11 01:26:04 +01:00
routersUDP := rtUDPManager . BuildHandlers ( ctx , f . entryPointsUDP )
rtConf . PopulateUsedBy ( )
return routersTCP , routersUDP
}