20
20
21
21
-export ([start /3 , reconnect /1 , is_enabled /0 , allow /1 , block /1 ]).
22
22
23
- -define (TABLE , ? MODULE ).
23
+ -define (NODES_TO_BLOCK , inet_tcp_proxy__nodes_to_block ).
24
+ -define (NODES_BLOCKED , inet_tcp_proxy__nodes_blocked ).
24
25
25
26
% % This can't start_link because there's no supervision hierarchy we
26
27
% % can easily fit it into (we need to survive all application
@@ -44,16 +45,16 @@ reconnect(Nodes) ->
44
45
ok .
45
46
46
47
is_enabled () ->
47
- lists :member (? TABLE , ets :all ()).
48
+ lists :member (? NODES_TO_BLOCK , ets :all ()).
48
49
49
50
allow (Node ) ->
50
- error_logger : info_msg (" (~s ) Allowing distribution between ~s and ~s~n " ,
51
+ rabbit_log : warning (" (~s ) Allowing distribution between ~s and ~s~n " ,
51
52
[? MODULE , node (), Node ]),
52
- ets :delete (? TABLE , Node ).
53
+ true = ets :delete (? NODES_TO_BLOCK , Node ).
53
54
block (Node ) ->
54
- error_logger : info_msg (" (~s ) BLOCKING distribution between ~s and ~s~n " ,
55
+ rabbit_log : warning (" (~s ) BLOCKING distribution between ~s and ~s~n " ,
55
56
[? MODULE , node (), Node ]),
56
- ets :insert (? TABLE , {Node , block }).
57
+ true = ets :insert (? NODES_TO_BLOCK , {Node , block }).
57
58
58
59
% %----------------------------------------------------------------------------
59
60
@@ -67,14 +68,23 @@ error_handler(Thunk) ->
67
68
% % over; die quietly.
68
69
ok ;
69
70
_ :X ->
70
- io :format (user , " TCP proxy died with ~p~n At ~p~n " ,
71
+ error_logger :error_msg (
72
+ " TCP proxy died with ~p~n At ~p~n " ,
73
+ [X , erlang :get_stacktrace ()]),
74
+ io :format (standard_error ,
75
+ " TCP proxy died with ~p~n At ~p~n " ,
71
76
[X , erlang :get_stacktrace ()]),
77
+ timer :sleep (1000 ),
72
78
erlang :halt (1 )
73
79
end
74
80
end .
75
81
76
82
go (Parent , Port , ProxyPort ) ->
77
- ets :new (? TABLE , [public , named_table ]),
83
+ ets :new (? NODES_TO_BLOCK , [public , named_table ]),
84
+ ets :new (? NODES_BLOCKED , [public , named_table ]),
85
+ rabbit_log :info (
86
+ " (~s ) Listening on proxy port ~p~n " ,
87
+ [? MODULE , ProxyPort ]),
78
88
{ok , Sock } = gen_tcp :listen (ProxyPort , [inet ,
79
89
{reuseaddr , true }]),
80
90
Parent ! ready ,
@@ -88,8 +98,12 @@ accept_loop(ListenSock, Port) ->
88
98
89
99
run_it (SockIn , Port ) ->
90
100
case {inet :peername (SockIn ), inet :sockname (SockIn )} of
91
- {{ok , {_Addr , SrcPort }}, {ok , {Addr , _OtherPort }}} ->
101
+ {{ok , {_Addr , SrcPort }}, {ok , {Addr , OtherPort }}} ->
92
102
{ok , Remote , This } = inet_tcp_proxy_manager :lookup (SrcPort ),
103
+ rabbit_log :info (
104
+ " (~s ) => Incoming proxied connection from node ~s (port ~b ) "
105
+ " to node ~s (port ~b )~n " ,
106
+ [? MODULE , Remote , SrcPort , This , OtherPort ]),
93
107
case node () of
94
108
This -> ok ;
95
109
_ -> exit ({not_me , node (), This })
@@ -101,18 +115,20 @@ run_it(SockIn, Port) ->
101
115
end .
102
116
103
117
run_loop (Sockets , RemoteNode , Buf0 ) ->
104
- Block = [{RemoteNode , block }] =:= ets :lookup (? TABLE , RemoteNode ),
118
+ Block = [{RemoteNode , block }] =:= ets :lookup (? NODES_TO_BLOCK , RemoteNode ),
119
+ WasBlocked = [{RemoteNode , blocked }] =:= ets :lookup (? NODES_BLOCKED ,
120
+ RemoteNode ),
105
121
receive
106
122
{tcp , Sock , Data } ->
107
123
Buf = [Data | Buf0 ],
108
- case {Block , get ( dist_was_blocked ) } of
109
- {true , false } ->
110
- put ( dist_was_blocked , Block ),
124
+ case {WasBlocked , Block } of
125
+ {false , true } ->
126
+ true = ets : insert ( ? NODES_BLOCKED , { RemoteNode , blocked } ),
111
127
rabbit_log :warning (
112
128
" (~s ) Distribution BLOCKED between ~s and ~s~n " ,
113
129
[? MODULE , node (), RemoteNode ]);
114
- {false , S } when S =:= true orelse S =:= undefined ->
115
- put ( dist_was_blocked , Block ),
130
+ {true , false } ->
131
+ true = ets : delete ( ? NODES_BLOCKED , RemoteNode ),
116
132
rabbit_log :warning (
117
133
" (~s ) Distribution allowed between ~s and ~s~n " ,
118
134
[? MODULE , node (), RemoteNode ]);
@@ -125,6 +141,9 @@ run_loop(Sockets, RemoteNode, Buf0) ->
125
141
true -> run_loop (Sockets , RemoteNode , Buf )
126
142
end ;
127
143
{tcp_closed , Sock } ->
144
+ rabbit_log :info (
145
+ " (~s ) Distribution closed between ~s and ~s~n " ,
146
+ [? MODULE , node (), RemoteNode ]),
128
147
gen_tcp :close (other (Sock , Sockets ));
129
148
X ->
130
149
exit ({weirdness , X })
0 commit comments