Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added example, ssh cap

  • Loading branch information...
commit fe59340ceeed439887323229c6cf8a2b702af1d9 1 parent ab017db
@tomerfiliba authored
View
21 capabilities/compression.py
@@ -9,4 +9,23 @@
be disabled to save time
-"""
+"""
+import zlib
+
+
+class CompressedChannel(object):
+ def send(self, data, timeout):
+ data2 = zlib.compress(data, self.COMPRESSION_LEVEL)
+ return self.channel.send(data2, timeout)
+
+ def recv(self, timeout):
+ data, timeout = self.channel.recv(timeout)
+ data2 = zlib.decompress(data)
+ return data2, timeout
+
+
+class CompressionCapability(object):
+ def handshake(self, medium):
+ medium.channel = CompressedChannel(medium.channel)
+
+
View
41 capabilities/session.py
@@ -9,4 +9,45 @@
* reconnect_attempt_backoff_interval
"""
+import random
+import time
+import hashlib
+
+
+class SessionChannel(object):
+ MAX_RECONNECT_ATTEMPTS = 3
+ RECONNECT_ATTEMPT_TIMEOUT = 4
+ RECONNECT_ATTEMPT_MIN_INTERVAL = 1
+ RECONNECT_ATTEMPT_MAX_INTERVAL = 10
+
+ def __init__(self, channel):
+ self.channel = channel
+
+ @classmethod
+ def generate_cookie(cls):
+ data = repr(time.time()) + " " + repr(random.random())
+ return hashlib.sha1(data * 10).digest()
+
+ def reconnect(self):
+ self.channel.stream.reconnect()
+ renegotiate()
+
+ #
+ # channel API
+ #
+ def send(self, data, timeout):
+ try:
+ return self.channel.send(data, timeout)
+ except ChannelError:
+ pass
+
+ def recv(self, timeout):
+ try:
+ return self.channel.recv(timeout)
+ except ChannelError:
+ pass
+
+
+if __name__ == "__main__":
+ print SessionChannel.generate_cookie().encode("hex")
View
25 capabilities/ssh.py
@@ -0,0 +1,25 @@
+"""
+establishes ssh port-forwarding on both sides, closes the existing connection,
+and starts a new connection over the port-forwarding
+"""
+
+
+class SshClientCapability(object):
+ def handshake(self, medium):
+ server_port = medium.call.bind_server_locally()
+ os.system("ssh -fN -L%d:%s:%d" % (free_port, host, server_port))
+ old_sock = medium.channel.stream.sock
+ new_sock = SocketStream.connect("localhost", free_port)
+ medium.channel.stream.sock = new_sock
+ old_sock.close()
+
+class SshServerCapability(object):
+ def handshake(self, medium):
+ medium.serve()
+
+ def bind_server_locally(self):
+ return local_port
+
+
+
+
View
18 capabilities/tls.py
@@ -0,0 +1,18 @@
+import ssl
+
+
+class TlsClientCapability(object):
+ def handshake(self, medium):
+ stream = medium.channel.stream
+ if not isinstance(stream, SocketChannel):
+ raise TypeError
+ stream.sock = ssl.wrap_socket(stream.sock)
+
+class TlsServerCapability(object):
+ def handshake(self, medium):
+ stream = medium.channel.stream
+ if not isinstance(stream, SocketChannel):
+ raise TypeError
+ stream.sock = ssl.wrap_socket(stream.sock, server = True)
+
+
View
22 examples/http.py
@@ -0,0 +1,22 @@
+import layer5
+
+
+############## server ########################################################
+class HttpService(layer5.SimpleService):
+ def exposed_get(self, page, host, agent = None, encoding = None, charset = "utf8"):
+ pass
+ def exposed_put(self, page, host, data, agent = None, encoding = None, charset = "utf8"):
+ pass
+
+s = layer5.Server(HttpService, port = 80,
+ capabilities = [layer5.caps.TLS, layer5.caps.zlib])
+s.start()
+
+
+############## client ########################################################
+medium = layer5.connect_tcp(layer5.VoidService, "localhost", 80,
+ capabilities = [layer5.caps.TLS, layer5.caps.zlib])
+
+data = medium.call.get("/foo", "www.spam.eggs/foo", agent = "mozilla")
+
+
View
2  streams/base.py
@@ -30,6 +30,8 @@ def _closed(self):
raise NotImplementedError()
def fileno(self):
raise NotImplementedError()
+ def reconnect(self):
+ raise NotImplementedError()
def recv(self, count, timeout):
if timeout is not None:
View
14 streams/posix.py
@@ -32,10 +32,13 @@ def select2(timeout):
class SocketStream(BaseStream):
- __slots__ = ["sock"]
+ __slots__ = ["sock", "peer_info"]
- def __init__(self, sock):
+ def __init__(self, sock, peer_info = None):
self.sock = sock
+ if not peer_info:
+ peer_info = sock.getpeername()
+ self.peer_info = peer_info
@classmethod
def connect(cls, host, port, connect_timeout = 2):
@@ -43,7 +46,7 @@ def connect(cls, host, port, connect_timeout = 2):
sock.settimeout(connect_timeout)
sock.connect((host, port))
sock.settimeout(None)
- return cls(sock)
+ return cls(sock, (host, port, connect_timeout))
def _close(self):
try:
@@ -58,6 +61,11 @@ def _closed(self):
def fileno(self):
return self.sock.fileno()
+
+ def reconnect(self):
+ if not self.closed:
+ raise ValueError("")
+ return self.connect(*self.peer_info)
def read(self, count, timeout):
data = []
Please sign in to comment.
Something went wrong with that request. Please try again.