sync.py
302 lines
| 11.7 KiB
| text/x-python
|
PythonLexer
|
r1177 | import xml.etree.ElementTree as et | ||
|
r1624 | import logging | ||
|
r1506 | |||
|
r1602 | from boards.abstracts.exceptions import SyncException | ||
from boards.models import KeyPair, GlobalId, Signature, Post, Tag | ||||
|
r1511 | from boards.models.attachment.downloaders import download | ||
|
r1518 | from boards.utils import get_file_mimetype, get_file_hash | ||
|
r1229 | from django.db import transaction | ||
|
r1177 | |||
|
r1582 | EXCEPTION_NODE = 'Sync node returned an error: {}.' | ||
EXCEPTION_OP = 'Load the OP first.' | ||||
EXCEPTION_DOWNLOAD = 'File was not downloaded.' | ||||
EXCEPTION_HASH = 'File hash does not match attachment hash.' | ||||
EXCEPTION_SIGNATURE = 'Invalid model signature for {}.' | ||||
EXCEPTION_AUTHOR_SIGNATURE = 'Model {} has no author signature.' | ||||
|
r1177 | ENCODING_UNICODE = 'unicode' | ||
TAG_MODEL = 'model' | ||||
TAG_REQUEST = 'request' | ||||
TAG_RESPONSE = 'response' | ||||
TAG_ID = 'id' | ||||
TAG_STATUS = 'status' | ||||
TAG_MODELS = 'models' | ||||
TAG_TITLE = 'title' | ||||
TAG_TEXT = 'text' | ||||
TAG_THREAD = 'thread' | ||||
TAG_PUB_TIME = 'pub-time' | ||||
TAG_SIGNATURES = 'signatures' | ||||
TAG_SIGNATURE = 'signature' | ||||
TAG_CONTENT = 'content' | ||||
TAG_ATTACHMENTS = 'attachments' | ||||
TAG_ATTACHMENT = 'attachment' | ||||
|
r1239 | TAG_TAGS = 'tags' | ||
TAG_TAG = 'tag' | ||||
|
r1508 | TAG_ATTACHMENT_REFS = 'attachment-refs' | ||
TAG_ATTACHMENT_REF = 'attachment-ref' | ||||
|
r1556 | TAG_TRIPCODE = 'tripcode' | ||
|
r1569 | TAG_VERSION = 'version' | ||
|
r1177 | |||
TYPE_GET = 'get' | ||||
ATTR_VERSION = 'version' | ||||
ATTR_TYPE = 'type' | ||||
ATTR_NAME = 'name' | ||||
ATTR_VALUE = 'value' | ||||
ATTR_MIMETYPE = 'mimetype' | ||||
|
r1237 | ATTR_KEY = 'key' | ||
|
r1508 | ATTR_REF = 'ref' | ||
ATTR_URL = 'url' | ||||
|
r1560 | ATTR_ID_TYPE = 'id-type' | ||
ID_TYPE_MD5 = 'md5' | ||||
|
r1177 | |||
STATUS_SUCCESS = 'success' | ||||
|
r1624 | logger = logging.getLogger('boards.sync') | ||
|
r1177 | class SyncManager: | ||
|
r1236 | @staticmethod | ||
def generate_response_get(model_list: list): | ||||
|
r1177 | response = et.Element(TAG_RESPONSE) | ||
status = et.SubElement(response, TAG_STATUS) | ||||
status.text = STATUS_SUCCESS | ||||
models = et.SubElement(response, TAG_MODELS) | ||||
for post in model_list: | ||||
model = et.SubElement(models, TAG_MODEL) | ||||
model.set(ATTR_NAME, 'post') | ||||
|
r1520 | global_id = post.global_id | ||
|
r1177 | |||
|
r1525 | attachments = post.attachments.all() | ||
|
r1520 | if global_id.content: | ||
model.append(et.fromstring(global_id.content)) | ||||
|
r1590 | if len(attachments) > 0: | ||
|
r1525 | attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) | ||
for file in attachments: | ||||
SyncManager._attachment_to_xml( | ||||
None, attachment_refs, file.file.file, | ||||
file.hash, file.file.url) | ||||
|
r1177 | else: | ||
|
r1520 | content_tag = et.SubElement(model, TAG_CONTENT) | ||
tag_id = et.SubElement(content_tag, TAG_ID) | ||||
global_id.to_xml_element(tag_id) | ||||
|
r1177 | |||
|
r1520 | title = et.SubElement(content_tag, TAG_TITLE) | ||
title.text = post.title | ||||
text = et.SubElement(content_tag, TAG_TEXT) | ||||
text.text = post.get_sync_text() | ||||
|
r1177 | |||
|
r1520 | thread = post.get_thread() | ||
if post.is_opening(): | ||||
tag_tags = et.SubElement(content_tag, TAG_TAGS) | ||||
for tag in thread.get_tags(): | ||||
tag_tag = et.SubElement(tag_tags, TAG_TAG) | ||||
tag_tag.text = tag.name | ||||
else: | ||||
tag_thread = et.SubElement(content_tag, TAG_THREAD) | ||||
thread_id = et.SubElement(tag_thread, TAG_ID) | ||||
thread.get_opening_post().global_id.to_xml_element(thread_id) | ||||
pub_time = et.SubElement(content_tag, TAG_PUB_TIME) | ||||
pub_time.text = str(post.get_pub_time_str()) | ||||
|
r1508 | |||
|
r1556 | if post.tripcode: | ||
tripcode = et.SubElement(content_tag, TAG_TRIPCODE) | ||||
tripcode.text = post.tripcode | ||||
|
r1590 | if len(attachments) > 0: | ||
|
r1520 | attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS) | ||
attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) | ||||
for file in attachments: | ||||
SyncManager._attachment_to_xml( | ||||
attachments_tag, attachment_refs, file.file.file, | ||||
file.hash, file.file.url) | ||||
|
r1569 | version_tag = et.SubElement(content_tag, TAG_VERSION) | ||
version_tag.text = str(post.version) | ||||
|
r1520 | |||
global_id.content = et.tostring(content_tag, ENCODING_UNICODE) | ||||
global_id.save() | ||||
|
r1506 | |||
|
r1177 | signatures_tag = et.SubElement(model, TAG_SIGNATURES) | ||
|
r1520 | post_signatures = global_id.signature_set.all() | ||
|
r1177 | if post_signatures: | ||
|
r1238 | signatures = post_signatures | ||
|
r1177 | else: | ||
|
r1520 | key = KeyPair.objects.get(public_key=global_id.key) | ||
|
r1386 | signature = Signature( | ||
|
r1177 | key_type=key.key_type, | ||
key=key.public_key, | ||||
|
r1520 | signature=key.sign(global_id.content), | ||
global_id=global_id, | ||||
|
r1386 | ) | ||
signature.save() | ||||
signatures = [signature] | ||||
|
r1177 | for signature in signatures: | ||
signature_tag = et.SubElement(signatures_tag, TAG_SIGNATURE) | ||||
signature_tag.set(ATTR_TYPE, signature.key_type) | ||||
signature_tag.set(ATTR_VALUE, signature.signature) | ||||
|
r1237 | signature_tag.set(ATTR_KEY, signature.key) | ||
|
r1177 | |||
return et.tostring(response, ENCODING_UNICODE) | ||||
|
r1236 | @staticmethod | ||
|
r1229 | @transaction.atomic | ||
|
r1511 | def parse_response_get(response_xml, hostname): | ||
|
r1177 | tag_root = et.fromstring(response_xml) | ||
tag_status = tag_root.find(TAG_STATUS) | ||||
if STATUS_SUCCESS == tag_status.text: | ||||
tag_models = tag_root.find(TAG_MODELS) | ||||
for tag_model in tag_models: | ||||
tag_content = tag_model.find(TAG_CONTENT) | ||||
|
r1237 | |||
|
r1537 | content_str = et.tostring(tag_content, ENCODING_UNICODE) | ||
|
r1237 | |||
|
r1177 | tag_id = tag_content.find(TAG_ID) | ||
|
r1233 | global_id, exists = GlobalId.from_xml_element(tag_id) | ||
|
r1582 | signatures = SyncManager._verify_model(global_id, content_str, tag_model) | ||
|
r1232 | |||
|
r1586 | version = int(tag_content.find(TAG_VERSION).text) | ||
is_old = exists and global_id.post.version < version | ||||
if exists and not is_old: | ||||
print('Post with same ID exists and is up to date.') | ||||
|
r1233 | else: | ||
|
r1537 | global_id.content = content_str | ||
|
r1233 | global_id.save() | ||
|
r1244 | for signature in signatures: | ||
signature.global_id = global_id | ||||
signature.save() | ||||
|
r1177 | |||
|
r1511 | title = tag_content.find(TAG_TITLE).text or '' | ||
text = tag_content.find(TAG_TEXT).text or '' | ||||
|
r1233 | pub_time = tag_content.find(TAG_PUB_TIME).text | ||
|
r1564 | tripcode_tag = tag_content.find(TAG_TRIPCODE) | ||
if tripcode_tag is not None: | ||||
tripcode = tripcode_tag.text or '' | ||||
else: | ||||
tripcode = '' | ||||
|
r1232 | |||
|
r1233 | thread = tag_content.find(TAG_THREAD) | ||
|
r1239 | tags = [] | ||
|
r1233 | if thread: | ||
|
r1386 | thread_id = thread.find(TAG_ID) | ||
op_global_id, exists = GlobalId.from_xml_element(thread_id) | ||||
if exists: | ||||
opening_post = Post.objects.get(global_id=op_global_id) | ||||
else: | ||||
|
r1539 | raise SyncException(EXCEPTION_OP) | ||
|
r1233 | else: | ||
opening_post = None | ||||
|
r1239 | tag_tags = tag_content.find(TAG_TAGS) | ||
for tag_tag in tag_tags: | ||||
|
r1386 | tag, created = Tag.objects.get_or_create( | ||
name=tag_tag.text) | ||||
|
r1239 | tags.append(tag) | ||
|
r1177 | |||
|
r1233 | # TODO Check that the replied posts are already present | ||
# before adding new ones | ||||
|
r1229 | |||
|
r1511 | files = [] | ||
tag_attachments = tag_content.find(TAG_ATTACHMENTS) or list() | ||||
tag_refs = tag_model.find(TAG_ATTACHMENT_REFS) | ||||
for attachment in tag_attachments: | ||||
tag_ref = tag_refs.find("{}[@ref='{}']".format( | ||||
TAG_ATTACHMENT_REF, attachment.text)) | ||||
url = tag_ref.get(ATTR_URL) | ||||
attached_file = download(hostname + url) | ||||
if attached_file is None: | ||||
|
r1539 | raise SyncException(EXCEPTION_DOWNLOAD) | ||
|
r1518 | |||
|
r1524 | hash = get_file_hash(attached_file) | ||
|
r1518 | if hash != attachment.text: | ||
|
r1539 | raise SyncException(EXCEPTION_HASH) | ||
|
r1518 | |||
|
r1511 | files.append(attached_file) | ||
|
r1238 | |||
|
r1586 | if is_old: | ||
post = global_id.post | ||||
Post.objects.update_post( | ||||
post, title=title, text=text, pub_time=pub_time, | ||||
tags=tags, files=files, tripcode=tripcode, | ||||
version=version) | ||||
|
r1624 | logger.debug('Parsed updated post {}'.format(global_id)) | ||
|
r1586 | else: | ||
Post.objects.import_post( | ||||
title=title, text=text, pub_time=pub_time, | ||||
opening_post=opening_post, tags=tags, | ||||
global_id=global_id, files=files, tripcode=tripcode, | ||||
version=version) | ||||
|
r1624 | logger.debug('Parsed new post {}'.format(global_id)) | ||
|
r1177 | else: | ||
|
r1539 | raise SyncException(EXCEPTION_NODE.format(tag_status.text)) | ||
|
r1237 | |||
@staticmethod | ||||
|
r1566 | def generate_response_list(): | ||
|
r1321 | response = et.Element(TAG_RESPONSE) | ||
status = et.SubElement(response, TAG_STATUS) | ||||
status.text = STATUS_SUCCESS | ||||
models = et.SubElement(response, TAG_MODELS) | ||||
|
r1562 | for post in Post.objects.prefetch_related('global_id').all(): | ||
|
r1571 | tag_model = et.SubElement(models, TAG_MODEL) | ||
tag_id = et.SubElement(tag_model, TAG_ID) | ||||
|
r1321 | post.global_id.to_xml_element(tag_id) | ||
|
r1571 | tag_version = et.SubElement(tag_model, TAG_VERSION) | ||
|
r1573 | tag_version.text = str(post.version) | ||
|
r1321 | |||
return et.tostring(response, ENCODING_UNICODE) | ||||
@staticmethod | ||||
|
r1582 | def _verify_model(global_id, content_str, tag_model): | ||
|
r1237 | """ | ||
Verifies all signatures for a single model. | ||||
""" | ||||
|
r1244 | signatures = [] | ||
|
r1237 | |||
tag_signatures = tag_model.find(TAG_SIGNATURES) | ||||
|
r1582 | has_author_signature = False | ||
|
r1237 | for tag_signature in tag_signatures: | ||
signature_type = tag_signature.get(ATTR_TYPE) | ||||
signature_value = tag_signature.get(ATTR_VALUE) | ||||
signature_key = tag_signature.get(ATTR_KEY) | ||||
|
r1582 | if global_id.key_type == signature_type and\ | ||
global_id.key == signature_key: | ||||
has_author_signature = True | ||||
|
r1244 | signature = Signature(key_type=signature_type, | ||
key=signature_key, | ||||
signature=signature_value) | ||||
|
r1386 | |||
|
r1537 | if not KeyPair.objects.verify(signature, content_str): | ||
|
r1539 | raise SyncException(EXCEPTION_SIGNATURE.format(content_str)) | ||
|
r1386 | |||
signatures.append(signature) | ||||
|
r1582 | if not has_author_signature: | ||
raise SyncException(EXCEPTION_AUTHOR_SIGNATURE.format(content_str)) | ||||
|
r1237 | |||
|
r1244 | return signatures | ||
|
r1508 | |||
@staticmethod | ||||
def _attachment_to_xml(tag_attachments, tag_refs, file, hash, url): | ||||
|
r1525 | if tag_attachments is not None: | ||
mimetype = get_file_mimetype(file) | ||||
attachment = et.SubElement(tag_attachments, TAG_ATTACHMENT) | ||||
attachment.set(ATTR_MIMETYPE, mimetype) | ||||
|
r1560 | attachment.set(ATTR_ID_TYPE, ID_TYPE_MD5) | ||
|
r1525 | attachment.text = hash | ||
|
r1508 | |||
attachment_ref = et.SubElement(tag_refs, TAG_ATTACHMENT_REF) | ||||
attachment_ref.set(ATTR_REF, hash) | ||||
attachment_ref.set(ATTR_URL, url) | ||||