Skip to content

Commit baa4d45

Browse files
authored
Add support for custom homeserver URLs (#85)
This can be used to connect to proxies like Pantalaimon or for servers whose automatic discovery is broken
1 parent b3cd9e2 commit baa4d45

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ Two notes on this screenshot:
7878
* no SSL/TLS
7979
* SASL username: your full matrix ID (`user:homeserver.example.org`)
8080
* SASL password: your matrix password
81+
* (Optional / advanced configuration) If you cannot use homeserver URL discovery, configure `homeserver-url=https://homeserver.example.org` as your IRC client's GECOS/"real name"
8182

8283
See below for extra instructions to work with web clients.
8384

8485
See `INSTALL.md` for a more production-oriented guide.
8586

87+
## End-to-end encryption
88+
89+
Matrix2051 does not support Matrix's end-to-end encryption (E2EE), but can optionally be used with [Pantalaimon](https://github.com/matrix-org/pantalaimon).
90+
91+
To do so, setup Pantalaimon locally, and configure `homeserverurl=http://localhost:8009` as your IRC client's GECOS/"real name".
92+
8693
## Architecture
8794

8895
* `matrix2051.exs` starts M51.Application, which starts M51.Supervisor, which

lib/irc/handler.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ defmodule M51.IrcConn.Handler do
187187
state = M51.IrcConn.Supervisor.state(sup_pid)
188188

189189
M51.IrcConn.State.set_nick(state, nick)
190-
M51.IrcConn.State.set_gecos(state, gecos)
191190

192191
case user_id do
193192
# all good
@@ -355,6 +354,8 @@ defmodule M51.IrcConn.Handler do
355354
nil
356355

357356
{"USER", [_, _, _, gecos | _]} ->
357+
state = M51.IrcConn.Supervisor.state(sup_pid)
358+
M51.IrcConn.State.set_gecos(state, gecos)
358359
{:user, gecos}
359360

360361
{"USER", _} ->
@@ -428,6 +429,7 @@ defmodule M51.IrcConn.Handler do
428429

429430
# TODO: support multi-line AUTHENTICATE
430431

432+
state = M51.IrcConn.Supervisor.state(sup_pid)
431433
matrix_client = M51.IrcConn.Supervisor.matrix_client(sup_pid)
432434

433435
case M51.MatrixClient.Client.user_id(matrix_client) do
@@ -440,11 +442,23 @@ defmodule M51.IrcConn.Handler do
440442
{:ok, {local_name, hostname}} ->
441443
user_id = authcid
442444

445+
# Keep deprecated alias "plaintextproxy" that was used back when
446+
# this was only meant to be used with Pantalaimon
447+
proxy =
448+
case Regex.named_captures(
449+
~R((plaintextproxy|homeserver-url\)=(?<url>https?://\S*\)),
450+
M51.IrcConn.State.gecos(state) || ""
451+
) do
452+
nil -> nil
453+
%{"url" => url} -> url
454+
end
455+
443456
case M51.MatrixClient.Client.connect(
444457
matrix_client,
445458
local_name,
446459
hostname,
447-
passwd
460+
passwd,
461+
proxy
448462
) do
449463
{:ok} ->
450464
# RPL_LOGGEDIN

lib/matrix_client/client.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ defmodule M51.MatrixClient.Client do
6666
end
6767

6868
@impl true
69-
def handle_call({:connect, local_name, hostname, password}, _from, state) do
69+
def handle_call({:connect, local_name, hostname, password, proxy}, _from, state) do
7070
case state do
7171
%M51.MatrixClient.Client{
7272
state: :initial_state,
7373
irc_pid: irc_pid
7474
} ->
7575
httpoison = M51.Config.httpoison()
76-
base_url = get_base_url(hostname)
76+
base_url = proxy || get_base_url(hostname)
7777

7878
# Check the server supports password login
7979
url = base_url <> "/_matrix/client/r0/login"
@@ -545,8 +545,8 @@ defmodule M51.MatrixClient.Client do
545545
end
546546
end
547547

548-
def connect(pid, local_name, hostname, password) do
549-
GenServer.call(pid, {:connect, local_name, hostname, password}, @timeout)
548+
def connect(pid, local_name, hostname, password, proxy \\ nil) do
549+
GenServer.call(pid, {:connect, local_name, hostname, password, proxy}, @timeout)
550550
end
551551

552552
def raw_client(pid) do

test/matrix_client/client_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ defmodule M51.MatrixClient.ClientTest do
117117
client = start_supervised!({M51.MatrixClient.Client, {sup_pid, [httpoison: MockHTTPoison]}})
118118

119119
assert {:error, :unknown, message} =
120-
GenServer.call(client, {:connect, "user", "example.org", "p4ssw0rd"})
120+
GenServer.call(client, {:connect, "user", "example.org", "p4ssw0rd", nil})
121121

122122
assert Regex.match?(~r/Could not reach the Matrix homeserver for example.org.*/, message)
123123

@@ -188,7 +188,7 @@ defmodule M51.MatrixClient.ClientTest do
188188

189189
client = start_supervised!({M51.MatrixClient.Client, {sup_pid, [httpoison: MockHTTPoison]}})
190190

191-
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd"}) == {:ok}
191+
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd", nil}) == {:ok}
192192

193193
assert GenServer.call(client, {:dump_state}) ==
194194
%M51.MatrixClient.Client{
@@ -274,7 +274,7 @@ defmodule M51.MatrixClient.ClientTest do
274274

275275
client = start_supervised!({M51.MatrixClient.Client, {sup_pid, [httpoison: MockHTTPoison]}})
276276

277-
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd"}) == {:ok}
277+
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd", nil}) == {:ok}
278278

279279
assert GenServer.call(client, {:dump_state}) ==
280280
%M51.MatrixClient.Client{
@@ -330,7 +330,7 @@ defmodule M51.MatrixClient.ClientTest do
330330

331331
client = start_supervised!({M51.MatrixClient.Client, {sup_pid, [httpoison: MockHTTPoison]}})
332332

333-
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd"}) ==
333+
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd", nil}) ==
334334
{:error, :no_password_flow, "No password flow"}
335335

336336
assert GenServer.call(client, {:dump_state}) ==
@@ -398,7 +398,7 @@ defmodule M51.MatrixClient.ClientTest do
398398

399399
client = start_supervised!({M51.MatrixClient.Client, {sup_pid, [httpoison: MockHTTPoison]}})
400400

401-
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd"}) ==
401+
assert GenServer.call(client, {:connect, "user", "matrix.example.org", "p4ssw0rd", nil}) ==
402402
{:error, :denied, "Invalid password"}
403403

404404
assert GenServer.call(client, {:dump_state}) ==

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ defmodule MockMatrixClient do
9696
end
9797

9898
@impl true
99-
def handle_call({:connect, local_name, hostname, password}, _from, state) do
99+
def handle_call({:connect, local_name, hostname, password, nil}, _from, state) do
100100
case {hostname, password} do
101101
{"i-hate-passwords.example.org", _} ->
102102
{:reply, {:error, :no_password_flow, "No password flow"}, state}

0 commit comments

Comments
 (0)