From 6a483e188d57e89a212155bd4f3ea3d3d2e9f922 Mon Sep 17 00:00:00 2001 From: DanyLE Date: Mon, 9 Jan 2023 20:54:06 +0100 Subject: [PATCH] Allow to break rules applying when command found in the current matched rule --- antd-config.ini | 5 ++++- http_server.c | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/antd-config.ini b/antd-config.ini index e97f8e7..be32ac8 100644 --- a/antd-config.ini +++ b/antd-config.ini @@ -48,8 +48,11 @@ ssl.enable=0 ; this is helpful to redirect many sub domains ; to a sub folder of the same server ; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1>? +; or +; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1>? +; prevents apply the rules below the current one when matched ; example of reverse proxy with the proxy plugin -; ^\/os\/+(.*)$ = /proxy/http://localhost:80/os/router.lua?r=<1>& +; ^\/os\/+(.*)$ = http://localhost:80/os/router.lua?r=<1>& ; Sytax: [regular expression on the original request]=[new request rule] [PORT:80] diff --git a/http_server.c b/http_server.c index 63abec9..9e56936 100644 --- a/http_server.c +++ b/http_server.c @@ -669,7 +669,7 @@ int rule_check(const char *k, const char *v, const char *host, const char *_url, char *tmp, rep[10]; int idx = 0; memset(rep, 0, 10); - LOG("Verify %s on %s or %s", k, url, host); + LOG("Verify %s=%s on %s or %s", k, v , url, host); // 1 group if (!host || !(ret = regex_match(k, host, 10, key_matches))) { @@ -709,6 +709,11 @@ int rule_check(const char *k, const char *v, const char *host, const char *_url, memcpy(buf + idx, target + key_matches[i].rm_so, key_matches[i].rm_eo - key_matches[i].rm_so); idx += key_matches[i].rm_eo - key_matches[i].rm_so; } + else if (strcasecmp(rep, "break") == 0) + { + // ignore it + LOG("Found break command, will break after this rule"); + } else { // just keep it memcpy(buf + idx, tmp + val_matches[1].rm_so - 1, val_matches[1].rm_eo + 2 - val_matches[1].rm_so); @@ -854,21 +859,35 @@ char *apply_rules(dictionary_t rules, const char *host, char *url) chain_t it; char *k; char *v; + int should_break = 0; for_each_assoc(it, rules) { k = it->key; - v = (char *)it->value; - // 1 group - if (rule_check(k, v, host, url, query_string, url)) + if(it->value) { - query_string = url; - - while ((*query_string != '?') && (*query_string != '\0')) - query_string++; - if (*query_string == '?') + v = (char *)it->value; + // 1 group + if (regex_match("$",v, 0, NULL)) { - *query_string = '\0'; - query_string++; + should_break = 1; + } + if (rule_check(k, v, host, url, query_string, url)) + { + query_string = url; + + while ((*query_string != '?') && (*query_string != '\0')) + query_string++; + if (*query_string == '?') + { + *query_string = '\0'; + query_string++; + } + if(should_break) + { + i = rules->cap; + LOG("Break rule check as matched found at %s -> %s", k, v); + break; + } } } }