Begin adding ws server

This commit is contained in:
connor rigby 2017-11-02 06:34:21 -07:00
parent 9eacd7f925
commit 7953d2100d
4 changed files with 46 additions and 31 deletions

View file

@ -12,10 +12,13 @@ defmodule Farmbot.BotState.Transport.HTTP.AuthPlug do
def call(conn, opts) do
fun = Keyword.fetch!(opts, :handler)
apply(__MODULE__, fun, [conn, opts])
case apply(__MODULE__, fun, [conn]) do
{conn, code, msg} -> send_resp(conn, code, msg)
conn -> conn
end
end
def handle_prod(conn, _opts) do
def handle_prod(conn) do
with [<<_ :: size(8), <<"earer ">>, maybe_token :: binary>>] <- get_req_header(conn, "authorization"),
[token] <- String.split(maybe_token, ","),
{:ok, key} <- HTTP.public_key(),
@ -24,20 +27,20 @@ defmodule Farmbot.BotState.Transport.HTTP.AuthPlug do
conn
else
# if the JWT failed to verify.
{false, _, _} -> send_resp(conn, 401, "unauthorized token.")
{false, _, _} -> {conn, 401, "unauthorized token."}
# An error happened verifying.
{:error, reason} -> send_resp(conn, 400, "token verification error: " <> format_error(reason))
{:error, reason} -> {conn, 400, "token verification error: " <> format_error(reason)}
# The header didn't match the pattern.
[_auth_header] -> send_resp(conn, 400, "bad auth header.")
[_ | _] -> send_resp(conn, 400, "too many auth headers.")
[] -> send_resp(conn, 400, "no auth header supplied.")
[_auth_header] -> {conn, 400, "bad auth header."}
[_ | _] -> {conn, 400, "too many auth headers."}
[] -> {conn, 400, "no auth header supplied."}
# some other problem
other -> send_resp(conn, 500, format_error(other))
other -> {conn, 500, format_error(other)}
end
end
def handle_dev(conn, _opts), do: conn
def handle_test(conn, opts), do: handle_dev(conn, opts)
def handle_dev(conn), do: conn
def handle_test(conn), do: handle_dev(conn)
defp format_error(err) when is_binary(err), do: err
defp format_error(err), do: inspect err

View file

@ -11,7 +11,7 @@ defmodule Farmbot.BotState.Transport.HTTP.Router do
end
plug Plug.Logger, log: :debug
plug AuthPlug, env: :prod
plug AuthPlug, env: Mix.env()
plug :match
plug :dispatch
@ -20,22 +20,6 @@ defmodule Farmbot.BotState.Transport.HTTP.Router do
send_resp conn, 200, data
end
get "/socket_test" do
html = """
<html>
<body>
<script>
var socket = new WebSocket("ws://localhost:27347/ws")
socket.onmessage = function (event) {
console.log(JSON.parse(event.data));
}
</script>
</body>
</html>
"""
send_resp(conn, 200, html)
end
match _ do
send_resp(conn, 404, "oops")
end

View file

@ -12,13 +12,23 @@ defmodule Farmbot.BotState.Transport.HTTP.SocketHandler do
@behaviour :cowboy_websocket_handler
def init(_, _req, _opts), do: {:upgrade, :protocol, :cowboy_websocket}
def init(blah, req, opts) do
conn = Plug.Adapters.Cowboy.Conn.conn(req, :tcp)
require IEx; IEx.pry
{:upgrade, :protocol, :cowboy_websocket}
end
# Called on websocket connection initialization.
def websocket_init(_type, req, _options) do
Logger.info "Opened websocket connection."
HTTP.subscribe()
{:ok, req, nil, @timeout}
conn = Plug.Adapters.Cowboy.Conn.conn(req, :tcp)
require IEx; IEx.pry
case Farmbot.BotState.Transport.HTTP.AuthPlug.handle_prod(conn) do
{_conn, _, _} -> {:shutdown, req}
_ ->
Logger.info "Websocket connect."
HTTP.subscribe()
{:ok, req, nil, @timeout}
end
end
# def websocket_handle({:text, @pong}, req, state), do: {:ok, req, state}

View file

@ -10,6 +10,24 @@ defmodule Farmbot.System.DebugRouter do
plug(:dispatch)
forward "/wobserver", to: Wobserver.Web.Router
get "/socket_test" do
html = """
<html>
<body>
<script>
var token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJib3QiLCJzdWIiOjIsImlhdCI6MTUwOTU2MjY2NywianRpIjoiNzk1NTFjMWMtYjUzNi00YzUyLWE4ODYtYjIwMDhlZDJiMDE5IiwiaXNzIjoiLy8xOTIuMTY4LjI5LjE2NTozMDAwIiwiZXhwIjoxNTEzMDE4NjY3LCJtcXR0IjoiMTkyLjE2OC4yOS4xNjUiLCJtcXR0X3dzIjoid3M6Ly8xOTIuMTY4LjI5LjE2NTozMDAyL3dzIiwib3NfdXBkYXRlX3NlcnZlciI6Imh0dHBzOi8vYXBpLmdpdGh1Yi5jb20vcmVwb3MvZmFybWJvdC9mYXJtYm90X29zL3JlbGVhc2VzL2xhdGVzdCIsImZ3X3VwZGF0ZV9zZXJ2ZXIiOiJERVBSRUNBVEVEIiwiYm90IjoiZGV2aWNlXzIifQ.coMXH40kr9K6tKAVzxZUmE2arq5g_bPyf0VzbEkSyeTmniTN6y2XDVdppIsee8ScQfx8h5EBi7jUYYIdeFp7_zofiq9n-twEPrF6a9kqyEPV8TshjaJb6zfJSby1JyOS0sd3acnvwOziVCPr64eHU_aFSWcVIAi6-YJryCdJ6kCdNC-Se1aA7gJhg0M8curq2Eh7BRD2jH7sJxqxrMiWT89FBSuUNv9lxBVIs40WbRElJGZaoZHV_R4pVC47dGuqX2VDCIa7HHU0ZDtI0FOkWZCVvfJgHh2EJdrpitrPz3Ev_dz4zuLB9N4fPoxmmtGn_GUR9KobiyFpCoiRg3zrhg"
var socket = new WebSocket("ws://" + token + "@localhost:27347/ws")
socket.onmessage = function (event) {
console.log(JSON.parse(event.data));
}
</script>
</body>
</html>
"""
send_resp(conn, 200, html)
end
match(_, do: send_resp(conn, 404, "Page not found"))
end