From 332d4265f220f182b21f0b5755541f7fe73758cd Mon Sep 17 00:00:00 2001 From: RTUnreal Date: Tue, 14 May 2024 16:28:00 +0200 Subject: [PATCH] proxy: make tcp closure stop gracefully --- service/proxy/lib/proxy.ex | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/service/proxy/lib/proxy.ex b/service/proxy/lib/proxy.ex index 1fba87e..f5adf6d 100644 --- a/service/proxy/lib/proxy.ex +++ b/service/proxy/lib/proxy.ex @@ -111,18 +111,26 @@ defmodule Proxy do end defp loop_sender(socket, conn) do - receive do - {:tcp, s, data} -> - case s do - ^socket -> - :gen_tcp.send(conn, data) - - ^conn -> - :gen_tcp.send(socket, data) - end + if (receive do + {:tcp, s, data} -> + case s do + ^socket -> + :gen_tcp.send(conn, data) + + ^conn -> + :gen_tcp.send(socket, data) + end + + true + + {:tcp_closed, _s} -> + false + end) do + loop_sender(socket, conn) + else + :gen_tcp.close(conn) + throw("connection closed") end - - loop_sender(socket, conn) end defp handle_rfc1929_auth(socket) do