|
| 1 | +package proxyproto_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/containerssh/libcontainerssh/internal/proxyproto" |
| 11 | + goproxyproto "github.com/pires/go-proxyproto" |
| 12 | +) |
| 13 | + |
| 14 | +type fakeConn struct { |
| 15 | + remoteAddr string |
| 16 | + localAddr string |
| 17 | + pipeReader io.ReadCloser |
| 18 | + pipeWriter io.WriteCloser |
| 19 | +} |
| 20 | + |
| 21 | +func NewFakeConn(clientAddr string, serverAddr string) (fakeConn, fakeConn) { |
| 22 | + clientPipeReader, clientPipeWriter := io.Pipe() |
| 23 | + serverPipeReader, serverPipeWriter := io.Pipe() |
| 24 | + return fakeConn{ |
| 25 | + remoteAddr: clientAddr, |
| 26 | + localAddr: serverAddr, |
| 27 | + pipeReader: serverPipeReader, |
| 28 | + pipeWriter: clientPipeWriter, |
| 29 | + }, fakeConn{ |
| 30 | + remoteAddr: serverAddr, |
| 31 | + localAddr: clientAddr, |
| 32 | + pipeReader: clientPipeReader, |
| 33 | + pipeWriter: serverPipeWriter, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (f fakeConn) Read(b []byte) (n int, err error) { |
| 38 | + return f.pipeReader.Read(b) |
| 39 | +} |
| 40 | + |
| 41 | +func (f fakeConn) Write(b []byte) (n int, err error) { |
| 42 | + return f.pipeWriter.Write(b) |
| 43 | +} |
| 44 | + |
| 45 | +func (f fakeConn) Close() error { |
| 46 | + f.pipeWriter.Close() |
| 47 | + f.pipeReader.Close() |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (f fakeConn) LocalAddr() net.Addr { |
| 52 | + return &net.TCPAddr{ |
| 53 | + IP: net.ParseIP(f.localAddr), |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (f fakeConn) RemoteAddr() net.Addr { |
| 58 | + return &net.TCPAddr{ |
| 59 | + IP: net.ParseIP(f.remoteAddr), |
| 60 | + } |
| 61 | +} |
| 62 | +func (f fakeConn) SetDeadline(t time.Time) error { |
| 63 | + return fmt.Errorf("Unimplemented") |
| 64 | +} |
| 65 | +func (f fakeConn) SetReadDeadline(t time.Time) error { |
| 66 | + return fmt.Errorf("Unimplemented") |
| 67 | +} |
| 68 | +func (f fakeConn) SetWriteDeadline(t time.Time) error { |
| 69 | + return fmt.Errorf("Unimplemented") |
| 70 | +} |
| 71 | + |
| 72 | +func TestProxyWithHeader(t *testing.T) { |
| 73 | + clientIP := "127.0.0.1" |
| 74 | + proxyIP := "127.0.0.2" |
| 75 | + serverIP := "127.0.0.3" |
| 76 | + |
| 77 | + server, proxy := NewFakeConn(proxyIP, serverIP) |
| 78 | + wrappedConn, proxyAddr, err := proxyproto.WrapProxy(server, []string{proxyIP}) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + header := &goproxyproto.Header{ |
| 84 | + Version: 1, |
| 85 | + Command: goproxyproto.PROXY, |
| 86 | + TransportProtocol: goproxyproto.TCPv4, |
| 87 | + SourceAddr: &net.TCPAddr{ |
| 88 | + IP: net.ParseIP(clientIP), |
| 89 | + Port: 1000, |
| 90 | + }, |
| 91 | + DestinationAddr: &net.TCPAddr{ |
| 92 | + IP: net.ParseIP(proxyIP), |
| 93 | + Port: 2000, |
| 94 | + }, |
| 95 | + } |
| 96 | + go func() { |
| 97 | + _, err := header.WriteTo(proxy) |
| 98 | + if err != nil { |
| 99 | + return |
| 100 | + } |
| 101 | + }() |
| 102 | + |
| 103 | + if proxyAddr == nil { |
| 104 | + t.Fatalf("Proxy info was rejected") |
| 105 | + } |
| 106 | + if proxyAddr.String() != proxyIP+":0" { |
| 107 | + t.Fatalf("Unexpected proxy address %s, expected %s", proxyAddr, proxyIP) |
| 108 | + } |
| 109 | + if wrappedConn.RemoteAddr().String() != clientIP+":1000" { |
| 110 | + t.Fatalf("Header not accepted when it should be %s != %s", wrappedConn.RemoteAddr().String(), clientIP+":1000") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestProxyUnauthorizedHeader(t *testing.T) { |
| 115 | + clientIP := "127.0.0.1" |
| 116 | + proxyIP := "127.0.0.2" |
| 117 | + serverIP := "127.0.0.3" |
| 118 | + |
| 119 | + server, proxy := NewFakeConn(proxyIP, serverIP) |
| 120 | + _, proxyAddr, err := proxyproto.WrapProxy(server, []string{"128.0.0.2"}) |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + header := &goproxyproto.Header{ |
| 126 | + Version: 1, |
| 127 | + Command: goproxyproto.PROXY, |
| 128 | + TransportProtocol: goproxyproto.TCPv4, |
| 129 | + SourceAddr: &net.TCPAddr{ |
| 130 | + IP: net.ParseIP(clientIP), |
| 131 | + Port: 1000, |
| 132 | + }, |
| 133 | + DestinationAddr: &net.TCPAddr{ |
| 134 | + IP: net.ParseIP(proxyIP), |
| 135 | + Port: 2000, |
| 136 | + }, |
| 137 | + } |
| 138 | + go func() { |
| 139 | + _, err := header.WriteTo(proxy) |
| 140 | + if err != nil { |
| 141 | + return |
| 142 | + } |
| 143 | + }() |
| 144 | + |
| 145 | + if proxyAddr != nil { |
| 146 | + t.Fatalf("Proxy info was accepted when unauthorized") |
| 147 | + } |
| 148 | +} |
0 commit comments