aboutsummaryrefslogtreecommitdiff
path: root/gpg_websocket.py
blob: 2a18eb1b1ed7d9fdf7813759dcfeccaee5402d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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())