Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add content-sensitive proxy behaviour for UDP #188

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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