-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from openziti/ziti-hosting
Ziti hosting
- Loading branch information
Showing
12 changed files
with
320 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) NetFoundry Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from flask import Flask | ||
import openziti | ||
import sys | ||
|
||
app = Flask(__name__) | ||
bind_opts = {} # populated in main | ||
|
||
|
||
@openziti.zitify(bindings={ | ||
('1.2.3.4', '18080'): bind_opts | ||
}) | ||
def runApp(): | ||
from waitress import serve | ||
serve(app,host='1.2.3.4',port=18080) | ||
|
||
|
||
@app.route('/') | ||
def hello_world(): # put application's code here | ||
return 'Have some Ziti!' | ||
|
||
|
||
if __name__ == '__main__': | ||
bind_opts['ztx'] = sys.argv[1] | ||
bind_opts['service'] = sys.argv[2] | ||
runApp() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
flask | ||
waitress | ||
openziti |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
|
||
import urllib3 | ||
import openziti | ||
|
||
# to run this sample | ||
# set ZITI_IDENTITIES environment variable to location of your Ziti identity file | ||
# | ||
# python http-get.py <url> | ||
# url should be the intercept address of a ziti service | ||
if __name__ == '__main__': | ||
openziti.monkeypatch() | ||
http = urllib3.PoolManager() | ||
r = http.request('GET', sys.argv[1]) | ||
print("{0} {1}".format(r.status, r.reason)) | ||
print(r.data.decode('utf-8')) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) NetFoundry Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import sys | ||
import openziti | ||
|
||
|
||
def run(ziti_id, service): | ||
ztx = openziti.load(ziti_id) | ||
server = ztx.bind(service) | ||
server.listen() | ||
|
||
while True: | ||
conn, peer = server.accept() | ||
print(f'processing incoming client[{peer}]') | ||
with conn: | ||
count = 0 | ||
while True: | ||
data = conn.recv(1024) | ||
if not data: | ||
print(f'client finished after sending {count} bytes') | ||
break | ||
count += len(data) | ||
conn.sendall(data) | ||
|
||
|
||
if __name__ == '__main__': | ||
run(sys.argv[1], sys.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright (c) NetFoundry Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import sys | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
import time | ||
|
||
import openziti | ||
|
||
hostName = "localhost" | ||
serverPort = 8080 | ||
|
||
cfg = dict( | ||
ztx=sys.argv[1], | ||
service=sys.argv[2] | ||
) | ||
openziti.monkeypatch(bindings={(hostName, serverPort): cfg}) | ||
|
||
|
||
class MyServer(BaseHTTPRequestHandler): | ||
def do_GET(self): | ||
self.send_response(200) | ||
self.send_header("Content-type", "application/json") | ||
self.end_headers() | ||
msg = """{"msg": "Help! I was zitified!"}""" | ||
self.wfile.write(bytes(msg, "utf-8")) | ||
|
||
|
||
if __name__ == "__main__": | ||
webServer = HTTPServer((hostName, serverPort), MyServer) | ||
print("Server started http://%s:%s" % (hostName, serverPort)) | ||
|
||
try: | ||
webServer.serve_forever(poll_interval=600) | ||
except KeyboardInterrupt: | ||
pass | ||
|
||
webServer.server_close() | ||
print("Server stopped.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ tag_prefix = v | |
parentdir_prefix = openziti- | ||
|
||
[openziti] | ||
ziti_sdk_version = 0.28.4 | ||
ziti_sdk_version = 0.28.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) NetFoundry Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import socket as sock | ||
from . import zitisock | ||
|
||
|
||
_patch_methods = { | ||
"create_connection": zitisock.create_ziti_connection, | ||
"getaddrinfo": zitisock.ziti_getaddrinfo | ||
} | ||
|
||
|
||
def _patchedSocket(patch_opts): | ||
class patchedSocket(zitisock.ZitiSocket): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **dict(kwargs, opts=patch_opts)) | ||
return patchedSocket | ||
|
||
|
||
class MonkeyPatch(): | ||
def __init__(self, **kwargs): | ||
self.orig_socket = sock.socket | ||
sock.socket = _patchedSocket(kwargs) | ||
self.orig_methods = {m: sock.__dict__[m] for m, _ in | ||
_patch_methods.items()} | ||
for m_name, _ in _patch_methods.items(): | ||
sock.__dict__[m_name] = _patch_methods[m_name] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
for m_name, _ in self.orig_methods.items(): | ||
sock.__dict__[m_name] = self.orig_methods[m_name] | ||
|
||
|
||
|
||
|
||
def zitify(**zkwargs): | ||
def zitify_func(func): | ||
def zitified(*args, **kwargs): | ||
with MonkeyPatch(**zkwargs): | ||
func(*args, **kwargs) | ||
return zitified | ||
return zitify_func |
Oops, something went wrong.