add proxy proto v2 support

master
dingjun 6 years ago
parent 6238ed3e39
commit e5010163d1

@ -43,14 +43,14 @@ func LogHandler(handler http.Handler) http.Handler {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Error(err) log.Error(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Infof("\"%s %s %s\" - %d %d \"%s\"", log.Infof("\"%s - %s %s %s\" - %d %d \"%s\"",
r.Method, r.RequestURI, r.Proto, 500, 0, r.UserAgent()) r.RemoteAddr, r.Method, r.RequestURI, r.Proto, 500, 0, r.UserAgent())
} }
}() }()
lh := &logHandler{w: w} lh := &logHandler{w: w}
handler.ServeHTTP(lh, r) handler.ServeHTTP(lh, r)
log.Infof("\"%s %s %s\" - %d %d \"%s\"", log.Infof("\"%s - %s %s %s\" - %d %d \"%s\"",
r.Method, r.RequestURI, r.Proto, lh.Status(), lh.size, r.UserAgent()) r.RemoteAddr, r.Method, r.RequestURI, r.Proto, lh.Status(), lh.size, r.UserAgent())
}) })
} }

@ -0,0 +1,50 @@
package main
import (
"bufio"
"net"
proxyproto "github.com/pires/go-proxyproto"
)
type protoListener struct {
net.Listener
}
type protoConn struct {
net.Conn
headerDone bool
r *bufio.Reader
proxy *proxyproto.Header
}
func (l *protoListener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
return &protoConn{Conn: c}, err
}
func (c *protoConn) Read(buf []byte) (int, error) {
var err error
if !c.headerDone {
c.r = bufio.NewReader(c.Conn)
c.proxy, err = proxyproto.Read(c.r)
if err != nil && err != proxyproto.ErrNoProxyProtocol {
return 0, err
}
c.headerDone = true
return c.r.Read(buf)
}
return c.r.Read(buf)
}
func (c *protoConn) RemoteAddr() net.Addr {
if c.proxy == nil {
return c.Conn.RemoteAddr()
}
return &net.TCPAddr{
IP: c.proxy.SourceAddress,
Port: int(c.proxy.SourcePort)}
}

@ -80,18 +80,21 @@ func (srv *server) serveTLS() {
if err != nil { if err != nil {
log.Fatalln("load certificate failed", err) log.Fatalln("load certificate failed", err)
} }
l, err := tls.Listen("tcp", srv.addr.Host,
&tls.Config{ l, err := net.Listen("tcp", srv.addr.Host)
Certificates: []tls.Certificate{cert},
//NextProtos: []string{"h2"},
})
if err != nil { if err != nil {
log.Fatalln("listen tls", err) log.Fatalln("listen tls", err)
} }
defer l.Close() defer l.Close()
log.Debugf("listen tls://%s", l.Addr().String()) log.Debugf("listen tls://%s", l.Addr().String())
tl := tls.NewListener(&protoListener{l}, &tls.Config{
Certificates: []tls.Certificate{cert},
//NextProtos: []string{"h2"},
})
for { for {
conn, err := l.Accept() conn, err := tl.Accept()
if err != nil { if err != nil {
log.Debugln("tls accept", err) log.Debugln("tls accept", err)
break break
@ -102,8 +105,17 @@ func (srv *server) serveTLS() {
func (srv *server) serveHTTPS() { func (srv *server) serveHTTPS() {
log.Debugf("listen https://%s", srv.addr.Host) log.Debugf("listen https://%s", srv.addr.Host)
err := http.ListenAndServeTLS(srv.addr.Host, srv.cert, srv.key, LogHandler(srv))
l, err := net.Listen("tcp", srv.addr.Host)
if err != nil { if err != nil {
log.Fatalln("listen https", err)
}
defer l.Close()
httpsrv := &http.Server{
Handler: LogHandler(srv),
}
if err := httpsrv.ServeTLS(&protoListener{l}, srv.cert, srv.key); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

Loading…
Cancel
Save