Commit 9e927964 authored by Andreas Kempe's avatar Andreas Kempe
Browse files

Send proper error reply on connection failure

When a connection through the SOCKS proxy fails, an error reply will be
sent to the client.
parent 7f0cb72f
socker
libsockserr.so
CC ?= clang
CFLAGS ?=
.PHONY: clean
.SILENT: clean
socker: *.pony libsockserr.so
ponyc -p $(.CURDIR)
libsockserr.so: socks_error.c
$(CC) -shared -fPIC $(CFLAGS) -o $(.TARGET) $(.ALLSRC)
clean:
- rm libsockserr.so socker 2>1
use "net"
use "lib:sockserr"
use @get_socks_error[U8](errno: U32)
class ProxyAcceptor is TCPListenNotify
let _env: Env
......@@ -26,7 +29,9 @@ class SocksNotifier is TCPConnectionNotify
conn.local_address().port())
fun ref connect_failed(conn: TCPConnection ref) =>
_proxy.kill(conn)
(let ret: U32, let errno: U32) = conn.get_so_error()
let err: U8 = @get_socks_error[U8](errno)
_proxy.socks_failed(conn, err)
fun ref closed(conn: TCPConnection ref) =>
_proxy.kill(conn)
......@@ -118,6 +123,14 @@ actor Proxy is ProxyInterface
_out.print("Failed to parse data from local client")
end
be socks_failed(conn: TCPConnection tag, err: U8) =>
try
let r = _socks_relay as SocksRelay
r.connection_failed(conn, err)
else
_out.print("Connection failed, but no relay active!")
end
be socks_relay(data: Array[U8 val] val) =>
try
let r = _socks_relay as SocksRelay
......
......@@ -396,6 +396,18 @@ class SocksRelay
addr, local_port)
msg.send(_connection)
fun ref connection_failed(conn: TCPConnection tag, err: U8) =>
_out.print("Connection failed")
try
let msg = SocksRequestReplyMessage(SocksErrors(err)?, SocksAddrIPv4,
U32(0), 0)
msg.send(_connection)
else
_out.print("Could not send error message. Incorrect error
type: " + err.string())
end
_out.print("Closing connections")
kill()
fun ref kill() =>
_connection.dispose()
......
#include <errno.h>
#include <stdint.h>
enum {
GENERAL_ERROR = 0x01,
CONNECTION_NOT_ALLOWED = 0x02,
NETWORK_UNREACHABLE = 0x03,
HOST_UNREACHABLE = 0x04,
CONNECTION_REFUSED = 0x05,
TTL_EXPIRED = 0x06,
COMMAND_NOT_SUPPORTED = 0x07,
ADDRESS_TYPE_NOT_SUPPORTED = 0x08
};
uint8_t get_socks_error(uint32_t err)
{
switch (err)
{
case ENETUNREACH:
return NETWORK_UNREACHABLE;
case EHOSTUNREACH:
return HOST_UNREACHABLE;
case ECONNREFUSED:
return CONNECTION_REFUSED;
default:
return GENERAL_ERROR;
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment