diff options
Diffstat (limited to 'beinc_poller.py')
| -rwxr-xr-x | beinc_poller.py | 221 |
1 files changed, 155 insertions, 66 deletions
diff --git a/beinc_poller.py b/beinc_poller.py index 2bdfe29..b5701ce 100755 --- a/beinc_poller.py +++ b/beinc_poller.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 |
| @@ -21,16 +21,13 @@ | |||
| 21 | import argparse | 21 | import argparse |
| 22 | import errno | 22 | import errno |
| 23 | import getpass | 23 | import getpass |
| 24 | import httplib | 24 | import httplib # for Python < 2.7.9 |
| 25 | import json | ||
| 26 | import os | 25 | import os |
| 27 | import sched | 26 | import socket # for Python < 2.7.9 |
| 28 | import socket | ||
| 29 | import ssl | 27 | import ssl |
| 30 | import sys | 28 | import sys |
| 31 | import time | 29 | import time |
| 32 | import urllib | 30 | import xmlrpclib |
| 33 | import urllib2 | ||
| 34 | 31 | ||
| 35 | try: | 32 | try: |
| 36 | import pynotify | 33 | import pynotify |
| @@ -50,38 +47,74 @@ except ImportError as e: | |||
| 50 | 47 | ||
| 51 | 48 | ||
| 52 | __author__ = 'Simeon Simeonov' | 49 | __author__ = 'Simeon Simeonov' |
| 53 | __version__ = '1.0' | 50 | __version__ = '2.0' |
| 54 | __license__ = 'GPL3' | 51 | __license__ = 'GPL3' |
| 55 | 52 | ||
| 56 | 53 | ||
| 57 | class ValidHTTPSConnection(httplib.HTTPConnection): | 54 | BEINC_SSL_METHODS = {'SSLv3': ssl.PROTOCOL_SSLv3, |
| 58 | """ | 55 | 'TLSv1': ssl.PROTOCOL_TLSv1} |
| 59 | Implements a simple CERT verification functionality | 56 | try: |
| 57 | BEINC_SSL_METHODS.update({'TLSv1_1': ssl.PROTOCOL_TLSv1_1}) | ||
| 58 | BEINC_SSL_METHODS.update({'TLSv1_2': ssl.PROTOCOL_TLSv1_2}) | ||
| 59 | except: | ||
| 60 | pass | ||
| 61 | |||
| 62 | |||
| 63 | class BEINCCustomHTTPSConnection(httplib.HTTPConnection): | ||
| 60 | """ | 64 | """ |
| 65 | This class allows communication via SSL. | ||
| 61 | 66 | ||
| 67 | It is a reimplementation of httplib.HTTPSConnection and | ||
| 68 | allows the server certificate to be validated against CA | ||
| 69 | This functionality lacks in Python < 2.7.9 | ||
| 70 | """ | ||
| 62 | default_port = httplib.HTTPS_PORT | 71 | default_port = httplib.HTTPS_PORT |
| 63 | 72 | ||
| 64 | def __init__(self, *args, **kwargs): | 73 | def __init__(self, host, port=None, key_file=None, cert_file=None, |
| 65 | httplib.HTTPConnection.__init__(self, *args, **kwargs) | 74 | strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
| 75 | source_address=None, custom_ssl_options={}): | ||
| 76 | httplib.HTTPConnection.__init__(self, host, port, strict, timeout, | ||
| 77 | source_address) | ||
| 78 | self.key_file = key_file | ||
| 79 | self.cert_file = cert_file | ||
| 80 | self.custom_ssl_options = custom_ssl_options | ||
| 66 | 81 | ||
| 67 | def connect(self): | 82 | def connect(self): |
| 83 | "Connect to a host on a given (SSL) port." | ||
| 68 | sock = socket.create_connection((self.host, self.port), | 84 | sock = socket.create_connection((self.host, self.port), |
| 69 | self.timeout, self.source_address) | 85 | self.timeout, self.source_address) |
| 70 | if self._tunnel_host: | 86 | if self._tunnel_host: |
| 71 | self.sock = sock | 87 | self.sock = sock |
| 72 | self._tunnel() | 88 | self._tunnel() |
| 73 | self.sock = ssl.wrap_socket(sock, | 89 | self.sock = ssl.wrap_socket(sock, |
| 74 | ca_certs=global_beinc_cert_file, | 90 | self.key_file, |
| 75 | cert_reqs=ssl.CERT_REQUIRED) | 91 | self.cert_file, |
| 92 | **self.custom_ssl_options) | ||
| 76 | 93 | ||
| 77 | 94 | ||
| 78 | class ValidHTTPSHandler(urllib2.HTTPSHandler): | 95 | class BEINCCustomSafeTransport(xmlrpclib.Transport): |
| 79 | """ | 96 | |
| 80 | Implements a simple CERT verification functionality | 97 | def __init__(self, use_datetime=0, custom_ssl_options={}): |
| 81 | """ | 98 | xmlrpclib.Transport.__init__(self, use_datetime=use_datetime) |
| 99 | self.custom_ssl_options = custom_ssl_options | ||
| 82 | 100 | ||
| 83 | def https_open(self, req): | 101 | def make_connection(self, host): |
| 84 | return self.do_open(ValidHTTPSConnection, req) | 102 | if self._connection and host == self._connection[0]: |
| 103 | return self._connection[1] | ||
| 104 | try: | ||
| 105 | HTTPS = BEINCCustomHTTPSConnection | ||
| 106 | except AttributeError: | ||
| 107 | raise NotImplementedError( | ||
| 108 | "your version of httplib doesn't support HTTPS" | ||
| 109 | ) | ||
| 110 | else: | ||
| 111 | chost, self._extra_headers, x509 = self.get_host_info(host) | ||
| 112 | self._connection = host, HTTPS( | ||
| 113 | chost, | ||
| 114 | None, | ||
| 115 | custom_ssl_options=self.custom_ssl_options, | ||
| 116 | **(x509 or {})) | ||
| 117 | return self._connection[1] | ||
| 85 | 118 | ||
| 86 | 119 | ||
| 87 | def display_notification(args, title, message): | 120 | def display_notification(args, title, message): |
| @@ -141,23 +174,35 @@ def poll_notifications(scheduler, args): | |||
| 141 | args: the argparse processed command-line arguments | 174 | args: the argparse processed command-line arguments |
| 142 | """ | 175 | """ |
| 143 | try: | 176 | try: |
| 144 | post_values = {'password': args.password} | 177 | ssl_version = BEINC_SSL_METHODS.get(args.ssl_version, |
| 145 | data = urllib.urlencode(post_values) | 178 | ssl.PROTOCOL_SSLv23) |
| 146 | req = urllib2.Request(args.url, data) | 179 | if sys.hexversion >= 0x20709f0: |
| 147 | if args.cert: # check for cert validity | 180 | # Python >= 2.7.9 |
| 148 | global global_beinc_cert_file # ugly hack | 181 | context = ssl.SSLContext(ssl_version) |
| 149 | global_beinc_cert_file = args.cert | 182 | context.verify_mode = ssl.CERT_REQUIRED |
| 150 | opener = urllib2.build_opener(ValidHTTPSHandler) | 183 | if args.no_cert_validate: |
| 151 | response = opener.open(req) | 184 | context.verify_mode = ssl.CERT_NONE |
| 152 | else: # ... or don't | 185 | context.check_hostname = bool(not args.disable_hostname_check) |
| 153 | response = urllib2.urlopen(req) | 186 | if args.cert and not args.no_cert_validate: |
| 154 | res_code = response.code | 187 | context.load_verify_locations(os.path.expanduser(args.cert)) |
| 155 | res_str = response.read() | 188 | if args.ciphers: |
| 156 | if res_code == 200 and args.debug: | 189 | context.set_ciphers(args.ciphers) |
| 157 | print('Server responded: OK') | 190 | transport = xmlrpclib.SafeTransport(context=context) |
| 158 | print('Body:\n{0}'.format(res_str)) | 191 | else: |
| 159 | response.close() | 192 | # Python < 2.7.9 |
| 160 | res_list = json.loads(res_str) | 193 | ssl_options = {} |
| 194 | ssl_options['ssl_version'] = ssl_version | ||
| 195 | if args.cert and not args.no_cert_validate: | ||
| 196 | ssl_options['ca_certs'] = os.path.expanduser(args.cert) | ||
| 197 | if not args.no_cert_validate: | ||
| 198 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED | ||
| 199 | if args.ciphers: | ||
| 200 | ssl_options['ciphers'] = args.ciphers | ||
| 201 | transport = BEINCCustomSafeTransport( | ||
| 202 | custom_ssl_options=ssl_options) | ||
| 203 | server = xmlrpclib.ServerProxy(args.url, | ||
| 204 | transport=transport) | ||
| 205 | res_list = server.pull(args.rname, args.password) | ||
| 161 | for entry in res_list: | 206 | for entry in res_list: |
| 162 | title = entry.get('title', '') | 207 | title = entry.get('title', '') |
| 163 | message = entry.get('message', '') | 208 | message = entry.get('message', '') |
| @@ -166,11 +211,16 @@ def poll_notifications(scheduler, args): | |||
| 166 | 1, | 211 | 1, |
| 167 | poll_notifications, | 212 | poll_notifications, |
| 168 | (scheduler, args)) | 213 | (scheduler, args)) |
| 169 | except urllib2.HTTPError as e: | 214 | except xmlrpclib.Fault as fault: |
| 170 | sys.stderr.write('BEINC-server error ({0} - {1})\n'.format(e.code, | 215 | sys.stderr.write( |
| 171 | e.reason)) | 216 | 'BEINC server answered with errorCode={0}: {1}\n'.format( |
| 217 | fault.faultCode, | ||
| 218 | fault.faultString)) | ||
| 219 | except ssl.SSLError as e: | ||
| 220 | sys.stderr.write('BEINC SSL/TLS error: {0}\n'.format(e)) | ||
| 221 | sys.exit(errno.EPERM) | ||
| 172 | except Exception as e: | 222 | except Exception as e: |
| 173 | sys.stderr.write('BEINC-poller error: {0}\nTerminating...'.format(e)) | 223 | sys.stderr.write('BEINC generic client error: {0}\n'.format(e)) |
| 174 | sys.exit(errno.EPERM) | 224 | sys.exit(errno.EPERM) |
| 175 | 225 | ||
| 176 | 226 | ||
| @@ -181,21 +231,14 @@ def main(): | |||
| 181 | 'url', | 231 | 'url', |
| 182 | metavar='URL', | 232 | metavar='URL', |
| 183 | type=str, | 233 | type=str, |
| 184 | help='BEINC destination URL') | 234 | help='BEINC server destination URL') |
| 185 | parser.add_argument( | 235 | parser.add_argument( |
| 186 | '-a', '--align', | 236 | '-a', '--align', |
| 187 | metavar='ALIGNMENT', | 237 | metavar='ALIGNMENT', |
| 188 | type=str, | 238 | type=str, |
| 189 | dest='alignment', | 239 | dest='alignment', |
| 190 | default='left', | 240 | default='left', |
| 191 | help='Alignment for "pyosd" (default: "left")') | 241 | help='Alignment for "pyosd": "left" (default), "center", "right"') |
| 192 | parser.add_argument( | ||
| 193 | '-c', '--cert-file', | ||
| 194 | metavar='FILE', | ||
| 195 | type=str, | ||
| 196 | dest='cert', | ||
| 197 | default='', | ||
| 198 | help='BEINC CA-cert to check the server-cert against (default: None)') | ||
| 199 | parser.add_argument( | 242 | parser.add_argument( |
| 200 | '-C', '--color', | 243 | '-C', '--color', |
| 201 | metavar='COLOR', | 244 | metavar='COLOR', |
| @@ -204,18 +247,27 @@ def main(): | |||
| 204 | default='blue', | 247 | default='blue', |
| 205 | help='Color for "pyosd" (default: "blue")') | 248 | help='Color for "pyosd" (default: "blue")') |
| 206 | parser.add_argument( | 249 | parser.add_argument( |
| 207 | '-d', '--debug', | 250 | '-c', '--cert-file', |
| 208 | action='store_true', | 251 | metavar='FILE', |
| 209 | dest='debug', | 252 | type=str, |
| 210 | default=False, | 253 | dest='cert', |
| 211 | help='Run the poller-process in debug-mode') | 254 | default='', |
| 255 | help='BEINC CA-cert to check the server-cert against ' | ||
| 256 | '(default: Check disabled)') | ||
| 212 | parser.add_argument( | 257 | parser.add_argument( |
| 213 | '-f', '--frequency', | 258 | '--ciphers', |
| 214 | metavar='SECONDS', | 259 | metavar='CIPHERS', |
| 215 | type=int, | 260 | type=str, |
| 216 | dest='frequency', | 261 | dest='ciphers', |
| 217 | default=10, | 262 | default='', |
| 218 | help='Polling frequency in seconds (default: 10)') | 263 | help='Preferred ciphers list (default: auto)') |
| 264 | if sys.hexversion >= 0x20709f0: | ||
| 265 | parser.add_argument( | ||
| 266 | '--disable-hostname-check', | ||
| 267 | action='store_true', | ||
| 268 | dest='disable_hostname_check', | ||
| 269 | default=False, | ||
| 270 | help='Do not check whether server cert matches server hostname') | ||
| 219 | parser.add_argument( | 271 | parser.add_argument( |
| 220 | '--font', | 272 | '--font', |
| 221 | metavar='FONT', | 273 | metavar='FONT', |
| @@ -224,6 +276,13 @@ def main(): | |||
| 224 | default=None, | 276 | default=None, |
| 225 | help='Custom font for "pyosd" (default: Default font)') | 277 | help='Custom font for "pyosd" (default: Default font)') |
| 226 | parser.add_argument( | 278 | parser.add_argument( |
| 279 | '-f', '--frequency', | ||
| 280 | metavar='SECONDS', | ||
| 281 | type=int, | ||
| 282 | dest='frequency', | ||
| 283 | default=10, | ||
| 284 | help='Polling frequency in seconds (default: 10)') | ||
| 285 | parser.add_argument( | ||
| 227 | '--h-offset', | 286 | '--h-offset', |
| 228 | metavar='OFFSET', | 287 | metavar='OFFSET', |
| 229 | type=int, | 288 | type=int, |
| @@ -231,14 +290,27 @@ def main(): | |||
| 231 | default=30, | 290 | default=30, |
| 232 | help='Horizontal offset for "pyosd" (default: 30)') | 291 | help='Horizontal offset for "pyosd" (default: 30)') |
| 233 | parser.add_argument( | 292 | parser.add_argument( |
| 293 | '-n', '--resource-name', | ||
| 294 | metavar='NAME', | ||
| 295 | type=str, | ||
| 296 | dest='rname', | ||
| 297 | required=True, | ||
| 298 | help='The name of the BEINC-resource on the remote server') | ||
| 299 | parser.add_argument( | ||
| 300 | '--no-cert-validate', | ||
| 301 | action='store_true', | ||
| 302 | dest='no_cert_validate', | ||
| 303 | default=False, | ||
| 304 | help='Do not validate server certificate') | ||
| 305 | parser.add_argument( | ||
| 234 | '-o', '--osd-system', | 306 | '-o', '--osd-system', |
| 235 | metavar='SYSTEM', | 307 | metavar='SYSTEM', |
| 236 | type=str, | 308 | type=str, |
| 237 | dest='osd_sys', | 309 | dest='osd_sys', |
| 238 | default='pynotify', | 310 | default='pynotify', |
| 239 | help='BEINC osd-system ("pynotify" or "pyosd") (default: pynotify)') | 311 | help='BEINC osd-system: "pynotify" (default), "pyosd"') |
| 240 | parser.add_argument( | 312 | parser.add_argument( |
| 241 | '-P', '--password', | 313 | '-p', '--password', |
| 242 | metavar='PASSWORD[FILE]', | 314 | metavar='PASSWORD[FILE]', |
| 243 | type=str, | 315 | type=str, |
| 244 | dest='password', | 316 | dest='password', |
| @@ -246,12 +318,29 @@ def main(): | |||
| 246 | help='BEINC taget-password / text-file containing the target password' | 318 | help='BEINC taget-password / text-file containing the target password' |
| 247 | ' (default & recommended: prompt for passwd)') | 319 | ' (default & recommended: prompt for passwd)') |
| 248 | parser.add_argument( | 320 | parser.add_argument( |
| 249 | '-p', '--position', | 321 | '-P', '--position', |
| 250 | metavar='POSITION', | 322 | metavar='POSITION', |
| 251 | type=str, | 323 | type=str, |
| 252 | dest='position', | 324 | dest='position', |
| 253 | default='bottom', | 325 | default='bottom', |
| 254 | help='Position for "pyosd" (default: "bottom")') | 326 | help='Position for "pyosd": "top", "middle", "bottom" (default)') |
| 327 | if sys.hexversion >= 0x20709f0: | ||
| 328 | parser.add_argument( | ||
| 329 | '-s', '--ssl-version', | ||
| 330 | metavar='VERSION', | ||
| 331 | type=str, | ||
| 332 | dest='ssl_version', | ||
| 333 | default='auto', | ||
| 334 | help='Use SSL version: "auto" (default), ' | ||
| 335 | '"SSLv3", "TLSv1", "TLSv1_1", "TLSv1_2"') | ||
| 336 | else: | ||
| 337 | parser.add_argument( | ||
| 338 | '-s', '--ssl-version', | ||
| 339 | metavar='VERSION', | ||
| 340 | type=str, | ||
| 341 | dest='ssl_version', | ||
| 342 | default='auto', | ||
| 343 | help='Use SSL version: "auto" (default), "SSLv3", "TLSv1"') | ||
| 255 | parser.add_argument( | 344 | parser.add_argument( |
| 256 | '-t', '--osd-timeout', | 345 | '-t', '--osd-timeout', |
| 257 | metavar='SECONDS', | 346 | metavar='SECONDS', |
