Files
3proxy/tests/cases/viaport.py
T
Vladimir Dubrovin 642ef96272
C/C++ CI Linux / ubuntu-24.04-arm (push) Canceled after 0s
C/C++ CI Linux / ubuntu-latest (push) Canceled after 0s
C/C++ CI MacOS / macos-15 (push) Canceled after 0s
C/C++ CI Windows / windows-2022 (push) Canceled after 0s
C/C++ CI cmake / macos-15 (push) Canceled after 0s
C/C++ CI cmake / ubuntu-24.04-arm (push) Canceled after 0s
C/C++ CI cmake / ubuntu-latest (push) Canceled after 0s
C/C++ CI cmake / windows-2022 (push) Canceled after 0s
C/C++ CI cmake / ubuntu-latest (wolfSSL) (push) Canceled after 0s
acl: the targetlist names the service by its addresses, too
One @ in the targetlist and one in the targetportlist delimit the lists
of destinations from the lists of the services the request came
through — the addresses it listens on in the first, its ports in the
second, both sides written as any other list:

 allow * * 192.168.1.0/24@10.0.0.1 443@1080,1090

matches only requests of the service on 10.0.0.1:1080 or :1090. A side
of the @ may be left out: @1080 names a port alone, *@10.0.0.1 an
address alone, and an @ with nothing after it is refused; an IPv6
address needs no brackets there, no port follows it.

The targetlist and the targetportlist are parsed by the same functions
now — ipadd() over scanipl() and portadd() — the chains of inline
allocations in make_ace() gone, and with them the mixed form of a
service named by an address and a port behind one @, which the two
fields make unnecessary.
2026-11-23 16:35:11 +03:00

116 lines
3.9 KiB
Python

"""A rule names, after one @, the service the request came through.
The rules of the limiters are shared by all the services, so there has
been no way to limit the connections of one service alone. The
targetlist names the service after its @ by the addresses it listens
on, the targetportlist by its ports — each side written as any other
list, a side left out standing for any. A deny so written closes one
service, a connlim so written caps one service, and neither touches
another service of the same configuration.
"""
import time
def run(t):
if not t.skip_unless("httpsrv"):
return
origin = t.free_port()
t.start("viaport_origin", f"""
auth iponly
allow *
http data * /data**
httpsrv -p{origin}
""", ports=[origin])
url = f"http://127.0.0.1:{origin}/data"
# a deny naming a service by its port closes that service alone
first, second = t.free_port(), t.free_port()
t.start("viaport_deny", f"""
log
auth iponly
deny * * * @{second}
allow *
proxy -p{first}
proxy -p{second}
""", ports=[first, second])
t.eq(200, t.http(url, proxy=f"127.0.0.1:{first}").status,
"the service the rule does not name is served")
t.ne(200, t.http(url, proxy=f"127.0.0.1:{second}").status,
"the service the rule names by its port is refused")
# a connlim naming a service by its port caps that service alone
third, fourth = t.free_port(), t.free_port()
t.start("viaport_connlim", f"""
log
auth iponly
connlim 1 0 * * * @{third}
allow *
proxy -p{third}
proxy -p{fourth}
""", ports=[third, fourth])
def slow(result):
# about two seconds: 400 KB in 8 KB blocks, 40 ms apart
r = t.http(f"http://127.0.0.1:{origin}"
f"/data?size=400000&block=8192&delay=40",
proxy=f"127.0.0.1:{third}")
result["status"] = r.status
result = {}
held = __import__("threading").Thread(target=slow, args=(result,))
held.start()
time.sleep(0.5)
t.ne(200, t.http(url, proxy=f"127.0.0.1:{third}").status,
"a second request of the service the rule names, while its first "
"is still going, is refused")
t.eq(200, t.http(url, proxy=f"127.0.0.1:{fourth}").status,
"another service is not counted in that limit")
held.join(60)
t.eq(200, result.get("status", 0),
"and the request the limit waited for finishes whole")
# a service named by its address, in the targetlist
if t.has_ipv6():
fifth, sixth = t.free_port(), t.free_port()
t.start("viaport_ip", f"""
log
auth iponly
deny * * *@::1 *
allow *
proxy -p{fifth} -i127.0.0.1
proxy -p{sixth} -i::1
""", ports=[fifth, ("::1", sixth)])
t.eq(200, t.http(url, proxy=f"127.0.0.1:{fifth}").status,
"the service on another address is served")
r = t.http(url, proxy=f"[::1]:{sixth}")
t.ne(200, r.status if r else 0,
"the service the rule names by its address is refused")
# several services named by one rule: the list after the @
sev1, sev2 = t.free_port(), t.free_port()
t.start("viaport_both", f"""
log
auth iponly
deny * * * @{sev1},{sev2}
allow *
proxy -p{sev1}
proxy -p{sev2}
""", ports=[sev1, sev2])
t.ne(200, t.http(url, proxy=f"127.0.0.1:{sev1}").status,
"the first service the rule names is refused")
t.ne(200, t.http(url, proxy=f"127.0.0.1:{sev2}").status,
"and so is the second")
# an @ with nothing after it is refused
out = t.run_config("viaport_bad",
"log\nallow * * * 443@\nnot_a_command\n")
t.contains(out, "Invalid port or port range",
"a service with nothing after the @ is refused")