1 | package proxyproto
|
---|
2 |
|
---|
3 | // ProtocolVersionAndCommand represents the command in proxy protocol v2.
|
---|
4 | // Command doesn't exist in v1 but it should be set since other parts of
|
---|
5 | // this library may rely on it for determining connection details.
|
---|
6 | type ProtocolVersionAndCommand byte
|
---|
7 |
|
---|
8 | const (
|
---|
9 | // LOCAL represents the LOCAL command in v2 or UNKNOWN transport in v1,
|
---|
10 | // in which case no address information is expected.
|
---|
11 | LOCAL ProtocolVersionAndCommand = '\x20'
|
---|
12 | // PROXY represents the PROXY command in v2 or transport is not UNKNOWN in v1,
|
---|
13 | // in which case valid local/remote address and port information is expected.
|
---|
14 | PROXY ProtocolVersionAndCommand = '\x21'
|
---|
15 | )
|
---|
16 |
|
---|
17 | var supportedCommand = map[ProtocolVersionAndCommand]bool{
|
---|
18 | LOCAL: true,
|
---|
19 | PROXY: true,
|
---|
20 | }
|
---|
21 |
|
---|
22 | // IsLocal returns true if the command in v2 is LOCAL or the transport in v1 is UNKNOWN,
|
---|
23 | // i.e. when no address information is expected, false otherwise.
|
---|
24 | func (pvc ProtocolVersionAndCommand) IsLocal() bool {
|
---|
25 | return LOCAL == pvc
|
---|
26 | }
|
---|
27 |
|
---|
28 | // IsProxy returns true if the command in v2 is PROXY or the transport in v1 is not UNKNOWN,
|
---|
29 | // i.e. when valid local/remote address and port information is expected, false otherwise.
|
---|
30 | func (pvc ProtocolVersionAndCommand) IsProxy() bool {
|
---|
31 | return PROXY == pvc
|
---|
32 | }
|
---|
33 |
|
---|
34 | // IsUnspec returns true if the command is unspecified, false otherwise.
|
---|
35 | func (pvc ProtocolVersionAndCommand) IsUnspec() bool {
|
---|
36 | return !(pvc.IsLocal() || pvc.IsProxy())
|
---|
37 | }
|
---|
38 |
|
---|
39 | func (pvc ProtocolVersionAndCommand) toByte() byte {
|
---|
40 | if pvc.IsLocal() {
|
---|
41 | return byte(LOCAL)
|
---|
42 | } else if pvc.IsProxy() {
|
---|
43 | return byte(PROXY)
|
---|
44 | }
|
---|
45 |
|
---|
46 | return byte(LOCAL)
|
---|
47 | }
|
---|