Browse Source

[mod] https rewrite pluginification

Adam Tauber 10 years ago
parent
commit
d2a636f75d
41 changed files with 29 additions and 26 deletions
  1. 0
    11
      searx/__init__.py
  2. 6
    3
      searx/plugins/__init__.py
  3. 22
    4
      searx/plugins/https_rewrite.py
  4. 0
    0
      searx/plugins/https_rules/00README
  5. 0
    0
      searx/plugins/https_rules/Bing.xml
  6. 0
    0
      searx/plugins/https_rules/Dailymotion.xml
  7. 0
    0
      searx/plugins/https_rules/Deviantart.xml
  8. 0
    0
      searx/plugins/https_rules/DuckDuckGo.xml
  9. 0
    0
      searx/plugins/https_rules/Flickr.xml
  10. 0
    0
      searx/plugins/https_rules/Github-Pages.xml
  11. 0
    0
      searx/plugins/https_rules/Github.xml
  12. 0
    0
      searx/plugins/https_rules/Google-mismatches.xml
  13. 0
    0
      searx/plugins/https_rules/Google.org.xml
  14. 0
    0
      searx/plugins/https_rules/GoogleAPIs.xml
  15. 0
    0
      searx/plugins/https_rules/GoogleCanada.xml
  16. 0
    0
      searx/plugins/https_rules/GoogleImages.xml
  17. 0
    0
      searx/plugins/https_rules/GoogleMainSearch.xml
  18. 0
    0
      searx/plugins/https_rules/GoogleMaps.xml
  19. 0
    0
      searx/plugins/https_rules/GoogleMelange.xml
  20. 0
    0
      searx/plugins/https_rules/GoogleSearch.xml
  21. 0
    0
      searx/plugins/https_rules/GoogleServices.xml
  22. 0
    0
      searx/plugins/https_rules/GoogleShopping.xml
  23. 0
    0
      searx/plugins/https_rules/GoogleSorry.xml
  24. 0
    0
      searx/plugins/https_rules/GoogleTranslate.xml
  25. 0
    0
      searx/plugins/https_rules/GoogleVideos.xml
  26. 0
    0
      searx/plugins/https_rules/GoogleWatchBlog.xml
  27. 0
    0
      searx/plugins/https_rules/Google_App_Engine.xml
  28. 0
    0
      searx/plugins/https_rules/Googleplex.com.xml
  29. 0
    0
      searx/plugins/https_rules/OpenStreetMap.xml
  30. 0
    0
      searx/plugins/https_rules/Rawgithub.com.xml
  31. 0
    0
      searx/plugins/https_rules/Soundcloud.xml
  32. 0
    0
      searx/plugins/https_rules/ThePirateBay.xml
  33. 0
    0
      searx/plugins/https_rules/Torproject.xml
  34. 0
    0
      searx/plugins/https_rules/Twitter.xml
  35. 0
    0
      searx/plugins/https_rules/Vimeo.xml
  36. 0
    0
      searx/plugins/https_rules/WikiLeaks.xml
  37. 0
    0
      searx/plugins/https_rules/Wikimedia.xml
  38. 0
    0
      searx/plugins/https_rules/Yahoo.xml
  39. 0
    0
      searx/plugins/https_rules/YouTube.xml
  40. 0
    1
      searx/settings.yml
  41. 1
    7
      searx/webapp.py

+ 0
- 11
searx/__init__.py View File

@@ -36,11 +36,6 @@ if 'SEARX_SETTINGS_PATH' in environ:
36 36
 else:
37 37
     settings_path = join(searx_dir, 'settings.yml')
38 38
 
39
-if 'SEARX_HTTPS_REWRITE_PATH' in environ:
40
-    https_rewrite_path = environ['SEARX_HTTPS_REWRITE_PATH']
41
-else:
42
-    https_rewrite_path = join(searx_dir, 'https_rules')
43
-
44 39
 # load settings
45 40
 with open(settings_path) as settings_yaml:
46 41
     settings = load(settings_yaml)
@@ -52,10 +47,4 @@ else:
52 47
 
53 48
 logger = logging.getLogger('searx')
54 49
 
55
-# load https rules only if https rewrite is enabled
56
-if settings.get('server', {}).get('https_rewrite'):
57
-    # loade https rules
58
-    from searx.https_rewrite import load_https_rules
59
-    load_https_rules(https_rewrite_path)
60
-
61 50
 logger.info('Initialisation done')

+ 6
- 3
searx/plugins/__init__.py View File

@@ -14,13 +14,15 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
14 14
 
15 15
 (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16 16
 '''
17
-from searx.plugins import (self_ip,
18
-                           search_on_category_select)
19
-from searx import logger
20 17
 from sys import exit
18
+from searx import logger
21 19
 
22 20
 logger = logger.getChild('plugins')
23 21
 
22
+from searx.plugins import (https_rewrite,
23
+                           self_ip,
24
+                           search_on_category_select)
25
+
24 26
 required_attrs = (('name', str),
25 27
                   ('description', str),
26 28
                   ('default_on', bool))
@@ -68,5 +70,6 @@ class PluginStore():
68 70
 
69 71
 
70 72
 plugins = PluginStore()
73
+plugins.register(https_rewrite)
71 74
 plugins.register(self_ip)
72 75
 plugins.register(search_on_category_select)

searx/https_rewrite.py → searx/plugins/https_rewrite.py View File

@@ -18,11 +18,22 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
18 18
 import re
19 19
 from urlparse import urlparse
20 20
 from lxml import etree
21
-from os import listdir
21
+from os import listdir, environ
22 22
 from os.path import isfile, isdir, join
23
-from searx import logger
23
+from searx.plugins import logger
24
+from flask.ext.babel import gettext
25
+from searx import searx_dir
24 26
 
25 27
 
28
+name = "HTTPS rewrite"
29
+description = gettext('Rewrite HTTP links to HTTPS if possible')
30
+default_on = True
31
+
32
+if 'SEARX_HTTPS_REWRITE_PATH' in environ:
33
+    rules_path = environ['SEARX_rules_path']
34
+else:
35
+    rules_path = join(searx_dir, 'plugins/https_rules')
36
+
26 37
 logger = logger.getChild("https_rewrite")
27 38
 
28 39
 # https://gitweb.torproject.org/\
@@ -33,7 +44,7 @@ https_rules = []
33 44
 
34 45
 
35 46
 # load single ruleset from a xml file
36
-def load_single_https_ruleset(filepath):
47
+def load_single_https_ruleset(rules_path):
37 48
     ruleset = ()
38 49
 
39 50
     # init parser
@@ -41,7 +52,7 @@ def load_single_https_ruleset(filepath):
41 52
 
42 53
     # load and parse xml-file
43 54
     try:
44
-        tree = etree.parse(filepath, parser)
55
+        tree = etree.parse(rules_path, parser)
45 56
     except:
46 57
         # TODO, error message
47 58
         return ()
@@ -207,3 +218,10 @@ def https_url_rewrite(result):
207 218
             # target has matched, do not search over the other rules
208 219
             break
209 220
     return result
221
+
222
+
223
+def on_result(request, ctx):
224
+    result = ctx['result']
225
+    if result['parsed_url'].scheme == 'http':
226
+        https_url_rewrite(result)
227
+    return True

searx/https_rules/00README → searx/plugins/https_rules/00README View File


searx/https_rules/Bing.xml → searx/plugins/https_rules/Bing.xml View File


searx/https_rules/Dailymotion.xml → searx/plugins/https_rules/Dailymotion.xml View File


searx/https_rules/Deviantart.xml → searx/plugins/https_rules/Deviantart.xml View File


searx/https_rules/DuckDuckGo.xml → searx/plugins/https_rules/DuckDuckGo.xml View File


searx/https_rules/Flickr.xml → searx/plugins/https_rules/Flickr.xml View File


searx/https_rules/Github-Pages.xml → searx/plugins/https_rules/Github-Pages.xml View File


searx/https_rules/Github.xml → searx/plugins/https_rules/Github.xml View File


searx/https_rules/Google-mismatches.xml → searx/plugins/https_rules/Google-mismatches.xml View File


searx/https_rules/Google.org.xml → searx/plugins/https_rules/Google.org.xml View File


searx/https_rules/GoogleAPIs.xml → searx/plugins/https_rules/GoogleAPIs.xml View File


searx/https_rules/GoogleCanada.xml → searx/plugins/https_rules/GoogleCanada.xml View File


searx/https_rules/GoogleImages.xml → searx/plugins/https_rules/GoogleImages.xml View File


searx/https_rules/GoogleMainSearch.xml → searx/plugins/https_rules/GoogleMainSearch.xml View File


searx/https_rules/GoogleMaps.xml → searx/plugins/https_rules/GoogleMaps.xml View File


searx/https_rules/GoogleMelange.xml → searx/plugins/https_rules/GoogleMelange.xml View File


searx/https_rules/GoogleSearch.xml → searx/plugins/https_rules/GoogleSearch.xml View File


searx/https_rules/GoogleServices.xml → searx/plugins/https_rules/GoogleServices.xml View File


searx/https_rules/GoogleShopping.xml → searx/plugins/https_rules/GoogleShopping.xml View File


searx/https_rules/GoogleSorry.xml → searx/plugins/https_rules/GoogleSorry.xml View File


searx/https_rules/GoogleTranslate.xml → searx/plugins/https_rules/GoogleTranslate.xml View File


searx/https_rules/GoogleVideos.xml → searx/plugins/https_rules/GoogleVideos.xml View File


searx/https_rules/GoogleWatchBlog.xml → searx/plugins/https_rules/GoogleWatchBlog.xml View File


searx/https_rules/Google_App_Engine.xml → searx/plugins/https_rules/Google_App_Engine.xml View File


searx/https_rules/Googleplex.com.xml → searx/plugins/https_rules/Googleplex.com.xml View File


searx/https_rules/OpenStreetMap.xml → searx/plugins/https_rules/OpenStreetMap.xml View File


searx/https_rules/Rawgithub.com.xml → searx/plugins/https_rules/Rawgithub.com.xml View File


searx/https_rules/Soundcloud.xml → searx/plugins/https_rules/Soundcloud.xml View File


searx/https_rules/ThePirateBay.xml → searx/plugins/https_rules/ThePirateBay.xml View File


searx/https_rules/Torproject.xml → searx/plugins/https_rules/Torproject.xml View File


searx/https_rules/Twitter.xml → searx/plugins/https_rules/Twitter.xml View File


searx/https_rules/Vimeo.xml → searx/plugins/https_rules/Vimeo.xml View File


searx/https_rules/WikiLeaks.xml → searx/plugins/https_rules/WikiLeaks.xml View File


searx/https_rules/Wikimedia.xml → searx/plugins/https_rules/Wikimedia.xml View File


searx/https_rules/Yahoo.xml → searx/plugins/https_rules/Yahoo.xml View File


searx/https_rules/YouTube.xml → searx/plugins/https_rules/YouTube.xml View File


+ 0
- 1
searx/settings.yml View File

@@ -6,7 +6,6 @@ server:
6 6
     base_url : False # Set custom base_url. Possible values: False or "https://your.custom.host/location/"
7 7
     themes_path : "" # Custom ui themes path - leave it blank if you didn't change
8 8
     default_theme : oscar # ui theme
9
-    https_rewrite : True # Force rewrite result urls. See searx/https_rewrite.py
10 9
     useragent_suffix : "" # suffix of searx_useragent, could contain informations like an email address to the administrator
11 10
     image_proxy : False # Proxying image results through searx
12 11
     default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section

+ 1
- 7
searx/webapp.py View File

@@ -59,7 +59,6 @@ from searx.utils import (
59 59
 )
60 60
 from searx.version import VERSION_STRING
61 61
 from searx.languages import language_codes
62
-from searx.https_rewrite import https_url_rewrite
63 62
 from searx.search import Search
64 63
 from searx.query import Query
65 64
 from searx.autocomplete import searx_bang, backends as autocomplete_backends
@@ -359,15 +358,10 @@ def index():
359 358
 
360 359
     for result in search.results:
361 360
 
361
+        plugins.call('on_result', request, locals())
362 362
         if not search.paging and engines[result['engine']].paging:
363 363
             search.paging = True
364 364
 
365
-        # check if HTTPS rewrite is required
366
-        if settings['server']['https_rewrite']\
367
-           and result['parsed_url'].scheme == 'http':
368
-
369
-            result = https_url_rewrite(result)
370
-
371 365
         if search.request_data.get('format', 'html') == 'html':
372 366
             if 'content' in result:
373 367
                 result['content'] = highlight_content(result['content'],