diff options
Diffstat (limited to 'gpg_websocket.py')
-rw-r--r-- | gpg_websocket.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/gpg_websocket.py b/gpg_websocket.py new file mode 100644 index 0000000..2a18eb1 --- /dev/null +++ b/gpg_websocket.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +# gpg_websocket - websocket server for gpg_over_web +# +# Copyright (C) 2023 Salahuddin <salahuddin@member.fsf.org> +# +# 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/>. + +# +# There are two different gnupg bindings. This script is using python3-gpg +# +# python3-gpg - Python interface to the GPGME GnuPG encryption library (Python 3) +# python3-gnupg - Python wrapper for the GNU Privacy Guard (Python 3.x) + + +import gpg +import asyncio +from websockets.server import serve + +async def gpg_over_web(websocket): + ctx = gpg.Context() + async for message in websocket: + if len(message) == 0: + return "" + try: + plaintext, result, verify_result = ctx.decrypt(message.encode()) + except gpg.errors.GPGMEError as e: + plaintext = "--- gpg error ---".encode() + print(e) + await websocket.send(plaintext.decode("utf-8")) + +async def main(): + async with serve(gpg_over_web, "localhost", 8765): + await asyncio.Future() + +asyncio.run(main()) |