mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 15:59:41 +08:00
port tcp|udp [ip:]port [socket_options] opens, binds and listens at the time the line is read, so a privileged port can be taken before setuid and chroot drop the rights to open it: a service of the configuration on the same address, of the same type, takes the socket over when it starts, instead of binding one of its own, and a port no service takes stays reserved until the configuration is left. A reservation of the configuration before holds the port over a reload: the new one shares its socket, each dropping its hold with its configuration. The socket is what the reservation made of it: the options which act only before bind() cannot follow a service's wishes on a socket already bound, so the service takes it as it is, its other options set on it. The reservation is built from the same default address a service is, so it matches in every byte a listener is compared by.
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""The port command: a listening socket reserved where the configuration
|
|
is read, a service of the same address taking it over.
|
|
|
|
A privileged port is opened before setuid and chroot take the rights
|
|
away, and the service named after them runs on the socket that is
|
|
already open: what would be a bind() denied is a takeover instead. A
|
|
port no service takes stays reserved, its socket listening on its own.
|
|
"""
|
|
|
|
import time
|
|
|
|
|
|
def run(t):
|
|
if not t.skip_unless("httpsrv"):
|
|
return
|
|
|
|
origin = t.free_port()
|
|
t.start("port_origin", f"""
|
|
log
|
|
auth iponly
|
|
allow *
|
|
http data * /data**
|
|
httpsrv -p{origin}
|
|
""", ports=[origin])
|
|
|
|
# a port reserved and never taken: the socket listens by itself
|
|
solo = t.free_port()
|
|
t.start("port_solo", f"""
|
|
log
|
|
port tcp 127.0.0.1:{solo}
|
|
""", ports=[solo])
|
|
t.ok("a port with no service is kept listening") \
|
|
if t.wait_port(solo) else \
|
|
t.fail("a port with no service is kept listening",
|
|
f"port {solo} open", "closed")
|
|
|
|
# a service after the reservation takes the socket over
|
|
reserved = t.free_port()
|
|
server = t.start("port_taken", f"""
|
|
log
|
|
port tcp 127.0.0.1:{reserved}
|
|
auth iponly
|
|
allow *
|
|
proxy -p{reserved} -i127.0.0.1
|
|
""", ports=[reserved])
|
|
|
|
out = t.wait_output(server, "taken over")
|
|
t.contains(out, "taken over",
|
|
"the service runs on the socket the reservation opened")
|
|
|
|
t.eq(200, t.http(f"http://127.0.0.1:{origin}/data",
|
|
proxy=f"127.0.0.1:{reserved}").status,
|
|
"a request through the taken-over port is served")
|
|
|
|
# a reserved UDP port is taken over the same way: bound by the
|
|
# reservation, answered by the service
|
|
udpp = t.free_port()
|
|
dns = t.start("port_udp", f"""
|
|
log
|
|
port udp 127.0.0.1:{udpp}
|
|
nserver 127.0.0.1
|
|
nscache 1024
|
|
nsrecord here.test 10.11.12.13
|
|
dnspr -p{udpp} -i127.0.0.1
|
|
""")
|
|
|
|
deadline = time.time() + 5
|
|
while time.time() < deadline:
|
|
if t.dns_query(udpp, "here.test"):
|
|
break
|
|
time.sleep(0.05)
|
|
t.eq(["10.11.12.13"], t.dns_query(udpp, "here.test"),
|
|
"a UDP service answers on its reserved port")
|