@@ -394,60 +394,69 @@ log_connection_exception(Name, ConnectedAt, Ex) ->
394
394
connection_closed_abruptly -> warning ;
395
395
_ -> error
396
396
end ,
397
- log_connection_exception (Severity , Name , ConnectedAt , Ex ).
397
+ Duration = connection_duration (ConnectedAt ),
398
+ log_connection_exception (Severity , Name , Duration , Ex ).
398
399
399
- log_connection_exception (Severity , Name , ConnectedAt , {heartbeat_timeout , TimeoutSec }) ->
400
- ConnDuration = connection_duration (ConnectedAt ),
400
+ log_connection_exception (Severity , Name , Duration , {heartbeat_timeout , TimeoutSec }) ->
401
401
Fmt = " closing AMQP connection ~tp (~ts , duration: '~ts '):~n "
402
402
" missed heartbeats from client, timeout: ~p s" ,
403
403
% % Long line to avoid extra spaces and line breaks in log
404
404
log_connection_exception_with_severity (Severity , Fmt ,
405
- [self (), Name , ConnDuration , TimeoutSec ]);
406
- log_connection_exception (Severity , Name , _ConnectedAt ,
405
+ [self (), Name , Duration , TimeoutSec ]);
406
+ log_connection_exception (Severity , Name , _Duration ,
407
407
{connection_closed_abruptly ,
408
408
# v1 {connection = # connection {user = # user {username = Username },
409
409
vhost = VHost ,
410
410
connected_at = ConnectedAt }}}) ->
411
- ConnDuration = connection_duration (ConnectedAt ),
411
+ Duration = connection_duration (ConnectedAt ),
412
412
Fmt = " closing AMQP connection ~tp (~ts , vhost: '~ts ', user: '~ts ', duration: '~ts '):~n "
413
413
" client unexpectedly closed TCP connection" ,
414
414
log_connection_exception_with_severity (Severity , Fmt ,
415
- [self (), Name , VHost , Username , ConnDuration ]);
415
+ [self (), Name , VHost , Username , Duration ]);
416
416
% % when client abruptly closes connection before connection.open/authentication/authorization
417
417
% % succeeded, don't log username and vhost as 'none'
418
- log_connection_exception (Severity , Name , ConnectedAt , {connection_closed_abruptly , _ }) ->
419
- ConnDuration = connection_duration (ConnectedAt ),
418
+ log_connection_exception (Severity , Name , Duration , {connection_closed_abruptly , _ }) ->
420
419
Fmt = " closing AMQP connection ~tp (~ts , duration: '~ts '):~n "
421
420
" client unexpectedly closed TCP connection" ,
422
421
log_connection_exception_with_severity (Severity , Fmt ,
423
- [self (), Name , ConnDuration ]);
422
+ [self (), Name , Duration ]);
424
423
% % failed connection.tune negotiations
425
- log_connection_exception (Severity , Name , ConnectedAt , {handshake_error , tuning ,
426
- {exit , # amqp_error {explanation = Explanation },
427
- _Method , _Stacktrace }}) ->
428
- ConnDuration = connection_duration (ConnectedAt ),
424
+ log_connection_exception (Severity , Name , Duration , {handshake_error , tuning ,
425
+ {exit , # amqp_error {explanation = Explanation },
426
+ _Method , _Stacktrace }}) ->
429
427
Fmt = " closing AMQP connection ~tp (~ts ):~n "
430
428
" failed to negotiate connection parameters: ~ts " ,
431
- log_connection_exception_with_severity (Severity , Fmt , [self (), Name , ConnDuration , Explanation ]);
432
- log_connection_exception (Severity , Name , ConnectedAt , {sasl_required , ProtocolId }) ->
433
- ConnDuration = connection_duration (ConnectedAt ),
429
+ log_connection_exception_with_severity (Severity , Fmt , [self (), Name , Duration , Explanation ]);
430
+ log_connection_exception (Severity , Name , Duration , {sasl_required , ProtocolId }) ->
434
431
Fmt = " closing AMQP 1.0 connection (~ts , duration: '~ts '): RabbitMQ requires SASL "
435
432
" security layer (expected protocol ID 3, but client sent protocol ID ~b )" ,
436
433
log_connection_exception_with_severity (Severity , Fmt ,
437
- [Name , ConnDuration , ProtocolId ]);
434
+ [Name , Duration , ProtocolId ]);
438
435
% % old exception structure
439
- log_connection_exception (Severity , Name , ConnectedAt , connection_closed_abruptly ) ->
440
- ConnDuration = connection_duration (ConnectedAt ),
436
+ log_connection_exception (Severity , Name , Duration , connection_closed_abruptly ) ->
441
437
Fmt = " closing AMQP connection ~tp (~ts , duration: '~ts '):~n "
442
438
" client unexpectedly closed TCP connection" ,
443
439
log_connection_exception_with_severity (Severity , Fmt ,
444
- [self (), Name , ConnDuration ]);
445
- log_connection_exception (Severity , Name , ConnectedAt , Ex ) ->
446
- ConnDuration = connection_duration (ConnectedAt ),
440
+ [self (), Name , Duration ]);
441
+ log_connection_exception (Severity , Name , Duration , {bad_header , detected_tls }) ->
442
+ Fmt = " closing AMQP connection ~ts (duration: '~ts '):~n "
443
+ " TLS client detected on non-TLS AMQP port. "
444
+ " Ensure the client is connecting to the correct port." ,
445
+ log_connection_exception_with_severity (Severity , Fmt , [Name , Duration ]);
446
+ log_connection_exception (Severity , Name , Duration , {bad_header , detected_http_get }) ->
447
+ Fmt = " closing AMQP connection ~ts (duration: '~ts '):~n "
448
+ " HTTP GET request detected on AMQP port. "
449
+ " Ensure the client is connecting to the correct port." ,
450
+ log_connection_exception_with_severity (Severity , Fmt , [Name , Duration ]);
451
+ log_connection_exception (Severity , Name , Duration , {bad_header , Other }) ->
452
+ Fmt = " closing AMQP connection ~ts (duration: '~ts '):~n "
453
+ " client did not start with AMQP protocol header: ~p " ,
454
+ log_connection_exception_with_severity (Severity , Fmt , [Name , Duration , Other ]);
455
+ log_connection_exception (Severity , Name , Duration , Ex ) ->
447
456
Fmt = " closing AMQP connection ~tp (~ts , duration: '~ts '):~n "
448
457
" ~tp " ,
449
458
log_connection_exception_with_severity (Severity , Fmt ,
450
- [self (), Name , ConnDuration , Ex ]).
459
+ [self (), Name , Duration , Ex ]).
451
460
452
461
log_connection_exception_with_severity (Severity , Fmt , Args ) ->
453
462
case Severity of
@@ -1118,18 +1127,16 @@ handle_input({frame_payload, Type, Channel, PayloadSize}, Data, State) ->
1118
1127
end ;
1119
1128
handle_input (handshake , <<" AMQP" , A , B , C , D , Rest /binary >>, State ) ->
1120
1129
{Rest , version_negotiation ({A , B , C , D }, State )};
1130
+ handle_input (handshake , <<" GET " , _URL /binary >>, # v1 {sock = Sock }) ->
1131
+ % % Looks like an HTTP request.
1132
+ refuse_connection (Sock , {bad_header , detected_http_get });
1133
+ handle_input (handshake ,
1134
+ <<16#16 , 16#03 , _Ver2 , _Len1 , _Len2 , 16#01 , _ /binary >>,
1135
+ # v1 {sock = Sock }) ->
1136
+ % % Looks like a TLS client hello.
1137
+ refuse_connection (Sock , {bad_header , detected_tls });
1121
1138
handle_input (handshake , <<Other :8 /binary , _ /binary >>, # v1 {sock = Sock }) ->
1122
- Reason = case Other of
1123
- <<16#16 , 16#03 , _Ver2 , _Len1 , _Len2 , 16#01 , _ , _ >> ->
1124
- % % Looks like a TLS client hello.
1125
- detected_unexpected_tls_header ;
1126
- <<" GET " , _URL /binary >> ->
1127
- % % Looks like an HTTP request.
1128
- detected_unexpected_http_header ;
1129
- _ ->
1130
- bad_header
1131
- end ,
1132
- refuse_connection (Sock , {Reason , Other });
1139
+ refuse_connection (Sock , {bad_header , Other });
1133
1140
handle_input (Callback , Data , _State ) ->
1134
1141
throw ({bad_input , Callback , Data }).
1135
1142
@@ -1872,8 +1879,8 @@ get_client_value_detail(_Field, _ClientValue) ->
1872
1879
" " .
1873
1880
1874
1881
connection_duration (ConnectedAt ) ->
1875
- Now = os :system_time (milli_seconds ),
1876
- DurationMillis = Now - ConnectedAt ,
1882
+ Now = os :system_time (millisecond ),
1883
+ DurationMillis = max ( 0 , Now - ConnectedAt ) ,
1877
1884
if
1878
1885
DurationMillis >= 1000 ->
1879
1886
DurationSecs = DurationMillis div 1000 ,
0 commit comments