implement local ruleset

This commit is contained in:
Gianni Carafa
2023-11-05 17:12:57 +01:00
parent 0dd5edd5ba
commit fba9db3d94
3 changed files with 42 additions and 34 deletions

View File

@@ -29,6 +29,7 @@ Freedom of information is an essential pillar of democracy and informed decision
- [x] Basic Auth - [x] Basic Auth
- [x] Disable logs - [x] Disable logs
- [x] No Tracking - [x] No Tracking
- [ ] Limit the proxy to a list of domains
- [ ] Optional TOR proxy - [ ] Optional TOR proxy
- [ ] A key to share only one URL - [ ] A key to share only one URL
- [ ] Fetch from Google Cache if not available - [ ] Fetch from Google Cache if not available
@@ -80,7 +81,7 @@ http://localhost:8080/raw/https://www.example.com
### Environment Variables ### Environment Variables
| Variable | Description | Default | | Variable | Description | Value |
| --- | --- | --- | | --- | --- | --- |
| `PORT` | Port to listen on | `8080` | | `PORT` | Port to listen on | `8080` |
| `PREFORK` | Spawn multiple server instances | `false` | | `PREFORK` | Spawn multiple server instances | `false` |
@@ -90,7 +91,7 @@ http://localhost:8080/raw/https://www.example.com
| `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` |
| `FORM_PATH` | Path to custom Form HTML | `` | | `FORM_PATH` | Path to custom Form HTML | `` |
| `RULES_URL` | URL to a ruleset file | `https://raw.githubusercontent.com/kubero-dev/ladder/main/ruleset.yaml` | | `RULESET` | URL to a ruleset file | `https://raw.githubusercontent.com/kubero-dev/ladder/main/ruleset.yaml` or `/path/to/my/rules.yaml` |
### Ruleset ### Ruleset

View File

@@ -98,7 +98,7 @@ 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+"/")
if os.Getenv("RULES_URL") != "" { if os.Getenv("RULESET") != "" {
body = applyRules(u.Host, u.Path, body) body = applyRules(u.Host, u.Path, body)
} }
return body return body
@@ -113,32 +113,41 @@ func getenv(key, fallback string) string {
} }
func loadRules() RuleSet { func loadRules() RuleSet {
rulesUrl := os.Getenv("RULES_URL") rulesUrl := os.Getenv("RULESET")
if rulesUrl == "" { if rulesUrl == "" {
RulesList := RuleSet{} RulesList := RuleSet{}
return RulesList return RulesList
} }
log.Println("Loading rules") log.Println("Loading rules")
resp, err := http.Get(rulesUrl)
if err != nil {
log.Println("ERROR:", err)
}
defer resp.Body.Close()
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 var ruleSet RuleSet
yaml.Unmarshal(body, &ruleSet) if strings.HasPrefix(rulesUrl, "http") {
if err != nil {
log.Println("ERROR:", err) resp, err := http.Get(rulesUrl)
if err != nil {
log.Println("ERROR:", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
log.Println("ERROR:", resp.StatusCode, rulesUrl)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR:", err)
}
yaml.Unmarshal(body, &ruleSet)
if err != nil {
log.Println("ERROR:", err)
}
} else {
yamlFile, err := os.ReadFile(rulesUrl)
if err != nil {
log.Println("ERROR:", err)
}
yaml.Unmarshal(yamlFile, &ruleSet)
} }
log.Println("Loaded rules for", len(ruleSet), "Domains") log.Println("Loaded rules for", len(ruleSet), "Domains")

View File

@@ -10,16 +10,14 @@
console.log("test"); console.log("test");
alert("Hello!"); alert("Hello!");
</script> </script>
- domain: www.anotherdomain.com # Domain where the rule applies - domain: www.americanbanker.com
path: /article # Path where the rule applies googleCache: false
googleCache: false # Search also in Google Cache
regexRules: # Regex rules to apply
- match: <script\s+([^>]*\s+)?src="(/)([^"]*)"
replace: <script $1 script="/https://www.example.com/$3"
injections: injections:
- position: .left-content article .post-title # Position where to inject the code into DOM - position: head
replace: | append: |
<h1>My Custom Title</h1> const inlineGate = document.querySelector('.inline-gate');
- position: .left-content article # Position where to inject the code into DOM if (inlineGate) {
prepend: | inlineGate.classList.remove('inline-gate');
<h2>Suptitle</h2> const inlineGated = document.querySelectorAll('.inline-gated');
for (const elem of inlineGated) { elem.classList.remove('inline-gated'); }
}