diff options
| author | Simeon Simeonov | 2015-04-16 15:43:59 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2015-04-16 15:43:59 +0200 |
| commit | 9c06ebe90039423809f51af1d62098ae7f5c813e (patch) | |
| tree | ce5bb4a1988b0a7ea0b0465eadd9fd0fe335b624 | |
| parent | b2ccfda61b7b7a1b37d4f9ffed57fcc4585d9ce0 (diff) | |
beinc_weechat.py v2.0 started
| -rw-r--r-- | beinc_config_sample.json.readme | 17 | ||||
| -rw-r--r-- | beinc_weechat.py | 77 |
2 files changed, 89 insertions, 5 deletions
diff --git a/beinc_config_sample.json.readme b/beinc_config_sample.json.readme index 3ac85a9..67cc5c2 100644 --- a/beinc_config_sample.json.readme +++ b/beinc_config_sample.json.readme | |||
| @@ -104,7 +104,7 @@ documentation to better illustrate the comments. | |||
| 104 | 104 | ||
| 105 | # Target URL. | 105 | # Target URL. |
| 106 | # Default: None (making the target useless) | 106 | # Default: None (making the target useless) |
| 107 | "target_url": "https://10.0.0.2:9898/push/secondtest", | 107 | "target_url": "https://10.0.0.2:9998", |
| 108 | 108 | ||
| 109 | # Password corresponding to the target BEINC instance | 109 | # Password corresponding to the target BEINC instance |
| 110 | # Default: None | 110 | # Default: None |
| @@ -117,6 +117,21 @@ documentation to better illustrate the comments. | |||
| 117 | # to verify the server's certificate | 117 | # to verify the server's certificate |
| 118 | "target_cert_file": "", | 118 | "target_cert_file": "", |
| 119 | 119 | ||
| 120 | # A list of preferred ciphers for this target | ||
| 121 | # Default: "" (use the OpenSSL default ciphers) | ||
| 122 | "ciphers": "ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384", | ||
| 123 | |||
| 124 | # Don't check whether the target's hostname matches the server's | ||
| 125 | # certificate. (Only Python >= 2.7.9) | ||
| 126 | # Default: 0 | ||
| 127 | "disable-hostname-check": 0, | ||
| 128 | |||
| 129 | # Force the client to use a specified SSL/TLS version | ||
| 130 | # Valid values: "auto", "SSLv3", "TLSv1", "TLSv1_1", "TLSv1_2" | ||
| 131 | # N.B. "TLSv1_1" and "TLSv1_2" only available in Python >= 2.7.9 | ||
| 132 | # Default: "auto" (Let's OpenSSL select the best option) | ||
| 133 | "ssl_version": "auto", | ||
| 134 | |||
| 120 | # The timestamp-format used by the BEINC weechat client | 135 | # The timestamp-format used by the BEINC weechat client |
| 121 | # See | 136 | # See |
| 122 | # https://docs.python.org/2.7/library/datetime.html#strftime-strptime-behavior | 137 | # https://docs.python.org/2.7/library/datetime.html#strftime-strptime-behavior |
diff --git a/beinc_weechat.py b/beinc_weechat.py index b875be6..5f7da5a 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | 3 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v1.0 | 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v2.0 |
| 5 | # Copyright (C) 2013-2015 Simeon Simeonov | 5 | # Copyright (C) 2013-2015 Simeon Simeonov |
| 6 | 6 | ||
| 7 | # This program is free software: you can redistribute it and/or modify | 7 | # This program is free software: you can redistribute it and/or modify |
| @@ -26,14 +26,13 @@ import random | |||
| 26 | import socket | 26 | import socket |
| 27 | import ssl | 27 | import ssl |
| 28 | import sys | 28 | import sys |
| 29 | import urllib | 29 | import xmlrpclib |
| 30 | import urllib2 | ||
| 31 | 30 | ||
| 32 | import weechat | 31 | import weechat |
| 33 | 32 | ||
| 34 | 33 | ||
| 35 | __author__ = 'Simeon Simeonov' | 34 | __author__ = 'Simeon Simeonov' |
| 36 | __version__ = '1.0' | 35 | __version__ = '2.0' |
| 37 | __license__ = 'GPL3' | 36 | __license__ = 'GPL3' |
| 38 | 37 | ||
| 39 | 38 | ||
| @@ -46,6 +45,14 @@ BEINC_POLICY_ALL = 1 | |||
| 46 | BEINC_POLICY_LIST_ONLY = 2 | 45 | BEINC_POLICY_LIST_ONLY = 2 |
| 47 | BEINC_CURRENT_CONFIG_VERSION = 1 | 46 | BEINC_CURRENT_CONFIG_VERSION = 1 |
| 48 | 47 | ||
| 48 | BEINC_SSL_METHODS = {'SSLv3': ssl.PROTOCOL_SSLv3, | ||
| 49 | 'TLSv1': ssl.PROTOCOL_TLSv1} | ||
| 50 | try: | ||
| 51 | BEINC_SSL_METHODS.update({'TLSv1_1': ssl.PROTOCOL_TLSv1_1}) | ||
| 52 | BEINC_SSL_METHODS.update({'TLSv1_2': ssl.PROTOCOL_TLSv1_2}) | ||
| 53 | except: | ||
| 54 | pass | ||
| 55 | |||
| 49 | 56 | ||
| 50 | class ValidHTTPSConnection(httplib.HTTPConnection): | 57 | class ValidHTTPSConnection(httplib.HTTPConnection): |
| 51 | """ | 58 | """ |
| @@ -121,6 +128,11 @@ class WeechatTarget(object): | |||
| 121 | self.__debug = bool(target_dict.get('debug', False)) | 128 | self.__debug = bool(target_dict.get('debug', False)) |
| 122 | self.__enabled = bool(target_dict.get('enabled', True)) | 129 | self.__enabled = bool(target_dict.get('enabled', True)) |
| 123 | self.__socket_timeout = int(target_dict.get('socket_timeout', 3)) | 130 | self.__socket_timeout = int(target_dict.get('socket_timeout', 3)) |
| 131 | self.__ciphers = target_dict.get('ciphers', '') | ||
| 132 | self.__disable_hostname_check = bool( | ||
| 133 | target_dict.get('disable-hostname-check', False)) | ||
| 134 | self.__ssl_version = target_dict.get('ssl_version', 'auto') | ||
| 135 | self.__connection = None # xmlrpclib.ServerProxy instance | ||
| 124 | 136 | ||
| 125 | @property | 137 | @property |
| 126 | def name(self): | 138 | def name(self): |
| @@ -298,6 +310,63 @@ class WeechatTarget(object): | |||
| 298 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' | 310 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 299 | 'for "{0}": {1}'.format(self.__name, e)) | 311 | 'for "{0}": {1}'.format(self.__name, e)) |
| 300 | 312 | ||
| 313 | def __connection_setup(self): | ||
| 314 | """ | ||
| 315 | """ | ||
| 316 | if self.__connection: | ||
| 317 | return True | ||
| 318 | try: | ||
| 319 | ssl_version = BEINC_SSL_METHODS.get(self.__ssl_version, | ||
| 320 | ssl.PROTOCOL_SSLv23) | ||
| 321 | if sys.hexversion >= 0x20709f0: | ||
| 322 | # Python >= 2.7.9 | ||
| 323 | context = ssl.SSLContext(ssl_version) | ||
| 324 | context.verify_mode = ssl.CERT_REQUIRED | ||
| 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: | ||
| 330 | context.load_verify_locations(os.path.expanduser( | ||
| 331 | self.__cert_file)) | ||
| 332 | if self.__ciphers: | ||
| 333 | context.set_ciphers(self.__ciphers) | ||
| 334 | transport = xmlrpclib.SafeTransport(context=context) | ||
| 335 | else: | ||
| 336 | # Python < 2.7.9 | ||
| 337 | ssl_options = {} | ||
| 338 | ssl_options['ssl_version'] = ssl_version | ||
| 339 | if self.__cert_file: | ||
| 340 | ssl_options['ca_certs'] = os.path.expanduser( | ||
| 341 | self.__cert_file) | ||
| 342 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED | ||
| 343 | if self.__ciphers: | ||
| 344 | ssl_options['ciphers'] = self.__ciphers | ||
| 345 | transport = BEINCCustomSafeTransport( | ||
| 346 | custom_ssl_options=ssl_options) | ||
| 347 | self.__connection = xmlrpclib.ServerProxy(self.__url, | ||
| 348 | transport=transport) | ||
| 349 | return True | ||
| 350 | except xmlrpclib.Fault as fault: | ||
| 351 | if self.__debug: | ||
| 352 | beinc_prnt( | ||
| 353 | 'BEINC DEBUG: Server answered with ' | ||
| 354 | 'errorCode={0}: {1}\n'.format( | ||
| 355 | fault.faultCode, | ||
| 356 | fault.faultString)) | ||
| 357 | except ssl.SSLError as e: | ||
| 358 | if self.__debug: | ||
| 359 | beinc_prnt('BEINC DEBUG: SSL/TLS error: {0}\n'.format(e)) | ||
| 360 | except socket.error as e: | ||
| 361 | if self.__debug: | ||
| 362 | beinc_prnt('BEINC DEBUG: Connection error: {0}\n'.format(e)) | ||
| 363 | except Exception as e: | ||
| 364 | if self.__debug: | ||
| 365 | beinc_prnt('BEINC DEBUG: Generic client error: {0}\n'.format( | ||
| 366 | e)) | ||
| 367 | self.__connection = None | ||
| 368 | return False | ||
| 369 | |||
| 301 | def __fetch_formatted_str(self, template, values): | 370 | def __fetch_formatted_str(self, template, values): |
| 302 | """ | 371 | """ |
| 303 | returns a formatted string by replacing the defined | 372 | returns a formatted string by replacing the defined |
