Skip to content

Commit 0bcb7e5

Browse files
authored
Merge pull request #1 from openziti/initial-implementation
initial ziti-sdk-c wrap
2 parents d24905c + b7091ce commit 0bcb7e5

File tree

8 files changed

+297
-0
lines changed

8 files changed

+297
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# see:
2+
# https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-code-owners
3+
* @openziti/maintainers
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: mattermost-ziti-webhook
2+
on:
3+
create:
4+
delete:
5+
issues:
6+
issue_comment:
7+
pull_request_review:
8+
pull_request_review_comment:
9+
pull_request:
10+
push:
11+
fork:
12+
release:
13+
14+
jobs:
15+
mattermost-ziti-webhook:
16+
runs-on: macos-latest
17+
name: POST Webhook
18+
steps:
19+
- uses: openziti/ziti-webhook-action@main
20+
with:
21+
ziti-id: ${{ secrets.ZITI_MATTERMOST_IDENTITY }}
22+
webhook-url: ${{ secrets.ZITI_MATTERMOST_WEBHOOK_URL }}
23+
webhook-secret: ${{ secrets.ZITI_MATTERMOSTI_WEBHOOK_SECRET }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.idea/

setup.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2022. NetFoundry, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import setuptools
16+
from setuptools import setup
17+
18+
setup(
19+
name='openziti',
20+
version='0.1.0',
21+
description='Ziti Python SDK',
22+
url='https://github.com/openziti/ziti-sdk-py',
23+
author='Eugene Kobyakov',
24+
author_email='eugene@openziti.org',
25+
license='Apache 2.0',
26+
packages=['ziti'],
27+
install_requires=[],
28+
include_package_data=True,
29+
package_data={
30+
"": ["libziti.so"],
31+
},
32+
ext_modules=[
33+
setuptools.Extension("zitilib", [])
34+
],
35+
36+
classifiers=[
37+
'Development Status :: 1 - Planning',
38+
'Intended Audience :: Developers',
39+
'License :: OSI Approved :: Apache Software License',
40+
'Operating System :: POSIX :: Linux',
41+
'Programming Language :: Python :: 2',
42+
'Programming Language :: Python :: 2.7',
43+
'Programming Language :: Python :: 3',
44+
'Programming Language :: Python :: 3.4',
45+
'Programming Language :: Python :: 3.5',
46+
],
47+
)

ziti/__init__.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2022. NetFoundry, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os.path
16+
from ziti import zitilib
17+
from ziti import context
18+
from ziti import zitisock
19+
import socket
20+
21+
_ziti_identities = (os.getenv('ZITI_IDENTITIES') or "").split(';')
22+
_id_map = dict()
23+
24+
zitilib.init()
25+
26+
version = zitilib.version
27+
shutdown = zitilib.shutdown
28+
load = context.load_identity
29+
30+
for id in _ziti_identities:
31+
if id != '':
32+
load(id)
33+
34+
_patch_methods = dict(
35+
create_connection = zitisock.create_ziti_connection,
36+
getaddrinfo = zitisock.ziti_getaddrinfo
37+
)
38+
39+
40+
class monkeypatch(object):
41+
def __init__(self):
42+
self.orig_socket = socket.socket
43+
socket.socket = zitisock.ZitiSocket
44+
self.orig_methods = dict((m, socket.__dict__[m]) for m in _patch_methods)
45+
for m in _patch_methods:
46+
socket.__dict__[m] = _patch_methods[m]
47+
48+
def __enter__(self):
49+
return self
50+
51+
def __exit__(self, exc_type, exc_val, exc_tb):
52+
for m in self.orig_methods:
53+
socket.__dict__[m] = self.orig_methods[m]

ziti/context.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2022. NetFoundry, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import socket
16+
from ziti import zitilib
17+
18+
class ZitiContext:
19+
def __init__(self, ctx_p):
20+
self._ctx = ctx_p
21+
22+
def connect(self, addr):
23+
fd = zitilib.ziti_socket(socket.SOCK_STREAM)
24+
service = bytes(addr, encoding = "utf-8")
25+
zitilib.ziti_connect(fd, self._ctx, service)
26+
return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0, fd)
27+
28+
29+
def load_identity(path) -> ZitiContext:
30+
return ZitiContext(zitilib.load(path))

ziti/zitilib.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright (c) 2022. NetFoundry, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import ctypes
16+
import os
17+
from typing import Tuple
18+
19+
_mod_path = os.path.dirname(__file__)
20+
ziti = ctypes.CDLL(_mod_path + '/libziti.so')
21+
22+
23+
class _Ver(ctypes.Structure):
24+
_fields_ = [('version', ctypes.c_char_p), ('revision', ctypes.c_char_p)]
25+
26+
def __repr__(self):
27+
return '({0}, {1})'.format(self.version, self.revision)
28+
29+
30+
_ziti_version = ziti.ziti_get_version
31+
_ziti_version.restype = ctypes.POINTER(_Ver)
32+
33+
_load_ctx = ziti.Ziti_load_context
34+
_load_ctx.argtypes = [ctypes.POINTER(ctypes.c_char), ]
35+
_load_ctx.restype = ctypes.c_void_p
36+
37+
ziti_socket = ziti.Ziti_socket
38+
ziti_socket.argtypes = [ctypes.c_int]
39+
ziti_socket.restype = ctypes.c_int
40+
41+
ziti_connect = ziti.Ziti_connect
42+
ziti_connect.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_char_p]
43+
44+
45+
_ziti_connect_addr = ziti.Ziti_connect_addr
46+
_ziti_connect_addr.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
47+
_ziti_connect_addr.restype = ctypes.c_int
48+
49+
50+
def version():
51+
ver = _ziti_version().contents
52+
return ver
53+
54+
55+
def init():
56+
ziti.Ziti_lib_init()
57+
58+
59+
def shutdown():
60+
ziti.Ziti_lib_shutdown()
61+
62+
63+
def load(path):
64+
b = bytes(path, encoding = "utf-8")
65+
return _load_ctx(b)
66+
67+
68+
def connect(fd, addr: Tuple[str, int]):
69+
host = bytes(addr[0], encoding = 'utf-8')
70+
port = addr[1]
71+
return _ziti_connect_addr(fd, host, port)

ziti/zitisock.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) 2022. NetFoundry, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import socket
16+
from typing import Tuple
17+
from ziti import zitilib
18+
19+
py_socket = socket.socket
20+
21+
class ZitiSocket(py_socket):
22+
def __init__(self, af = -1, type = -1, proto = -1, fileno = None):
23+
self._ziti_af = af
24+
self._ziti_type = type
25+
self._ziti_proto = proto
26+
if fileno:
27+
py_socket.__init__(self, af, type, proto, fileno)
28+
return
29+
30+
self._zitifd = zitilib.ziti_socket(type)
31+
py_socket.__init__(self, af, type, proto, self._zitifd)
32+
33+
def connect(self, addr) -> None:
34+
if self._zitifd is None:
35+
pass
36+
37+
if isinstance(addr, Tuple):
38+
rc = zitilib.connect(self._zitifd, addr)
39+
if rc != 0:
40+
py_socket.close(self)
41+
self._zitifd = None
42+
py_socket.__init__(self, self._ziti_af, self._ziti_type, self._ziti_proto)
43+
py_socket.connect(self, addr)
44+
45+
def setsockopt(self, __level: int, __optname: int, __value: int | bytes) -> None:
46+
try:
47+
py_socket.setsockopt(self, __level, __optname, __value)
48+
except:
49+
pass
50+
51+
52+
def create_ziti_connection(address,
53+
timeout= socket._GLOBAL_DEFAULT_TIMEOUT,
54+
source_address=None):
55+
s = ZitiSocket(socket.SOCK_STREAM)
56+
s.connect(address)
57+
return s
58+
59+
60+
def ziti_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
61+
addrlist = []
62+
addr = (
63+
socket._intenum_converter(socket.AF_INET, socket.AddressFamily),
64+
socket._intenum_converter(type, socket.SocketKind),
65+
proto, '', (host,port))
66+
67+
addrlist.append(addr)
68+
return addrlist

0 commit comments

Comments
 (0)