2020-04-03 09:20:55 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-08-09 20:07:32 +00:00
|
|
|
|
|
|
|
Return a useful tuple of (metadata (header), meta, and data) by accepting raw block data
|
2020-04-03 09:20:55 +00:00
|
|
|
"""
|
2020-04-06 13:51:20 +00:00
|
|
|
from json import JSONDecodeError
|
2020-04-03 09:20:55 +00:00
|
|
|
import ujson as json
|
|
|
|
|
|
|
|
from onionrutils import bytesconverter
|
2020-10-15 03:56:43 +00:00
|
|
|
import logger
|
2020-04-03 09:20:55 +00:00
|
|
|
"""
|
2019-08-09 20:07:32 +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-04-03 09:20:55 +00:00
|
|
|
"""
|
2019-08-09 20:07:32 +00:00
|
|
|
|
2019-09-10 20:25:50 +00:00
|
|
|
|
2019-08-09 20:07:32 +00:00
|
|
|
def get_block_metadata_from_data(block_data):
|
2020-04-03 09:20:55 +00:00
|
|
|
"""
|
|
|
|
accepts block contents as string, returns a tuple of
|
|
|
|
metadata, meta (meta being internal metadata, which will be
|
2019-08-09 20:07:32 +00:00
|
|
|
returned as an encrypted base64 string if it is encrypted, dict if not).
|
2020-04-03 09:20:55 +00:00
|
|
|
"""
|
2019-08-09 20:07:32 +00:00
|
|
|
meta = {}
|
|
|
|
metadata = {}
|
|
|
|
data = block_data
|
|
|
|
try:
|
|
|
|
block_data = block_data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2019-09-10 20:25:50 +00:00
|
|
|
metadata = json.loads(bytesconverter.bytes_to_str(block_data[:block_data.find(b'\n')]))
|
2020-04-06 13:51:20 +00:00
|
|
|
except JSONDecodeError:
|
2019-08-09 20:07:32 +00:00
|
|
|
pass
|
2020-10-15 03:56:43 +00:00
|
|
|
except ValueError:
|
|
|
|
logger.warn("Could not get metadata from:", terminal=True)
|
|
|
|
logger.warn(block_data, terminal=True)
|
2019-08-09 20:07:32 +00:00
|
|
|
else:
|
2019-09-10 20:25:50 +00:00
|
|
|
data = block_data[block_data.find(b'\n'):]
|
2019-08-09 20:07:32 +00:00
|
|
|
|
|
|
|
meta = metadata['meta']
|
|
|
|
return (metadata, meta, data)
|