mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 08:44:14 +01:00
updated the zrok sdk to be simpler
This commit is contained in:
parent
7af28dada9
commit
4d14617ed7
@ -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)
|
||||
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()
|
@ -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
|
||||
|
@ -1,4 +1,2 @@
|
||||
from . import environment
|
||||
from . import access, decor, model, share, overview
|
||||
|
||||
monkeypatch = decor.MonkeyPatch
|
||||
from . import access, decor, model, share, overview
|
@ -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
|
9
sdk/python/sdk/zrok/zrok/dialer.py
Normal file
9
sdk/python/sdk/zrok/zrok/dialer.py
Normal file
@ -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
|
26
sdk/python/sdk/zrok/zrok/listener.py
Normal file
26
sdk/python/sdk/zrok/zrok/listener.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user