2019-12-23 07:51:24 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-07-10 22:38:20 +00:00
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
This file implements logic for performing requests to Onionr peers
|
|
|
|
"""
|
2020-07-31 01:15:36 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2019-12-23 07:51:24 +00:00
|
|
|
import streamedrequests
|
|
|
|
import logger
|
|
|
|
from onionrutils import epoch, basicrequests
|
|
|
|
from coredb import keydb
|
|
|
|
from . import onlinepeers
|
2020-07-31 01:15:36 +00:00
|
|
|
from onionrtypes import OnionAddressString
|
|
|
|
from onionrpeers.peerprofiles import PeerProfiles
|
2019-12-23 07:51:24 +00:00
|
|
|
"""
|
2019-07-10 22:38:20 +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-08-08 06:25:48 +00:00
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
def get_peer_profile(kv, address: OnionAddressString) -> 'PeerProfiles':
|
|
|
|
profile_inst_list = kv.get('peerProfiles')
|
|
|
|
for profile in profile_inst_list:
|
|
|
|
if profile.address == address:
|
|
|
|
return profile
|
|
|
|
p = PeerProfiles(address)
|
|
|
|
return p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def peer_action(shared_state, peer, action,
|
2019-12-23 07:51:24 +00:00
|
|
|
returnHeaders=False, max_resp_size=5242880):
|
|
|
|
"""Perform a get request to a peer."""
|
2019-07-10 22:38:20 +00:00
|
|
|
penalty_score = -10
|
2020-07-31 01:15:36 +00:00
|
|
|
kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
|
2019-08-07 23:14:13 +00:00
|
|
|
if len(peer) == 0:
|
2019-07-10 22:38:20 +00:00
|
|
|
return False
|
|
|
|
url = 'http://%s/%s' % (peer, action)
|
|
|
|
|
|
|
|
try:
|
2020-07-31 01:15:36 +00:00
|
|
|
ret_data = basicrequests.do_get_request(url, port=kv.get('proxyPort'),
|
2019-08-07 05:42:37 +00:00
|
|
|
max_size=max_resp_size)
|
2019-07-10 22:38:20 +00:00
|
|
|
except streamedrequests.exceptions.ResponseLimitReached:
|
2019-12-23 07:51:24 +00:00
|
|
|
logger.warn(
|
|
|
|
'Request failed due to max response size being overflowed',
|
|
|
|
terminal=True)
|
2019-08-07 05:42:37 +00:00
|
|
|
ret_data = False
|
2019-07-10 22:38:20 +00:00
|
|
|
penalty_score = -100
|
|
|
|
# if request failed, (error), mark peer offline
|
2019-12-23 07:51:24 +00:00
|
|
|
if ret_data is False:
|
2019-07-10 22:38:20 +00:00
|
|
|
try:
|
2020-07-31 01:15:36 +00:00
|
|
|
get_peer_profile(kv, peer).addScore(penalty_score)
|
|
|
|
onlinepeers.remove_online_peer(kv, peer)
|
2019-12-23 07:51:24 +00:00
|
|
|
keydb.transportinfo.set_address_info(
|
|
|
|
peer, 'lastConnectAttempt', epoch.get_epoch())
|
2020-07-26 02:36:48 +00:00
|
|
|
if action != 'ping' and not kv.get('shutdown'):
|
2019-09-17 01:16:06 +00:00
|
|
|
logger.warn(f'Lost connection to {peer}', terminal=True)
|
2019-12-23 07:51:24 +00:00
|
|
|
# Will only add a new peer to pool if needed
|
2020-07-31 01:15:36 +00:00
|
|
|
onlinepeers.get_online_peers(kv)
|
2019-07-10 22:38:20 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2019-08-06 21:19:26 +00:00
|
|
|
peer_profile = comm_inst.getPeerProfileInstance(peer)
|
|
|
|
peer_profile.update_connect_time()
|
|
|
|
peer_profile.addScore(1)
|
2019-12-23 07:51:24 +00:00
|
|
|
# If returnHeaders, returns tuple of data, headers.
|
|
|
|
# If not, just data string
|
|
|
|
return ret_data
|