improve Rules file
This commit is contained in:
@@ -82,3 +82,4 @@ http://localhost:8080/raw/https://www.google.com
|
|||||||
| `USERPASS` | Enables Basic Auth, format `admin:123456` | `` |
|
| `USERPASS` | Enables Basic Auth, format `admin:123456` | `` |
|
||||||
| `LOG_URLS` | Log fetched URL's | `true` |
|
| `LOG_URLS` | Log fetched URL's | `true` |
|
||||||
| `DISABLE_FORM` | Disables URL Form Frontpage | `false` |
|
| `DISABLE_FORM` | Disables URL Form Frontpage | `false` |
|
||||||
|
| `RULES_URL` | URL to a ruleset file | `https://raw.githubusercontent.com/kubero-dev/ladder/main/ruleset.yaml` |
|
||||||
|
|||||||
@@ -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, "url(/", "url(/https://"+u.Host+"/")
|
||||||
body = strings.ReplaceAll(body, "href=\"https://"+u.Host, "href=\"/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
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,17 +126,21 @@ func loadRules() RuleSet {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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 {
|
if err != nil {
|
||||||
log.Println("ERROR:", err)
|
log.Println("ERROR:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ruleSet RuleSet
|
var ruleSet RuleSet
|
||||||
yaml.Unmarshal(bodyB, &ruleSet)
|
yaml.Unmarshal(body, &ruleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR:", err)
|
log.Println("ERROR:", err)
|
||||||
}
|
}
|
||||||
|
log.Println(ruleSet)
|
||||||
return ruleSet
|
return ruleSet
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,11 +156,6 @@ func applyRules(domain string, path string, body string) string {
|
|||||||
if rule.Path != "" && rule.Path != path {
|
if rule.Path != "" && rule.Path != path {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
for _, domRule := range rule.DomRules {
|
|
||||||
// run the dom rules
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
for _, regexRule := range rule.RegexRules {
|
for _, regexRule := range rule.RegexRules {
|
||||||
re := regexp.MustCompile(regexRule.Match)
|
re := regexp.MustCompile(regexRule.Match)
|
||||||
body = re.ReplaceAllString(body, regexRule.Replace)
|
body = re.ReplaceAllString(body, regexRule.Replace)
|
||||||
|
|||||||
20
ruleset.yaml
20
ruleset.yaml
@@ -1,15 +1,21 @@
|
|||||||
- domain: www.example.com
|
- domain: www.example.com
|
||||||
path: /article
|
|
||||||
regexRules:
|
regexRules:
|
||||||
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
|
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
|
||||||
replace: <script $1 script="/https://www.example.com/$3"
|
replace: <script $1 script="/https://www.example.com/$3"
|
||||||
domRules:
|
injectCode: |
|
||||||
- match: "tobe.defined"
|
<script>
|
||||||
replace: "test"
|
window.localStorage.clear();
|
||||||
|
console.log("test");
|
||||||
|
</script>
|
||||||
- domain: www.anotherdomain.com
|
- domain: www.anotherdomain.com
|
||||||
|
path: /article
|
||||||
|
googleCache: false
|
||||||
regexRules:
|
regexRules:
|
||||||
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
|
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
|
||||||
replace: <script $1 script="/https://www.example.com/$3"
|
replace: <script $1 script="/https://www.example.com/$3"
|
||||||
domRules:
|
injectCode: |
|
||||||
- match: "tobe.defined"
|
<script>
|
||||||
replace: "test"
|
window.localStorage.clear();
|
||||||
|
console.log("test");
|
||||||
|
</script>
|
||||||
|
|
||||||
Reference in New Issue
Block a user