From 96dd4de8768b2b64085b848cbe21a77794f9c20f Mon Sep 17 00:00:00 2001 From: Gianni Carafa Date: Fri, 3 Nov 2023 21:46:57 +0100 Subject: [PATCH] improve Rules file --- README.md | 1 + handlers/proxy.go | 20 +++++++++++--------- ruleset.yaml | 20 +++++++++++++------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index fca6e45..72c019e 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/handlers/proxy.go b/handlers/proxy.go index 6fe4462..1e0d4b9 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -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) diff --git a/ruleset.yaml b/ruleset.yaml index fe1eeed..cc03f01 100644 --- a/ruleset.yaml +++ b/ruleset.yaml @@ -1,15 +1,21 @@ - domain: www.example.com - path: /article regexRules: - match: ]*\s+)?src="(/)([^"]*)" replace: - domain: www.anotherdomain.com + path: /article + googleCache: false regexRules: - match: ]*\s+)?src="(/)([^"]*)" replace: + \ No newline at end of file