2019-07-10 22:38:20 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
This file implements logic for performing requests to Onionr peers
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
|
|
|
'''
|
|
|
|
import streamedrequests
|
|
|
|
import logger
|
|
|
|
from onionrutils import epoch, basicrequests
|
2019-07-18 17:40:48 +00:00
|
|
|
from coredb import keydb
|
2019-08-07 05:42:37 +00:00
|
|
|
from . import onlinepeers
|
2019-08-08 06:25:48 +00:00
|
|
|
|
2019-08-07 05:42:37 +00:00
|
|
|
def peer_action(comm_inst, peer, action, returnHeaders=False, max_resp_size=5242880):
|
2019-07-10 22:38:20 +00:00
|
|
|
'''Perform a get request to a peer'''
|
|
|
|
penalty_score = -10
|
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:
|
2019-08-07 05:42:37 +00:00
|
|
|
ret_data = basicrequests.do_get_request(url, port=comm_inst.proxyPort,
|
|
|
|
max_size=max_resp_size)
|
2019-07-10 22:38:20 +00:00
|
|
|
except streamedrequests.exceptions.ResponseLimitReached:
|
2019-07-11 07:39:35 +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-08-07 23:14:13 +00:00
|
|
|
if ret_data == False: # For some reason "if not" breaks this. Prob has to do with empty string.
|
2019-07-10 22:38:20 +00:00
|
|
|
try:
|
|
|
|
comm_inst.getPeerProfileInstance(peer).addScore(penalty_score)
|
2019-07-11 07:39:35 +00:00
|
|
|
onlinepeers.remove_online_peer(comm_inst, peer)
|
2019-08-08 06:25:48 +00:00
|
|
|
keydb.transportinfo.set_address_info(peer, 'lastConnectAttempt', epoch.get_epoch())
|
2019-07-10 22:38:20 +00:00
|
|
|
if action != 'ping' and not comm_inst.shutdown:
|
2019-09-17 06:56:13 +00:00
|
|
|
logger.warn(f'Lost connection to {peer}', terminal=True)
|
2019-07-10 22:38:20 +00:00
|
|
|
onlinepeers.get_online_peers(comm_inst) # Will only add a new peer to pool if needed
|
|
|
|
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-08-07 05:42:37 +00:00
|
|
|
return ret_data # If returnHeaders, returns tuple of data, headers. if not, just data string
|