2020-02-11 00:26:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/static"
|
2021-04-30 08:22:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/metrics"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/server/middleware"
|
2022-03-17 17:02:08 +00:00
|
|
|
tcpmiddleware "github.com/traefik/traefik/v2/pkg/server/middleware/tcp"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/server/router"
|
2022-03-17 17:02:08 +00:00
|
|
|
tcprouter "github.com/traefik/traefik/v2/pkg/server/router/tcp"
|
|
|
|
udprouter "github.com/traefik/traefik/v2/pkg/server/router/udp"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/server/service"
|
2022-12-09 08:58:05 +00:00
|
|
|
tcpsvc "github.com/traefik/traefik/v2/pkg/server/service/tcp"
|
|
|
|
udpsvc "github.com/traefik/traefik/v2/pkg/server/service/udp"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/tcp"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/tls"
|
2022-12-09 08:58:05 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/udp"
|
2020-02-11 00:26:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RouterFactory the factory of TCP/UDP routers.
|
|
|
|
type RouterFactory struct {
|
|
|
|
entryPointsTCP []string
|
|
|
|
entryPointsUDP []string
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
managerFactory *service.ManagerFactory
|
|
|
|
metricsRegistry metrics.Registry
|
2020-02-11 00:26:04 +00:00
|
|
|
|
2020-09-07 11:58:03 +00:00
|
|
|
pluginBuilder middleware.PluginsBuilder
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
chainBuilder *middleware.ChainBuilder
|
|
|
|
tlsManager *tls.Manager
|
2022-11-16 10:38:07 +00:00
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
dialerManager *tcp.DialerManager
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
cancelPrevState func()
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// NewRouterFactory creates a new RouterFactory.
|
2021-04-30 08:22:04 +00:00
|
|
|
func NewRouterFactory(staticConfiguration static.Configuration, managerFactory *service.ManagerFactory, tlsManager *tls.Manager,
|
2022-12-09 08:58:05 +00:00
|
|
|
chainBuilder *middleware.ChainBuilder, pluginBuilder middleware.PluginsBuilder, metricsRegistry metrics.Registry, dialerManager *tcp.DialerManager,
|
2022-03-21 09:42:08 +00:00
|
|
|
) *RouterFactory {
|
2020-02-11 00:26:04 +00:00
|
|
|
var entryPointsTCP, entryPointsUDP []string
|
|
|
|
for name, cfg := range staticConfiguration.EntryPoints {
|
|
|
|
protocol, err := cfg.GetProtocol()
|
|
|
|
if err != nil {
|
|
|
|
// Should never happen because Traefik should not start if protocol is invalid.
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msg("Invalid protocol")
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if protocol == "udp" {
|
|
|
|
entryPointsUDP = append(entryPointsUDP, name)
|
|
|
|
} else {
|
|
|
|
entryPointsTCP = append(entryPointsTCP, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RouterFactory{
|
2021-04-30 08:22:04 +00:00
|
|
|
entryPointsTCP: entryPointsTCP,
|
|
|
|
entryPointsUDP: entryPointsUDP,
|
|
|
|
managerFactory: managerFactory,
|
|
|
|
metricsRegistry: metricsRegistry,
|
|
|
|
tlsManager: tlsManager,
|
|
|
|
chainBuilder: chainBuilder,
|
|
|
|
pluginBuilder: pluginBuilder,
|
2022-12-09 08:58:05 +00:00
|
|
|
dialerManager: dialerManager,
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// CreateRouters creates new TCPRouters and UDPRouters.
|
2022-12-09 08:58:05 +00:00
|
|
|
func (f *RouterFactory) CreateRouters(rtConf *runtime.Configuration) (map[string]*tcprouter.Router, map[string]udp.Handler) {
|
2022-11-16 10:38:07 +00:00
|
|
|
if f.cancelPrevState != nil {
|
|
|
|
f.cancelPrevState()
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
ctx, f.cancelPrevState = context.WithCancel(context.Background())
|
2020-02-11 00:26:04 +00:00
|
|
|
|
|
|
|
// HTTP
|
|
|
|
serviceManager := f.managerFactory.Build(rtConf)
|
|
|
|
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, f.pluginBuilder)
|
2020-02-11 00:26:04 +00:00
|
|
|
|
2022-12-06 17:28:05 +00:00
|
|
|
routerManager := router.NewManager(rtConf, serviceManager, middlewaresBuilder, f.chainBuilder, f.metricsRegistry, f.tlsManager)
|
2020-02-11 00:26:04 +00:00
|
|
|
|
|
|
|
handlersNonTLS := routerManager.BuildHandlers(ctx, f.entryPointsTCP, false)
|
|
|
|
handlersTLS := routerManager.BuildHandlers(ctx, f.entryPointsTCP, true)
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
serviceManager.LaunchHealthCheck(ctx)
|
2020-02-28 23:13:44 +00:00
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
// TCP
|
2022-12-09 08:58:05 +00:00
|
|
|
svcTCPManager := tcpsvc.NewManager(rtConf, f.dialerManager)
|
2020-02-11 00:26:04 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
middlewaresTCPBuilder := tcpmiddleware.NewBuilder(rtConf.TCPMiddlewares)
|
2021-06-11 13:30:05 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
rtTCPManager := tcprouter.NewManager(rtConf, svcTCPManager, middlewaresTCPBuilder, handlersNonTLS, handlersTLS, f.tlsManager)
|
2020-02-11 00:26:04 +00:00
|
|
|
routersTCP := rtTCPManager.BuildHandlers(ctx, f.entryPointsTCP)
|
|
|
|
|
|
|
|
// UDP
|
2022-12-09 08:58:05 +00:00
|
|
|
svcUDPManager := udpsvc.NewManager(rtConf)
|
2022-03-17 17:02:08 +00:00
|
|
|
rtUDPManager := udprouter.NewManager(rtConf, svcUDPManager)
|
2020-02-11 00:26:04 +00:00
|
|
|
routersUDP := rtUDPManager.BuildHandlers(ctx, f.entryPointsUDP)
|
|
|
|
|
|
|
|
rtConf.PopulateUsedBy()
|
|
|
|
|
|
|
|
return routersTCP, routersUDP
|
|
|
|
}
|