@@ -41,15 +41,16 @@ func ValidateL4Routes(tcpRoutes []TraefikTCPRoute, udpRoutes []TraefikUDPRoute)
4141 return nil
4242}
4343
44- func UpdateHttpRoutesWithL4 (httpRoutes []TraefikRoute , tcpRoutes []TraefikTCPRoute , udpRoutes []TraefikUDPRoute ) error {
44+ func UpdateHttpRoutesWithL4 (httpRoutes []TraefikRoute , tcpRoutes []TraefikTCPRoute , udpRoutes []TraefikUDPRoute , serverName string ) error {
4545 if err := ValidateL4Routes (tcpRoutes , udpRoutes ); err != nil {
4646 return fmt .Errorf ("port validation failed: %w" , err )
4747 }
4848
49- config := traefikFullConfig {
50- HTTP : httpConfig {
51- Routers : make (map [string ]router ),
52- Services : make (map [string ]service ),
49+ config := traefikFullConfigWithMiddlewares {
50+ HTTP : httpConfigWithMiddlewares {
51+ Routers : make (map [string ]routerWithMiddleware ),
52+ Services : make (map [string ]service ),
53+ Middlewares : make (map [string ]middleware ),
5354 },
5455 TCP : tcpConfig {
5556 Routers : make (map [string ]tcpRouter ),
@@ -61,16 +62,29 @@ func UpdateHttpRoutesWithL4(httpRoutes []TraefikRoute, tcpRoutes []TraefikTCPRou
6162 },
6263 }
6364
65+ var middlewareNames []string
66+ if serverName != "" {
67+ config .HTTP .Middlewares ["forwarded_server" ] = middleware {
68+ Headers : & headersMiddleware {
69+ CustomRequestHeaders : map [string ]string {
70+ "X-Forwarded-Server" : serverName ,
71+ },
72+ },
73+ }
74+ middlewareNames = []string {"forwarded_server@file" }
75+ }
76+
6477 for _ , route := range httpRoutes {
6578 if len (route .Upstreams ) == 0 {
6679 continue
6780 }
6881
69- config .HTTP .Routers [route .ServiceId ] = router {
82+ config .HTTP .Routers [route .ServiceId ] = routerWithMiddleware {
7083 Rule : fmt .Sprintf ("Host(`%s`)" , route .Domain ),
7184 EntryPoints : []string {"websecure" },
7285 Service : route .ServiceId ,
7386 TLS : & tlsConfig {},
87+ Middlewares : middlewareNames ,
7488 }
7589
7690 servers := make ([]server , len (route .Upstreams ))
@@ -238,6 +252,7 @@ func GetCurrentL4ConfigHash() string {
238252 for routerName , rtr := range config .TCP .Routers {
239253 var externalPort int
240254 var serviceId string
255+
241256 fmt .Sscanf (routerName , "tcp_%s_%d" , & serviceId , & externalPort )
242257
243258 for _ , ep := range rtr .EntryPoints {
@@ -302,15 +317,16 @@ func GetCurrentL4ConfigHash() string {
302317 return HashTCPRoutes (tcpRoutes ) + HashUDPRoutes (udpRoutes )
303318}
304319
305- func readCurrentFullConfig () (* traefikFullConfig , error ) {
320+ func readCurrentFullConfig () (* traefikFullConfigWithMiddlewares , error ) {
306321 routesPath := filepath .Join (traefikDynamicDir , routesFileName )
307322 data , err := os .ReadFile (routesPath )
308323 if err != nil {
309324 if os .IsNotExist (err ) {
310- return & traefikFullConfig {
311- HTTP : httpConfig {
312- Routers : make (map [string ]router ),
313- Services : make (map [string ]service ),
325+ return & traefikFullConfigWithMiddlewares {
326+ HTTP : httpConfigWithMiddlewares {
327+ Routers : make (map [string ]routerWithMiddleware ),
328+ Services : make (map [string ]service ),
329+ Middlewares : make (map [string ]middleware ),
314330 },
315331 TCP : tcpConfig {
316332 Routers : make (map [string ]tcpRouter ),
@@ -325,17 +341,20 @@ func readCurrentFullConfig() (*traefikFullConfig, error) {
325341 return nil , err
326342 }
327343
328- var config traefikFullConfig
344+ var config traefikFullConfigWithMiddlewares
329345 if err := yaml .Unmarshal (data , & config ); err != nil {
330346 return nil , err
331347 }
332348
333349 if config .HTTP .Routers == nil {
334- config .HTTP .Routers = make (map [string ]router )
350+ config .HTTP .Routers = make (map [string ]routerWithMiddleware )
335351 }
336352 if config .HTTP .Services == nil {
337353 config .HTTP .Services = make (map [string ]service )
338354 }
355+ if config .HTTP .Middlewares == nil {
356+ config .HTTP .Middlewares = make (map [string ]middleware )
357+ }
339358 if config .TCP .Routers == nil {
340359 config .TCP .Routers = make (map [string ]tcpRouter )
341360 }
0 commit comments