diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..01ecd10 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +liberapay: jarbasAI diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..26e59a2 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file + +version: 2 +updates: + - package-ecosystem: "pip" # See documentation for possible values + directory: "/requirements" # Location of package manifests + schedule: + interval: "weekly" diff --git a/.github/workflows/build_tests.yml b/.github/workflows/build_tests.yml new file mode 100644 index 0000000..c982751 --- /dev/null +++ b/.github/workflows/build_tests.yml @@ -0,0 +1,25 @@ +name: Run Build Tests +on: + push: + workflow_dispatch: + +jobs: + build_tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install Build Tools + run: | + python -m pip install build wheel + - name: Build Distribution Packages + run: | + python setup.py bdist_wheel + - name: Install package + run: | + pip install . diff --git a/.github/workflows/conventional-label.yaml b/.github/workflows/conventional-label.yaml new file mode 100644 index 0000000..0a449cb --- /dev/null +++ b/.github/workflows/conventional-label.yaml @@ -0,0 +1,10 @@ +# auto add labels to PRs +on: + pull_request_target: + types: [ opened, edited ] +name: conventional-release-labels +jobs: + label: + runs-on: ubuntu-latest + steps: + - uses: bcoe/conventional-release-labels@v1 \ No newline at end of file diff --git a/.github/workflows/publish_stable.yml b/.github/workflows/publish_stable.yml new file mode 100644 index 0000000..b71ff8d --- /dev/null +++ b/.github/workflows/publish_stable.yml @@ -0,0 +1,58 @@ +name: Stable Release +on: + push: + branches: [master] + workflow_dispatch: + +jobs: + publish_stable: + uses: TigreGotico/gh-automations/.github/workflows/publish-stable.yml@master + secrets: inherit + with: + branch: 'master' + version_file: 'hivemind_http_protocol/version.py' + setup_py: 'setup.py' + publish_release: true + + publish_pypi: + needs: publish_stable + if: success() # Ensure this job only runs if the previous job succeeds + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: dev + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install Build Tools + run: | + python -m pip install build wheel + - name: version + run: echo "::set-output name=version::$(python setup.py --version)" + id: version + - name: Build Distribution Packages + run: | + python setup.py sdist bdist_wheel + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@master + with: + password: ${{secrets.PYPI_TOKEN}} + + + sync_dev: + needs: publish_stable + if: success() # Ensure this job only runs if the previous job succeeds + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. + ref: master + - name: Push master -> dev + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: dev \ No newline at end of file diff --git a/.github/workflows/release_workflow.yml b/.github/workflows/release_workflow.yml new file mode 100644 index 0000000..5608c5d --- /dev/null +++ b/.github/workflows/release_workflow.yml @@ -0,0 +1,108 @@ +name: Release Alpha and Propose Stable + +on: + pull_request: + types: [closed] + branches: [dev] + +jobs: + publish_alpha: + if: github.event.pull_request.merged == true + uses: TigreGotico/gh-automations/.github/workflows/publish-alpha.yml@master + secrets: inherit + with: + branch: 'dev' + version_file: 'hivemind_http_protocol/version.py' + setup_py: 'setup.py' + update_changelog: true + publish_prerelease: true + changelog_max_issues: 100 + + notify: + if: github.event.pull_request.merged == true + needs: publish_alpha + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Send message to Matrix bots channel + id: matrix-chat-message + uses: fadenb/matrix-chat-message@v0.0.6 + with: + homeserver: 'matrix.org' + token: ${{ secrets.MATRIX_TOKEN }} + channel: '!WjxEKjjINpyBRPFgxl:krbel.duckdns.org' + message: | + new ${{ github.event.repository.name }} PR merged! https://github.com/${{ github.repository }}/pull/${{ github.event.number }} + + publish_pypi: + needs: publish_alpha + if: success() # Ensure this job only runs if the previous job succeeds + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: dev + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install Build Tools + run: | + python -m pip install build wheel + - name: version + run: echo "::set-output name=version::$(python setup.py --version)" + id: version + - name: Build Distribution Packages + run: | + python setup.py sdist bdist_wheel + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@master + with: + password: ${{secrets.PYPI_TOKEN}} + + + propose_release: + needs: publish_alpha + if: success() # Ensure this job only runs if the previous job succeeds + runs-on: ubuntu-latest + steps: + - name: Checkout dev branch + uses: actions/checkout@v3 + with: + ref: dev + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + + - name: Get version from setup.py + id: get_version + run: | + VERSION=$(python setup.py --version) + echo "VERSION=$VERSION" >> $GITHUB_ENV + + - name: Create and push new branch + run: | + git checkout -b release-${{ env.VERSION }} + git push origin release-${{ env.VERSION }} + + - name: Open Pull Request from dev to master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # Variables + BRANCH_NAME="release-${{ env.VERSION }}" + BASE_BRANCH="master" + HEAD_BRANCH="release-${{ env.VERSION }}" + PR_TITLE="Release ${{ env.VERSION }}" + PR_BODY="Human review requested!" + + # Create a PR using GitHub API + curl -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token $GITHUB_TOKEN" \ + -d "{\"title\":\"$PR_TITLE\",\"body\":\"$PR_BODY\",\"head\":\"$HEAD_BRANCH\",\"base\":\"$BASE_BRANCH\"}" \ + https://api.github.com/repos/${{ github.repository }}/pulls + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6769e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..86e4f82 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2024 Casimiro Ferreira + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0e7bc1 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# HiveMind http protocol + +Transport HiveMessages via http + +This is the reference implementation of HiveMind, but you can theoretically replace http with anything \ No newline at end of file diff --git a/hivemind_http_protocol/__init__.py b/hivemind_http_protocol/__init__.py new file mode 100644 index 0000000..a151c63 --- /dev/null +++ b/hivemind_http_protocol/__init__.py @@ -0,0 +1,338 @@ +import asyncio +import dataclasses +import os +import os.path +import random +import threading +from os import makedirs +from os.path import exists, join +from socket import gethostname +from typing import Dict, Any, Optional, Tuple, List +from collections import defaultdict +import pybase64 +from OpenSSL import crypto +from ovos_bus_client.session import Session +from ovos_utils.log import LOG +from ovos_utils.xdg_utils import xdg_data_home +from tornado import ioloop +from tornado import web +from tornado.platform.asyncio import AnyThreadEventLoopPolicy + +from hivemind_bus_client.message import HiveMessageType +from hivemind_core.protocol import ( + HiveMindListenerProtocol, + HiveMindClientConnection, + HiveMindNodeType +) +from hivemind_plugin_manager.protocols import ClientCallbacks +from hivemind_plugin_manager.protocols import NetworkProtocol +from poorman_handshake import PasswordHandShake + +_LOCK = threading.RLock() +CLIENTS: Dict[str, HiveMindClientConnection] = {} +UNDELIVERED: Dict[str, List[str]] = defaultdict(list) # key: [messages] +UNDELIVERED_BIN: Dict[str, List[str]] = defaultdict(list) # key: [b64_messages] + + +@dataclasses.dataclass +class HiveMindHttpProtocol(NetworkProtocol): + """ + HTTP handler for managing HiveMind client connections. + + Attributes: + hm_protocol (Optional[HiveMindListenerProtocol]): The protocol instance for handling HiveMind messages. + """ + config: Dict[str, Any] = dataclasses.field(default_factory=dict) + hm_protocol: Optional[HiveMindListenerProtocol] = None + callbacks: ClientCallbacks = dataclasses.field(default_factory=ClientCallbacks) + + + def run(self): + LOG.debug(f"HTTP server config: {self.config}") + asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy()) + HiveMindHttpHandler.hm_protocol = self.hm_protocol + + ssl = self.config.get("ssl", False) + cert_dir: str = self.config.get("cert_dir") or f"{xdg_data_home()}/hivemind" + cert_name: str = self.config.get("cert_name") or "hivemind" + host = self.config.get("host", "0.0.0.0") + port = int(self.config.get("port", 5678)) + + routes = [ + (r"/connect", ConnectHandler), + (r"/disconnect", DisconnectHandler), + (r"/send_message", SendMessageHandler), + (r"/get_messages", GetMessagesHandler), + (r"/get_binary_messages", GetBinMessagesHandler), + ] + application = web.Application(routes) + if ssl: + cert_file = f"{cert_dir}/{cert_name}.crt" + key_file = f"{cert_dir}/{cert_name}.key" + if not os.path.isfile(key_file): + LOG.info(f"generating self-signed SSL certificate") + cert_file, key_file = self.create_self_signed_cert(cert_dir, cert_name) + LOG.debug("using ssl key at " + key_file) + LOG.debug("using ssl certificate at " + cert_file) + ssl_options = {"certfile": cert_file, "keyfile": key_file} + LOG.info(f"HTTPS listener started at port: {port}") + application.listen(port, host, ssl_options=ssl_options) + else: + LOG.info(f"HTTP listener started at port: {port}") + application.listen(port, host) + + ioloop.IOLoop.current().start() + + @staticmethod + def create_self_signed_cert( + cert_dir: str = f"{xdg_data_home()}/hivemind", + name: str = "hivemind" + ) -> Tuple[str, str]: + """ + Create a self-signed certificate and key pair if they do not already exist. + + Args: + cert_dir (str): The directory where the certificate and key will be stored. + name (str): The base name for the certificate and key files. + + Returns: + Tuple[str, str]: The paths to the created certificate and key files. + """ + cert_file = name + ".crt" + key_file = name + ".key" + cert_path = join(cert_dir, cert_file) + key_path = join(cert_dir, key_file) + makedirs(cert_dir, exist_ok=True) + + if not exists(join(cert_dir, cert_file)) or not exists(join(cert_dir, key_file)): + # create a key pair + k = crypto.PKey() + k.generate_key(crypto.TYPE_RSA, 2048) + + # Create a self-signed certificate + cert = crypto.X509() + cert.get_subject().C = "PT" + cert.get_subject().ST = "Europe" + cert.get_subject().L = "Mountains" + cert.get_subject().O = "Jarbas AI" + cert.get_subject().OU = "Powered by HiveMind" + cert.get_subject().CN = gethostname() + cert.set_serial_number(random.randint(0, 2000)) + cert.gmtime_adj_notBefore(0) + cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60) + cert.set_issuer(cert.get_subject()) + cert.set_pubkey(k) + # TODO: Don't use SHA1 + cert.sign(k, "sha1") + + open(cert_path, "wb").write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) + open(key_path, "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) + + return cert_path, key_path + + +class HiveMindHttpHandler(web.RequestHandler): + """Base handler for HTTP requests.""" + hm_protocol = None + + def decode_auth(self): + auth = self.get_argument("authorization", "") + if not auth: + self.set_status(400) + return None, None + userpass_encoded = bytes(auth, encoding="utf-8") + userpass_decoded = pybase64.b64decode(userpass_encoded).decode("utf-8") + return userpass_decoded.split(":") + + def get_client(self, useragent, key, cache = True) -> Optional[HiveMindClientConnection]: + global CLIENTS, UNDELIVERED + + if cache and key in CLIENTS: + return CLIENTS[key] + + def do_send(payload: str, is_bin: bool): + with _LOCK: + if is_bin: + payload = pybase64.b64encode(payload).decode("utf-8") + UNDELIVERED_BIN[key].append(payload) + else: + UNDELIVERED[key].append(payload) + + def do_disconnect(): + with _LOCK: + if key in UNDELIVERED: + UNDELIVERED.pop(key) + if key in CLIENTS: + CLIENTS.pop(key) + + client = HiveMindClientConnection( + key=key, + disconnect=do_disconnect, + send_msg=do_send, + sess=Session(session_id="default"), # will be re-assigned once client sends handshake + name=useragent, + hm_protocol=self.hm_protocol + ) + self.hm_protocol.db.sync() + user = self.hm_protocol.db.get_client_by_api_key(key) + if not user: + LOG.error("Client provided an invalid api key") + self.hm_protocol.handle_invalid_key_connected(client) + return None + + client.name = f"{useragent}::{user.client_id}::{user.name}" + client.crypto_key = user.crypto_key + client.msg_blacklist = user.message_blacklist or [] + client.skill_blacklist = user.skill_blacklist or [] + client.intent_blacklist = user.intent_blacklist or [] + client.allowed_types = user.allowed_types + client.can_propagate = user.can_propagate + client.can_escalate = user.can_escalate + client.is_admin = user.is_admin + if user.password: + # pre-shared password to derive aes_key + client.pswd_handshake = PasswordHandShake(user.password) + + client.node_type = HiveMindNodeType.NODE # TODO . placeholder + if cache: + CLIENTS[key] = client + return client + + +class ConnectHandler(HiveMindHttpHandler): + async def post(self): + try: + useragent, key = self.decode_auth() + if not key: + self.write({"error": "Missing authorization"}) + return + + client = self.get_client(useragent, key) + + if ( + not client.crypto_key + and not self.hm_protocol.handshake_enabled + and self.hm_protocol.require_crypto + ): + LOG.error( + "No pre-shared crypto key for client and handshake disabled, " + "but configured to require crypto!" + ) + # clients requiring handshake support might fail here + self.hm_protocol.handle_invalid_protocol_version(client) + return + + self.hm_protocol.handle_new_client(client) + self.write({"status": "Connected"}) + except Exception as e: + LOG.error(f"Connection failed: {e}") + self.set_status(500) + self.write({"error": "Connection failed"}) + + +class DisconnectHandler(HiveMindHttpHandler): + async def post(self): + global CLIENTS + + try: + useragent, key = self.decode_auth() + if not key: + self.write({"error": "Missing authorization"}) + return + if key in CLIENTS: + client = self.get_client(useragent, key) + LOG.info(f"disconnecting client: {client.peer}") + self.hm_protocol.handle_client_disconnected(client) + CLIENTS.pop(key) + self.write({"status": "Disconnected"}) + else: + self.write({"error": "Already Disconnected"}) + except Exception as e: + LOG.error(f"Disconnection failed: {e}") + self.set_status(500) + self.write({"error": "Disconnection failed"}) + + +class SendMessageHandler(HiveMindHttpHandler): + async def post(self): + try: + useragent, key = self.decode_auth() + if not key: + self.write({"error": "Missing authorization"}) + return + # refuse if connect wasnt called first + if key not in CLIENTS: + self.write({"error": "Client is not connected"}) + return + + client = self.get_client(useragent, key) + + message = self.get_argument("message", "") + if not message: + self.set_status(400) + self.write({"error": "Missing message"}) + return + + message = client.decode(message) + if ( + message.msg_type == HiveMessageType.BUS + and message.payload.msg_type == "recognizer_loop:b64_audio" + ): + LOG.info(f"Received {client.peer} sent base64 audio for STT") + else: + LOG.info(f"Received {client.peer} message: {message}") + self.hm_protocol.handle_message(message, client) + + self.write({"status": "message sent"}) + except Exception as e: + LOG.error(f"Message sending failed: {e}") + self.set_status(500) + self.write({"error": "Message sending failed"}) + + +class GetMessagesHandler(HiveMindHttpHandler): + + async def get(self): + try: + useragent, key = self.decode_auth() + if not key: + self.write({"error": "Missing authorization"}) + return + + # refuse if connect wasnt called first + if key not in CLIENTS: + self.write({"error": "Client is not connected"}) + return + + # send non-binary payloads to the client + messages = UNDELIVERED[key] + UNDELIVERED[key] = [] + self.write({"status": "messages retrieved", "messages": messages}) + except Exception as e: + LOG.error(f"Retrieving messages failed: {e}") + self.set_status(500) + self.write({"error": "Retrieving messages failed"}) + + +class GetBinMessagesHandler(HiveMindHttpHandler): + + async def get(self): + try: + useragent, key = self.decode_auth() + if not key: + self.write({"error": "Missing authorization"}) + return + + # refuse if connect wasnt called first + if key not in CLIENTS: + self.write({"error": "Client is not connected"}) + return + + # send non-binary payloads to the client + messages = UNDELIVERED_BIN[key] + UNDELIVERED_BIN[key] = [] + self.write({"status": "messages retrieved", "b64_messages": messages}) + except Exception as e: + LOG.error(f"Retrieving messages failed: {e}") + self.set_status(500) + self.write({"error": "Retrieving messages failed"}) diff --git a/hivemind_http_protocol/version.py b/hivemind_http_protocol/version.py new file mode 100644 index 0000000..3b6b9b5 --- /dev/null +++ b/hivemind_http_protocol/version.py @@ -0,0 +1,6 @@ +# START_VERSION_BLOCK +VERSION_MAJOR = 0 +VERSION_MINOR = 0 +VERSION_BUILD = 1 +VERSION_ALPHA = 1 +# END_VERSION_BLOCK diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9baa0a6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +tornado +hivemind-plugin-manager +poorman_handshake>=0.1.0 +pyOpenSSL \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..287ffff --- /dev/null +++ b/setup.py @@ -0,0 +1,55 @@ +import os +from setuptools import setup + +BASEDIR = os.path.abspath(os.path.dirname(__file__)) + + +def get_version(): + """ Find the version of the package""" + version_file = os.path.join(BASEDIR, 'hivemind_http_protocol', 'version.py') + major, minor, build, alpha = (None, None, None, None) + with open(version_file) as f: + for line in f: + if 'VERSION_MAJOR' in line: + major = line.split('=')[1].strip() + elif 'VERSION_MINOR' in line: + minor = line.split('=')[1].strip() + elif 'VERSION_BUILD' in line: + build = line.split('=')[1].strip() + elif 'VERSION_ALPHA' in line: + alpha = line.split('=')[1].strip() + + if ((major and minor and build and alpha) or + '# END_VERSION_BLOCK' in line): + break + version = f"{major}.{minor}.{build}" + if int(alpha) > 0: + version += f"a{alpha}" + return version + + +def required(requirements_file): + """ Read requirements file and remove comments and empty lines. """ + with open(os.path.join(BASEDIR, requirements_file), 'r') as f: + requirements = f.read().splitlines() + if 'MYCROFT_LOOSE_REQUIREMENTS' in os.environ: + print('USING LOOSE REQUIREMENTS!') + requirements = [r.replace('==', '>=').replace('~=', '>=') for r in requirements] + return [pkg for pkg in requirements + if pkg.strip() and not pkg.startswith("#")] + + +PLUGIN_ENTRY_POINT = 'hivemind-http-plugin=hivemind_http_protocol:HiveMindHttpProtocol' + +setup( + name='hivemind-http-protocol', + version=get_version(), + packages=['hivemind_http_protocol'], + url='https://github.com/JarbasHiveMind/hivemind-http-protocol', + license='Apache-2.0', + author='jarbasAi', + install_requires=required("requirements.txt"), + entry_points={'hivemind.network.protocol': PLUGIN_ENTRY_POINT}, + author_email='jarbasai@mailfence.com', + description='http network protocol for hivemind-core' +)