onionr/src/httpapi/miscpublicapi/announce.py

62 lines
2.2 KiB
Python
Raw Normal View History

2020-01-20 03:02:04 +00:00
"""Onionr - Private P2P Communication.
2019-05-07 17:56:20 +00:00
2020-01-20 03:02:04 +00:00
Handle announcements to the public API server
"""
from flask import Response, g
import deadsimplekv
import logger
from etc import onionrvalues
from onionrutils import stringvalidators, bytesconverter
import filepaths
from communicator import OnionrCommunicatorDaemon
"""
2019-05-07 17:56: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/>.
2020-01-20 03:02:04 +00:00
"""
2019-08-02 23:00:04 +00:00
def handle_announce(request):
2020-01-20 03:02:04 +00:00
"""accept announcement posts, validating POW
2019-05-07 17:56:20 +00:00
clientAPI should be an instance of the clientAPI server running, request is a instance of a flask request
2020-01-20 03:02:04 +00:00
"""
2019-05-07 17:56:20 +00:00
resp = 'failure'
newNode = ''
2019-08-02 23:00:04 +00:00
2019-05-07 17:56:20 +00:00
try:
newNode = request.form['node'].encode()
except KeyError:
logger.warn('No node specified for upload')
else:
2020-01-20 03:02:04 +00:00
newNode = bytesconverter.bytes_to_str(newNode)
announce_queue = deadsimplekv.DeadSimpleKV(filepaths.announce_cache)
announce_queue_list = announce_queue.get('new_peers')
if announce_queue_list is None:
announce_queue_list = []
2019-05-07 17:56:20 +00:00
else:
2020-01-20 03:02:04 +00:00
if len(announce_queue_list) >= onionrvalues.MAX_NEW_PEER_QUEUE:
newNode = ''
if stringvalidators.validate_transport(newNode) and \
newNode not in announce_queue_list:
g.shared_state.get(
OnionrCommunicatorDaemon).newPeers.append(newNode)
announce_queue.put('new_peers',
announce_queue_list.append(newNode))
announce_queue.flush()
resp = 'Success'
2019-08-05 17:57:25 +00:00
2019-05-07 17:56:20 +00:00
resp = Response(resp)
if resp == 'failure':
return resp, 406
return resp