]>
git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.dnsdist/proxyprotocolutils.py
8 from proxyprotocol
import ProxyProtocol
10 def ProxyProtocolUDPResponder(port
, fromQueue
, toQueue
):
11 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
12 sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEPORT
, 1)
14 sock
.bind(("127.0.0.1", port
))
15 except socket
.error
as e
:
16 print("Error binding in the Proxy Protocol UDP responder: %s" % str(e
))
20 data
, addr
= sock
.recvfrom(4096)
22 proxy
= ProxyProtocol()
23 if len(data
) < proxy
.HEADER_SIZE
:
26 if not proxy
.parseHeader(data
):
30 # likely a healthcheck
31 data
= data
[proxy
.HEADER_SIZE
:]
32 request
= dns
.message
.from_wire(data
)
33 response
= dns
.message
.make_response(request
)
34 wire
= response
.to_wire()
36 sock
.sendto(wire
, addr
)
41 payload
= data
[:(proxy
.HEADER_SIZE
+ proxy
.contentLen
)]
42 dnsData
= data
[(proxy
.HEADER_SIZE
+ proxy
.contentLen
):]
43 toQueue
.put([payload
, dnsData
], True, 2.0)
44 # computing the correct ID for the response
45 request
= dns
.message
.from_wire(dnsData
)
46 response
= fromQueue
.get(True, 2.0)
47 response
.id = request
.id
50 sock
.sendto(response
.to_wire(), addr
)
55 def ProxyProtocolTCPResponder(port
, fromQueue
, toQueue
):
56 # be aware that this responder will not accept a new connection
57 # until the last one has been closed. This is done on purpose to
58 # to check for connection reuse, making sure that a lot of connections
59 # are not opened in parallel.
60 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
61 sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEPORT
, 1)
62 sock
.setsockopt(socket
.IPPROTO_TCP
, socket
.TCP_NODELAY
, 1)
64 sock
.bind(("127.0.0.1", port
))
65 except socket
.error
as e
:
66 print("Error binding in the TCP responder: %s" % str(e
))
71 (conn
, _
) = sock
.accept()
73 # try to read the entire Proxy Protocol header
74 proxy
= ProxyProtocol()
75 header
= conn
.recv(proxy
.HEADER_SIZE
)
80 if not proxy
.parseHeader(header
):
84 proxyContent
= conn
.recv(proxy
.contentLen
)
89 payload
= header
+ proxyContent
93 except socket
.timeout
:
100 (datalen
,) = struct
.unpack("!H", data
)
101 data
= conn
.recv(datalen
)
103 toQueue
.put([payload
, data
], True, 2.0)
105 response
= copy
.deepcopy(fromQueue
.get(True, 2.0))
110 # computing the correct ID for the response
111 request
= dns
.message
.from_wire(data
)
112 response
.id = request
.id
114 wire
= response
.to_wire()
115 conn
.send(struct
.pack("!H", len(wire
)))