@@ -60,13 +60,45 @@ func fetchSite(urlpath string, queries map[string]string) (string, *http.Request
60
60
log .Println (u .String () + urlQuery )
61
61
}
62
62
63
+ rule := fetchRule (u .Host , u .Path )
64
+
65
+ if rule .GoogleCache {
66
+ u , err = url .Parse ("https://webcache.googleusercontent.com/search?q=cache:" + u .String ())
67
+ if err != nil {
68
+ return "" , nil , nil , err
69
+ }
70
+ }
71
+
63
72
// Fetch the site
64
73
client := & http.Client {}
65
74
req , _ := http .NewRequest ("GET" , u .String ()+ urlQuery , nil )
66
- req .Header .Set ("User-Agent" , UserAgent )
67
- req .Header .Set ("X-Forwarded-For" , ForwardedFor )
68
- req .Header .Set ("Referer" , u .String ())
69
- req .Header .Set ("Host" , u .Host )
75
+
76
+ if rule .Headers .UserAgent != "" {
77
+ req .Header .Set ("User-Agent" , rule .Headers .UserAgent )
78
+ } else {
79
+ req .Header .Set ("User-Agent" , UserAgent )
80
+ }
81
+
82
+ if rule .Headers .XForwardedFor != "" {
83
+ if rule .Headers .XForwardedFor != "none" {
84
+ req .Header .Set ("X-Forwarded-For" , rule .Headers .XForwardedFor )
85
+ }
86
+ } else {
87
+ req .Header .Set ("X-Forwarded-For" , ForwardedFor )
88
+ }
89
+
90
+ if rule .Headers .Referer != "" {
91
+ if rule .Headers .Referer != "none" {
92
+ req .Header .Set ("Referer" , rule .Headers .Referer )
93
+ }
94
+ } else {
95
+ req .Header .Set ("Referer" , u .String ())
96
+ }
97
+
98
+ if rule .Headers .Cookie != "" {
99
+ req .Header .Set ("Cookie" , rule .Headers .Cookie )
100
+ }
101
+
70
102
resp , err := client .Do (req )
71
103
72
104
if err != nil {
@@ -79,11 +111,12 @@ func fetchSite(urlpath string, queries map[string]string) (string, *http.Request
79
111
return "" , nil , nil , err
80
112
}
81
113
82
- body := rewriteHtml (bodyB , u )
114
+ log .Print ("rule" , rule )
115
+ body := rewriteHtml (bodyB , u , rule )
83
116
return body , req , resp , nil
84
117
}
85
118
86
- func rewriteHtml (bodyB []byte , u * url.URL ) string {
119
+ func rewriteHtml (bodyB []byte , u * url.URL , rule Rule ) string {
87
120
// Rewrite the HTML
88
121
body := string (bodyB )
89
122
@@ -104,7 +137,7 @@ func rewriteHtml(bodyB []byte, u *url.URL) string {
104
137
body = strings .ReplaceAll (body , "href=\" https://" + u .Host , "href=\" /https://" + u .Host + "/" )
105
138
106
139
if os .Getenv ("RULESET" ) != "" {
107
- body = applyRules (u . Host , u . Path , body )
140
+ body = applyRules (body , rule )
108
141
}
109
142
return body
110
143
}
@@ -169,67 +202,57 @@ func loadRules() RuleSet {
169
202
return ruleSet
170
203
}
171
204
172
- func applyRules (domain string , path string , body string ) string {
205
+ func fetchRule (domain string , path string ) Rule {
173
206
if len (rulesSet ) == 0 {
174
- return body
207
+ return Rule {}
175
208
}
176
-
209
+ rule := Rule {}
177
210
for _ , rule := range rulesSet {
178
211
domains := rule .Domains
179
212
domains = append (domains , rule .Domain )
180
213
for _ , ruleDomain := range domains {
181
- if ruleDomain != domain {
182
- continue
183
- }
184
- if len (rule .Paths ) > 0 && ! StringInSlice (path , rule .Paths ) {
185
- continue
186
- }
187
- for _ , regexRule := range rule .RegexRules {
188
- re := regexp .MustCompile (regexRule .Match )
189
- body = re .ReplaceAllString (body , regexRule .Replace )
190
- }
191
- for _ , injection := range rule .Injections {
192
- doc , err := goquery .NewDocumentFromReader (strings .NewReader (body ))
193
- if err != nil {
194
- log .Fatal (err )
195
- }
196
- if injection .Replace != "" {
197
- doc .Find (injection .Position ).ReplaceWithHtml (injection .Replace )
198
- }
199
- if injection .Append != "" {
200
- doc .Find (injection .Position ).AppendHtml (injection .Append )
201
- }
202
- if injection .Prepend != "" {
203
- doc .Find (injection .Position ).PrependHtml (injection .Prepend )
204
- }
205
- body , err = doc .Html ()
206
- if err != nil {
207
- log .Fatal (err )
214
+ if ruleDomain == domain {
215
+ if len (rule .Paths ) > 0 && ! StringInSlice (path , rule .Paths ) {
216
+ continue
208
217
}
218
+ // return first match
219
+ return rule
209
220
}
210
221
}
211
222
}
212
-
213
- return body
223
+ return rule
214
224
}
215
225
216
- type Rule struct {
217
- Match string `yaml:"match"`
218
- Replace string `yaml:"replace"`
219
- }
226
+ func applyRules ( body string , rule Rule ) string {
227
+ if len ( rulesSet ) == 0 {
228
+ return body
229
+ }
220
230
221
- type RuleSet []struct {
222
- Domain string `yaml:"domain"`
223
- Domains []string `yaml:"domains,omitempty"`
224
- Paths []string `yaml:"paths,omitempty"`
225
- GoogleCache bool `yaml:"googleCache,omitempty"`
226
- RegexRules []Rule `yaml:"regexRules"`
227
- Injections []struct {
228
- Position string `yaml:"position"`
229
- Append string `yaml:"append"`
230
- Prepend string `yaml:"prepend"`
231
- Replace string `yaml:"replace"`
232
- } `yaml:"injections"`
231
+ for _ , regexRule := range rule .RegexRules {
232
+ re := regexp .MustCompile (regexRule .Match )
233
+ body = re .ReplaceAllString (body , regexRule .Replace )
234
+ }
235
+ for _ , injection := range rule .Injections {
236
+ doc , err := goquery .NewDocumentFromReader (strings .NewReader (body ))
237
+ if err != nil {
238
+ log .Fatal (err )
239
+ }
240
+ if injection .Replace != "" {
241
+ doc .Find (injection .Position ).ReplaceWithHtml (injection .Replace )
242
+ }
243
+ if injection .Append != "" {
244
+ doc .Find (injection .Position ).AppendHtml (injection .Append )
245
+ }
246
+ if injection .Prepend != "" {
247
+ doc .Find (injection .Position ).PrependHtml (injection .Prepend )
248
+ }
249
+ body , err = doc .Html ()
250
+ if err != nil {
251
+ log .Fatal (err )
252
+ }
253
+ }
254
+
255
+ return body
233
256
}
234
257
235
258
func StringInSlice (s string , list []string ) bool {
0 commit comments