From c9f21862006f50937f22f51155ee81ff47399730 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 7 Mar 2020 16:27:55 +0100 Subject: bump --- notmuch-tcp-client | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 notmuch-tcp-client (limited to 'notmuch-tcp-client') diff --git a/notmuch-tcp-client b/notmuch-tcp-client new file mode 100644 index 0000000..b29d6b2 --- /dev/null +++ b/notmuch-tcp-client @@ -0,0 +1,61 @@ +#!@python@/bin/python + +from os import environ +from os.path import expanduser +import socket +import ssl +from shlex import quote +from sys import argv, stdin, stdout +from multiprocessing import Process +from select import select + + +port = environ.get('NOTMUCH_TCP') +host = environ.get('NOTMUCH_HOST') +hostname = socket.gethostname() + +if port is None: + port = 2010 +if host is None: + host = "odin.asgard.yggdrasil" + + +sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) +sslcontext.load_verify_locations(cafile = expanduser('~/.notmuch-tcp/ca.pem')) +sslcontext.load_cert_chain(certfile = expanduser(f"~/.notmuch-tcp/{hostname}.pem"), keyfile = expanduser(f"~/.notmuch-tcp/{hostname}.key")) + +with socket.create_connection((host, port)) as sock: + with sslcontext.wrap_socket(sock, server_hostname = host) as ssock: + def send_args(): + escaped_args = ' '.join(map(quote, argv[1:])) + ssock.sendall(f"{escaped_args}\n".encode()) + + def send_stdin(): + with open(0, 'rb') as stdin_bin: + while True: + to_send = stdin_bin.read(256) + + if to_send: + ssock.sendall(to_send) + else: + break + + def recv_stdout(): + with open(1, 'wb') as stdout_bin: + while True: + ready = select([ssock], [], [], 5) + if ready[0]: + received = ssock.recv(256) + if len(received) <= 0: + break + stdout_bin.write(received) + else: + break + + send_args() + send = Process(target = send_stdin) + recv = Process(target = recv_stdout) + send.start() + recv.start() + recv.join() + send.terminate() -- cgit v1.2.3