def buildCPoW(): if bmpow is not None: return if paths.frozen is not None: notifyBuild(False) return if sys.platform in ["win32", "win64"]: notifyBuild(False) return try: if "bsd" in sys.platform: # BSD make call([ "make", "-C", os.path.join(paths.codePath(), "bitmsghash"), '-f', 'Makefile.bsd' ]) else: # GNU make call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash")]) if os.path.exists( os.path.join(paths.codePath(), "bitmsghash", "bitmsghash.so")): init() notifyBuild(True) else: notifyBuild(True) except: notifyBuild(True)
def init(): """Initialise PoW""" # pylint: disable=global-statement global bitmsglib, bmpow openclpow.initCL() if sys.platform == "win32": if ctypes.sizeof(ctypes.c_voidp) == 4: bitmsglib = 'bitmsghash32.dll' else: bitmsglib = 'bitmsghash64.dll' try: # MSVS bso = ctypes.WinDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (stdcall) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (stdcall) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) try: # MinGW bso = ctypes.CDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (cdecl) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (cdecl) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) bso = None else: try: bso = ctypes.CDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) except OSError: import glob try: bso = ctypes.CDLL(glob.glob(os.path.join( paths.codePath(), "bitmsghash", "bitmsghash*.so" ))[0]) except (OSError, IndexError): bso = None except: bso = None else: logger.info("Loaded C PoW DLL %s", bitmsglib) if bso: try: bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong except: bmpow = None else: bmpow = None if bmpow is None: buildCPoW()
def init(): global bitmsglib, bso, bmpow openclpow.initCL() if "win32" == sys.platform: if ctypes.sizeof(ctypes.c_voidp) == 4: bitmsglib = 'bitmsghash32.dll' else: bitmsglib = 'bitmsghash64.dll' try: # MSVS bso = ctypes.WinDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (stdcall) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (stdcall) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) try: # MinGW bso = ctypes.CDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (cdecl) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (cdecl) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) bso = None else: try: bso = ctypes.CDLL(os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) except OSError: import glob try: bso = ctypes.CDLL(glob.glob(os.path.join( paths.codePath(), "bitmsghash", "bitmsghash*.so" ))[0]) except (OSError, IndexError): bso = None except: bso = None else: logger.info("Loaded C PoW DLL %s", bitmsglib) if bso: try: bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong except: bmpow = None else: bmpow = None if bmpow is None: buildCPoW()
def initCL(): """Initlialise OpenCL engine""" # pylint: disable=global-statement global ctx, queue, program, hash_dt, libAvailable if libAvailable is False: return del enabledGpus[:] del vendors[:] del gpus[:] ctx = False try: hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) try: for platform in cl.get_platforms(): gpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) if BMConfigParser().safeGet("bitmessagesettings", "opencl") == platform.vendor: enabledGpus.extend(platform.get_devices( device_type=cl.device_type.GPU)) if platform.vendor not in vendors: vendors.append(platform.vendor) except: pass if enabledGpus: ctx = cl.Context(devices=enabledGpus) queue = cl.CommandQueue(ctx) f = open(os.path.join(paths.codePath(), "bitmsghash", 'bitmsghash.cl'), 'r') fstr = ''.join(f.readlines()) program = cl.Program(ctx, fstr).build(options="") logger.info("Loaded OpenCL kernel") else: logger.info("No OpenCL GPUs found") del enabledGpus[:] except Exception: logger.error("OpenCL fail: ", exc_info=True) del enabledGpus[:]
def initCL(): global ctx, queue, program, hash_dt, libAvailable if libAvailable is False: return del enabledGpus[:] del vendors[:] del gpus[:] ctx = False try: hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) try: for platform in cl.get_platforms(): gpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) if BMConfigParser().safeGet("bitmessagesettings", "opencl") == platform.vendor: enabledGpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) if platform.vendor not in vendors: vendors.append(platform.vendor) except: pass if (len(enabledGpus) > 0): ctx = cl.Context(devices=enabledGpus) queue = cl.CommandQueue(ctx) f = open(os.path.join(paths.codePath(), "bitmsghash", 'bitmsghash.cl'), 'r') fstr = ''.join(f.readlines()) program = cl.Program(ctx, fstr).build(options="") logger.info("Loaded OpenCL kernel") else: logger.info("No OpenCL GPUs found") del enabledGpus[:] except Exception as e: logger.error("OpenCL fail: ", exc_info=True) del enabledGpus[:]
def populate(self): self.languages = [] self.clear() localesPath = os.path.join(paths.codePath(), 'translations') configuredLocale = "system" try: configuredLocale = BMConfigParser().get('bitmessagesettings', 'userlocale', "system") except: pass self.addItem( QtGui.QApplication.translate("settingsDialog", "System Settings", "system"), "system") self.setCurrentIndex(0) self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) for translationFile in sorted( glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))): localeShort = os.path.split(translationFile)[1].split("_", 1)[1][:-3] locale = QtCore.QLocale(QtCore.QString(localeShort)) if localeShort in LanguageBox.languageName: self.addItem(LanguageBox.languageName[localeShort], localeShort) elif locale.nativeLanguageName() == "": self.addItem(localeShort, localeShort) else: self.addItem(locale.nativeLanguageName(), localeShort) for i in range(self.count()): if self.itemData(i) == configuredLocale: self.setCurrentIndex(i) break
def populate(self): """Populates drop down list with all available languages.""" self.clear() localesPath = os.path.join(paths.codePath(), 'translations') self.addItem(QtGui.QApplication.translate( "settingsDialog", "System Settings", "system"), "system") self.setCurrentIndex(0) self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) for translationFile in sorted( glob.glob(os.path.join(localesPath, "bitmessage_*.qm")) ): localeShort = \ os.path.split(translationFile)[1].split("_", 1)[1][:-3] if localeShort in LanguageBox.languageName: self.addItem( LanguageBox.languageName[localeShort], localeShort) else: locale = QtCore.QLocale(localeShort) self.addItem( locale.nativeLanguageName() or localeShort, localeShort) configuredLocale = BMConfigParser().safeGet( 'bitmessagesettings', 'userlocale', "system") for i in range(self.count()): if self.itemData(i) == configuredLocale: self.setCurrentIndex(i) break
def populate(self): self.languages = [] self.clear() localesPath = os.path.join (paths.codePath(), 'translations') configuredLocale = "system" try: configuredLocale = BMConfigParser().get('bitmessagesettings', 'userlocale', "system") except: pass self.addItem(QtGui.QApplication.translate("settingsDialog", "System Settings", "system"), "system") self.setCurrentIndex(0) self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) for translationFile in sorted(glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))): localeShort = os.path.split(translationFile)[1].split("_", 1)[1][:-3] locale = QtCore.QLocale(QtCore.QString(localeShort)) if localeShort in LanguageBox.languageName: self.addItem(LanguageBox.languageName[localeShort], localeShort) elif locale.nativeLanguageName() == "": self.addItem(localeShort, localeShort) else: self.addItem(locale.nativeLanguageName(), localeShort) for i in range(self.count()): if self.itemData(i) == configuredLocale: self.setCurrentIndex(i) break
def init(): global bitmsglib, bso, bmpow if "win32" == sys.platform: if ctypes.sizeof(ctypes.c_voidp) == 4: bitmsglib = 'bitmsghash32.dll' else: bitmsglib = 'bitmsghash64.dll' try: # MSVS bso = ctypes.WinDLL( os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (stdcall) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (stdcall) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) try: # MinGW bso = ctypes.CDLL( os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL (cdecl) %s", bitmsglib) bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong _doCPoW(2**63, "") logger.info("Successfully tested C PoW DLL (cdecl) %s", bitmsglib) except: logger.error("C PoW test fail.", exc_info=True) bso = None else: try: bso = ctypes.CDLL( os.path.join(paths.codePath(), "bitmsghash", bitmsglib)) logger.info("Loaded C PoW DLL %s", bitmsglib) except: bso = None if bso: try: bmpow = bso.BitmessagePOW bmpow.restype = ctypes.c_ulonglong except: bmpow = None else: bmpow = None
def sslHandshake(self): self.sslSock = self.sock if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and protocol.haveSSL(not self.initiatedConnection)): logger.debug("Initialising TLS") if sys.version_info >= (2,7,9): context = ssl.SSLContext(protocol.sslProtocolVersion) context.set_ciphers(protocol.sslProtocolCiphers) context.set_ecdh_curve("secp256k1") context.check_hostname = False context.verify_mode = ssl.CERT_NONE # also exclude TLSv1 and TLSv1.1 in the future context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE self.sslSock = context.wrap_socket(self.sock, server_side = not self.initiatedConnection, do_handshake_on_connect=False) else: self.sslSock = ssl.wrap_socket(self.sock, keyfile = os.path.join(paths.codePath(), 'sslkeys', 'key.pem'), certfile = os.path.join(paths.codePath(), 'sslkeys', 'cert.pem'), server_side = not self.initiatedConnection, ssl_version=protocol.sslProtocolVersion, do_handshake_on_connect=False, ciphers=protocol.sslProtocolCiphers) self.sendDataThreadQueue.join() while True: try: self.sslSock.do_handshake() logger.debug("TLS handshake success") if sys.version_info >= (2, 7, 9): logger.debug("TLS protocol version: %s", self.sslSock.version()) break except ssl.SSLError as e: if sys.hexversion >= 0x02070900: if isinstance (e, ssl.SSLWantReadError): logger.debug("Waiting for SSL socket handhake read") select.select([self.sslSock], [], [], 10) continue elif isinstance (e, ssl.SSLWantWriteError): logger.debug("Waiting for SSL socket handhake write") select.select([], [self.sslSock], [], 10) continue else: if e.args[0] == ssl.SSL_ERROR_WANT_READ: logger.debug("Waiting for SSL socket handhake read") select.select([self.sslSock], [], [], 10) continue elif e.args[0] == ssl.SSL_ERROR_WANT_WRITE: logger.debug("Waiting for SSL socket handhake write") select.select([], [self.sslSock], [], 10) continue logger.error("SSL socket handhake failed: shutting down connection, %s", str(e)) self.sendDataThreadQueue.put((0, 'shutdown','tls handshake fail %s' % (str(e)))) return False except socket.error as err: logger.debug('SSL socket handshake failed, shutting down connection, %s', str(err)) self.sendDataThreadQueue.put((0, 'shutdown','tls handshake fail')) return False except Exception: logger.error("SSL socket handhake failed, shutting down connection", exc_info=True) self.sendDataThreadQueue.put((0, 'shutdown','tls handshake fail')) return False # SSL in the background should be blocking, otherwise the error handling is difficult self.sslSock.settimeout(None) return True # no SSL return True
def __init__(self, address=None, sock=None, certfile=None, keyfile=None, server_side=False, ciphers=protocol.sslProtocolCiphers): self.want_read = self.want_write = True if certfile is None: self.certfile = os.path.join(paths.codePath(), 'sslkeys', 'cert.pem') else: self.certfile = certfile if keyfile is None: self.keyfile = os.path.join(paths.codePath(), 'sslkeys', 'key.pem') else: self.keyfile = keyfile self.server_side = server_side self.ciphers = ciphers self.tlsStarted = False self.tlsDone = False self.tlsVersion = "N/A" self.isSSL = False
def __init__(self, address=None, sock=None, certfile=None, keyfile=None, server_side=False, ciphers=protocol.sslProtocolCiphers): self.want_read = self.want_write = True if certfile is None: self.certfile = os.path.join(paths.codePath(), 'sslkeys', 'cert.pem') else: self.certfile = certfile if keyfile is None: self.keyfile = os.path.join(paths.codePath(), 'sslkeys', 'key.pem') else: self.keyfile = keyfile self.server_side = server_side self.ciphers = ciphers self.tlsStarted = False self.tlsDone = False self.tlsVersion = "N/A" self.isSSL = False
def buildCPoW(): if bmpow is not None: return if paths.frozen is not None: notifyBuild(False) return if sys.platform in ["win32", "win64"]: notifyBuild(False) return try: if "bsd" in sys.platform: # BSD make call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash"), '-f', 'Makefile.bsd']) else: # GNU make call(["make", "-C", os.path.join(paths.codePath(), "bitmsghash")]) if os.path.exists(os.path.join(paths.codePath(), "bitmsghash", "bitmsghash.so")): init() notifyBuild(True) else: notifyBuild(True) except: notifyBuild(True)
def sslHandshake(self): logger.debug("Initialising TLS") if sys.version_info >= (2,7,9): context = ssl.SSLContext(protocol.sslProtocolVersion) context.set_ciphers(protocol.sslProtocolCiphers) context.set_ecdh_curve("secp256k1") context.check_hostname = False context.verify_mode = ssl.CERT_NONE # also exclude TLSv1 and TLSv1.1 in the future context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE self.sock = context.wrap_socket(self.sock, server_side = not self.initiatedConnection, do_handshake_on_connect=False) else: self.sock = ssl.wrap_socket(self.sock, keyfile = os.path.join(paths.codePath(), 'sslkeys', 'key.pem'), certfile = os.path.join(paths.codePath(), 'sslkeys', 'cert.pem'), server_side = not self.initiatedConnection, ssl_version=protocol.sslProtocolVersion, do_handshake_on_connect=False, ciphers=protocol.sslProtocolCiphers) self.sendDataThreadQueue.join() self.sock.do_handshake() cipher, definition, strength = self.sock.cipher() logger.debug("TLS handshake success - %s defined in %s using %d secret bits", cipher, definition, strength) if sys.version_info >= (2, 7, 9): logger.debug("TLS protocol version: %s", self.sock.version())
def resource_path(resFile): baseDir = paths.codePath() for subDir in ["ui", "bitmessageqt"]: if os.path.isdir(os.path.join(baseDir, subDir)) and os.path.isfile( os.path.join(baseDir, subDir, resFile)): return os.path.join(baseDir, subDir, resFile)
def sslHandshake(self): self.sslSock = self.sock if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and protocol.haveSSL(not self.initiatedConnection)): logger.debug("Initialising TLS") if sys.version_info >= (2, 7, 9): context = ssl.SSLContext(protocol.sslProtocolVersion) context.set_ciphers(protocol.sslProtocolCiphers) context.set_ecdh_curve("secp256k1") context.check_hostname = False context.verify_mode = ssl.CERT_NONE # also exclude TLSv1 and TLSv1.1 in the future context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE self.sslSock = context.wrap_socket( self.sock, server_side=not self.initiatedConnection, do_handshake_on_connect=False) else: self.sslSock = ssl.wrap_socket( self.sock, keyfile=os.path.join(paths.codePath(), 'sslkeys', 'key.pem'), certfile=os.path.join(paths.codePath(), 'sslkeys', 'cert.pem'), server_side=not self.initiatedConnection, ssl_version=protocol.sslProtocolVersion, do_handshake_on_connect=False, ciphers=protocol.sslProtocolCiphers) self.sendDataThreadQueue.join() while True: try: self.sslSock.do_handshake() logger.debug("TLS handshake success") break except ssl.SSLError as e: if sys.hexversion >= 0x02070900: if isinstance(e, ssl.SSLWantReadError): logger.debug( "Waiting for SSL socket handhake read") select.select([self.sslSock], [], [], 10) continue elif isinstance(e, ssl.SSLWantWriteError): logger.debug( "Waiting for SSL socket handhake write") select.select([], [self.sslSock], [], 10) continue else: if e.args[0] == ssl.SSL_ERROR_WANT_READ: logger.debug( "Waiting for SSL socket handhake read") select.select([self.sslSock], [], [], 10) continue elif e.args[0] == ssl.SSL_ERROR_WANT_WRITE: logger.debug( "Waiting for SSL socket handhake write") select.select([], [self.sslSock], [], 10) continue logger.error( "SSL socket handhake failed: %s, shutting down connection", str(e)) self.sendDataThreadQueue.put( (0, 'shutdown', 'tls handshake fail %s' % (str(e)))) return False except Exception: logger.error( "SSL socket handhake failed, shutting down connection", exc_info=True) self.sendDataThreadQueue.put( (0, 'shutdown', 'tls handshake fail')) return False # SSL in the background should be blocking, otherwise the error handling is difficult self.sslSock.settimeout(None) return True # no SSL return True