run mix formatter for consistency

master
Thurloat 6 years ago
parent 49b26be43e
commit c23178c0f0
  1. 20
      lib/dough.ex
  2. 11
      lib/dough/context.ex
  3. 35
      lib/dough/dohplug.ex
  4. 4
      lib/dough/exceptions.ex
  5. 13
      lib/dough/router.ex
  6. 5
      lib/dough/ttlcache.ex

@ -6,21 +6,27 @@ defmodule Dough do
import Supervisor.Spec import Supervisor.Spec
import Cachex.Spec import Cachex.Spec
def start(_type, _args) do def start(_type, _args) do
# List all child processes to be supervised # List all child processes to be supervised
children = [ children = [
# Starts a worker by calling: Dough.Worker.start_link(arg) # Starts a worker by calling: Dough.Worker.start_link(arg)
# {Dough.Worker, arg}, # {Dough.Worker, arg},
# Plug.Adapters.Cowboy.child_spec(:https, Dough.Router, [], port: 8331, keyfile: "priv/ssl/localhost.key", certfile: "priv/ssl/localhost.crt", otp_app: :dough) # Plug.Adapters.Cowboy.child_spec(:https, Dough.Router, [], port: 8331, keyfile: "priv/ssl/localhost.key", certfile: "priv/ssl/localhost.crt", otp_app: :dough)
worker(Cachex, [:dough, [ worker(Cachex, [
expiration: expiration( :dough,
[
expiration:
expiration(
default: :timer.seconds(6000), default: :timer.seconds(6000),
interval: :timer.seconds(300), interval: :timer.seconds(300),
lazy: true) lazy: true
]]), )
]
{Plug.Adapters.Cowboy2, scheme: :https, plug: Dough.Router, options: [ ]),
{Plug.Adapters.Cowboy2,
scheme: :https,
plug: Dough.Router,
options: [
port: 8331, port: 8331,
otp_app: :dough, otp_app: :dough,
keyfile: "priv/ssl/localhost.key", keyfile: "priv/ssl/localhost.key",

@ -1,26 +1,33 @@
defmodule Dough.RequestContext do defmodule Dough.RequestContext do
require Logger require Logger
defstruct [:start, :close, :lookup, :cache, :ttl] defstruct [:start, :close, :lookup, :cache, :ttl]
def ctx_start(ctx) do def ctx_start(ctx) do
Map.put(ctx, :start, System.monotonic_time(:milliseconds)) Map.put(ctx, :start, System.monotonic_time(:milliseconds))
end end
def ctx_close(ctx) do def ctx_close(ctx) do
Map.put(ctx, :close, System.monotonic_time(:milliseconds)) Map.put(ctx, :close, System.monotonic_time(:milliseconds))
end end
def ctx_lookup(ctx, dns_record) do def ctx_lookup(ctx, dns_record) do
Map.put(ctx, :lookup, dns_record.qdlist |> List.first()) Map.put(ctx, :lookup, dns_record.qdlist |> List.first())
end end
def ctx_cachehit(ctx, v) do def ctx_cachehit(ctx, v) do
Map.put(ctx, :cache, v) Map.put(ctx, :cache, v)
end end
def ctx_ttl(ctx, ttl) do def ctx_ttl(ctx, ttl) do
Map.put(ctx, :ttl, ttl) Map.put(ctx, :ttl, ttl)
end end
def ctx_log_out(ctx) do def ctx_log_out(ctx) do
Logger.info "#{ctx.lookup.type} - #{ctx.lookup.domain} - TTL #{ctx.ttl} | #{ctx.cache} - #{ctx.close - ctx.start}ms" Logger.info(
"#{ctx.lookup.type} - #{ctx.lookup.domain} - TTL #{ctx.ttl} | #{ctx.cache} - #{
ctx.close - ctx.start
}ms"
)
end end
end end

@ -10,13 +10,15 @@ defmodule Dough.DoHPlug do
def init(options), do: options def init(options), do: options
def call(conn, _opts) do def call(conn, _opts) do
ctx = %Dough.RequestContext{} |> ctx_start() ctx = %Dough.RequestContext{} |> ctx_start()
dns_message = case parse_doh(conn) do dns_message =
{:ok, msg} -> msg case parse_doh(conn) do
{:ok, msg} ->
msg
{:error, _} -> {:error, _} ->
Logger.error conn.method Logger.error(conn.method)
raise Dough.NotAllowed, "" raise Dough.NotAllowed, ""
end end
@ -24,7 +26,8 @@ defmodule Dough.DoHPlug do
decoded = DNS.Record.decode(dns_message) decoded = DNS.Record.decode(dns_message)
ctx = ctx_lookup(ctx, decoded) ctx =
ctx_lookup(ctx, decoded)
|> ctx_cachehit(cache_hit) |> ctx_cachehit(cache_hit)
|> ctx_ttl(ttl) |> ctx_ttl(ttl)
|> ctx_close() |> ctx_close()
@ -33,7 +36,10 @@ defmodule Dough.DoHPlug do
conn conn
# support for [RFC5861] / cache control extensions # support for [RFC5861] / cache control extensions
|> put_resp_header("cache-control", "max-age=#{ttl}, stale-while-revalidate=#{ttl * 2}, stale-if-error=#{ttl * 10}") |> put_resp_header(
"cache-control",
"max-age=#{ttl}, stale-while-revalidate=#{ttl * 2}, stale-if-error=#{ttl * 10}"
)
|> put_resp_header("content-type", content_type(conn)) |> put_resp_header("content-type", content_type(conn))
|> send_resp(200, dns_resp) |> send_resp(200, dns_resp)
end end
@ -53,30 +59,33 @@ defmodule Dough.DoHPlug do
ttlcache_set(dns_message, resp, ttl) ttlcache_set(dns_message, resp, ttl)
{:miss, ttl, resp} {:miss, ttl, resp}
result -> result ->
ttl = ttl_extract(result) ttl = ttl_extract(result)
{:hit, ttl, result} {:hit, ttl, result}
end end
end end
def parse_doh(conn) do def parse_doh(conn) do
conn = fetch_query_params conn conn = fetch_query_params(conn)
case conn.method do case conn.method do
"GET" -> {:ok, Base.url_decode64!(conn.query_params["dns"], padding: false, ignore: :whitespace)} "GET" ->
{:ok, Base.url_decode64!(conn.query_params["dns"], padding: false, ignore: :whitespace)}
"POST" -> "POST" ->
{:ok, body, _conn} = read_body(conn) {:ok, body, _conn} = read_body(conn)
{:ok, body} {:ok, body}
_ -> {:error, nil}
_ ->
{:error, nil}
end end
end end
def handoff_dns(dns_message) do def handoff_dns(dns_message) do
client = Socket.UDP.open! client = Socket.UDP.open!()
send!(client, dns_message, {"8.8.8.8", 53}) send!(client, dns_message, {"8.8.8.8", 53})
{data, _server} = recv!(client) {data, _server} = recv!(client)
data data
end end
end end

@ -1,9 +1,9 @@
defmodule Dough.NotAllowed do defmodule Dough.NotAllowed do
defexception [plug_status: 405, message: "Method Not Allowed"] defexception plug_status: 405, message: "Method Not Allowed"
end end
defmodule Dough.ServerError do defmodule Dough.ServerError do
defexception [plug_status: 500, message: "Server Error"] defexception plug_status: 500, message: "Server Error"
end end
defimpl Plug.Exception, for: [Dough.NotAllowed, Dough.ServerError] do defimpl Plug.Exception, for: [Dough.NotAllowed, Dough.ServerError] do

@ -2,17 +2,16 @@ defmodule Dough.Router do
use Plug.Router use Plug.Router
use Plug.ErrorHandler use Plug.ErrorHandler
if Mix.env == :dev do if Mix.env() == :dev do
use Plug.Debugger use Plug.Debugger
end end
require Logger require Logger
plug :match plug(:match)
plug :dispatch plug(:dispatch)
get "/", do: send_resp(conn, 200, "")
match "/dns-query", to: Dough.DoHPlug
match _, do: send_resp(conn, 404, "oopsie")
get("/", do: send_resp(conn, 200, ""))
match("/dns-query", to: Dough.DoHPlug)
match(_, do: send_resp(conn, 404, "oopsie"))
end end

@ -1,5 +1,4 @@
defmodule Dough.TTL do defmodule Dough.TTL do
require Logger require Logger
def ttlcache_lookup(dns_message) do def ttlcache_lookup(dns_message) do
@ -29,6 +28,7 @@ defmodule Dough.TTL do
{ans, nil} -> {ans, nil} ->
ans.ttl ans.ttl
{ans, _} -> {ans, _} ->
ans.ttl ans.ttl
end end
@ -36,7 +36,6 @@ defmodule Dough.TTL do
def _hashify_dns(dns_message) do def _hashify_dns(dns_message) do
:crypto.hash(:sha256, dns_message) :crypto.hash(:sha256, dns_message)
|> Base.encode16 |> Base.encode16()
end end
end end

Loading…
Cancel
Save