• Stars
    star
    428
  • Rank 97,669 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Go library implementation of the PROXY protocol, versions 1 and 2.

go-proxyproto

Actions Status Coverage Status Go Report Card

A Go library implementation of the PROXY protocol, versions 1 and 2, which provides, as per specification:

(...) a convenient way to safely transport connection information such as a client's address across multiple layers of NAT or TCP proxies. It is designed to require little changes to existing components and to limit the performance impact caused by the processing of the transported information.

This library is to be used in one of or both proxy clients and proxy servers that need to support said protocol. Both protocol versions, 1 (text-based) and 2 (binary-based) are supported.

Installation

$ go get -u github.com/pires/go-proxyproto

Usage

Client

package main

import (
	"io"
	"log"
	"net"

	proxyproto "github.com/pires/go-proxyproto"
)

func chkErr(err error) {
	if err != nil {
		log.Fatalf("Error: %s", err.Error())
	}
}

func main() {
	// Dial some proxy listener e.g. https://github.com/mailgun/proxyproto
	target, err := net.ResolveTCPAddr("tcp", "127.0.0.1:2319")
	chkErr(err)

	conn, err := net.DialTCP("tcp", nil, target)
	chkErr(err)

	defer conn.Close()

	// Create a proxyprotocol header or use HeaderProxyFromAddrs() if you
	// have two conn's
	header := &proxyproto.Header{
		Version:            1,
		Command:            proxyproto.PROXY,
		TransportProtocol:  proxyproto.TCPv4,
		SourceAddr: &net.TCPAddr{
			IP:   net.ParseIP("10.1.1.1"),
			Port: 1000,
		},
		DestinationAddr: &net.TCPAddr{
			IP:   net.ParseIP("20.2.2.2"),
			Port: 2000,
		},
	}
	// After the connection was created write the proxy headers first
	_, err = header.WriteTo(conn)
	chkErr(err)
	// Then your data... e.g.:
	_, err = io.WriteString(conn, "HELO")
	chkErr(err)
}

Server

package main

import (
	"log"
	"net"

	proxyproto "github.com/pires/go-proxyproto"
)

func main() {
	// Create a listener
	addr := "localhost:9876"
	list, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatalf("couldn't listen to %q: %q\n", addr, err.Error())
	}

	// Wrap listener in a proxyproto listener
	proxyListener := &proxyproto.Listener{Listener: list}
	defer proxyListener.Close()

	// Wait for a connection and accept it
	conn, err := proxyListener.Accept()
	defer conn.Close()

	// Print connection details
	if conn.LocalAddr() == nil {
		log.Fatal("couldn't retrieve local address")
	}
	log.Printf("local address: %q", conn.LocalAddr().String())

	if conn.RemoteAddr() == nil {
		log.Fatal("couldn't retrieve remote address")
	}
	log.Printf("remote address: %q", conn.RemoteAddr().String())
}

HTTP Server

package main

import (
	"net"
	"net/http"
	"time"

	"github.com/pires/go-proxyproto"
)

func main() {
	server := http.Server{
		Addr: ":8080",
	}

	ln, err := net.Listen("tcp", server.Addr)
	if err != nil {
		panic(err)
	}

	proxyListener := &proxyproto.Listener{
		Listener:          ln,
		ReadHeaderTimeout: 10 * time.Second,
	}
	defer proxyListener.Close()

	server.Serve(proxyListener)
}

Special notes

AWS

AWS Network Load Balancer (NLB) does not push the PPV2 header until the client starts sending the data. This is a problem if your server speaks first. e.g. SMTP, FTP, SSH etc.

By default, NLB target group attribute proxy_protocol_v2.client_to_server.header_placement has the value on_first_ack_with_payload. You need to contact AWS support to change it to on_first_ack, instead.

Just to be clear, you need this fix only if your server is designed to speak first.

More Repositories

1

kubernetes-elasticsearch-cluster

Elasticsearch cluster on top of Kubernetes made easy.
1,509
star
2

android-obd-reader

Android OBD-II Reader application that uses pure OBD-II PID's Java API.
Java
792
star
3

kubernetes-vagrant-coreos-cluster

Kubernetes cluster (for testing purposes) made easy with Vagrant and CoreOS.
Shell
599
star
4

obd-java-api

OBD-II Java API
Java
583
star
5

docker-elasticsearch-kubernetes

Ready to use Elasticsearch + Kubernetes discovery plug-in Docker image.
Dockerfile
225
star
6

kubernetes-nats-cluster

NATS cluster on top of Kubernetes made easy.
Go
168
star
7

docker-elasticsearch

Dockerfile for a base Elasticsearch image to be extended by others (allow to install plug-ins, change configuration, etc.)
Shell
160
star
8

kubernetes-elk-cluster

ELK (Elasticsearch + Logstash + Kibana) cluster on top of Kubernetes made easy.
Dockerfile
148
star
9

spring-boot-shiro-orientdb

A simple REST API built with Spring Boot and OrientDB (Object API) datastore, secured with Apache Shiro (distributed session storage powered by Hazelcast).
Java
98
star
10

obd-server

Webapp responsible for storing OBD (Android OBD Reader) readings.
Java
68
star
11

hibernate-postgres-jsonb

A working implementation of JSONB support on a Java + Hibernate application.
Java
63
star
12

hazelcast-kubernetes

Hazelcast clustering for Kubernetes made easy.
Dockerfile
49
star
13

simple-shiro-web-app

A simple proof-of-concept of Shiro authentication with JDBC Realm and MySQL.
CSS
36
star
14

nomad-vagrant-coreos-cluster

Nomad cluster (for testing purposes) made easy with Vagrant and CoreOS.
Shell
35
star
15

docker-jre

Lean JRE 8 Docker container
Dockerfile
24
star
16

hazelcast-kubernetes-bootstrapper

Hazelcast cluster discovery mechanism for Kubernetes.
Java
22
star
17

rethinkdb-coreos-cluster

RethinkDB clustering made easy with CoreOS, etcd2 and Docker
Shell
22
star
18

netty-tcnative-alpine

Build netty-tcnative native binaries for Alpine Linux.
Dockerfile
20
star
19

kubernetes-squid

Squid proxy for Kubernetes
14
star
20

apache-ignite-discovery-kubernetes

Apache Ignite discovery for Kubernetes.
Java
10
star
21

nats-coreos-cluster

NATS clustering made easy with CoreOS and etcd.
Shell
10
star
22

docker-logstash

Dockerfile for a base Logstash image to be extended by others (allow to install plug-ins, change configuration, etc.)
10
star
23

consul-lb-gce

A smart Google Cloud Engine load-balancer manager for Consul backed services.
Go
10
star
24

simple-jersey-rest-app

A simple proof-of-concept of RESTful web service implemented with Jersey and Glassfish.
Java
10
star
25

alpine-linux-build

Docker image of Alpine Linux with many compilers needed to build code that's meant to run on Alpine Linux.
9
star
26

fabric8-persistence-hibernate

Fabric8 + E-OSGi JPA 2.1 managed persistence (Aries + Hibernate 4.3.x) + REST service demonstration code.
Java
9
star
27

docker-elasticsearch-aws

Ready to use Elasticsearch + AWS plug-in Docker image.
Dockerfile
9
star
28

nats-sniffer

A simple sniffer for NATS, the cloud native messaging system. https://nats.io
Go
9
star
29

simple-jpa-hibernate-guice-desktop-app

A simple proof-of-concept of a desktop Java application with database access using JPA, Hibernate and Guice.
Java
9
star
30

fabric8-cxf-shiro

OSGi-enabled authentication & authorization service, powered by Apache Shiro and Hazelcast cluster. Goes really well with Fabric8 or JBoss Fuse for auto-scaling.
Java
8
star
31

docker-haproxy-ssl

5
star
32

docker-apache-ignite

Lean (310MB) Apache Ignite docker image.
5
star
33

docker-hbase-standalone

Simple HBase standalone container image.
4
star
34

pgskail

PostgreSQL high-availability made easy.
Go
4
star
35

docker-logstash-forwarder

logstash-forwarder minimal Docker container image
Shell
4
star
36

docker-elasticsearch-curator

Lean Elasticsearch Curator container image, based on Alpine Linux 3.7 and Python 3.
Dockerfile
4
star
37

docker-squid

Run Squid on a Docker container. The main purpose of this it to use it as Docker registry cache.
Shell
4
star
38

docker-apollomq

Docker image for Apache ActiveMQ Apollo broker.
3
star
39

dojo-go-consul

A repo for my experiments with Consul and Go.
Go
3
star
40

go-dojo-scalability-protocols

Go experiment with [Scalability Protocols](http://bravenewgeek.com/a-look-at-nanomsg-and-scalability-protocols/).
Go
3
star
41

sherlock

Message-driven, NoSQL-powered auditing framework
Java
3
star
42

fabric8-cxf-dosgi-demo-blueprint

Simple Fabric8-oriented CXF + DOSGi demonstration code.
Java
2
star
43

springboot-stomp-ws-jms-integration

Example of integration between two apps and one frontend with STOMP over WebSocket and JMS.
JavaScript
2
star
44

configo

TOML-based, strong-typed, environment-oriented, a la fullstack configuration for Go applications.
Go
2
star
45

kubernetes-operator-dev

Dockerfile
2
star
46

go-dojo-rectangles

Go experiments with rectangles.
Go
2
star
47

fabric8-amq-example

Fabric8 ActiveMQ (JMS) example with Pax-Exam integration-tests
Java
2
star
48

opensignals-android

BITalino OpenSignals application for Android plattform.
Java
2
star
49

netty-socketio-osgi-example

Netty-SocketIO OSGi example
Java
2
star
50

replicatorg-mavenized

Mavenized version of ReplicatorG project
Java
1
star
51

metricas

A pipeline for metrics acquisition and storage.
Go
1
star
52

simple-hibernate-kundera-cassandra-polyglot-app

Simple polyglot proof-of-concept with Hibernate (PostgreSQL) and Kundera (Cassandra) support.
Java
1
star
53

fabric8-osgi-aspectj

Fabric8 example with OSGi and AspectJ (AOP) aspects woven in compile-time.
Java
1
star
54

simple-kundera-embedded-cassandra-app

A simple proof-of-concept of a Java application with embedded Cassandra integration, using Kundera and Guice.
Java
1
star