Golang package `github.com/c0va23/go-proxyprotocol' provide receiver for HA ProxyProtocol v1 and v2.
This package provides a wrapper for the interface net.Listener, which extracts remote and local address of the connection from the headers in the format HA proxyprotocol.
package main
import (
"fmt"
"log"
"net"
"net/http"
"github.com/c0va23/go-proxyprotocol"
)
func main() {
rawList, _ := net.Listen("tcp", ":8080")
list := proxyprotocol.
NewDefaultListener(rawList).
WithLogger(proxyprotocol.LoggerFunc(log.Printf))
http.Serve(list, http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
log.Printf("Remote Addr: %s, URI: %s", req.RemoteAddr, req.RequestURI)
fmt.Fprintf(res, "Hello, %s!\n", req.RemoteAddr)
}))
}
DefaultListener try parse proxyprotocol v1 and v2 header. If header signature not recognized, then used raw connection.
If you want to use only proxy protocol V1 or v2 headers, you can initialize the listener as follows:
list := proxyprotocol.NewListener(rawList, proxyprotocol.TextHeaderParserBuilder)
- UNKNOWN
- IPv4
- IPv6
- Unspec
- TCP over IPv4
- TCP over IPv6
- UDP over IPv4
- UDP over IPv6
- Unix Stream
- Unix Datagram