Пример #1
0
 def pull(self, hk):
     print('STORE HANDLER PULL', hk)
     t = self.store.get(hk)
     if t:
         try:
             pk_owner, pk_signature, pk_value = self.store.get(hk)
             revision, data = cdx.decode_bson_val(pk_value)
             # print(data, data[self.ON_PUSH_PEER_KEY])
             if self.ON_PUSH_PEER_KEY in data:
                 # self.dhf.pus
                 v = data[self.ON_PUSH_PEER_KEY]
                 host = v['h']
                 port = v['p']
                 print('\n \n \n \n \n + + @ SEND PEER TO BOOT', host, port)
                 for item in self.dhf.peers:
                     try:
                         peer_host = item['host']
                         peer_port = item['port']
                         key = {'ert': 'boot_to'}
                         value = {
                             'ert:boot_to': {
                                 'h': peer_host,
                                 'p': peer_port
                             }
                         }
                         self.dhf.direct_push_pubkey(host, port)
                         self.dhf.direct_push(key, value, host, port)
                         # self.dhf.boot_to(peer_host, peer_port)
                     except Exception as e:
                         print('ERROR bootstaraping peers to requester',
                               str(e))
         except Exception as e:
             print('ON PULL DECODING FAIL', str(e))
     return self.store.get(hk)
Пример #2
0
    def pull(self, owner: HashIO, key: dict) -> dict:
        print("PULL K", key)
        print("PULL O", owner.hex())
        print("PULL B", owner())
        guid_bin = owner()
        t = self.dht.pull_remote(key, guid=guid_bin)
        if not t:
            t = self.dht.pull_local(key, guid=guid_bin)
            if not t:
                return dict()

        guid, sign, val = t
        val_d = decode_bson_val(val)
        return val_d
Пример #3
0
    def pull(self, hk):
        # logger('STORE HANDLER PULL', hk)
        logger('LOCAL PULL DATA', hk)

        t = self.store.get(hk)
        if t:
            try:
                pk_owner, pk_signature, pk_value = self.store.get(hk)
                revision, data = cdx.decode_bson_val(pk_value)
                # # logger(data, data[self.ON_PUSH_PEER_KEY])
                self.on_pull_handle(hk, data)
                if self.ON_PUSH_GUID_KEY in data:
                    # logger('\n\n\n GUIDS REQUESTED \n\n\n')
                    v = data[self.ON_PUSH_GUID_KEY]
                    # logger(v)
                    return

                if self.ON_PUSH_PEER_KEY in data:
                    # self.dhf.pus
                    v = data[self.ON_PUSH_PEER_KEY]
                    host = v['h']
                    port = v['p']
                    # logger('\n \n \n \n \n + + @ SEND PEER TO BOOT', host, port)
                    for item in self.dhf.peers:
                        try:
                            peer_host = item['host']
                            peer_port = item['port']
                            key = {'ert': 'boot_to'}
                            value = {
                                'ert:boot_to': {
                                    'h': peer_host,
                                    'p': peer_port
                                }
                            }
                            self.dhf.direct_push_pubkey(host, port)
                            self.dhf.direct_push(key, value, host, port)
                            # self.dhf.boot_to(peer_host, peer_port)
                        except Exception as e:
                            # logger('ERROR bootstaraping peers to requester', str(e))
                            pass
                    return
                    #? todo return ?
            except Exception as e:
                # logger('ON PULL DECODING FAIL', str(e))
                pass
        if not t:
            self.on_pull_handle(hk, dict())
        return self.store.get(hk)
Пример #4
0
    def verify(self, own, sig, val):
        if own in self.pubkeys:
            hk_own = self.pubkeys[own]
            if hk_own in self.store:
                pk_o, pk_sig, pk_coded = self.pull(hk_own)
                pk_rev, pk_d = cdx.decode_bson_val(pk_coded)
                pk_der = pk_d['ert:pubkey']
                is_ok = cdx.verify_message(val, sig, pk_der)
                return is_ok
            else:
                print('HK PUBKEY NOT FOUND, STORE INTEGRITY ERR')
        else:

            # event handler here

            print('PUBKEY NOT FOUND, TRIGGER PULL HERE')
Пример #5
0
    def pull(self, k, hkey=None):
        item = self.dhf.pull_local(k, hk_hex=hkey)
        if not item:
            item = self.dhf.pull_remote(k, hk_hex=hkey)
        if item:
            print(item)
        guid_bin = self.owner.bin()
        t = self.dhf.pull_remote(k, guid=guid_bin, hk_hex=hkey)
        if not t:
            t = self.dhf.pull_local(k, guid=guid_bin, hk_hex=hkey)
            if not t:
                return dict()

        guid, sign, val = t
        rev, val_d = decode_bson_val(val)
        # print('VAL D', val_d)
        return val_d
Пример #6
0
    def push(self, key, val, signature, guid_owner):
        print('STORE HANDLER PUSH')
        print('HK', key)
        # sig_val = (signature, val)
        owner_signature_value = (guid_owner, signature, val)
        revision, data = cdx.decode_bson_val(val)

        if 'ert:pubkey' in data:
            pubkey_der = data['ert:pubkey']
            # print('PUBKEY DER', pubkey_der)
            is_ok = cdx.verify_message(val, signature, pubkey_der)
            print('PUBKEY RCV')
            if is_ok:
                print('SIGNATURE OK')
                if cdx.pub_der_guid_bts(pubkey_der) == guid_owner:
                    print('FROM HASH OK')
                    # todo may be check before all checks
                    if guid_owner not in self.pubkeys:
                        print('STORE PUB KEY')
                        # store only reference
                        self.pubkeys[guid_owner] = key
                        print('STORE IN DHT')
                        return self.store.__setitem__(key,
                                                      owner_signature_value)
                    else:
                        print('PUB KEY EXIST')
                        return
            else:
                print('CHECKS FAILED PUBKEY NOT STORED')
                return
        else:
            print('STORE RCV')
            print('guid_owner', guid_owner)
            if guid_owner in self.pubkeys:
                print('HAVE PUBKEY', guid_owner)
                hk_from_store = self.pubkeys[guid_owner]
                hk = hk_from_store
                if isinstance(hk_from_store, bytes):
                    hk = cdx.guid_bts_to_int(hk_from_store)
                elif isinstance(hk_from_store, int):
                    hk = hk_from_store
                else:
                    raise ValueError('HKEY REF PUBKEY ERROR')

                pk_owner, pk_signature, pk_value = self.pull(hk)
                # pk_value =
                pk_rev, pubkey_data = cdx.decode_bson_val(pk_value)
                if 'ert:pubkey' in pubkey_data:
                    print('ert:pubkey IN local DHT')
                    pubkey_der = pubkey_data['ert:pubkey']
                    val_ok = cdx.verify_message(val, signature, pubkey_der)
                    if val_ok:
                        print('VAL SIG OK STORE IN DHT')
                        # pk_rev, data = cdx.decode_bson_val(pk_value)
                        # if self.ON_PUSH_PEER_KEY in key:
                        print('\n\n\n +++')
                        self.on_pushed_ip4_peer(data, hk_int=key)
                        print('\n\n\n +++')
                        # event handler here
                        return self.store.__setitem__(key,
                                                      owner_signature_value)
                    else:
                        print('VAL SIG FAILED')
                else:
                    'ERR ert:pubkey not found'
            else:
                print('NO PUB KEY FOUND')
Пример #7
0
 def get_data_from_raw(raw_t):
     if len(raw_t) == 3:
         revision, data = cdx.decode_bson_val(raw_t[2])
         return data
     return None