fix+allow usage of session keys for upstream connections

This commit is contained in:
Alain Zscheile 2022-11-25 01:29:17 +01:00
parent f341237271
commit 2a6405cd71
2 changed files with 13 additions and 6 deletions

View file

@ -38,7 +38,7 @@ defmodule Floof do
loop_acceptor(socket)
end
def connect(host, port) do
def connect(host, port, sesskey \\ nil) do
{:ok, socket} =
:gen_tcp.connect(host, port,
packet: 4,
@ -47,6 +47,12 @@ defmodule Floof do
Floof.Distributor.register(self())
Logger.info("Established connection to #{inspect(host)}:#{port}")
if sesskey != nil do
{:ok, encd} = :FloofProtocol.encode(:ProtoMessage, {:session, {:attach, sesskey}})
:ok = :gen_tcp.send(socket, encd)
end
serve(socket, %{}, nil)
:ok = :gen_tcp.close(socket)
end
@ -87,7 +93,7 @@ defmodule Floof do
Logger.debug("got packet #{inspect(dcd)}")
case dcd do
{:session, {:SessionModify, {:attach, key}}} ->
{:session, {:attach, key}} ->
Floof.Distributor.register_key(key, self())
if Floof.SessionManager.attach(key, self()) do

View file

@ -45,14 +45,15 @@ defmodule Floof.Application do
[]
end ++
for upstream <- upstreams do
{host, port} =
{host, port, sesskey} =
case upstream do
{host, port} -> {host, port}
{host} -> {host, 2540}
{host, port, sesskey} -> {host, port, sesskey}
{host, port} -> {host, port, nil}
{host} -> {host, 2540, nil}
end
# this establishes connections to upstream hosts
Supervisor.child_spec({Task, fn -> Floof.connect(host, port) end},
Supervisor.child_spec({Task, fn -> Floof.connect(host, port, sesskey) end},
id: "upstream:#{inspect(host)}:#{port}"
)
end