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 <me@stei.gr>
This commit is contained in:
steigr 2018-02-20 19:48:13 +01:00
parent 158ab243b2
commit b4e0bbf0a4
4 changed files with 80 additions and 4 deletions

View File

@ -21,10 +21,11 @@ package main
import ( import (
"bytes" "bytes"
"log" "log"
"net"
"strings" "strings"
"sync" "sync"
"time" "time"
proxyproto "github.com/Freeaqingme/go-proxyproto"
) )
const ( const (
@ -37,7 +38,7 @@ var (
) )
type Client struct { type Client struct {
conn net.Conn conn *proxyproto.Conn
registered bool registered bool
nickname *string nickname *string
username *string username *string
@ -55,7 +56,7 @@ func (c Client) String() string {
return *c.nickname + "!" + *c.username + "@" + c.conn.RemoteAddr().String() return *c.nickname + "!" + *c.username + "@" + c.conn.RemoteAddr().String()
} }
func NewClient(conn net.Conn) *Client { func NewClient(conn *proxyproto.Conn) *Client {
nickname := "*" nickname := "*"
username := "" username := ""
c := Client{ c := Client{

View File

@ -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

View File

@ -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

View File

@ -27,6 +27,9 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
proxyproto "github.com/Freeaqingme/go-proxyproto"
) )
var ( var (
@ -42,6 +45,10 @@ var (
verbose = flag.Bool("v", false, "Enable verbose logging.") verbose = flag.Bool("v", false, "Enable verbose logging.")
) )
const (
PROXY_TIMEOUT = 5 * time.Second
)
func listenerLoop(sock net.Listener, events chan ClientEvent) { func listenerLoop(sock net.Listener, events chan ClientEvent) {
for { for {
conn, err := sock.Accept() conn, err := sock.Accept()
@ -49,7 +56,8 @@ func listenerLoop(sock net.Listener, events chan ClientEvent) {
log.Println("Error during accepting connection", err) log.Println("Error during accepting connection", err)
continue continue
} }
client := NewClient(conn) proxied_conn := proxyproto.NewConn(conn, PROXY_TIMEOUT)
client := NewClient(proxied_conn)
go client.Processor(events) go client.Processor(events)
} }
} }