Onionr/src/httpapi/miscpublicapi/announce.py

76 lines
3.0 KiB
Python
Raw Normal View History

2019-05-07 17:56:20 +00:00
'''
Onionr - Private P2P Communication
2019-05-07 17:56:20 +00:00
Handle announcements to the public API server
'''
'''
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 base64
2019-08-05 17:57:25 +00:00
from flask import Response, g
2019-08-02 23:00:04 +00:00
import deadsimplekv
2019-05-07 17:56:20 +00:00
import logger
from etc import onionrvalues
from onionrutils import stringvalidators, bytesconverter
2019-07-24 18:10:37 +00:00
from utils import gettransports
2019-08-02 23:00:04 +00:00
import onionrcrypto as crypto, filepaths
2019-08-05 17:57:25 +00:00
from communicator import OnionrCommunicatorDaemon
2019-08-02 23:00:04 +00:00
def handle_announce(request):
2019-05-07 17:56:20 +00:00
'''
accept announcement posts, validating POW
clientAPI should be an instance of the clientAPI server running, request is a instance of a flask request
'''
resp = 'failure'
powHash = ''
randomData = ''
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')
pass
else:
try:
randomData = request.form['random']
randomData = base64.b64decode(randomData)
except KeyError:
logger.warn('No random data specified for upload')
else:
2019-07-24 18:10:37 +00:00
nodes = newNode + bytesconverter.str_to_bytes(gettransports.get()[0])
nodes = crypto.hashers.blake2b_hash(nodes)
powHash = crypto.hashers.blake2b_hash(randomData + nodes)
2019-05-07 17:56:20 +00:00
try:
powHash = powHash.decode()
except AttributeError:
pass
2019-08-09 20:41:27 +00:00
if powHash.startswith('0' * onionrvalues.ANNOUNCE_POW):
newNode = bytesconverter.bytes_to_str(newNode)
2019-08-02 23:00:04 +00:00
announce_queue = deadsimplekv.DeadSimpleKV(filepaths.announce_cache)
2019-08-05 17:57:25 +00:00
announce_queue_list = announce_queue.get('new_peers')
if announce_queue_list is None:
announce_queue_list = []
if stringvalidators.validate_transport(newNode) and not newNode in announce_queue_list:
#clientAPI.onionrInst.communicatorInst.newPeers.append(newNode)
g.shared_state.get(OnionrCommunicatorDaemon).newPeers.append(newNode)
announce_queue.put('new_peers', announce_queue_list.append(newNode))
2019-08-02 23:00:04 +00:00
announce_queue.flush()
2019-05-07 17:56:20 +00:00
resp = 'Success'
else:
logger.warn(newNode.decode() + ' failed to meet POW: ' + powHash)
resp = Response(resp)
if resp == 'failure':
return resp, 406
return resp