|
| 1 | +package proxyproto_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/pires/go-proxyproto" |
| 8 | +) |
| 9 | + |
| 10 | +// proxyV1Line is a minimal PROXY protocol v1 header for examples. |
| 11 | +const proxyV1Line = "PROXY TCP4 192.168.1.1 192.168.1.2 12345 443\r\n" |
| 12 | + |
| 13 | +func ExampleListener_default() { |
| 14 | + l, _ := net.Listen("tcp", "127.0.0.1:0") |
| 15 | + pl := &proxyproto.Listener{Listener: l} |
| 16 | + defer pl.Close() |
| 17 | + |
| 18 | + go func() { |
| 19 | + c, _ := net.Dial("tcp", pl.Addr().String()) |
| 20 | + if c != nil { |
| 21 | + _, _ = c.Write([]byte("x")) |
| 22 | + _ = c.Close() |
| 23 | + } |
| 24 | + }() |
| 25 | + |
| 26 | + conn, _ := pl.Accept() |
| 27 | + if conn != nil { |
| 28 | + buf := make([]byte, 1) |
| 29 | + _, _ = conn.Read(buf) |
| 30 | + _ = conn.Close() |
| 31 | + } |
| 32 | + // Output: |
| 33 | +} |
| 34 | + |
| 35 | +func ExampleListener_readHeaderTimeout() { |
| 36 | + l, _ := net.Listen("tcp", "127.0.0.1:0") |
| 37 | + pl := &proxyproto.Listener{ |
| 38 | + Listener: l, |
| 39 | + ReadHeaderTimeout: 2 * time.Second, |
| 40 | + } |
| 41 | + defer pl.Close() |
| 42 | + |
| 43 | + go func() { |
| 44 | + c, _ := net.Dial("tcp", pl.Addr().String()) |
| 45 | + if c != nil { |
| 46 | + _, _ = c.Write([]byte("a")) |
| 47 | + _ = c.Close() |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + conn, _ := pl.Accept() |
| 52 | + if conn != nil { |
| 53 | + _ = conn.SetReadDeadline(time.Now().Add(time.Second)) |
| 54 | + buf := make([]byte, 1) |
| 55 | + _, _ = conn.Read(buf) |
| 56 | + _ = conn.Close() |
| 57 | + } |
| 58 | + // Output: |
| 59 | +} |
| 60 | + |
| 61 | +func ExampleListener_readBufferSize() { |
| 62 | + l, _ := net.Listen("tcp", "127.0.0.1:0") |
| 63 | + pl := &proxyproto.Listener{ |
| 64 | + Listener: l, |
| 65 | + ReadBufferSize: 4096, |
| 66 | + } |
| 67 | + defer pl.Close() |
| 68 | + |
| 69 | + go func() { |
| 70 | + c, _ := net.Dial("tcp", pl.Addr().String()) |
| 71 | + if c != nil { |
| 72 | + _, _ = c.Write([]byte("b")) |
| 73 | + _ = c.Close() |
| 74 | + } |
| 75 | + }() |
| 76 | + |
| 77 | + conn, _ := pl.Accept() |
| 78 | + if conn != nil { |
| 79 | + buf := make([]byte, 1) |
| 80 | + _, _ = conn.Read(buf) |
| 81 | + _ = conn.Close() |
| 82 | + } |
| 83 | + // Output: |
| 84 | +} |
| 85 | + |
| 86 | +func ExampleListener_policyRequire() { |
| 87 | + l, _ := net.Listen("tcp", "127.0.0.1:0") |
| 88 | + pl := &proxyproto.Listener{ |
| 89 | + Listener: l, |
| 90 | + Policy: func(net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }, |
| 91 | + } |
| 92 | + defer pl.Close() |
| 93 | + |
| 94 | + go func() { |
| 95 | + c, _ := net.Dial("tcp", pl.Addr().String()) |
| 96 | + if c != nil { |
| 97 | + _, _ = c.Write([]byte(proxyV1Line)) |
| 98 | + _, _ = c.Write([]byte("p")) |
| 99 | + _ = c.Close() |
| 100 | + } |
| 101 | + }() |
| 102 | + |
| 103 | + conn, _ := pl.Accept() |
| 104 | + if conn != nil { |
| 105 | + buf := make([]byte, 1) |
| 106 | + _, _ = conn.Read(buf) |
| 107 | + _ = conn.Close() |
| 108 | + } |
| 109 | + // Output: |
| 110 | +} |
| 111 | + |
| 112 | +func ExampleListener_validateHeader() { |
| 113 | + l, _ := net.Listen("tcp", "127.0.0.1:0") |
| 114 | + pl := &proxyproto.Listener{ |
| 115 | + Listener: l, |
| 116 | + ValidateHeader: func(*proxyproto.Header) error { return nil }, |
| 117 | + } |
| 118 | + defer pl.Close() |
| 119 | + |
| 120 | + go func() { |
| 121 | + c, _ := net.Dial("tcp", pl.Addr().String()) |
| 122 | + if c != nil { |
| 123 | + _, _ = c.Write([]byte(proxyV1Line)) |
| 124 | + _, _ = c.Write([]byte("v")) |
| 125 | + _ = c.Close() |
| 126 | + } |
| 127 | + }() |
| 128 | + |
| 129 | + conn, _ := pl.Accept() |
| 130 | + if conn != nil { |
| 131 | + buf := make([]byte, 1) |
| 132 | + _, _ = conn.Read(buf) |
| 133 | + _ = conn.Close() |
| 134 | + } |
| 135 | + // Output: |
| 136 | +} |
0 commit comments