• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

emqx / esockd / 399

08 Jul 2024 11:36AM UTC coverage: 69.305%. First build
399

Pull #188

github

web-flow
Merge 6de8b0fd5 into 313713eff
Pull Request #188: feat: add content-sensitive proxy behaviour for UDP

0 of 70 new or added lines in 3 files covered. (0.0%)

867 of 1251 relevant lines covered (69.3%)

59.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/udp_proxy/esockd_udp_proxy.erl
1
%%--------------------------------------------------------------------
2
%% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved.
3
%%
4
%% Licensed under the Apache License, Version 2.0 (the "License");
5
%% you may not use this file except in compliance with the License.
6
%% You may obtain a copy of the License at
7
%%
8
%%     http://www.apache.org/licenses/LICENSE-2.0
9
%%
10
%% Unless required by applicable law or agreed to in writing, software
11
%% distributed under the License is distributed on an "AS IS" BASIS,
12
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
%% See the License for the specific language governing permissions and
14
%% limitations under the License.
15
%%--------------------------------------------------------------------
16

17
-module(esockd_udp_proxy).
18

19
-behaviour(gen_server).
20

21
-include("include/esockd_proxy.hrl").
22

23
%% API
24
-export([start_link/3, send/2]).
25

26
%% gen_server callbacks
27
-export([
28
    init/1,
29
    handle_call/3,
30
    handle_cast/2,
31
    handle_info/2,
32
    terminate/2
33
]).
34

35
-export_type([connection_options/0]).
36

37
-define(NOW, erlang:system_time(second)).
38
-define(ERROR_MSG(Format, Args),
39
    error_logger:error_msg("[~s]: " ++ Format, [?MODULE | Args])
40
).
41
-define(DEF_HEARTBEAT, 60).
42

43
-type timespan() :: non_neg_integer().
44

45
%%--------------------------------------------------------------------
46
%%  Definitions
47
%%--------------------------------------------------------------------
48

49
-type state() :: #{
50
    connection_mod := connection_module(),
51
    connection_id := connection_id() | undefined,
52
    connection_state := connection_state(),
53
    connection_options := connection_options(),
54
    %% last source's connection active time
55
    last_time := pos_integer(),
56
    transport := transport(),
57
    peer := peer()
58
}.
59

60
%%--------------------------------------------------------------------
61
%%- API
62
%%--------------------------------------------------------------------
63

64
start_link(Transport, Peer, Opts) ->
NEW
65
    gen_server:start_link(?MODULE, [Transport, Peer, Opts], []).
×
66

67
-spec send(proxy_id(), binary()) -> ok.
68
send(ProxyId, Data) ->
NEW
69
    gen_server:cast(ProxyId, {send, Data}).
×
70

71
%%--------------------------------------------------------------------
72
%%- gen_server callbacks
73
%%--------------------------------------------------------------------
74

75
init([Transport, Peer, #{esockd_proxy_opts := Opts} = COpts]) ->
NEW
76
    #{connection_mod := Mod} = Opts,
×
NEW
77
    heartbeat(maps:get(heartbeat, Opts, ?DEF_HEARTBEAT)),
×
NEW
78
    {ok, #{
×
79
        last_time => ?NOW,
80
        peer => Peer,
81
        transport => Transport,
82
        connection_mod => Mod,
83
        connection_options => COpts,
84
        connection_state => esockd_udp_proxy_connection:initialize(Mod, COpts),
85
        connection_id => undefined
86
    }}.
87

88
handle_call(Request, _From, State) ->
NEW
89
    ?ERROR_MSG("Unexpected call: ~p", [Request]),
×
NEW
90
    {reply, ok, State}.
×
91

92
handle_cast({send, Data}, #{transport := Transport, peer := Peer} = State) ->
NEW
93
    send(Transport, Peer, Data),
×
NEW
94
    {noreply, State};
×
95
handle_cast(Request, State) ->
NEW
96
    ?ERROR_MSG("Unexpected cast: ~p", [Request]),
×
NEW
97
    {noreply, State}.
×
98

99
handle_info({datagram, _SockPid, Data}, State) ->
NEW
100
    {noreply, handle_incoming(Data, State)};
×
101
handle_info({ssl, _Socket, Data}, State) ->
NEW
102
    {noreply, handle_incoming(Data, State)};
×
103
handle_info({heartbeat, Span}, #{last_time := LastTime} = State) ->
NEW
104
    Now = ?NOW,
×
NEW
105
    case Now - LastTime > Span of
×
106
        true ->
NEW
107
            {stop, normal, State};
×
108
        _ ->
NEW
109
            heartbeat(Span),
×
NEW
110
            {noreply, State}
×
111
    end;
112
handle_info({ssl_error, _Sock, Reason}, State) ->
NEW
113
    {stop, Reason, socket_exit(State)};
×
114
handle_info({ssl_closed, _Sock}, State) ->
NEW
115
    {stop, ssl_closed, socket_exit(State)};
×
116
handle_info(
117
    {'DOWN', _, process, _, _Reason},
118
    State
119
) ->
NEW
120
    {stop, connection_closed, State};
×
121
handle_info(Info, State) ->
NEW
122
    ?ERROR_MSG("Unexpected info: ~p", [Info]),
×
NEW
123
    {noreply, State}.
×
124

125
terminate(Reason, State) ->
NEW
126
    detach(State, Reason =/= connection_closed).
×
127

128
%%--------------------------------------------------------------------
129
%%- Internal functions
130
%%--------------------------------------------------------------------
131
-spec handle_incoming(socket_packet(), state()) -> state().
132
handle_incoming(
133
    Data,
134
    #{transport := Transport, peer := Peer, connection_mod := Mod, connection_state := CState} =
135
        State
136
) ->
NEW
137
    State2 = State#{last_time := ?NOW},
×
NEW
138
    case esockd_udp_proxy_connection:get_connection_id(Mod, Transport, Peer, CState, Data) of
×
139
        {ok, CId, Packet, CState2} ->
NEW
140
            dispatch(Mod, CId, Data, Packet, State2#{connection_state := CState2});
×
141
        invalid ->
NEW
142
            State2
×
143
    end.
144

145
-spec dispatch(
146
    connection_module(),
147
    connection_id(),
148
    socket_packet(),
149
    connection_packet(),
150
    state()
151
) ->
152
    state().
153
dispatch(
154
    Mod,
155
    CId,
156
    Data,
157
    Packet,
158
    #{connection_state := CState, connection_options := Opts} = State
159
) ->
NEW
160
    {ok, Pid} = lookup(Mod, CId, Opts),
×
NEW
161
    esockd_udp_proxy_connection:dispatch(
×
162
        Mod, Pid, CState, {?PROXY_TRANSPORT, self(), Data, Packet}
163
    ),
NEW
164
    attach(CId, State).
×
165

166
-spec attach(connection_id(), state()) -> state().
167
attach(CId, #{connection_id := undefined} = State) ->
NEW
168
    esockd_udp_proxy_db:attach(CId),
×
NEW
169
    State#{connection_id := CId};
×
170
attach(CId, #{connection_id := OldId} = State) when CId =/= OldId ->
NEW
171
    State2 = detach(State),
×
NEW
172
    attach(CId, State2);
×
173
attach(_CId, State) ->
NEW
174
    State.
×
175

176
-spec detach(state()) -> state().
177
detach(State) ->
NEW
178
    detach(State, true).
×
179

180
-spec detach(connection_id(), state()) -> state().
181
detach(#{connection_id := undefined} = State, _Clear) ->
NEW
182
    State;
×
183
detach(#{connection_id := CId, connection_mod := Mod, connection_state := CState} = State, Clear) ->
NEW
184
    case esockd_udp_proxy_db:detach(CId) of
×
185
        {Clear, Pid} ->
NEW
186
            esockd_udp_proxy_connection:close(Mod, Pid, CState);
×
187
        _ ->
NEW
188
            ok
×
189
    end,
NEW
190
    State#{connection_id := undefined}.
×
191

192
-spec socket_exit(state()) -> state().
193
socket_exit(State) ->
NEW
194
    detach(State).
×
195

196
-spec heartbeat(timespan()) -> ok.
197
heartbeat(Span) ->
NEW
198
    erlang:send_after(timer:seconds(Span), self(), {?FUNCTION_NAME, Span}),
×
NEW
199
    ok.
×
200

201
-spec lookup(
202
    connection_module(),
203
    connection_id(),
204
    connection_options()
205
) -> {ok, pid()}.
206
lookup(Mod, CId, Opts) ->
NEW
207
    case esockd_udp_proxy_db:lookup(CId) of
×
208
        {ok, _} = Ok ->
NEW
209
            Ok;
×
210
        undefined ->
NEW
211
            {ok, Pid} = esockd_udp_proxy_connection:create(Mod, ?PROXY_TRANSPORT, self(), Opts),
×
NEW
212
            _ = erlang:monitor(process, Pid),
×
NEW
213
            {ok, Pid}
×
214
    end.
215

216
-spec send(transport(), peer(), binary()) -> _.
217
send({udp, _, Socket}, {IP, Port}, Data) ->
NEW
218
    gen_udp:send(Socket, IP, Port, Data);
×
219
send(?SSL_TRANSPORT, Socket, Data) ->
NEW
220
    socket:send(Socket, Data).
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc