From 59756eeb62ea2f7b7d0cfa6ba00922dd1b7ddd03 Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Sat, 21 Nov 2026 16:55:44 +0300 Subject: [PATCH] acl: a rule's port may name the service it came through (#665) A port in a rule's targetportlist may carry a service port after a slash, X-Y/S, or, for any destination, */S: the rule matches only requests that arrived through the service listening on the port S, the port of its proxy -p or socks -p, whatever their destination. The limiters are shared by all the services of a configuration, so this is how a limit is given to the connections of one service alone: connlim 5 60 * * * */1080 denies the sixth concurrent connection of the service on port 1080. The destination port and the service port are both ranges or single ports, a slash with nothing after it is refused, and a rule without a slash is what it always was. --- man/3proxy.cfg.5 | 12 ++++++- src/acl.c | 11 ++++++ src/conf.c | 48 ++++++++++++++++++------- src/datatypes.c | 5 +++ src/mainconf.c | 1 + src/structures.h | 1 + tests/cases/viaport.py | 80 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 tests/cases/viaport.py diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 480ea25..52a2ccf 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -936,7 +936,17 @@ quoted or written \fB$$\fR, since a lone dollar outside quotes begins the name of a file to include. The same patterns, and the same prefix, are used by the \fBhttp\fR command, see BUILT IN HTTP SERVER. Regular expressions are matched per request and cost more than a wildmask, which is enough for most rules. -Targetportlist may contain ports (X) or port ranges lists (X-Y). For any field * +Targetportlist may contain ports (X) or port ranges lists (X-Y). A port may +carry a service port after a slash, as X-Y/S or, for any destination, */S: +the rule matches only requests that came through the service listening on +the port S — the port of the proxy or socks it connected to — whatever +their destination. The limiters are shared by all the services, so this is +how a limit is given to one service's connections alone, e.g. +.br + connlim 5 60 * * * */1080 +.br +denies the sixth concurrent connection of the service on port 1080, +whatever it connects to. For any field * sign means ANY. If access list is empty it\'s assumed to be .br allow * diff --git a/src/acl.c b/src/acl.c index f5134ca..1617a91 100644 --- a/src/acl.c +++ b/src/acl.c @@ -310,6 +310,17 @@ int ACLmatches(struct ace* acentry, struct clientparam * param){ } if(!ipentry && !hstentry) return 0; } + /* the service the request came through, by the port it listens on: + what the part after the slash of a port in a rule names */ + if(acentry->viaports) { + int viaport = ntohs(*SAPORT(¶m->srv->intsa)); + + for (portentry = acentry->viaports; portentry; portentry = portentry->next) + if(viaport >= portentry->startport && viaport <= portentry->endport) { + break; + } + if(!portentry) return 0; + } if(preauth && acentry->ports) { dstdep = 1; } diff --git a/src/conf.c b/src/conf.c index 221e580..b4fd847 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1385,6 +1385,23 @@ int scanipl(unsigned char *arg, struct iplist *dst){ return 0; } +/* Appends a port or a port range to a list: 0, or 1 for one that is + neither, or for want of memory. */ +static int portadd(struct portlist **head, const char *s){ + struct portlist **tail; + uint16_t start = 0, end = 0; + int res; + + for(tail = head; *tail; tail = &(*tail)->next); + res = sscanf(s, "%hu-%hu", &start, &end); + if(res < 1) return 1; + if(!(*tail = malloc(sizeof(struct portlist)))) return 1; + (*tail)->next = NULL; + (*tail)->startport = start; + (*tail)->endport = (res == 1)? start : end; + return 0; +} + struct ace * make_ace (int argc, unsigned char ** argv){ struct ace * acl; unsigned char *arg; @@ -1484,24 +1501,29 @@ struct ace * make_ace (int argc, unsigned char ** argv){ if(argc > 3 && strcmp("*", (char *)argv[3])) { arg = (unsigned char *)strtok((char *)argv[3], ","); if(arg) do { - if(!acl->ports) { - acl->ports = portl = malloc(sizeof(struct portlist)); + unsigned char *via = (unsigned char *)strchr((char *)arg, '/'); + + if(via) { + *via++ = 0; + if(!*via || !*arg) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + return(NULL); + } } - else { - portl->next = malloc(sizeof(struct portlist)); - portl = portl -> next; + if(strcmp((char *)arg, "*")) { + if(portadd(&acl->ports, (char *)arg)) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + return(NULL); + } } - if(!portl) { - fprintf(stderr, "No memory for ACL entry, line %d\n", linenum); - return(NULL); - } - memset(portl, 0, sizeof(struct portlist)); - res = sscanf((char *)arg, "%hu-%hu", &portl->startport, &portl->endport); - if(res < 1) { + else if(!via) { + fprintf(stderr, "Invalid port or port range, line %d\n", linenum); + return(NULL); + } + if(via && portadd(&acl->viaports, (char *)via)) { fprintf(stderr, "Invalid port or port range, line %d\n", linenum); return(NULL); } - if (res == 1) portl->endport = portl->startport; } while((arg = (unsigned char *)strtok((char *)NULL, ","))); } if(argc > 4 && strcmp("*", (char *)argv[4])) { diff --git a/src/datatypes.c b/src/datatypes.c index 52a707c..dbfd49f 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -418,6 +418,10 @@ static void * ef_ace_ports(struct node * node){ return ((struct ace *)node->value) -> ports; } +static void * ef_ace_viaports(struct node * node){ + return ((struct ace *)node->value) -> viaports; +} + static void * ef_ace_chain(struct node * node){ return ((struct ace *)node->value) -> chains; } @@ -851,6 +855,7 @@ static struct property prop_ace[] = { {"dst", ef_ace_dst, TYPE_IPLIST, "list of destination ips"}, {"dstnames", ef_ace_dstnames, TYPE_HOSTNAME, "list of destination hostnames"}, {"ports", ef_ace_ports, TYPE_PORTLIST, "list of destination ports"}, + {"viaports", ef_ace_viaports, TYPE_PORTLIST, "list of the services' ports it matches"}, {"chain", ef_ace_chain, TYPE_CHAIN, "redirect to parent(s)"}, {"wdays", ef_ace_weekdays, TYPE_WEEKDAYS, "days of week"}, {"periods", ef_ace_period, TYPE_PERIOD, "time of the day"}, diff --git a/src/mainconf.c b/src/mainconf.c index c8515e5..2f27e1e 100644 --- a/src/mainconf.c +++ b/src/mainconf.c @@ -1221,6 +1221,7 @@ void freeacl(struct ace *ac){ for(ipl = ac->src; ipl; ipl = (struct iplist *)itfree(ipl, ipl->next)); for(ipl = ac->dst; ipl; ipl = (struct iplist *)itfree(ipl,ipl->next)); for(pl = ac->ports; pl; pl = (struct portlist *)itfree(pl, pl->next)); + for(pl = ac->viaports; pl; pl = (struct portlist *)itfree(pl, pl->next)); for(pel = ac->periods; pel; pel = (struct period *)itfree(pel, pel->next)); for(ul = ac->users; ul; ul = (struct userlist *)itfree(ul, ul->next)){ if(ul->user)free(ul->user); diff --git a/src/structures.h b/src/structures.h index 7d7182d..f18b73c 100644 --- a/src/structures.h +++ b/src/structures.h @@ -413,6 +413,7 @@ struct ace { struct iplist *src, *dst; struct hostname *dstnames; struct portlist *ports; + struct portlist *viaports; struct chain *chains; }; diff --git a/tests/cases/viaport.py b/tests/cases/viaport.py new file mode 100644 index 0000000..54c2e21 --- /dev/null +++ b/tests/cases/viaport.py @@ -0,0 +1,80 @@ +"""A port in a rule may carry a service port after a slash. + +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. X/S — or +*/S for any destination — matches only requests that came through +the service listening on the port S: 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 written with a service 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 written with a service 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 slash with nothing after it, or a bare * in a list, 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 port with no destination port is refused")