improve to allow multiple injections

This commit is contained in:
Gianni Carafa
2023-11-03 22:40:40 +01:00
parent cde6ed7229
commit 67f9af0296
2 changed files with 20 additions and 21 deletions

View File

@@ -99,7 +99,6 @@ func rewriteHtml(bodyB []byte, u *url.URL) string {
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("RULES_URL") != "" {
log.Println("Applying rules")
body = applyRules(u.Host, u.Path, body) body = applyRules(u.Host, u.Path, body)
} }
return body return body
@@ -162,12 +161,12 @@ func applyRules(domain string, path string, body string) string {
re := regexp.MustCompile(regexRule.Match) re := regexp.MustCompile(regexRule.Match)
body = re.ReplaceAllString(body, regexRule.Replace) body = re.ReplaceAllString(body, regexRule.Replace)
} }
if rule.Inject.Code != "" { for _, injection := range rule.Injections {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)) doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
doc.Find(rule.Inject.Position).AppendHtml(rule.Inject.Code) doc.Find(injection.Position).AppendHtml(injection.Code)
body, err = doc.Html() body, err = doc.Html()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -186,10 +185,10 @@ type Rule struct {
type RuleSet []struct { type RuleSet []struct {
Domain string `yaml:"domain"` Domain string `yaml:"domain"`
Path string `yaml:"path,omitempty"` Path string `yaml:"path,omitempty"`
googleCache bool `yaml:"googleCache,omitempty"` GoogleCache bool `yaml:"googleCache,omitempty"`
RegexRules []Rule `yaml:"regexRules"` RegexRules []Rule `yaml:"regexRules"`
Inject struct { Injections []struct {
Position string `yaml:"position"` Position string `yaml:"position"`
Code string `yaml:"code"` Code string `yaml:"code"`
} `yaml:"inject"` } `yaml:"injections"`
} }

View File

@@ -2,8 +2,8 @@
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"
inject: injections:
position: head # Position where to inject the code - position: head # Position where to inject the code
code: | code: |
<script> <script>
window.localStorage.clear(); window.localStorage.clear();
@@ -16,8 +16,8 @@
regexRules: # Regex rules to apply regexRules: # Regex rules to apply
- 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"
inject: injections:
position: .left-content article .post-title # Position where to inject the code - position: .left-content article .post-title # Position where to inject the code
code: | code: |
<script> <script>
window.localStorage.clear(); window.localStorage.clear();