2019-06-29 20:17:48 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
Create blocks with the client 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 json, threading
|
2019-08-13 22:28:53 +00:00
|
|
|
from flask import Blueprint, Response, request, g
|
2019-07-19 04:59:44 +00:00
|
|
|
import onionrblocks
|
2019-08-13 22:28:53 +00:00
|
|
|
from onionrcrypto import hashers
|
|
|
|
from onionrutils import bytesconverter
|
2019-09-09 08:23:09 +00:00
|
|
|
from onionrutils import mnemonickeys
|
2019-06-29 20:17:48 +00:00
|
|
|
ib = Blueprint('insertblock', __name__)
|
|
|
|
|
|
|
|
@ib.route('/insertblock', methods=['POST'])
|
|
|
|
def client_api_insert_block():
|
|
|
|
encrypt = False
|
|
|
|
bData = request.get_json(force=True)
|
|
|
|
message = bData['message']
|
2019-08-13 22:28:53 +00:00
|
|
|
message_hash = bytesconverter.bytes_to_str(hashers.sha3_hash(message))
|
2019-06-29 20:17:48 +00:00
|
|
|
|
|
|
|
# Detect if message (block body) is not specified
|
|
|
|
if type(message) is None:
|
2019-08-13 22:28:53 +00:00
|
|
|
return 'failure due to unspecified message', 400
|
|
|
|
|
|
|
|
# Detect if block with same message is already being inserted
|
|
|
|
if message_hash in g.too_many.get_by_string("OnionrCommunicatorDaemon").generating_blocks:
|
|
|
|
return 'failure due to duplicate insert', 400
|
|
|
|
else:
|
|
|
|
g.too_many.get_by_string("OnionrCommunicatorDaemon").generating_blocks.append(message_hash)
|
2019-06-29 20:17:48 +00:00
|
|
|
|
|
|
|
subject = 'temp'
|
|
|
|
encryptType = ''
|
|
|
|
sign = True
|
|
|
|
meta = {}
|
|
|
|
to = ''
|
|
|
|
try:
|
|
|
|
if bData['encrypt']:
|
2019-09-09 08:23:09 +00:00
|
|
|
to = bData['to'].strip()
|
2019-09-23 22:56:05 +00:00
|
|
|
if "-" in to:
|
2019-09-09 08:23:09 +00:00
|
|
|
to = mnemonickeys.get_base32(to)
|
2019-06-29 20:17:48 +00:00
|
|
|
encrypt = True
|
|
|
|
encryptType = 'asym'
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if not bData['sign']:
|
|
|
|
sign = False
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
bType = bData['type']
|
|
|
|
except KeyError:
|
|
|
|
bType = 'bin'
|
|
|
|
try:
|
|
|
|
meta = json.loads(bData['meta'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2020-02-12 10:29:34 +00:00
|
|
|
threading.Thread(
|
|
|
|
target=onionrblocks.insert, args=(message,),
|
|
|
|
kwargs={'header': bType, 'encryptType': encryptType,
|
|
|
|
'sign':sign, 'asymPeer': to, 'meta': meta, 'disableForward': bData['forward']}).start()
|
2019-06-29 20:17:48 +00:00
|
|
|
return Response('success')
|