Page MenuHomePhorge

No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None
diff --git a/debug_server.exs b/debug_server.exs
new file mode 100644
index 0000000..69df482
--- /dev/null
+++ b/debug_server.exs
@@ -0,0 +1,56 @@
+require Logger
+
+Application.ensure_started(:cacophony)
+Application.ensure_all_started(:ranch)
+
+alias Cacophony.Message
+
+defmodule DebugServer do
+ def handle_message(%Message.BindRequest{} = request) do
+ response = %Message.BindResponse{
+ id: request.id,
+ result_code: :success,
+ matched_cn: "cn=kaniini"
+ }
+
+ {:ok, response}
+ end
+
+ def handle_message(%Message.UnbindRequest{}), do: :noreply
+
+ def handle_message(_message), do: {:error, :badmatch}
+end
+
+defmodule DebugListener do
+ def start_link(_opts) do
+ :ranch.start_listener(make_ref(), :ranch_tcp, [{:port, 6389}], Cacophony.Server, [mod: DebugServer])
+ end
+
+ def child_spec(_) do
+ %{id: __MODULE__, start: {__MODULE__, :start_link, [[]]}}
+ end
+end
+
+defmodule DebugSupervisor do
+ use Supervisor
+
+ def start_link(opts) do
+ Supervisor.start_link(__MODULE__, :ok, opts)
+ end
+
+ @impl true
+ def init(:ok) do
+ children = [DebugListener]
+
+ Supervisor.init(children, strategy: :one_for_one)
+ end
+end
+
+{:ok, pid} = DebugSupervisor.start_link([])
+ref = Process.monitor(pid)
+
+Logger.info("Debugging server started on port 6389.")
+
+receive do
+ {:DOWN, ^ref, _, _, _} -> Process.exit()
+end
diff --git a/lib/cacophony/server.ex b/lib/cacophony/server.ex
new file mode 100644
index 0000000..339269b
--- /dev/null
+++ b/lib/cacophony/server.ex
@@ -0,0 +1,64 @@
+defmodule Cacophony.Server do
+ @moduledoc """
+ A simple ranch protocol handler that services LDAP requests and dispatches
+ them to a handler module.
+ """
+ @behaviour :ranch_protocol
+
+ require Logger
+
+ alias Cacophony.Message
+
+ def start_link(ref, socket, transport, mod: mod) do
+ pid = :proc_lib.spawn_link(__MODULE__, :init, [ref, socket, transport, mod])
+ {:ok, pid}
+ end
+
+ def init(ref, _, transport, mod) do
+ {:ok, socket} = :ranch.handshake(ref)
+ :ok = transport.setopts(socket, [{:active, true}])
+
+ Logger.debug("Opening connection: #{inspect(socket)}")
+
+ :gen_server.enter_loop(__MODULE__, [], %{
+ ref: ref,
+ socket: socket,
+ transport: transport,
+ mod: mod
+ })
+ end
+
+ def handle_info({:tcp, socket, data}, %{socket: socket, transport: transport, mod: mod} = state) do
+ {:ok, %{} = msg} = Message.decode(data)
+
+ Logger.debug("Incoming message: #{inspect(msg)}")
+
+ case mod.handle_message(msg) do
+ {:ok, reply} ->
+ {:ok, bin} = Message.encode(reply)
+ transport.send(socket, bin)
+ {:noreply, state}
+
+ :noreply ->
+ {:noreply, state}
+
+ {:error, e} ->
+ Logger.error("While processing message #{inspect(msg)}, #{inspect(e)} occured.")
+ transport.close(socket)
+ {:stop, :normal, state}
+ end
+ end
+
+ def handle_info({:tcp_closed, socket}, %{socket: socket, transport: transport} = state) do
+ Logger.debug("Closing connection: #{inspect(socket)}")
+
+ transport.close(socket)
+ {:stop, :normal, state}
+ end
+
+ def handle_info({:tcp_error, socket}, %{socket: socket} = state) do
+ Logger.debug("Closing connection: #{inspect(socket)}")
+
+ {:stop, :normal, state}
+ end
+end
diff --git a/mix.exs b/mix.exs
index 7c177f6..d8ab50b 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,32 +1,33 @@
defmodule Cacophony.MixProject do
use Mix.Project
def project do
[
app: :cacophony,
version: "0.1.0",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps(),
compilers: [:asn1] ++ Mix.compilers()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
applications: [:asn1],
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:asn1ex, git: "https://github.com/vicentfg/asn1ex"},
{:credo, "~> 1.0.0", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.19", only: :dev, runtime: false},
- {:dialyxir, "~> 1.0.0-rc.5", only: [:dev], runtime: false}
+ {:dialyxir, "~> 1.0.0-rc.5", only: [:dev], runtime: false},
+ {:ranch, "~> 1.0"}
]
end
end
diff --git a/mix.lock b/mix.lock
index a51517f..93383f0 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,13 +1,14 @@
%{
"asn1ex": {:git, "https://github.com/vicentfg/asn1ex", "0255348e2fffbdfd1eef7b46f71dc733318a36a0", []},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "1.0.5", "fdea745579f8845315fe6a3b43e2f9f8866839cfbc8562bb72778e9fdaa94214", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.7", "6287f8f2cb45df8584317a4be1075b8c9b8a69de8eeb82b4d9e6c761cf2664cd", [:mix], [{:erlex, ">= 0.2.5", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"},
"erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm"},
+ "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 21, 3:19 PM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55723
Default Alt Text
(6 KB)

Event Timeline