File: proxyprotocol.go

package info (click to toggle)
go-mmproxy 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 248 kB
  • sloc: makefile: 12; sh: 7
file content (175 lines) | stat: -rw-r--r-- 4,768 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2019 Path Network, Inc. All rights reserved.
// Copyright 2024 Konrad Zemek <konrad.zemek@gmail.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proxyprotocol

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"net/netip"
	"strings"

	"github.com/kzemek/go-mmproxy/utils"
)

func readRemoteAddrPROXYv2(ctrlBuf []byte, protocol utils.Protocol) (saddr, daddr netip.AddrPort, data []byte, resultErr error) {
	if (ctrlBuf[12] >> 4) != 2 {
		resultErr = fmt.Errorf("unknown protocol version %d", ctrlBuf[12]>>4)
		return
	}

	if ctrlBuf[12]&0xF > 1 {
		resultErr = fmt.Errorf("unknown command %d", ctrlBuf[12]&0xF)
		return
	}

	if ctrlBuf[12]&0xF == 1 && ((protocol == utils.TCP && ctrlBuf[13] != 0x11 && ctrlBuf[13] != 0x21) ||
		(protocol == utils.UDP && ctrlBuf[13] != 0x12 && ctrlBuf[13] != 0x22)) {
		resultErr = fmt.Errorf("invalid family/protocol %d/%d", ctrlBuf[13]>>4, ctrlBuf[13]&0xF)
		return
	}

	var dataLen uint16
	reader := bytes.NewReader(ctrlBuf[14:16])
	if err := binary.Read(reader, binary.BigEndian, &dataLen); err != nil {
		resultErr = fmt.Errorf("failed to decode address data length: %w", err)
		return
	}

	if len(ctrlBuf) < 16+int(dataLen) {
		resultErr = fmt.Errorf("incomplete PROXY header")
		return
	}

	if ctrlBuf[12]&0xF == 0 { // LOCAL
		data = ctrlBuf[16+dataLen:]
		return
	}

	var sport, dport uint16
	if ctrlBuf[13]>>4 == 0x1 { // IPv4
		reader = bytes.NewReader(ctrlBuf[24:])
	} else {
		reader = bytes.NewReader(ctrlBuf[48:])
	}
	if err := binary.Read(reader, binary.BigEndian, &sport); err != nil {
		resultErr = fmt.Errorf("failed to decode source port: %w", err)
		return
	}
	if sport == 0 {
		resultErr = fmt.Errorf("invalid source port %d", sport)
		return
	}
	if err := binary.Read(reader, binary.BigEndian, &dport); err != nil {
		resultErr = fmt.Errorf("failed to decode destination port: %w", err)
		return
	}
	if dport == 0 {
		resultErr = fmt.Errorf("invalid destination port %d", sport)
		return
	}

	var srcIP, dstIP netip.Addr
	if ctrlBuf[13]>>4 == 0x1 { // IPv4
		srcIP, _ = netip.AddrFromSlice(ctrlBuf[16:20])
		dstIP, _ = netip.AddrFromSlice(ctrlBuf[20:24])
	} else {
		srcIP, _ = netip.AddrFromSlice(ctrlBuf[16:32])
		dstIP, _ = netip.AddrFromSlice(ctrlBuf[32:48])
	}

	saddr = netip.AddrPortFrom(srcIP, sport)
	daddr = netip.AddrPortFrom(dstIP, dport)
	data = ctrlBuf[16+dataLen:]
	return
}

func readRemoteAddrPROXYv1(ctrlBuf []byte) (saddr, daddr netip.AddrPort, data []byte, resultErr error) {
	str := string(ctrlBuf)
	idx := strings.Index(str, "\r\n")
	if idx < 0 {
		resultErr = fmt.Errorf("did not find \\r\\n in first data segment")
		return
	}

	var headerProtocol string
	n, err := fmt.Sscanf(str, "PROXY %s", &headerProtocol)
	if err != nil {
		resultErr = err
		return
	}
	if n != 1 {
		resultErr = fmt.Errorf("failed to decode elements")
		return
	}
	if headerProtocol == "UNKNOWN" {
		data = ctrlBuf[idx+2:]
		return
	}
	if headerProtocol != "TCP4" && headerProtocol != "TCP6" {
		resultErr = fmt.Errorf("unknown protocol %s", headerProtocol)
		return
	}

	var src, dst string
	var sport, dport int
	n, err = fmt.Sscanf(str, "PROXY %s %s %s %d %d", &headerProtocol, &src, &dst, &sport, &dport)
	if err != nil {
		resultErr = err
		return
	}
	if n != 5 {
		resultErr = fmt.Errorf("failed to decode elements")
		return
	}
	if sport <= 0 || sport > 65535 {
		resultErr = fmt.Errorf("invalid source port %d", sport)
		return
	}
	if dport <= 0 || dport > 65535 {
		resultErr = fmt.Errorf("invalid destination port %d", sport)
		return
	}
	srcIP, err := netip.ParseAddr(src)
	if err != nil {
		resultErr = fmt.Errorf("failed to parse source IP address %s: %w", src, err)
		return
	}
	dstIP, err := netip.ParseAddr(dst)
	if err != nil {
		resultErr = fmt.Errorf("failed to parse destination IP address %s: %w", dst, err)
		return
	}

	saddr = netip.AddrPortFrom(srcIP, uint16(sport))
	daddr = netip.AddrPortFrom(dstIP, uint16(dport))
	data = ctrlBuf[idx+2:]
	return
}

var proxyv2header = []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A}

func ReadRemoteAddr(buf []byte, protocol utils.Protocol) (saddr, daddr netip.AddrPort, rest []byte, err error) {
	if len(buf) >= 16 && bytes.Equal(buf[:12], proxyv2header) {
		saddr, daddr, rest, err = readRemoteAddrPROXYv2(buf, protocol)
		if err != nil {
			err = fmt.Errorf("failed to parse PROXY v2 header: %w", err)
		}
		return
	}

	// PROXYv1 only works with TCP
	if protocol == utils.TCP && len(buf) >= 8 && bytes.Equal(buf[:5], []byte("PROXY")) {
		saddr, daddr, rest, err = readRemoteAddrPROXYv1(buf)
		if err != nil {
			err = fmt.Errorf("failed to parse PROXY v1 header: %w", err)
		}
		return
	}

	err = fmt.Errorf("PROXY header missing")
	return
}