@@ -243,6 +243,9 @@ var defaults = map[string]any{
243243 "uni_sse" : false ,
244244 "uni_http_stream" : false ,
245245
246+ "client_connect_code_to_unidirectional_disconnect.enabled" : false ,
247+ "client_connect_code_to_unidirectional_disconnect.transforms" : []any {},
248+
246249 "uni_sse_connect_code_to_http_response.enabled" : false ,
247250 "uni_sse_connect_code_to_http_response.transforms" : []any {},
248251 "uni_http_stream_connect_code_to_http_response.enabled" : false ,
@@ -1911,6 +1914,11 @@ func granularProxiesFromConfig(v *viper.Viper) []proxy.Config {
19111914 if p .Endpoint == "" {
19121915 log .Fatal ().Msgf ("no endpoint set for proxy %s" , p .Name )
19131916 }
1917+ for i , transform := range p .HttpStatusTransforms {
1918+ if err := transform .Validate (); err != nil {
1919+ log .Fatal ().Msgf ("error validating proxy_http_status_code_transforms[%d] in proxy %s: %v" , i , p .Name , err )
1920+ }
1921+ }
19141922 names [p .Name ] = struct {}{}
19151923 }
19161924
@@ -2075,28 +2083,9 @@ func proxyMapConfig() (*client.ProxyMap, bool) {
20752083 if v .IsSet ("proxy_http_status_code_transforms" ) {
20762084 tools .DecodeSlice (v , & httpStatusTransforms , "proxy_http_status_code_transforms" )
20772085 }
2078- for _ , transform := range httpStatusTransforms {
2079- if transform .StatusCode == 0 {
2080- log .Fatal ().Msg ("status should be set in proxy_http_status_code_transforms item" )
2081- }
2082- if transform .ToDisconnect .Code == 0 && transform .ToError .Code == 0 {
2083- log .Fatal ().Msg ("no error or disconnect code set in proxy_http_status_code_transforms item" )
2084- }
2085- if transform .ToDisconnect .Code > 0 && transform .ToError .Code > 0 {
2086- log .Fatal ().Msg ("only error or disconnect code can be set in proxy_http_status_code_transforms item, but not both" )
2087- }
2088- if ! tools .IsASCII (transform .ToDisconnect .Reason ) {
2089- log .Fatal ().Msg ("proxy_http_status_code_transforms item disconnect reason must be ASCII" )
2090- }
2091- if ! tools .IsASCII (transform .ToError .Message ) {
2092- log .Fatal ().Msg ("proxy_http_status_code_transforms item error message must be ASCII" )
2093- }
2094- const reasonOrMessageMaxLength = 123 // limit comes from WebSocket close reason length limit. See https://datatracker.ietf.org/doc/html/rfc6455.
2095- if len (transform .ToError .Message ) > reasonOrMessageMaxLength {
2096- log .Fatal ().Msgf ("proxy_http_status_code_transforms item error message can be up to %d characters long" , reasonOrMessageMaxLength )
2097- }
2098- if len (transform .ToDisconnect .Reason ) > reasonOrMessageMaxLength {
2099- log .Fatal ().Msgf ("proxy_http_status_code_transforms item disconnect reason can be up to %d characters long" , reasonOrMessageMaxLength )
2086+ for i , transform := range httpStatusTransforms {
2087+ if err := transform .Validate (); err != nil {
2088+ log .Fatal ().Msgf ("error validating proxy_http_status_code_transforms[%d]: %v" , i , err )
21002089 }
21012090 }
21022091 proxyConfig .HttpStatusTransforms = httpStatusTransforms
@@ -2431,6 +2420,23 @@ func nodeConfig(version string) centrifuge.Config {
24312420 }
24322421 cfg .LogLevel = level
24332422 cfg .LogHandler = newLogHandler ().handle
2423+
2424+ uniCodeTransformsEnabled := viper .GetBool ("client_connect_code_to_unidirectional_disconnect.enabled" )
2425+ if uniCodeTransformsEnabled {
2426+ var uniCodeToDisconnectTransforms []tools.UniConnectCodeToDisconnectTransform
2427+ if viper .IsSet ("client_connect_code_to_unidirectional_disconnect.transforms" ) {
2428+ tools .DecodeSlice (viper .GetViper (), & uniCodeToDisconnectTransforms , "client_connect_code_to_unidirectional_disconnect.transforms" )
2429+ }
2430+ uniCodeTransforms := make (map [uint32 ]centrifuge.Disconnect )
2431+ for _ , transform := range uniCodeToDisconnectTransforms {
2432+ if err := transform .Validate (); err != nil {
2433+ log .Fatal ().Msgf ("error validating unidirectional code to disconnect transform: %v" , err )
2434+ }
2435+ uniCodeTransforms [transform .Code ] = centrifuge.Disconnect {Code : transform .To .Code , Reason : transform .To .Reason }
2436+ }
2437+ cfg .UnidirectionalCodeToDisconnect = uniCodeTransforms
2438+ }
2439+
24342440 return cfg
24352441}
24362442
@@ -2570,6 +2576,11 @@ func uniSSEHandlerConfig() unisse.Config {
25702576 if viper .IsSet ("uni_sse_connect_code_to_http_response.transforms" ) {
25712577 tools .DecodeSlice (viper .GetViper (), & connectCodeToHTTPStatusTransforms , "uni_sse_connect_code_to_http_response.transforms" )
25722578 }
2579+ for i , transform := range connectCodeToHTTPStatusTransforms {
2580+ if err := transform .Validate (); err != nil {
2581+ log .Fatal ().Msgf ("error validating uni_sse_connect_code_to_http_response.transforms[%d]: %v" , i , err )
2582+ }
2583+ }
25732584 return unisse.Config {
25742585 MaxRequestBodySize : viper .GetInt ("uni_sse_max_request_body_size" ),
25752586 PingPongConfig : getPingPongConfig (),
@@ -2586,6 +2597,11 @@ func uniStreamHandlerConfig() unihttpstream.Config {
25862597 if viper .IsSet ("uni_http_stream_connect_code_to_http_response.transforms" ) {
25872598 tools .DecodeSlice (viper .GetViper (), & connectCodeToHTTPStatusTransforms , "uni_http_stream_connect_code_to_http_response.transforms" )
25882599 }
2600+ for i , transform := range connectCodeToHTTPStatusTransforms {
2601+ if err := transform .Validate (); err != nil {
2602+ log .Fatal ().Msgf ("error validating uni_http_stream_connect_code_to_http_response.transforms[%d]: %v" , i , err )
2603+ }
2604+ }
25892605 return unihttpstream.Config {
25902606 MaxRequestBodySize : viper .GetInt ("uni_http_stream_max_request_body_size" ),
25912607 PingPongConfig : getPingPongConfig (),
0 commit comments