Permalink
Browse files

Move seds to a standalone repository

  • Loading branch information...
1 parent 93660b1 commit 16e3252dc7bf47e96de8bf62c8dd37ca222142ab @msantos committed Nov 4, 2010
Showing with 0 additions and 912 deletions.
  1. +0 −6 seds/Emakefile
  2. +0 −15 seds/Makefile
  3. +0 −56 seds/include/seds.hrl
  4. +0 −2 seds/priv/seds.cfg.dist
  5. +0 −101 seds/src/base32.erl
  6. +0 −185 seds/src/seds.erl
  7. +0 −245 seds/src/seds_protocol.erl
  8. +0 −299 seds/src/seds_proxy.erl
  9. +0 −3 seds/start.sh
View
@@ -1,6 +0,0 @@
-{["src/*"],
- [{i, "include"},
- {outdir, "ebin"},
- debug_info]
-}.
-
View
@@ -1,15 +0,0 @@
-
-ERL=erl
-
-all: dir erl
-
-dir:
- -@mkdir -p ebin deps
-
-erl:
- @$(ERL) -noinput +B \
- -eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
-
-clean:
- @rm -fv ebin/*.beam
-
View
@@ -1,56 +0,0 @@
-%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
-%% All rights reserved.
-%%
-%% Redistribution and use in source and binary forms, with or without
-%% modification, are permitted provided that the following conditions
-%% are met:
-%%
-%% Redistributions of source code must retain the above copyright
-%% notice, this list of conditions and the following disclaimer.
-%%
-%% Redistributions in binary form must reproduce the above copyright
-%% notice, this list of conditions and the following disclaimer in the
-%% documentation and/or other materials provided with the distribution.
-%%
-%% Neither the name of the author nor the names of its contributors
-%% may be used to endorse or promote products derived from this software
-%% without specific prior written permission.
-%%
-%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-%% POSSIBILITY OF SUCH DAMAGE.
-
--define(DNS_PORT, 53).
--define(PROXY_TIMEOUT, 5 * 60 * 1000).
-
--define(CFG, "seds.cfg").
-
--define(MAXLABEL, 63). % 1 byte for length of label
-
--record(seds, {
- q, % decoded DNS query
- type, % 'up' or 'down'
- id = 0, % 2 or 4 byte session ID
- forward, % tuple describing destination ip/port
- sum = 0, % byte count
- domain = [], % domain names
- data = [] % base64 encoded data
- }).
-
--record(config, {
- acf = false, % allow client forwarding
- acl = [], % forward IP blacklist
- acl_port = [22], % forward IP blacklist
- f, % forwarders map
- d = [] % domains
- }).
-
View
@@ -1,2 +0,0 @@
-{forward, [ {{127,0,0,1}, 22} ]}.
-{domains, []}.
View
@@ -1,101 +0,0 @@
-%% Taken from eadc-hub (http://github.com/JLarky/eadc-hub)
-%% http://github.com/JLarky/eadc-hub/blob/master/src/eadc_utils.erl
--module(base32).
--author('jlarky@gmail.com').
-
--export([b32/1, encode/1,unb32/1, decode/1 ]).
-
-
-%% @spec b32(integer()) -> [base32char()]
-%% @doc returns base32 character corresponding to V like 1 -> 'B', 31 -> '7'.
-%% V is integer from 0 to 31
-%% @see unb32/1
-b32(V) when V < 0 -> % wrong argument
- throw({b32,wrong_argument, V});
-b32(V) when V < 26 ->
- [V+65];
-b32(V) when V < 32 ->
- [V+24]; % V-26+48+2
-b32(V) when is_integer(V) ->
- b32_(V, "").
-
-b32_(0, Buf) ->
- Buf;
-b32_(V, Buf) ->
- [A]=b32(V rem 32),
- b32_(V bsr 5, [A|Buf]).
-
-%% @spec encode(string()) -> base32string()
-%% @doc returns base32 encoded string of String
-%% @see decode/1
-encode(Bin) when is_binary(Bin) ->
- lists:reverse(encode_(Bin, _Out=[]));
-encode(String) ->
- encode(list_to_binary(String)).
-
-encode_(Bin, Out) ->
- case Bin of
- <<>> ->
- Out;
- <<A:1>> ->
- [B]=b32(A bsl 4),[B|Out];
- <<A:2>> ->
- [B]=b32(A bsl 3),[B|Out];
- <<A:3>> ->
- [B]=b32(A bsl 2),[B|Out];
- <<A:4>> ->
- [B]=b32(A bsl 1),[B|Out];
- Bin ->
- <<A:5, T/bitstring>>=Bin,
- [B]=b32(A),encode_(T, [B|Out])
- end.
-
-%% @spec unb32(base32char()) -> integer()
-%% @doc A=unb32(b32(A))
-%% @see b32/1
-unb32([V]) when ((V >= $A) and (V =< $Z)) ->
- V-$A;
-unb32([V]) when ((V >= $2) and (V =< $7)) ->
- V-$2+26;
-unb32([V]) ->
- throw({badarg, [V]});
-unb32(String=[_|_]) ->
- lists:foldl(fun(Char, Acc) ->
- Acc*32+unb32([Char])
- end, 0, String).
-
-%% @spec decode(base32string()) -> string()
-%% @doc returns base32 decoded string of String
-%% @see encode/1
-decode(Bin) when is_binary(Bin) ->
- decode(binary_to_list(Bin));
-decode(String) ->
- Bits=lists:foldl(fun(Elem, Acc) ->
- A= unb32([Elem]),
- New= <<Acc/bitstring, A:5>>,
- New
- end, <<>>, String),
- decode_(Bits, _Out=[]).
-
-decode_(<<>>, Out) ->
- Out;
-decode_(Bits, Out) ->
- case Bits of
- <<Head:8, Rest/bitstring>> ->
- decode_(Rest, Out++[Head]);
- <<0:1>> -> Out;
- <<0:2>> -> Out;
- <<0:3>> -> Out;
- <<0:4>> -> Out;
- <<0:5>> -> Out;
- <<0:6>> -> Out;
- <<0:7>> -> Out;
- <<H:1>> -> Out++[H bsl 7];
- <<H:2>> -> Out++[H bsl 6];
- <<H:3>> -> Out++[H bsl 5];
- <<H:4>> -> Out++[H bsl 4];
- <<H:5>> -> Out++[H bsl 3];
- <<H:6>> -> Out++[H bsl 2];
- <<H:7>> -> Out++[H bsl 1]
- end.
-
View
@@ -1,185 +0,0 @@
-%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
-%% All rights reserved.
-%%
-%% Redistribution and use in source and binary forms, with or without
-%% modification, are permitted provided that the following conditions
-%% are met:
-%%
-%% Redistributions of source code must retain the above copyright
-%% notice, this list of conditions and the following disclaimer.
-%%
-%% Redistributions in binary form must reproduce the above copyright
-%% notice, this list of conditions and the following disclaimer in the
-%% documentation and/or other materials provided with the distribution.
-%%
-%% Neither the name of the author nor the names of its contributors
-%% may be used to endorse or promote products derived from this software
-%% without specific prior written permission.
-%%
-%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-%% POSSIBILITY OF SUCH DAMAGE.
--module(seds).
--behaviour(gen_server).
-
--include_lib("kernel/src/inet_dns.hrl").
--include("seds.hrl").
-
--define(SERVER, ?MODULE).
-
--export([start_link/0, start_link/1, send/2]).
--export([config/2, privpath/1]).
--export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
-
--record(state, {
- acf = false, % allow client forwarding
- acl = [], % forward IP blacklist
- acl_port = [], % allowed ports (whitelist)
-
- f, % forwarders map
- s, % socket
- d = [], % domains
- p = [] % list of proxies
- }).
-
-
-send({IP, Port, #dns_rec{} = Rec}, #seds{} = Query) ->
- gen_server:call(?SERVER, {send, {IP, Port, Rec, Query}}).
-
-
-start_link() ->
- start_link(?DNS_PORT).
-start_link(Port) ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
-
-init([Port]) when Port > 1024 ->
- init(Port, []);
-init([Port]) ->
- {ok, FD} = procket:listen(Port, [
- {protocol, udp},
- {family, inet},
- {type, dgram}
- ]),
- init(Port, [{fd, FD}]).
-
-init(Port, Opt) ->
- {ok, Socket} = gen_udp:open(Port, [
- binary,
- {active, once}
- ] ++ Opt),
- {ok, #state{
- acf = config(dynamic, ?CFG, false),
- acl = config(acl, ?CFG, []),
- acl_port = config(allowed_ports, ?CFG, [22]),
- f = config(forward, ?CFG, []),
- d = [ string:tokens(N, ".") || N <- config(domains, ?CFG) ],
- s = Socket,
- p = dict:new()
- }}.
-
-
-handle_call({send, {IP, Port, #dns_rec{} = Rec,
- #seds{type = Type, sum = Sum, data = Data} = Query}},
- _From, State) ->
- Session = seds_protocol:session(Query, map(State)),
- {Proxy, Proxies} = proxy(Session, State),
- ok = seds_proxy:send(Proxy, IP, Port, Rec, {Type, Sum, Data}),
- {reply, ok, State#state{p = Proxies}};
-
-handle_call(Request, _From, State) ->
- error_logger:error_report([{wtf, Request}]),
- {reply, ok, State}.
-
-handle_cast(_Msg, State) ->
- {noreply, State}.
-
-% DNS request from client
-handle_info({udp, Socket, IP, Port, Data}, #state{
- s = Socket
- } = State) ->
- ok = inet:setopts(Socket, [{active, once}]),
- spawn(seds_protocol, decode, [{IP, Port, Data}, map(State)]),
- {noreply, State};
-
-% Session terminated
-handle_info({'DOWN', _Ref, process, Pid, _Reason}, #state{
- p = Proxies
- } = State) ->
- {noreply, State#state{
- p = dict:filter(
- fun (_,V) when V == Pid -> false;
- (_,_) -> true
- end,
- Proxies)
- }};
-
-% WTF?
-handle_info(Info, State) ->
- error_logger:error_report([{wtf, Info}]),
- {noreply, State}.
-
-terminate(_Reason, _State) ->
- ok.
-code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
-
-
-%%%
-%%% Internal Functions
-%%%
-proxy({{IP, Port}, Id} = Session, #state{
- s = Socket,
- p = Proxies
- }) ->
- case dict:find(Session, Proxies) of
- error ->
- error_logger:info_report([
- {session_start, {IP, Port}},
- {id, Id}
- ]),
- {ok, Pid} = seds_proxy:start_link(Socket, {IP, Port}),
- {Pid, dict:store(Session, Pid, Proxies)};
- {ok, Pid} ->
- {Pid, Proxies}
- end.
-
-config(Key, Cfg) ->
- config(Key, Cfg, undefined).
-config(Key, Cfg, Default) ->
- {ok, Map} = file:consult(privpath(Cfg)),
- proplists:get_value(Key, Map, Default).
-
-privpath(Cfg) ->
- filename:join([
- filename:dirname(code:which(?MODULE)),
- "..",
- "priv",
- Cfg
- ]).
-
-map(#state{
- acf = ACF,
- acl = ACL,
- acl_port = ACP,
- f = Fwd,
- d = Domains
- }) ->
- #config{
- acf = ACF,
- acl = ACL,
- acl_port = ACP,
- f = Fwd,
- d = Domains
- }.
-
-
Oops, something went wrong.

0 comments on commit 16e3252

Please sign in to comment.