def store(self, key, value, originalPublisherID=None, age=0, expireSeconds=KEY_EXPIRE_MAX_SECONDS, **kwargs): # TODO: add signature validation to be sure this is the owner of that key:value pair count('store_dht_service') if _Debug: lg.out( _DebugLevel, 'dht_service.DHTNode.store key=[%s] with %d bytes for %d seconds, counter=%d' % (strng.to_string(key, errors='ignore')[:6], len( str(value)), expireSeconds, counter('store'))) try: return super(DHTNode, self).store(key=key, value=value, originalPublisherID=originalPublisherID, age=age, expireSeconds=expireSeconds, **kwargs) except: lg.exc() return 'OK'
def _on_node_observe_failed(self, err, arg=None): try: self.failed += 1 if _Debug: lg.warn('%r : %s' % (arg, strng.to_string(repr(err), errors='ignore'))) except: lg.exc() return None
def sign(self, message, as_digits=True): if not self.keyObject: raise ValueError('key object is not exist') if not strng.is_bin(message): raise ValueError('message must be byte string') h = hashes.sha1(message, return_object=True) signature_bytes = pkcs1_15.new(self.keyObject).sign(h) if not as_digits: return signature_bytes signature_raw = strng.to_bin(strng.to_string(number.bytes_to_long(signature_bytes))) if signature_bytes[0:1] == b'\x00': signature_raw = b'0' + signature_raw return signature_raw
def request(self, key): count('request') if _Debug: lg.out(_DebugLevel, 'dht_service.DHTNode.request key=[%s]' % strng.to_string(key, errors='ignore')[:10]) internal_value = get_node_data(key) if internal_value is None and key in self._dataStore: value = self._dataStore[key] self.data[key] = value if _Debug: lg.out(_DebugLevel, ' found in _dataStore and saved as internal') else: value = internal_value if value is None: value = 0 if _Debug: lg.out(_DebugLevel, ' read internal value, counter=%d' % counter('request')) return {key: value, }
def UrlFilename(url): """ Generate a 'safe' filename from URL address. This is useful when need to store identity files on disk. nameurl.UrlFilename('http://id.bitdust.io/veselin.xml') 'http###id.bitdust.io#veselin.xml' """ if not url: return None result = strng.to_string(url) result = result.replace("://", "###") result = result.replace("/", "#") result = re.sub('(\:)(\d+)', '(#\g<2>#)', result) result = result.lower() # global legalset # # TODO: SECURITY Test that all characters are legal ones # for x in result: # if x not in legalset: # raise Exception("nameurl.UrlFilename ERROR illegal character: \n" + url) return result
def setVersion(self, version_string): """ """ self.version = strng.to_bin(strng.to_string(version_string).strip())