improve Rules file

This commit is contained in:
Gianni Carafa
2023-11-03 21:46:57 +01:00
parent d5c58f42da
commit 96dd4de876
3 changed files with 25 additions and 16 deletions

View File

@@ -82,3 +82,4 @@ http://localhost:8080/raw/https://www.google.com
| `USERPASS` | Enables Basic Auth, format `admin:123456` | `` |
| `LOG_URLS` | Log fetched URL's | `true` |
| `DISABLE_FORM` | Disables URL Form Frontpage | `false` |
| `RULES_URL` | URL to a ruleset file | `https://raw.githubusercontent.com/kubero-dev/ladder/main/ruleset.yaml` |

View File

@@ -97,7 +97,10 @@ func rewriteHtml(bodyB []byte, u *url.URL) string {
body = strings.ReplaceAll(body, "url(/", "url(/https://"+u.Host+"/")
body = strings.ReplaceAll(body, "href=\"https://"+u.Host, "href=\"/https://"+u.Host+"/")
body = applyRules(u.Host, u.Path, body)
if os.Getenv("RULES_URL") != "" {
log.Println("Applying rules")
body = applyRules(u.Host, u.Path, body)
}
return body
}
@@ -123,17 +126,21 @@ func loadRules() RuleSet {
}
defer resp.Body.Close()
bodyB, err := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
log.Println("ERROR:", resp.StatusCode, rulesUrl)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR:", err)
}
var ruleSet RuleSet
yaml.Unmarshal(bodyB, &ruleSet)
yaml.Unmarshal(body, &ruleSet)
if err != nil {
log.Println("ERROR:", err)
}
log.Println(ruleSet)
return ruleSet
}
@@ -149,11 +156,6 @@ func applyRules(domain string, path string, body string) string {
if rule.Path != "" && rule.Path != path {
continue
}
/*
for _, domRule := range rule.DomRules {
// run the dom rules
}
*/
for _, regexRule := range rule.RegexRules {
re := regexp.MustCompile(regexRule.Match)
body = re.ReplaceAllString(body, regexRule.Replace)

View File

@@ -1,15 +1,21 @@
- domain: www.example.com
path: /article
regexRules:
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
replace: <script $1 script="/https://www.example.com/$3"
domRules:
- match: "tobe.defined"
replace: "test"
injectCode: |
<script>
window.localStorage.clear();
console.log("test");
</script>
- domain: www.anotherdomain.com
path: /article
googleCache: false
regexRules:
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
replace: <script $1 script="/https://www.example.com/$3"
domRules:
- match: "tobe.defined"
replace: "test"
injectCode: |
<script>
window.localStorage.clear();
console.log("test");
</script>