Exemplo n.º 1
0
 def test_valid_merge(self):
     adders = 'facebookcorewwwi.onion,mporbyyjhmz2c62shctbi3ngrslne5lpcyav6uzhxok45iblodhgjoad.onion'
     networkmerger.mergeAdders(adders)
     added = keydb.listkeys.list_adders()
     self.assertIn(
         'mporbyyjhmz2c62shctbi3ngrslne5lpcyav6uzhxok45iblodhgjoad.onion',
         added)
     self.assertNotIn('inwalidkcorewwi.onion', added)
     self.assertIn('facebookcorewwwi.onion', added)
Exemplo n.º 2
0
 def test_invalid_mergeself(self):
     adders = 'facebookc0rewwi.onion,sdfsdfsdf.onion, ssdf324, null, \n'
     networkmerger.mergeAdders(adders)
     added = keydb.listkeys.list_adders()
     for adder in adders:
         self.assertNotIn(adder, added)
Exemplo n.º 3
0
def connect_new_peer_to_communicator(shared_state,
                                     peer='',
                                     useBootstrap=False):
    retData = False
    kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
    tried = kv.get('offlinePeers')
    transports = gettransports.get()
    if peer != '':
        if stringvalidators.validate_transport(peer):
            peerList = [peer]
        else:
            raise onionrexceptions.InvalidAddress(
                'Will not attempt connection test to invalid address')
    else:
        peerList = keydb.listkeys.list_adders()

    mainPeerList = keydb.listkeys.list_adders()
    peerList = onionrpeers.get_score_sorted_peer_list()
    """
    If we don't have enough peers connected or random chance,
    select new peers to try
    """
    if len(peerList) < 8 or secrets.randbelow(4) == 3:
        tryingNew = []
        for x in kv.get('newPeers'):
            if x not in peerList:
                peerList.append(x)
                tryingNew.append(x)
        for i in tryingNew:
            kv.get('newPeers').remove(i)

    if len(peerList) == 0 or useBootstrap:
        # Avoid duplicating bootstrap addresses in peerList
        if config.get('general.use_bootstrap_list', True):
            bootstrappeers.add_bootstrap_list_to_peer_list(kv, peerList)

    for address in peerList:
        address = address.strip()

        # Don't connect to our own address
        if address in transports:
            continue
        """Don't connect to invalid address or
        if its already been tried/connected, or if its cooled down
        """
        if len(address) == 0 or address in tried \
            or address in kv.get('onlinePeers') \
                or address in kv.get('cooldownPeer'):
            continue
        if kv.get('shutdown'):
            return
        # Ping a peer,
        ret = peeraction.peer_action(shared_state, address, 'ping')
        if ret == 'pong!':
            time.sleep(0.1)
            if address not in mainPeerList:
                # Add a peer to our list if it isn't already since it connected
                networkmerger.mergeAdders(address)
            if address not in kv.get('onlinePeers'):
                logger.info('Connected to ' + address, terminal=True)
                kv.get('onlinePeers').append(address)
                kv.get('connectTimes')[address] = epoch.get_epoch()
            retData = address

            # add peer to profile list if they're not in it
            for profile in kv.get('peerProfiles'):
                if profile.address == address:
                    break
            else:
                kv.get('peerProfiles').append(
                    onionrpeers.PeerProfiles(address))
            break
        else:
            # Mark a peer as tried if they failed to respond to ping
            tried.append(address)
            logger.debug('Failed to connect to %s: %s ' % (address, ret))
    return retData
Exemplo n.º 4
0
def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False):
    config = comm_inst._core.config
    retData = False
    tried = comm_inst.offlinePeers
    if peer != '':
        if comm_inst._core._utils.validateID(peer):
            peerList = [peer]
        else:
            raise onionrexceptions.InvalidAddress(
                'Will not attempt connection test to invalid address')
    else:
        peerList = comm_inst._core.listAdders()

    mainPeerList = comm_inst._core.listAdders()
    peerList = onionrpeers.getScoreSortedPeerList(comm_inst._core)

    # If we don't have enough peers connected or random chance, select new peers to try
    if len(peerList) < 8 or secrets.randbelow(4) == 3:
        tryingNew = []
        for x in comm_inst.newPeers:
            if x not in peerList:
                peerList.append(x)
                tryingNew.append(x)
        for i in tryingNew:
            comm_inst.newPeers.remove(i)

    if len(peerList) == 0 or useBootstrap:
        # Avoid duplicating bootstrap addresses in peerList
        comm_inst.addBootstrapListToPeerList(peerList)

    for address in peerList:
        if not config.get('tor.v3onions') and len(address) == 62:
            continue
        # Don't connect to our own address
        if address == comm_inst._core.hsAddress:
            continue
        # Don't connect to invalid address or if its already been tried/connected, or if its cooled down
        if len(
                address
        ) == 0 or address in tried or address in comm_inst.onlinePeers or address in comm_inst.cooldownPeer:
            continue
        if comm_inst.shutdown:
            return
        # Ping a peer,
        if comm_inst.peerAction(address, 'ping') == 'pong!':
            time.sleep(0.1)
            if address not in mainPeerList:
                # Add a peer to our list if it isn't already since it successfully connected
                networkmerger.mergeAdders(address, comm_inst._core)
            if address not in comm_inst.onlinePeers:
                logger.info('Connected to ' + address)
                comm_inst.onlinePeers.append(address)
                comm_inst.connectTimes[
                    address] = comm_inst._core._utils.getEpoch()
            retData = address

            # add peer to profile list if they're not in it
            for profile in comm_inst.peerProfiles:
                if profile.address == address:
                    break
            else:
                comm_inst.peerProfiles.append(
                    onionrpeers.PeerProfiles(address, comm_inst._core))
            break
        else:
            # Mark a peer as tried if they failed to respond to ping
            tried.append(address)
            logger.debug('Failed to connect to ' + address)
    return retData