forked from electric-sql/electric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(electric): Allow the migrations proxy to accept WebSocket connec…
…tions (electric-sql#738) Closes VAX-1417.
- Loading branch information
Showing
12 changed files
with
230 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@core/electric": patch | ||
--- | ||
|
||
[VAX-1417] Add the option to tunnel TCP connections to the migrations proxy over regular WebSocket connections. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
components/electric/lib/electric/plug/proxy_websocket_plug.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule Electric.Plug.ProxyWebsocketPlug do | ||
@moduledoc """ | ||
This plug handles requests to establish a WebSocket connection to the migrations proxy. | ||
On hosting providers that only allow inbound HTTP traffic on port 80 (and HTTPS traffic on port 443), Electric's proxy | ||
cannot open its own dedicated port to listen on incoming TCP connections. This plug, together with the server | ||
implementation in `Electric.Postgres.Proxy.WebsocketSerevr`, enables Electric to tunnel TCP traffic over the WebSocket | ||
protocol provided that a matching tunnel is set up on the client side. | ||
Note that unless the migrations proxy's port is configured with the special value "http", this plug will reject any | ||
incoming requests with a 404 status code. | ||
""" | ||
|
||
@behaviour Plug | ||
|
||
import Plug.Conn | ||
|
||
alias Electric.Replication.Connectors | ||
|
||
require Logger | ||
|
||
def init(handler_opts), do: handler_opts | ||
|
||
def call(conn, handler_opts) do | ||
conn_config = conn_config() | ||
proxy_config = Connectors.get_proxy_opts(conn_config) | ||
|
||
if proxy_config.use_http_tunnel? do | ||
upgrade_to_websocket(conn, Keyword.put_new(handler_opts, :proxy_config, proxy_config)) | ||
else | ||
Logger.warning( | ||
"Attempted WebSocket connection to the migrations proxy but it wasn't enabled." | ||
) | ||
|
||
send_resp(conn, 404, "Migrations proxy is not configured to accept WebSocket connections") | ||
end | ||
end | ||
|
||
defp upgrade_to_websocket(conn, websocket_opts) do | ||
with {:ok, conn} <- check_if_valid_upgrade(conn) do | ||
conn | ||
|> upgrade_adapter( | ||
:websocket, | ||
{Electric.Postgres.Proxy.WebsocketServer, websocket_opts, []} | ||
) | ||
else | ||
{:error, code, body} -> | ||
Logger.debug("Clients WebSocket connection failed with reason: #{body}") | ||
send_resp(conn, code, body) | ||
end | ||
end | ||
|
||
defp check_if_valid_upgrade(%Plug.Conn{} = conn) do | ||
if Bandit.WebSocket.Handshake.valid_upgrade?(conn) do | ||
{:ok, conn} | ||
else | ||
{:error, 400, "Bad request"} | ||
end | ||
end | ||
|
||
defp conn_config, do: Electric.Application.pg_connection_opts() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
components/electric/lib/electric/plug/satellite_websocket_plug.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.