2020-02-17 12:13:57 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-29 20:17:48 +00:00
|
|
|
|
2020-02-17 12:13:57 +00:00
|
|
|
Create blocks with the client api server
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import threading
|
|
|
|
|
|
|
|
from flask import Blueprint, Response, request, g
|
|
|
|
import onionrblocks
|
|
|
|
from onionrcrypto import hashers
|
|
|
|
from onionrutils import bytesconverter
|
|
|
|
from onionrutils import mnemonickeys
|
|
|
|
from onionrtypes import JSONSerializable
|
|
|
|
"""
|
2019-06-29 20:17:48 +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-02-17 12:13:57 +00:00
|
|
|
"""
|
2019-06-29 20:17:48 +00:00
|
|
|
ib = Blueprint('insertblock', __name__)
|
|
|
|
|
2020-02-17 12:13:57 +00:00
|
|
|
|
2019-06-29 20:17:48 +00:00
|
|
|
@ib.route('/insertblock', methods=['POST'])
|
|
|
|
def client_api_insert_block():
|
2020-02-17 12:13:57 +00:00
|
|
|
encrypt: bool = False
|
|
|
|
insert_data: JSONSerializable = request.get_json(force=True)
|
|
|
|
message = insert_data['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
|
2020-02-17 12:13:57 +00:00
|
|
|
if message_hash in g.too_many.get_by_string(
|
|
|
|
"OnionrCommunicatorDaemon").generating_blocks:
|
2019-08-13 22:28:53 +00:00
|
|
|
return 'failure due to duplicate insert', 400
|
|
|
|
else:
|
2020-02-17 12:13:57 +00:00
|
|
|
g.too_many.get_by_string(
|
|
|
|
"OnionrCommunicatorDaemon").generating_blocks.append(message_hash)
|
2019-06-29 20:17:48 +00:00
|
|
|
|
2020-02-17 12:13:57 +00:00
|
|
|
encrypt_type = ''
|
2019-06-29 20:17:48 +00:00
|
|
|
sign = True
|
|
|
|
meta = {}
|
|
|
|
to = ''
|
|
|
|
try:
|
2020-02-17 12:13:57 +00:00
|
|
|
if insert_data['encrypt']:
|
|
|
|
to = insert_data['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)
|
2020-02-17 12:13:57 +00:00
|
|
|
encrypt_type = 'asym'
|
2019-06-29 20:17:48 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
2020-02-17 12:13:57 +00:00
|
|
|
if not insert_data['sign']:
|
2019-06-29 20:17:48 +00:00
|
|
|
sign = False
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
2020-02-17 12:13:57 +00:00
|
|
|
bType = insert_data['type']
|
2019-06-29 20:17:48 +00:00
|
|
|
except KeyError:
|
|
|
|
bType = 'bin'
|
|
|
|
try:
|
2020-02-17 12:13:57 +00:00
|
|
|
meta = json.loads(insert_data['meta'])
|
2019-06-29 20:17:48 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2020-02-17 12:13:57 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# The setting in the UI is for if forward secrecy is enabled, not disabled
|
|
|
|
disable_forward_secrecy = not insert_data['forward']
|
|
|
|
except KeyError:
|
|
|
|
disable_forward_secrecy = False
|
|
|
|
|
2020-02-12 10:29:34 +00:00
|
|
|
threading.Thread(
|
|
|
|
target=onionrblocks.insert, args=(message,),
|
2020-02-17 12:13:57 +00:00
|
|
|
kwargs={'header': bType, 'encryptType': encrypt_type,
|
|
|
|
'sign': sign, 'asymPeer': to, 'meta': meta,
|
|
|
|
'disableForward': disable_forward_secrecy}).start()
|
|
|
|
return Response('success')
|