2019-12-23 07:51:24 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-05-09 05:27:15 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
Upload blocks in the upload queue to peers from the communicator
|
|
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING
|
2020-01-30 06:54:43 +00:00
|
|
|
from time import sleep
|
|
|
|
from threading import Thread
|
2020-10-21 09:46:05 +00:00
|
|
|
from secrets import SystemRandom
|
2020-01-30 06:54:43 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
from . import sessionmanager
|
|
|
|
|
|
|
|
from onionrtypes import UserID
|
|
|
|
import logger
|
|
|
|
from communicatorutils import proxypicker
|
|
|
|
import onionrexceptions
|
|
|
|
from onionrblocks import onionrblockapi as block
|
2020-11-16 06:57:38 +00:00
|
|
|
from onionrblocks.blockmetadata.fromdata import get_block_metadata_from_data
|
2019-12-23 07:51:24 +00:00
|
|
|
from onionrutils import stringvalidators, basicrequests
|
2020-11-16 06:57:38 +00:00
|
|
|
from onionrutils.validatemetadata import validate_metadata
|
2019-12-23 07:51:24 +00:00
|
|
|
from communicator import onlinepeers
|
|
|
|
if TYPE_CHECKING:
|
2020-07-26 03:28:32 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-12-23 07:51:24 +00:00
|
|
|
from communicator import OnionrCommunicatorDaemon
|
|
|
|
"""
|
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-12-23 07:51:24 +00:00
|
|
|
"""
|
|
|
|
|
2019-09-16 07:50:13 +00:00
|
|
|
|
2020-11-16 06:57:38 +00:00
|
|
|
def upload_blocks_from_communicator(shared_state: 'OnionrCommunicatorDaemon'):
|
2019-12-23 07:51:24 +00:00
|
|
|
"""Accept a communicator instance + upload blocks from its upload queue."""
|
2019-09-17 01:16:06 +00:00
|
|
|
"""when inserting a block, we try to upload
|
|
|
|
it to a few peers to add some deniability & increase functionality"""
|
2020-11-16 06:57:38 +00:00
|
|
|
kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
|
2019-07-11 18:26:57 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
session_manager: sessionmanager.BlockUploadSessionManager
|
2020-11-16 06:57:38 +00:00
|
|
|
session_manager = shared_state.get(
|
2019-12-23 07:51:24 +00:00
|
|
|
sessionmanager.BlockUploadSessionManager)
|
|
|
|
tried_peers: UserID = []
|
2019-05-09 05:27:15 +00:00
|
|
|
finishedUploads = []
|
2020-10-21 09:46:05 +00:00
|
|
|
|
|
|
|
SystemRandom().shuffle(kv.get('blocksToUpload'))
|
2019-12-23 07:51:24 +00:00
|
|
|
|
2020-01-30 06:54:43 +00:00
|
|
|
def remove_from_hidden(bl):
|
|
|
|
sleep(60)
|
|
|
|
try:
|
2020-11-16 06:57:38 +00:00
|
|
|
shared_state.get_by_string(
|
2020-01-30 06:54:43 +00:00
|
|
|
'PublicAPI').hideBlocks.remove(bl)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2020-07-27 00:02:39 +00:00
|
|
|
if len(kv.get('blocksToUpload')) != 0:
|
|
|
|
for bl in kv.get('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)
|
2019-05-09 05:27:15 +00:00
|
|
|
return
|
2019-09-17 01:16:06 +00:00
|
|
|
session = session_manager.add_session(bl)
|
2020-07-26 03:28:32 +00:00
|
|
|
for _ in range(min(len(kv.get('onlinePeers')), 6)):
|
2019-12-20 07:24:38 +00:00
|
|
|
try:
|
2020-10-10 08:49:33 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(kv)
|
2020-10-10 08:43:20 +00:00
|
|
|
if not block.Block(bl).isEncrypted:
|
2020-10-10 08:41:55 +00:00
|
|
|
if peer in kv.get('plaintextDisabledPeers'):
|
|
|
|
logger.info(f"Cannot upload plaintext block to peer that denies it {peer}") # noqa
|
|
|
|
continue
|
2019-12-20 07:24:38 +00:00
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
|
|
|
continue
|
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:
|
2019-12-23 07:51:24 +00:00
|
|
|
if session.peer_fails[peer] > 3:
|
|
|
|
continue
|
2019-09-17 01:16:06 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-12-23 07:51:24 +00:00
|
|
|
if peer in tried_peers:
|
|
|
|
continue
|
|
|
|
tried_peers.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()
|
2020-10-15 03:56:43 +00:00
|
|
|
if not data:
|
|
|
|
logger.warn(
|
2020-11-16 06:57:38 +00:00
|
|
|
f"Couldn't get data for block in upload list {bl}",
|
2020-10-15 03:56:43 +00:00
|
|
|
terminal=True)
|
|
|
|
raise onionrexceptions.NoDataAvailable
|
2020-11-16 06:57:38 +00:00
|
|
|
try:
|
|
|
|
def __check_metadata():
|
|
|
|
metadata = get_block_metadata_from_data(data)[0]
|
|
|
|
if not validate_metadata(metadata, data):
|
|
|
|
logger.warn(
|
|
|
|
f"Metadata for uploading block not valid {bl}")
|
|
|
|
raise onionrexceptions.InvalidMetadata
|
|
|
|
__check_metadata()
|
|
|
|
except onionrexceptions.DataExists:
|
|
|
|
pass
|
|
|
|
except( # noqa
|
|
|
|
onionrexceptions.NoDataAvailable,
|
|
|
|
onionrexceptions.InvalidMetadata) as _:
|
2019-08-18 20:10:28 +00:00
|
|
|
finishedUploads.append(bl)
|
|
|
|
break
|
2019-12-23 07:51:24 +00:00
|
|
|
proxy_type = proxypicker.pick_proxy(peer)
|
|
|
|
logger.info(
|
|
|
|
f"Uploading block {bl[:8]} to {peer}", terminal=True)
|
|
|
|
resp = basicrequests.do_post_request(
|
|
|
|
url, data=data, proxyType=proxy_type,
|
|
|
|
content_type='application/octet-stream')
|
|
|
|
if resp is not False:
|
2019-08-12 04:00:08 +00:00
|
|
|
if resp == 'success':
|
2020-01-30 06:54:43 +00:00
|
|
|
Thread(target=remove_from_hidden,
|
|
|
|
args=[bl], daemon=True).start()
|
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)
|
2020-11-16 06:57:38 +00:00
|
|
|
shared_state.get_by_string(
|
|
|
|
'OnionrCommunicatorDaemon').getPeerProfileInstance(
|
|
|
|
peer).addScore(-5)
|
2019-12-23 07:51:24 +00:00
|
|
|
logger.warn(
|
2020-01-20 03:10:01 +00:00
|
|
|
f'Failed to upload {bl[:8]}, reason: {resp}',
|
|
|
|
terminal=True)
|
2019-09-17 01:16:06 +00:00
|
|
|
else:
|
|
|
|
session.fail()
|
|
|
|
session_manager.clean_session()
|
2019-05-09 05:27:15 +00:00
|
|
|
for x in finishedUploads:
|
|
|
|
try:
|
2020-07-27 00:02:39 +00:00
|
|
|
kv.get('blocksToUpload').remove(x)
|
2020-01-30 06:54:43 +00:00
|
|
|
|
2020-11-16 06:57:38 +00:00
|
|
|
shared_state.get_by_string(
|
2020-01-30 06:54:43 +00:00
|
|
|
'PublicAPI').hideBlocks.remove(x)
|
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
except ValueError:
|
2020-11-16 06:57:38 +00:00
|
|
|
pass
|