From b4e0bbf0a46ea685437e2ba4a3d7b6f40f7e5a94 Mon Sep 17 00:00:00 2001 From: steigr Date: Tue, 20 Feb 2018 19:48:13 +0100 Subject: [PATCH] Wrap up net.Conn with go-proxyproto.Conn Wrap up connection into proxy-protocol handler. See examples/proxy-protocol/haproxy* to test it Signed-off-by: steigr --- client.go | 7 ++-- examples/proxy-protocol/haproxy.cfg | 13 +++++++ examples/proxy-protocol/haproxy.sh | 54 +++++++++++++++++++++++++++++ goircd.go | 10 +++++- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 examples/proxy-protocol/haproxy.cfg create mode 100755 examples/proxy-protocol/haproxy.sh diff --git a/client.go b/client.go index a43cc1e..3d6d9c3 100644 --- a/client.go +++ b/client.go @@ -21,10 +21,11 @@ package main import ( "bytes" "log" - "net" "strings" "sync" "time" + + proxyproto "github.com/Freeaqingme/go-proxyproto" ) const ( @@ -37,7 +38,7 @@ var ( ) type Client struct { - conn net.Conn + conn *proxyproto.Conn registered bool nickname *string username *string @@ -55,7 +56,7 @@ func (c Client) String() string { return *c.nickname + "!" + *c.username + "@" + c.conn.RemoteAddr().String() } -func NewClient(conn net.Conn) *Client { +func NewClient(conn *proxyproto.Conn) *Client { nickname := "*" username := "" c := Client{ diff --git a/examples/proxy-protocol/haproxy.cfg b/examples/proxy-protocol/haproxy.cfg new file mode 100644 index 0000000..2f6bfdf --- /dev/null +++ b/examples/proxy-protocol/haproxy.cfg @@ -0,0 +1,13 @@ +global + daemon + maxconn 4 + +defaults + mode tcp + timeout server 3600 + timeout client 3600 + timeout connect 5 + +listen ircd-demo + bind *:9667 + server goircd-pv2 127.0.0.1:6667 send-proxy-v2 diff --git a/examples/proxy-protocol/haproxy.sh b/examples/proxy-protocol/haproxy.sh new file mode 100755 index 0000000..c279f0e --- /dev/null +++ b/examples/proxy-protocol/haproxy.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +[[ -f examples/proxy-protocol/haproxy.pid ]] && rm examples/proxy-protocol/haproxy.pid +fail() { echjo "$*"; exit 1; } + +set -x +which haproxy || fail haproxy is missing +which socat || fail socat is missing + +test -f goircd || fail goircd is missing + +haproxy_cfg() { + cat<<__cfg +global + daemon + maxconn 4 + +defaults + mode tcp + timeout server 3600 + timeout client 3600 + timeout connect 5 + +listen ircd-demo + bind *:9667 + server goircd-pv2 127.0.0.1:6667 $1 +__cfg +} + +./goircd & +trap "kill $!" EXIT + +# direct connect +haproxy_cfg > examples/proxy-protocol/haproxy.cfg +haproxy -f examples/proxy-protocol/haproxy.cfg -c +haproxy -f examples/proxy-protocol/haproxy.cfg -p examples/proxy-protocol/haproxy.pid +(sleep 1; echo "CONNECT" ) | socat stdin tcp:127.0.0.1:9667 +kill $(cat examples/proxy-protocol/haproxy.pid) + + +# proxy v1 protocol +haproxy_cfg send-proxy > examples/proxy-protocol/haproxy.cfg +haproxy -f examples/proxy-protocol/haproxy.cfg -c +haproxy -f examples/proxy-protocol/haproxy.cfg -p examples/proxy-protocol/haproxy.pid +(sleep 1; echo "CONNECT" ) | socat stdin tcp:127.0.0.1:9667 +kill $(cat examples/proxy-protocol/haproxy.pid) + +# proxy v2 protocol +haproxy_cfg send-proxy-v2 > examples/proxy-protocol/haproxy.cfg +haproxy -f examples/proxy-protocol/haproxy.cfg -c +haproxy -f examples/proxy-protocol/haproxy.cfg -p examples/proxy-protocol/haproxy.pid +(sleep 1; echo "CONNECT" ) | socat stdin tcp:127.0.0.1:9667 +kill $(cat examples/proxy-protocol/haproxy.pid) + +rm examples/proxy-protocol/haproxy.pid \ No newline at end of file diff --git a/goircd.go b/goircd.go index 8bc5e3e..2cced26 100644 --- a/goircd.go +++ b/goircd.go @@ -27,6 +27,9 @@ import ( "path" "path/filepath" "strings" + "time" + + proxyproto "github.com/Freeaqingme/go-proxyproto" ) var ( @@ -42,6 +45,10 @@ var ( verbose = flag.Bool("v", false, "Enable verbose logging.") ) +const ( + PROXY_TIMEOUT = 5 * time.Second +) + func listenerLoop(sock net.Listener, events chan ClientEvent) { for { conn, err := sock.Accept() @@ -49,7 +56,8 @@ func listenerLoop(sock net.Listener, events chan ClientEvent) { log.Println("Error during accepting connection", err) continue } - client := NewClient(conn) + proxied_conn := proxyproto.NewConn(conn, PROXY_TIMEOUT) + client := NewClient(proxied_conn) go client.Processor(events) } }