diff options
| author | Simeon Simeonov | 2015-04-16 21:53:16 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2015-04-16 21:53:16 +0200 |
| commit | 4a9f282bf7a153eecc5b205268df69cbeb4111b3 (patch) | |
| tree | 6bc93fbc11c2bbf2f2fc28835d0486c8624d2bc3 | |
| parent | 9c06ebe90039423809f51af1d62098ae7f5c813e (diff) | |
Implement connection_setup for beinc_weechat.py
| -rwxr-xr-x | beinc_generic_client.py | 18 | ||||
| -rwxr-xr-x | beinc_poller.py | 18 | ||||
| -rw-r--r-- | beinc_weechat.py | 63 |
3 files changed, 54 insertions, 45 deletions
diff --git a/beinc_generic_client.py b/beinc_generic_client.py index 48d6a13..ae4762d 100755 --- a/beinc_generic_client.py +++ b/beinc_generic_client.py | |||
| @@ -109,12 +109,11 @@ def action_execute(args): | |||
| 109 | if sys.hexversion >= 0x20709f0: | 109 | if sys.hexversion >= 0x20709f0: |
| 110 | # Python >= 2.7.9 | 110 | # Python >= 2.7.9 |
| 111 | context = ssl.SSLContext(ssl_version) | 111 | context = ssl.SSLContext(ssl_version) |
| 112 | context.verify_mode = ssl.CERT_REQUIRED | 112 | context.verify_mode = ssl.CERT_NONE |
| 113 | if args.no_cert_validate: | 113 | if args.cert: |
| 114 | context.verify_mode = ssl.CERT_NONE | 114 | context.verify_mode = ssl.CERT_REQUIRED |
| 115 | context.check_hostname = bool(not args.disable_hostname_check) | ||
| 116 | if args.cert and not args.no_cert_validate: | ||
| 117 | context.load_verify_locations(os.path.expanduser(args.cert)) | 115 | context.load_verify_locations(os.path.expanduser(args.cert)) |
| 116 | context.check_hostname = bool(not args.disable_hostname_check) | ||
| 118 | if args.ciphers: | 117 | if args.ciphers: |
| 119 | context.set_ciphers(args.ciphers) | 118 | context.set_ciphers(args.ciphers) |
| 120 | transport = xmlrpclib.SafeTransport(context=context) | 119 | transport = xmlrpclib.SafeTransport(context=context) |
| @@ -122,9 +121,8 @@ def action_execute(args): | |||
| 122 | # Python < 2.7.9 | 121 | # Python < 2.7.9 |
| 123 | ssl_options = {} | 122 | ssl_options = {} |
| 124 | ssl_options['ssl_version'] = ssl_version | 123 | ssl_options['ssl_version'] = ssl_version |
| 125 | if args.cert and not args.no_cert_validate: | 124 | if args.cert: |
| 126 | ssl_options['ca_certs'] = os.path.expanduser(args.cert) | 125 | ssl_options['ca_certs'] = os.path.expanduser(args.cert) |
| 127 | if not args.no_cert_validate: | ||
| 128 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED | 126 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED |
| 129 | if args.ciphers: | 127 | if args.ciphers: |
| 130 | ssl_options['ciphers'] = args.ciphers | 128 | ssl_options['ciphers'] = args.ciphers |
| @@ -200,12 +198,6 @@ def main(): | |||
| 200 | required=True, | 198 | required=True, |
| 201 | help='The name of the BEINC-resource on the remote server') | 199 | help='The name of the BEINC-resource on the remote server') |
| 202 | parser.add_argument( | 200 | parser.add_argument( |
| 203 | '--no-cert-validate', | ||
| 204 | action='store_true', | ||
| 205 | dest='no_cert_validate', | ||
| 206 | default=False, | ||
| 207 | help='Do not validate server certificate') | ||
| 208 | parser.add_argument( | ||
| 209 | '-p', '--password', | 201 | '-p', '--password', |
| 210 | metavar='PASSWORD[FILE]', | 202 | metavar='PASSWORD[FILE]', |
| 211 | type=str, | 203 | type=str, |
diff --git a/beinc_poller.py b/beinc_poller.py index f9c5cca..d4eb1e5 100755 --- a/beinc_poller.py +++ b/beinc_poller.py | |||
| @@ -180,12 +180,11 @@ def poll_notifications(scheduler, args): | |||
| 180 | if sys.hexversion >= 0x20709f0: | 180 | if sys.hexversion >= 0x20709f0: |
| 181 | # Python >= 2.7.9 | 181 | # Python >= 2.7.9 |
| 182 | context = ssl.SSLContext(ssl_version) | 182 | context = ssl.SSLContext(ssl_version) |
| 183 | context.verify_mode = ssl.CERT_REQUIRED | 183 | context.verify_mode = ssl.CERT_NONE |
| 184 | if args.no_cert_validate: | 184 | if args.cert: |
| 185 | context.verify_mode = ssl.CERT_NONE | 185 | context.verify_mode = ssl.CERT_REQUIRED |
| 186 | context.check_hostname = bool(not args.disable_hostname_check) | ||
| 187 | if args.cert and not args.no_cert_validate: | ||
| 188 | context.load_verify_locations(os.path.expanduser(args.cert)) | 186 | context.load_verify_locations(os.path.expanduser(args.cert)) |
| 187 | context.check_hostname = bool(not args.disable_hostname_check) | ||
| 189 | if args.ciphers: | 188 | if args.ciphers: |
| 190 | context.set_ciphers(args.ciphers) | 189 | context.set_ciphers(args.ciphers) |
| 191 | transport = xmlrpclib.SafeTransport(context=context) | 190 | transport = xmlrpclib.SafeTransport(context=context) |
| @@ -193,9 +192,8 @@ def poll_notifications(scheduler, args): | |||
| 193 | # Python < 2.7.9 | 192 | # Python < 2.7.9 |
| 194 | ssl_options = {} | 193 | ssl_options = {} |
| 195 | ssl_options['ssl_version'] = ssl_version | 194 | ssl_options['ssl_version'] = ssl_version |
| 196 | if args.cert and not args.no_cert_validate: | 195 | if args.cert: |
| 197 | ssl_options['ca_certs'] = os.path.expanduser(args.cert) | 196 | ssl_options['ca_certs'] = os.path.expanduser(args.cert) |
| 198 | if not args.no_cert_validate: | ||
| 199 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED | 197 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED |
| 200 | if args.ciphers: | 198 | if args.ciphers: |
| 201 | ssl_options['ciphers'] = args.ciphers | 199 | ssl_options['ciphers'] = args.ciphers |
| @@ -301,12 +299,6 @@ def main(): | |||
| 301 | required=True, | 299 | required=True, |
| 302 | help='The name of the BEINC-resource on the remote server') | 300 | help='The name of the BEINC-resource on the remote server') |
| 303 | parser.add_argument( | 301 | parser.add_argument( |
| 304 | '--no-cert-validate', | ||
| 305 | action='store_true', | ||
| 306 | dest='no_cert_validate', | ||
| 307 | default=False, | ||
| 308 | help='Do not validate server certificate') | ||
| 309 | parser.add_argument( | ||
| 310 | '-o', '--osd-system', | 302 | '-o', '--osd-system', |
| 311 | metavar='SYSTEM', | 303 | metavar='SYSTEM', |
| 312 | type=str, | 304 | type=str, |
diff --git a/beinc_weechat.py b/beinc_weechat.py index 5f7da5a..442b01f 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 3 | 2 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v2.0 | 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v2.0 |
| @@ -54,34 +53,61 @@ except: | |||
| 54 | pass | 53 | pass |
| 55 | 54 | ||
| 56 | 55 | ||
| 57 | class ValidHTTPSConnection(httplib.HTTPConnection): | 56 | class BEINCCustomHTTPSConnection(httplib.HTTPConnection): |
| 58 | """ | ||
| 59 | Implements a simple CERT verification functionality | ||
| 60 | """ | 57 | """ |
| 58 | This class allows communication via SSL. | ||
| 61 | 59 | ||
| 60 | It is a reimplementation of httplib.HTTPSConnection and | ||
| 61 | allows the server certificate to be validated against CA | ||
| 62 | This functionality lacks in Python < 2.7.9 | ||
| 63 | """ | ||
| 62 | default_port = httplib.HTTPS_PORT | 64 | default_port = httplib.HTTPS_PORT |
| 63 | 65 | ||
| 64 | def __init__(self, *args, **kwargs): | 66 | def __init__(self, host, port=None, key_file=None, cert_file=None, |
| 65 | httplib.HTTPConnection.__init__(self, *args, **kwargs) | 67 | strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
| 68 | source_address=None, custom_ssl_options={}): | ||
| 69 | httplib.HTTPConnection.__init__(self, host, port, strict, timeout, | ||
| 70 | source_address) | ||
| 71 | self.key_file = key_file | ||
| 72 | self.cert_file = cert_file | ||
| 73 | self.custom_ssl_options = custom_ssl_options | ||
| 66 | 74 | ||
| 67 | def connect(self): | 75 | def connect(self): |
| 76 | "Connect to a host on a given (SSL) port." | ||
| 68 | sock = socket.create_connection((self.host, self.port), | 77 | sock = socket.create_connection((self.host, self.port), |
| 69 | self.timeout, self.source_address) | 78 | self.timeout, self.source_address) |
| 70 | if self._tunnel_host: | 79 | if self._tunnel_host: |
| 71 | self.sock = sock | 80 | self.sock = sock |
| 72 | self._tunnel() | 81 | self._tunnel() |
| 73 | self.sock = ssl.wrap_socket(sock, | 82 | self.sock = ssl.wrap_socket(sock, |
| 74 | ca_certs=global_beinc_cert_file, | 83 | self.key_file, |
| 75 | cert_reqs=ssl.CERT_REQUIRED) | 84 | self.cert_file, |
| 85 | **self.custom_ssl_options) | ||
| 76 | 86 | ||
| 77 | 87 | ||
| 78 | class ValidHTTPSHandler(urllib2.HTTPSHandler): | 88 | class BEINCCustomSafeTransport(xmlrpclib.Transport): |
| 79 | """ | ||
| 80 | Implements a simple CERT verification functionality | ||
| 81 | """ | ||
| 82 | 89 | ||
| 83 | def https_open(self, req): | 90 | def __init__(self, use_datetime=0, custom_ssl_options={}): |
| 84 | return self.do_open(ValidHTTPSConnection, req) | 91 | xmlrpclib.Transport.__init__(self, use_datetime=use_datetime) |
| 92 | self.custom_ssl_options = custom_ssl_options | ||
| 93 | |||
| 94 | def make_connection(self, host): | ||
| 95 | if self._connection and host == self._connection[0]: | ||
| 96 | return self._connection[1] | ||
| 97 | try: | ||
| 98 | HTTPS = BEINCCustomHTTPSConnection | ||
| 99 | except AttributeError: | ||
| 100 | raise NotImplementedError( | ||
| 101 | "your version of httplib doesn't support HTTPS" | ||
| 102 | ) | ||
| 103 | else: | ||
| 104 | chost, self._extra_headers, x509 = self.get_host_info(host) | ||
| 105 | self._connection = host, HTTPS( | ||
| 106 | chost, | ||
| 107 | None, | ||
| 108 | custom_ssl_options=self.custom_ssl_options, | ||
| 109 | **(x509 or {})) | ||
| 110 | return self._connection[1] | ||
| 85 | 111 | ||
| 86 | 112 | ||
| 87 | class WeechatTarget(object): | 113 | class WeechatTarget(object): |
| @@ -321,14 +347,13 @@ class WeechatTarget(object): | |||
| 321 | if sys.hexversion >= 0x20709f0: | 347 | if sys.hexversion >= 0x20709f0: |
| 322 | # Python >= 2.7.9 | 348 | # Python >= 2.7.9 |
| 323 | context = ssl.SSLContext(ssl_version) | 349 | context = ssl.SSLContext(ssl_version) |
| 324 | context.verify_mode = ssl.CERT_REQUIRED | 350 | context.verify_mode = ssl.CERT_NONE |
| 325 | if not self.__cert_file: | ||
| 326 | context.verify_mode = ssl.CERT_NONE | ||
| 327 | context.check_hostname = bool( | ||
| 328 | not self.__disable_hostname_check) | ||
| 329 | if self.__cert_file: | 351 | if self.__cert_file: |
| 352 | context.verify_mode = ssl.CERT_REQUIRED | ||
| 330 | context.load_verify_locations(os.path.expanduser( | 353 | context.load_verify_locations(os.path.expanduser( |
| 331 | self.__cert_file)) | 354 | self.__cert_file)) |
| 355 | context.check_hostname = bool( | ||
| 356 | not self.__disable_hostname_check) | ||
| 332 | if self.__ciphers: | 357 | if self.__ciphers: |
| 333 | context.set_ciphers(self.__ciphers) | 358 | context.set_ciphers(self.__ciphers) |
| 334 | transport = xmlrpclib.SafeTransport(context=context) | 359 | transport = xmlrpclib.SafeTransport(context=context) |
