[mod] https rewrite pluginification
This commit is contained in:
		
							parent
							
								
									146928a749
								
							
						
					
					
						commit
						d2a636f75d
					
				| @ -36,11 +36,6 @@ if 'SEARX_SETTINGS_PATH' in environ: | ||||
| else: | ||||
|     settings_path = join(searx_dir, 'settings.yml') | ||||
| 
 | ||||
| if 'SEARX_HTTPS_REWRITE_PATH' in environ: | ||||
|     https_rewrite_path = environ['SEARX_HTTPS_REWRITE_PATH'] | ||||
| else: | ||||
|     https_rewrite_path = join(searx_dir, 'https_rules') | ||||
| 
 | ||||
| # load settings | ||||
| with open(settings_path) as settings_yaml: | ||||
|     settings = load(settings_yaml) | ||||
| @ -52,10 +47,4 @@ else: | ||||
| 
 | ||||
| logger = logging.getLogger('searx') | ||||
| 
 | ||||
| # load https rules only if https rewrite is enabled | ||||
| if settings.get('server', {}).get('https_rewrite'): | ||||
|     # loade https rules | ||||
|     from searx.https_rewrite import load_https_rules | ||||
|     load_https_rules(https_rewrite_path) | ||||
| 
 | ||||
| logger.info('Initialisation done') | ||||
|  | ||||
| @ -14,13 +14,15 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >. | ||||
| 
 | ||||
| (C) 2015 by Adam Tauber, <asciimoo@gmail.com> | ||||
| ''' | ||||
| from searx.plugins import (self_ip, | ||||
|                            search_on_category_select) | ||||
| from searx import logger | ||||
| from sys import exit | ||||
| from searx import logger | ||||
| 
 | ||||
| logger = logger.getChild('plugins') | ||||
| 
 | ||||
| from searx.plugins import (https_rewrite, | ||||
|                            self_ip, | ||||
|                            search_on_category_select) | ||||
| 
 | ||||
| required_attrs = (('name', str), | ||||
|                   ('description', str), | ||||
|                   ('default_on', bool)) | ||||
| @ -68,5 +70,6 @@ class PluginStore(): | ||||
| 
 | ||||
| 
 | ||||
| plugins = PluginStore() | ||||
| plugins.register(https_rewrite) | ||||
| plugins.register(self_ip) | ||||
| plugins.register(search_on_category_select) | ||||
|  | ||||
| @ -18,11 +18,22 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >. | ||||
| import re | ||||
| from urlparse import urlparse | ||||
| from lxml import etree | ||||
| from os import listdir | ||||
| from os import listdir, environ | ||||
| from os.path import isfile, isdir, join | ||||
| from searx import logger | ||||
| from searx.plugins import logger | ||||
| from flask.ext.babel import gettext | ||||
| from searx import searx_dir | ||||
| 
 | ||||
| 
 | ||||
| name = "HTTPS rewrite" | ||||
| description = gettext('Rewrite HTTP links to HTTPS if possible') | ||||
| default_on = True | ||||
| 
 | ||||
| if 'SEARX_HTTPS_REWRITE_PATH' in environ: | ||||
|     rules_path = environ['SEARX_rules_path'] | ||||
| else: | ||||
|     rules_path = join(searx_dir, 'plugins/https_rules') | ||||
| 
 | ||||
| logger = logger.getChild("https_rewrite") | ||||
| 
 | ||||
| # https://gitweb.torproject.org/\ | ||||
| @ -33,7 +44,7 @@ https_rules = [] | ||||
| 
 | ||||
| 
 | ||||
| # load single ruleset from a xml file | ||||
| def load_single_https_ruleset(filepath): | ||||
| def load_single_https_ruleset(rules_path): | ||||
|     ruleset = () | ||||
| 
 | ||||
|     # init parser | ||||
| @ -41,7 +52,7 @@ def load_single_https_ruleset(filepath): | ||||
| 
 | ||||
|     # load and parse xml-file | ||||
|     try: | ||||
|         tree = etree.parse(filepath, parser) | ||||
|         tree = etree.parse(rules_path, parser) | ||||
|     except: | ||||
|         # TODO, error message | ||||
|         return () | ||||
| @ -207,3 +218,10 @@ def https_url_rewrite(result): | ||||
|             # target has matched, do not search over the other rules | ||||
|             break | ||||
|     return result | ||||
| 
 | ||||
| 
 | ||||
| def on_result(request, ctx): | ||||
|     result = ctx['result'] | ||||
|     if result['parsed_url'].scheme == 'http': | ||||
|         https_url_rewrite(result) | ||||
|     return True | ||||
| @ -6,7 +6,6 @@ server: | ||||
|     base_url : False # Set custom base_url. Possible values: False or "https://your.custom.host/location/" | ||||
|     themes_path : "" # Custom ui themes path - leave it blank if you didn't change | ||||
|     default_theme : oscar # ui theme | ||||
|     https_rewrite : True # Force rewrite result urls. See searx/https_rewrite.py | ||||
|     useragent_suffix : "" # suffix of searx_useragent, could contain informations like an email address to the administrator | ||||
|     image_proxy : False # Proxying image results through searx | ||||
|     default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the 'locales' config section | ||||
|  | ||||
| @ -59,7 +59,6 @@ from searx.utils import ( | ||||
| ) | ||||
| from searx.version import VERSION_STRING | ||||
| from searx.languages import language_codes | ||||
| from searx.https_rewrite import https_url_rewrite | ||||
| from searx.search import Search | ||||
| from searx.query import Query | ||||
| from searx.autocomplete import searx_bang, backends as autocomplete_backends | ||||
| @ -359,15 +358,10 @@ def index(): | ||||
| 
 | ||||
|     for result in search.results: | ||||
| 
 | ||||
|         plugins.call('on_result', request, locals()) | ||||
|         if not search.paging and engines[result['engine']].paging: | ||||
|             search.paging = True | ||||
| 
 | ||||
|         # check if HTTPS rewrite is required | ||||
|         if settings['server']['https_rewrite']\ | ||||
|            and result['parsed_url'].scheme == 'http': | ||||
| 
 | ||||
|             result = https_url_rewrite(result) | ||||
| 
 | ||||
|         if search.request_data.get('format', 'html') == 'html': | ||||
|             if 'content' in result: | ||||
|                 result['content'] = highlight_content(result['content'], | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Adam Tauber
						Adam Tauber