-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy.go
78 lines (64 loc) · 1.45 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"errors"
"net"
"strconv"
"sync"
"github.com/nabeken/go-proxyproto"
)
type ProxyConn struct {
net.Conn
version int
once sync.Once
}
func (c *ProxyConn) Read(b []byte) (int, error) {
var err error
c.once.Do(func() { err = c.writeProxyProtocolHeader() })
if err != nil {
return 0, err
}
return c.Conn.Read(b)
}
func (c *ProxyConn) Write(b []byte) (int, error) {
var err error
c.once.Do(func() { err = c.writeProxyProtocolHeader() })
if err != nil {
return 0, err
}
return c.Conn.Write(b)
}
func (c *ProxyConn) writeProxyProtocolHeader() error {
s := c.Conn.LocalAddr()
saddr, sportStr, err := net.SplitHostPort(s.String())
if err != nil {
return err
}
d := c.Conn.RemoteAddr()
daddr, dportStr, err := net.SplitHostPort(d.String())
if err != nil {
return err
}
raddr, ok := d.(*net.TCPAddr)
if !ok {
return errors.New("proxyconn: must be tcp or tcp6")
}
sport, _ := strconv.Atoi(sportStr)
dport, _ := strconv.Atoi(dportStr)
hdr := &proxyproto.Header{
Version: c.version,
SrcAddr: net.ParseIP(saddr),
DstAddr: net.ParseIP(daddr),
SrcPort: uint16(sport),
DstPort: uint16(dport),
Command: proxyproto.PROXY,
}
if rip4 := raddr.IP.To4(); len(rip4) == net.IPv4len {
hdr.TransportProtocol = proxyproto.TCPv4
} else if len(raddr.IP) == net.IPv6len {
hdr.TransportProtocol = proxyproto.TCPv6
} else {
return errors.New("proxyconn: unknown length")
}
_, err = hdr.WriteTo(c.Conn)
return err
}