From 115afe9b873f763082c89d427b2140834aa75c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Fri, 29 Sep 2017 02:51:36 +0200 Subject: [PATCH] [httpupload] Add ConfigDialog - Add a option that allows to disable HTTPS verification --- httpupload/config_dialog.py | 41 +++++++++++++++++++++++++++++++++++++ httpupload/httpupload.py | 24 ++++++++++++++++++---- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 httpupload/config_dialog.py diff --git a/httpupload/config_dialog.py b/httpupload/config_dialog.py new file mode 100644 index 0000000..bb80c40 --- /dev/null +++ b/httpupload/config_dialog.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2017 Philipp Hörist +# +# This file is part of Gajim. +# +# Gajim is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see . + +from enum import IntEnum, unique + +from gi.repository import Gtk + +from gajim.options_dialog import OptionsDialog +from gajim.common.const import Option, OptionKind, OptionType + + +class HTTPUploadConfigDialog(OptionsDialog): + def __init__(self, plugin, parent): + self.plugin = plugin + options = [ + Option(OptionKind.SWITCH, _('Enable HTTPS Verification'), + OptionType.BOOL, self.plugin.config['verify'], + callback=self.on_option, data='verify'), + ] + + OptionsDialog.__init__(self, parent, _('HTTP Upload Options'), + Gtk.DialogFlags.MODAL, options, None) + + def on_option(self, value, data): + self.plugin.config[data] = value diff --git a/httpupload/httpupload.py b/httpupload/httpupload.py index 12dfe8b..95d8b20 100644 --- a/httpupload/httpupload.py +++ b/httpupload/httpupload.py @@ -23,6 +23,7 @@ from urllib.parse import urlparse import io import mimetypes import logging +from functools import partial if os.name == 'nt': import certifi @@ -34,6 +35,8 @@ from gajim.common import ged from gajim.plugins import GajimPlugin from gajim.dialogs import FileChooserDialog, ErrorDialog +from .config_dialog import HTTPUploadConfigDialog + log = logging.getLogger('gajim.plugin_system.httpupload') IQ_CALLBACK = {} @@ -42,7 +45,10 @@ NS_HTTPUPLOAD = 'urn:xmpp:http:upload' class HTTPUploadPlugin(GajimPlugin): def init(self): - self.config_dialog = None + self.config_default_values = { + 'verify': (True, '') + } + self.config_dialog = partial(HTTPUploadConfigDialog, self) self.events_handlers = { 'agent-info-received': ( ged.PRECORE, self.handle_agent_info_received), @@ -342,10 +348,19 @@ class Base(object): request = Request( file.put, data=file.stream, headers=headers, method='PUT') log.info("Opening Urllib upload request...") - if os.name == 'nt': - transfer = urlopen(request, cafile=certifi.where(), timeout=30) + + if not self.plugin.config['verify']: + context = ssl.create_default_context() + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + log.warning('CERT Verification disabled') + transfer = urlopen(request, timeout=30, context=context) else: - transfer = urlopen(request, timeout=30) + if os.name == 'nt': + transfer = urlopen( + request, cafile=certifi.where(), timeout=30) + else: + transfer = urlopen(request, timeout=30) file.stream.close() log.info('Urllib upload request done, response code: %s', transfer.getcode()) @@ -492,6 +507,7 @@ class UploadAbortedException(Exception): def __str__(self): return "Upload Aborted" + class UnsecureTransportError(Exception): def __str__(self): return 'Server returned unsecure transport'