diff --git a/sdk/python/examples/pastebin.py b/sdk/python/examples/pastebin.py index 991cf9e0..6466052b 100755 --- a/sdk/python/examples/pastebin.py +++ b/sdk/python/examples/pastebin.py @@ -3,21 +3,17 @@ import argparse import sys import os import zrok +import zrok.listener +import zrok.dialer from zrok.model import AccessRequest, ShareRequest -from http.server import BaseHTTPRequestHandler, HTTPServer -import urllib3 +import signal +import threading -class MyServer(BaseHTTPRequestHandler): - def __init__(self, data, *args, **kwargs): - self.data = data - super(MyServer, self).__init__(*args, **kwargs) +exit_signal = threading.Event() - def do_GET(self): - self.send_response(200) - self.send_header("Content-type", "text/plain") - self.send_header("Content-length", len(self.data)) - self.end_headers() - self.wfile.write(bytes(self.data, "utf-8")) +def signal_handler(signum, frame): + print("\nCtrl-C detected. Next connection will close server") + exit_signal.set() class copyto: def handle(self, *args, **kwargs): @@ -34,18 +30,18 @@ class copyto: sys.exit(1) data = self.loadData() - def handler(*args): - MyServer(data, *args) - zrok.monkeypatch(bindHost="127.0.0.1", bindPort=8082, root=root, shrToken=shr.Token) - webServer = HTTPServer(("127.0.0.1", 8082), handler) print("access your pastebin using 'pastebin.py pastefrom " + shr.Token + "'") try: - webServer.serve_forever(poll_interval=600) + with zrok.listener.Listener(shr.Token, root) as server: + while not exit_signal.is_set(): + conn, peer = server.accept() + with conn: + conn.sendall(data.encode('utf-8')) + except KeyboardInterrupt: pass - webServer.server_close() zrok.share.DeleteShare(root, shr) print("Server stopped.") @@ -67,16 +63,9 @@ def pastefrom(options): print("unable to create access", e) sys.exit(1) - zrok.monkeypatch(bindHost="127.0.0.1", bindPort=8082, root=root, shrToken=options.shrToken) - - http = urllib3.PoolManager() - try: - r = http.request('GET', "http://" + options.shrToken) - except Exception as e: - print("Error on request: ", e) - zrok.access.DeleteAccess(root, acc) - return - print(r.data.decode('utf-8')) + client = zrok.dialer.Dialer(options.shrToken, root) + data = client.recv(1024) + print(data.decode('utf-8')) try: zrok.access.DeleteAccess(root, acc) except Exception as e: @@ -97,4 +86,9 @@ if __name__ == "__main__": parser_pastefrom.add_argument("shrToken") options = parser.parse_args() - options.func(options) \ No newline at end of file + signal.signal(signal.SIGINT, signal_handler) + # Create a separate thread to run the server so we can respond to ctrl-c when in 'accept' + server_thread = threading.Thread(target=options.func, args=[options]) + server_thread.start() + + server_thread.join() \ No newline at end of file diff --git a/sdk/python/sdk/zrok/setup.py b/sdk/python/sdk/zrok/setup.py index 7889aa74..e8eb84d5 100644 --- a/sdk/python/sdk/zrok/setup.py +++ b/sdk/python/sdk/zrok/setup.py @@ -2,9 +2,9 @@ from setuptools import setup, find_packages # noqa: H301 import os NAME = "zrok_sdk" -VERSION = "dev" +VERSION = "0.0.0.dev" try: - VERSION += '.dev' + os.environ['ZROK_VERSION'] + VERSION = os.environ['ZROK_VERSION'] except KeyError: pass # To install the library, run the following diff --git a/sdk/python/sdk/zrok/zrok/__init__.py b/sdk/python/sdk/zrok/zrok/__init__.py index c0c01b1e..ba6933b4 100644 --- a/sdk/python/sdk/zrok/zrok/__init__.py +++ b/sdk/python/sdk/zrok/zrok/__init__.py @@ -1,4 +1,2 @@ from . import environment -from . import access, decor, model, share, overview - -monkeypatch = decor.MonkeyPatch \ No newline at end of file +from . import access, decor, model, share, overview \ No newline at end of file diff --git a/sdk/python/sdk/zrok/zrok/decor.py b/sdk/python/sdk/zrok/zrok/decor.py deleted file mode 100644 index ca877ab0..00000000 --- a/sdk/python/sdk/zrok/zrok/decor.py +++ /dev/null @@ -1,22 +0,0 @@ -import openziti -from zrok.environment.root import Root - -class MonkeyPatch(openziti.monkeypatch): - def __init__(self, bindHost: str, bindPort: int, root: Root, shrToken: str, **kwargs): - zif = root.ZitiIdentityNamed(root.EnvironmentIdentityName()) - cfg = dict(ztx=openziti.load(zif), service=shrToken) - super(MonkeyPatch, self).__init__(bindings={(bindHost, bindPort):cfg}) - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - super(MonkeyPatch, self).__exit__(exc_type, exc_val, exc_tb) - -def zrok(bindHost: str, bindPort: int, root: Root, shrToken: str, **zkwargs): - def zrockify_func(func): - def zrockified(*args, **kwargs): - with MonkeyPatch(bindHost=bindHost, bindPort=bindPort, root=root, shrToken=shrToken, **zkwargs): - func(*args, **kwargs) - return zrockified - return zrockify_func \ No newline at end of file diff --git a/sdk/python/sdk/zrok/zrok/dialer.py b/sdk/python/sdk/zrok/zrok/dialer.py new file mode 100644 index 00000000..37d7fdd0 --- /dev/null +++ b/sdk/python/sdk/zrok/zrok/dialer.py @@ -0,0 +1,9 @@ +from zrok.environment.root import Root +import openziti +from socket import SOCK_STREAM + +def Dialer(shrToken: str, root: Root) -> openziti.zitisock.ZitiSocket: + openziti.load(root.ZitiIdentityNamed(root.EnvironmentIdentityName())) + client = openziti.socket(type = SOCK_STREAM) + client.connect((shrToken, 1)) + return client \ No newline at end of file diff --git a/sdk/python/sdk/zrok/zrok/listener.py b/sdk/python/sdk/zrok/zrok/listener.py new file mode 100644 index 00000000..bce83cb7 --- /dev/null +++ b/sdk/python/sdk/zrok/zrok/listener.py @@ -0,0 +1,26 @@ +from zrok.environment.root import Root +import openziti + +class Listener(): + shrToken: str + root: Root + __server: openziti.zitisock.ZitiSocket + + def __init__(self, shrToken: str, root: Root): + self.shrToken = shrToken + self.root = root + ztx = openziti.load(self.root.ZitiIdentityNamed(self.root.EnvironmentIdentityName())) + self.__server = ztx.bind(self.shrToken) + + def __enter__(self) -> openziti.zitisock.ZitiSocket: + self.listen() + return self.__server + + def __exit__(self, exception_type, exception_value, exception_traceback): + self.close() + + def listen(self): + self.__server.listen() + + def close(self): + self.__server.close() \ No newline at end of file