Allow to break rules applying when <break> command found in the current matched rule
All checks were successful
gitea-sync/ant-http/pipeline/head This commit looks good

This commit is contained in:
DanyLE 2023-01-09 20:54:06 +01:00
parent a78815c3b6
commit 6a483e188d
2 changed files with 34 additions and 12 deletions

View File

@ -48,8 +48,11 @@ ssl.enable=0
; this is helpful to redirect many sub domains ; this is helpful to redirect many sub domains
; to a sub folder of the same server ; to a sub folder of the same server
; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query> ; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query>
; or
; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query><break>
; <break> prevents apply the rules below the current one when matched
; example of reverse proxy with the proxy plugin ; example of reverse proxy with the proxy plugin
; ^\/os\/+(.*)$ = /proxy/http://localhost:80/os/router.lua?r=<1>&<query> ; ^\/os\/+(.*)$ = http://localhost:80/os/router.lua?r=<1>&<query>
; Sytax: [regular expression on the original request]=[new request rule] ; Sytax: [regular expression on the original request]=[new request rule]
[PORT:80] [PORT:80]

View File

@ -669,7 +669,7 @@ int rule_check(const char *k, const char *v, const char *host, const char *_url,
char *tmp, rep[10]; char *tmp, rep[10];
int idx = 0; int idx = 0;
memset(rep, 0, 10); 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 // 1 group
if (!host || !(ret = regex_match(k, host, 10, key_matches))) 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); 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; 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 else
{ // just keep it { // just keep it
memcpy(buf + idx, tmp + val_matches[1].rm_so - 1, val_matches[1].rm_eo + 2 - val_matches[1].rm_so); 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; chain_t it;
char *k; char *k;
char *v; char *v;
int should_break = 0;
for_each_assoc(it, rules) for_each_assoc(it, rules)
{ {
k = it->key; k = it->key;
v = (char *)it->value; if(it->value)
// 1 group
if (rule_check(k, v, host, url, query_string, url))
{ {
query_string = url; v = (char *)it->value;
// 1 group
while ((*query_string != '?') && (*query_string != '\0')) if (regex_match("<break>$",v, 0, NULL))
query_string++;
if (*query_string == '?')
{ {
*query_string = '\0'; should_break = 1;
query_string++; }
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;
}
} }
} }
} }