2019-05-09 05:27:15 +00:00
|
|
|
'''
|
2019-06-12 06:44:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
Upload blocks in the upload queue to peers from the communicator
|
|
|
|
'''
|
2019-09-17 01:16:06 +00:00
|
|
|
from __future__ import annotations
|
2019-05-09 05:27:15 +00:00
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2019-09-17 01:16:06 +00:00
|
|
|
from typing import Union, TYPE_CHECKING
|
2019-05-09 05:27:15 +00:00
|
|
|
import logger
|
|
|
|
from communicatorutils import proxypicker
|
2019-08-18 20:10:28 +00:00
|
|
|
import onionrexceptions
|
2019-09-21 23:49:24 +00:00
|
|
|
from onionrblocks import onionrblockapi as block
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import localcommand, stringvalidators, basicrequests
|
2019-07-10 22:38:20 +00:00
|
|
|
from communicator import onlinepeers
|
2019-07-22 05:24:42 +00:00
|
|
|
import onionrcrypto
|
2019-09-17 01:16:06 +00:00
|
|
|
from . import sessionmanager
|
2019-09-16 07:50:13 +00:00
|
|
|
|
2019-09-17 01:16:06 +00:00
|
|
|
def upload_blocks_from_communicator(comm_inst: OnionrCommunicatorDaemon):
|
|
|
|
"""Accepts a communicator instance and uploads blocks from its upload queue"""
|
|
|
|
"""when inserting a block, we try to upload
|
|
|
|
it to a few peers to add some deniability & increase functionality"""
|
2019-07-11 18:26:57 +00:00
|
|
|
TIMER_NAME = "upload_blocks_from_communicator"
|
|
|
|
|
2019-09-17 01:16:06 +00:00
|
|
|
session_manager: sessionmanager.BlockUploadSessionManager = comm_inst.shared_state.get(sessionmanager.BlockUploadSessionManager)
|
2019-05-09 05:27:15 +00:00
|
|
|
triedPeers = []
|
|
|
|
finishedUploads = []
|
2019-07-22 05:24:42 +00:00
|
|
|
comm_inst.blocksToUpload = onionrcrypto.cryptoutils.random_shuffle(comm_inst.blocksToUpload)
|
2019-05-09 05:27:15 +00:00
|
|
|
if len(comm_inst.blocksToUpload) != 0:
|
|
|
|
for bl in comm_inst.blocksToUpload:
|
2019-06-25 23:07:35 +00:00
|
|
|
if not stringvalidators.validate_hash(bl):
|
2019-07-11 18:26:57 +00:00
|
|
|
logger.warn('Requested to upload invalid block', terminal=True)
|
|
|
|
comm_inst.decrementThreadCount(TIMER_NAME)
|
2019-05-09 05:27:15 +00:00
|
|
|
return
|
2019-09-17 01:16:06 +00:00
|
|
|
session = session_manager.add_session(bl)
|
2019-05-09 05:27:15 +00:00
|
|
|
for i in range(min(len(comm_inst.onlinePeers), 6)):
|
2019-07-10 22:38:20 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(comm_inst)
|
2019-09-17 01:16:06 +00:00
|
|
|
try:
|
|
|
|
session.peer_exists[peer]
|
2019-05-09 05:27:15 +00:00
|
|
|
continue
|
2019-09-17 01:16:06 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if session.peer_fails[peer] > 3: continue
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if peer in triedPeers: continue
|
2019-05-09 05:27:15 +00:00
|
|
|
triedPeers.append(peer)
|
2019-09-17 01:16:06 +00:00
|
|
|
url = f'http://{peer}/upload'
|
2019-08-18 20:10:28 +00:00
|
|
|
try:
|
2019-09-12 15:54:36 +00:00
|
|
|
data = block.Block(bl).getRaw()
|
2019-08-18 20:10:28 +00:00
|
|
|
except onionrexceptions.NoDataAvailable:
|
|
|
|
finishedUploads.append(bl)
|
|
|
|
break
|
2019-05-09 05:27:15 +00:00
|
|
|
proxyType = proxypicker.pick_proxy(peer)
|
2019-09-17 01:16:06 +00:00
|
|
|
logger.info(f"Uploading block {bl[:8]} to {peer}", terminal=True)
|
2019-09-12 15:54:36 +00:00
|
|
|
resp = basicrequests.do_post_request(url, data=data, proxyType=proxyType, content_type='application/octet-stream')
|
2019-08-12 04:00:08 +00:00
|
|
|
if not resp == False:
|
|
|
|
if resp == 'success':
|
2019-09-17 01:16:06 +00:00
|
|
|
session.success()
|
|
|
|
session.peer_exists[peer] = True
|
2019-08-12 04:00:08 +00:00
|
|
|
elif resp == 'exists':
|
2019-09-17 01:16:06 +00:00
|
|
|
session.success()
|
|
|
|
session.peer_exists[peer] = True
|
2019-09-12 15:54:36 +00:00
|
|
|
else:
|
2019-09-17 01:16:06 +00:00
|
|
|
session.fail()
|
|
|
|
session.fail_peer(peer)
|
|
|
|
comm_inst.getPeerProfileInstance(peer).addScore(-5)
|
|
|
|
logger.warn(f'Failed to upload {bl[:8]}, reason: {resp[:15]}', terminal=True)
|
|
|
|
else:
|
|
|
|
session.fail()
|
|
|
|
session_manager.clean_session()
|
2019-05-09 05:27:15 +00:00
|
|
|
for x in finishedUploads:
|
|
|
|
try:
|
|
|
|
comm_inst.blocksToUpload.remove(x)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-08-12 04:00:08 +00:00
|
|
|
comm_inst.decrementThreadCount(TIMER_NAME)
|