mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 15:59:41 +08:00
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.
This commit is contained in:
1 parent
a2e400622b
commit
59756eeb62
7 files changed
+144
-14
No files matched your search
+11
-1
@@ -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 *
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+35
-13
@@ -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])) {
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -413,6 +413,7 @@ struct ace {
|
||||
struct iplist *src, *dst;
|
||||
struct hostname *dstnames;
|
||||
struct portlist *ports;
|
||||
struct portlist *viaports;
|
||||
struct chain *chains;
|
||||
};
|
||||
|
||||
|
||||
@@ -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")
|
||||
Reference in new issue
Block a user