Skip to content

Commit

Permalink
Merge pull request #188 from lafirest/feat/udp_proxy
Browse files Browse the repository at this point in the history
feat: add content-sensitive proxy behaviour for UDP
  • Loading branch information
lafirest authored Jul 10, 2024
2 parents 313713e + 6aa62db commit 3251771
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 4 deletions.
63 changes: 63 additions & 0 deletions include/esockd_proxy.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2024 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-ifndef(ESOCKD_PROXY_HRL).
-define(ESOCKD_PROXY_HRL, true).

-define(SSL_TRANSPORT, esockd_transport).
-define(PROXY_TRANSPORT, esockd_udp_proxy).

-type proxy_id() :: pid().
-type socket_packet() :: binary().
-type socket() :: inet:socket() | ssl:sslsocket().

-type transport() :: {udp, pid(), socket()} | ?SSL_TRANSPORT.
-type proxy_transport() :: {?PROXY_TRANSPORT, pid(), socket()}.
-type address() :: {inet:ip_address(), inet:port_number()}.
-type peer() :: socket() | address().

-type connection_module() :: atom().
-type connection_state() :: term().
-type connection_packet() :: term().

-type connection_id() ::
peer()
| integer()
| string()
| binary().

-type proxy_packet() ::
{?PROXY_TRANSPORT, proxy_id(), binary(), connection_packet()}.

%% Routing information search results

%% send raw socket packet
-type get_connection_id_result() ::
%% send decoded packet
{ok, connection_id(), connection_packet(), connection_state()}
| invalid.

-type connection_options() :: #{
esockd_proxy_opts := proxy_options(),
atom() => term()
}.

-type proxy_options() :: #{
connection_mod := connection_module(),
heartbeat => non_neg_integer()
}.

-endif.
4 changes: 4 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
{src_dirs, ["src"]}.


{deps,
[ {gproc, {git, "https://github.com/uwiger/gproc", {tag, "0.8.0"}}}
]}.

{cover_enabled, true}.
{cover_opts, [verbose]}.
{cover_export_enabled, true}.
Expand Down
14 changes: 10 additions & 4 deletions src/esockd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ start_link() ->
child_spec(Proto, ListenOn, Opts) when is_atom(Proto) ->
ListenerRef = {Proto, ListenOn},
_ = esockd_server:set_listener_prop(ListenerRef, type, tcp),
_ = esockd_server:set_listener_prop(ListenerRef, options, Opts),
_ = esockd_server:set_listener_prop(ListenerRef, options, Opts),
#{id => child_id(Proto, ListenOn),
start => {esockd_listener_sup, start_link, [Proto, ListenOn]},
restart => transient,
Expand All @@ -76,7 +76,7 @@ udp_child_spec(Proto, Port, Opts) when is_atom(Proto) ->
dtls_child_spec(Proto, Port, Opts) when is_atom(Proto) ->
ListenerRef = {Proto, Port},
_ = esockd_server:set_listener_prop(ListenerRef, type, dtls),
_ = esockd_server:set_listener_prop(ListenerRef, options, Opts),
_ = esockd_server:set_listener_prop(ListenerRef, options, Opts),
#{id => child_id(Proto, Port),
start => {esockd_listener_sup, start_link, [Proto, Port]},
restart => transient,
Expand Down Expand Up @@ -191,5 +191,11 @@ init([]) ->
type => worker,
modules => [esockd_server]
},
{ok, {SupFlags, [Limiter, Server]}}.

ProxyDB = #{id => esockd_udp_proxy_db,
start => {esockd_udp_proxy_db, start_link, []},
restart => permanent,
shutdown => 5000,
type => worker,
modules => [esockd_udp_proxy_db]
},
{ok, {SupFlags, [Limiter, Server, ProxyDB]}}.
43 changes: 43 additions & 0 deletions src/udp_proxy/doc/proxy.plantuml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@startuml
skinparam dpi 300
left to right direction

frame client {
card clinet1
card client2
card client3
}

frame proxy {
label "proxy converts the udp connection\nto long connection in the application"
agent proxy1
agent proxy2
agent proxy3
}

frame connection {
card connection1
card connection2
card connection3
}

clinet1 .[#red].> proxy1
client2 .[#blue].> proxy1
client3 ..> proxy2
client3 ..> proxy3

proxy1 -[#red]-> connection1 : client1
proxy1 -[#blue]-> connection2 : client2
proxy2 --> connection3 : client3
proxy3 --> connection3 : client3

note bottom of proxy1
different clients use the same UDP channel,e.g NAT, the proxy will first disconnect the existing connection and then connect to the new one
endnote


note right of proxy
proxy2 and proxy3 are multiple channels used by a client,e.g LB
endnote

@enduml
Binary file added src/udp_proxy/doc/proxy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3251771

Please sign in to comment.