From 3a664c8e4dce6e0babe71567f6db6958d567aa04 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 21 Jul 2021 20:22:29 +0200 Subject: [PATCH 1/2] facilitate websocket traversal --- lib/mu_identifier.ex | 24 ++++++++++++++++++++++-- mix.exs | 3 +++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/mu_identifier.ex b/lib/mu_identifier.ex index 59d1ea0..a0b7f14 100644 --- a/lib/mu_identifier.ex +++ b/lib/mu_identifier.ex @@ -10,13 +10,33 @@ defmodule MuIdentifier do port = 80 IO.puts("Running Proxy with Cowboy on port #{port}") + # TODO, this could be cleaner with `:cowboy.start_clear` children = [ - {Secret,%{}}, - {Plug.Cowboy, scheme: :http, plug: Proxy, options: [port: port, compress: true]} + {Secret, %{}}, + {Plug.Cowboy, + scheme: :http, plug: Proxy, options: [dispatch: dispatch, port: port, compress: true]} ] Logger.info("Mu Identifier starting on port #{port}") Supervisor.start_link(children, strategy: :one_for_one) end + + defp dispatch do + default = %{ + host: "dispatcher", + port: 80, + path: "/" + } + + f = fn req -> default |> Map.put(:path, req.path <> "?" <> req.qs) end + + [ + {:_, + [ + {"/ws/[...]", WsHandler, {f, default}}, + {:_, Plug.Cowboy.Handler, {Proxy, []}} + ]} + ] + end end diff --git a/mix.exs b/mix.exs index 40ee15d..6abe176 100644 --- a/mix.exs +++ b/mix.exs @@ -30,9 +30,12 @@ defmodule Proxy.Mixfile do # Type `mix help deps` for more examples and options defp deps do [{:plug_mint_proxy, git: "https://github.com/madnificent/plug-mint-proxy.git", tag: "v0.2.0"}, + {:cowboy_ws_proxy, git: "https://github.com/ajuvercr/elixir-cowboy-ws-proxy-handler.git", tag: "v0.1"}, {:uuid, "~> 1.1"}, + {:gun, "~> 2.0.0-rc.2"}, {:replug, "~> 0.1.0"}, {:secure_random, "~> 0.5"}, + {:exsync, "~> 0.2", only: :dev}, {:observer_cli, "~> 1.5"}] end end From d78634b89703f4f54a262c6e3b9b576c9b3cf73f Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Fri, 17 Sep 2021 12:59:15 +0200 Subject: [PATCH 2/2] Update mu_identifier.ex --- lib/mu_identifier.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mu_identifier.ex b/lib/mu_identifier.ex index a0b7f14..d44e299 100644 --- a/lib/mu_identifier.ex +++ b/lib/mu_identifier.ex @@ -34,7 +34,7 @@ defmodule MuIdentifier do [ {:_, [ - {"/ws/[...]", WsHandler, {f, default}}, + {"/.mu/ws/[...]", WsHandler, {f, default}}, {:_, Plug.Cowboy.Handler, {Proxy, []}} ]} ]