[Pkg-privacy-commits] [pyptlib] 15/136: A rough draft implementation of the framework, a manager for testing (instead of using Tor), and some transports

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:00 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository pyptlib.

commit e8c2256339a2c7760761b1a08bab39ba8f54a3e0
Author: Brandon Wiley <brandon at blanu.net>
Date:   Fri Jun 8 14:37:30 2012 -0500

    A rough draft implementation of the framework, a manager for testing (instead of using Tor), and some transports
---
 src/pyptlib/framework/client.py      | 60 ++++++++++++++++++++----------------
 src/pyptlib/framework/daemon.py      | 52 +++++++++++++++++++++++++++++++
 src/pyptlib/framework/proxy.py       | 27 ++++++++++++++++
 src/pyptlib/framework/server.py      | 46 +++++++++++++++++++--------
 src/pyptlib/framework/socks.py       | 30 ++++++++++++++++++
 src/pyptlib/manager/clientManager.py |  5 ++-
 src/pyptlib/manager/manager.py       |  8 ++++-
 src/pyptlib/manager/serverManager.py |  4 ++-
 src/pyptlib/transports/dummy.py      | 27 +++++++++++-----
 src/pyptlib/transports/rot13.py      | 14 +++++++++
 10 files changed, 223 insertions(+), 50 deletions(-)

diff --git a/src/pyptlib/framework/client.py b/src/pyptlib/framework/client.py
index fdca3f4..c27d09b 100644
--- a/src/pyptlib/framework/client.py
+++ b/src/pyptlib/framework/client.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python
+
 import sys
 import time
 
@@ -12,30 +14,34 @@ from monocle.stack import eventloop
 from monocle.stack.network import add_service, Service, Client, ConnectionLost
 from loopback import FakeSocket
 
-from shared import *
-from socks import *
-
- at _o
-def handle_socks(conn):
-  print('handle_socks')
-  yield readHandshake(conn)
-  print('read handshake')
-  yield sendHandshake(conn)
-  print('send handshake')
-  dest=yield readRequest(conn)
-  print('read request: '+str(dest))
-  yield sendResponse(dest, conn)
-  print('sent response')
-
-  addr, port=uncompact(dest)
-  print(addr)
-  print(port)
-
-  client = Client()
-  yield client.connect(addr, port)
-  print('connected '+str(addr)+', '+str(port))
-  monocle.launch(pump, conn, client, None)
-  yield pump(client, conn, None)
-
-add_service(Service(handle_socks, port=7051))
-eventloop.run()
+from socks import SocksHandler
+
+from config.client import ClientConfig
+from daemon import *
+
+class ManagedClient(Daemon):
+  def __init__(self):
+    try:
+      Daemon.__init__(ClientConfig(), SocksHandler())
+    except UnsupportedManagedTransportVersionException:
+      return
+    except NoSupportedTransportsException:
+      return
+
+    try:
+      self.launchClient(self.supportedTransport)
+      self.config.writeMethod(self.supportedTransport)
+    except TransportLaunchException as e:
+      self.config.writeMethodError(self.supportedTransport, e.message)
+
+    self.config.writeMethodEnd()
+    
+    self.run()
+      
+  def launchClient(self, name, port):
+    if name!=self.supportedTransport:
+      raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
+
+    client=DummyClient()
+    self.handler.setTransport(client)
+    add_service(Service(self.handler, port=port))    
diff --git a/src/pyptlib/framework/daemon.py b/src/pyptlib/framework/daemon.py
new file mode 100644
index 0000000..888ecd4
--- /dev/null
+++ b/src/pyptlib/framework/daemon.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+
+import sys
+import time
+
+from struct import unpack
+from socket import inet_ntoa
+
+import monocle
+from monocle import _o, Return
+monocle.init('tornado')
+
+from monocle.stack import eventloop
+from monocle.stack.network import add_service, Service, Client, ConnectionLost
+from loopback import FakeSocket
+
+from shared import *
+from socks import *
+
+class Daemon:
+  config=None
+  handler=None
+  
+  supportedTransportVersion='1'
+  supportedTransport='dummy'
+
+  def __init__(self, configManager, handler):
+    self.config=configManager
+    self.handler=handler
+    
+    if self.config.checkManagedTransportVersion(supportedTransportVersion):
+      self.config.writeVersion(supportedTransportVersion)
+    else:
+      self.config.writeVersionError()
+      raise UnsupportedManagedTransportVersionException()
+        
+    if not self.config.checkTransportEnabled(supportedTransport):
+      raise NoSupportedTransportsException()
+  
+  def run(self):
+    eventloop.run()
+    
+class UnsupportedManagedTransportVersionException(Exception):
+  pass
+
+class NoSupportedTransportsException(Exception):
+  pass
+
+class TransportLaunchException(Exception):
+  def __init__(self, message):
+    self.message=message
+  
\ No newline at end of file
diff --git a/src/pyptlib/framework/proxy.py b/src/pyptlib/framework/proxy.py
new file mode 100644
index 0000000..0bb95b0
--- /dev/null
+++ b/src/pyptlib/framework/proxy.py
@@ -0,0 +1,27 @@
+from struct import unpack
+from socket import inet_ntoa
+
+import monocle
+from monocle import _o, Return
+
+from dust.core.util import encode
+
+from shared import pump
+
+class ProxyHandler:
+  transport=None
+
+  def setTransport(self, transport):
+    self.transport=transport
+
+  @_o
+  def handle(self, conn):
+    print('connection')
+    client = Client()
+    yield client.connect('blanu.net', 7051)
+
+    coder=yield handshake(client)
+
+    monocle.launch(pump, conn, client, coder.encrypt)
+    yield pump(client, conn, coder.decrypt)
+
diff --git a/src/pyptlib/framework/server.py b/src/pyptlib/framework/server.py
index b805ff6..747ebdf 100644
--- a/src/pyptlib/framework/server.py
+++ b/src/pyptlib/framework/server.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python
+
 import sys
 import time
 
@@ -13,16 +15,34 @@ from monocle.stack.network import add_service, Service, Client
 
 from shared import pump
 
- at _o
-def handle_proxy(conn):
-  print('connection')
-  client = Client()
-  yield client.connect('blanu.net', 7051)
-
-  coder=yield handshake(client)
-
-  monocle.launch(pump, conn, client, coder.encrypt)
-  yield pump(client, conn, coder.decrypt)
-
-add_service(Service(handle_proxy, port=7050))
-eventloop.run()
+from config.server import ServerConfig
+from daemon import Daemon
+
+from proxy import ProxyHandler
+
+class ManagedServer(Daemon):
+  def __init__(self):
+    try:
+      Daemon.__init__(ServerConfig(), ProxyHandler())
+    except UnsupportedManagedTransportVersionException:
+      return
+    except NoSupportedTransportsException:
+      return
+
+    try:
+      self.launchServer(self.supportedTransport)
+      self.config.writeMethod(self.supportedTransport)
+    except TransportLaunchException as e:
+      self.config.writeMethodError(self.supportedTransport, e.message)
+
+    self.config.writeMethodEnd()
+    
+    self.run()
+    
+  def launchServer(self, name, port):
+    if name!=self.supportedTransport:
+      raise TransportLaunchException('Tried to launch unsupported transport %s' % (name))
+
+    client=DummyServer()
+    self.handler.setTransport(client)
+    add_service(Service(self.handler, port=port))   
diff --git a/src/pyptlib/framework/socks.py b/src/pyptlib/framework/socks.py
index 3aa337a..1ef84e9 100644
--- a/src/pyptlib/framework/socks.py
+++ b/src/pyptlib/framework/socks.py
@@ -6,6 +6,8 @@ from monocle import _o, Return
 
 from dust.core.util import encode
 
+from shared import pump
+
 def uncompact(x):
     ip, port = unpack("!4sH", x)
     return inet_ntoa(ip), port
@@ -39,3 +41,31 @@ def readRequest(input):
 @_o
 def sendResponse(dest, output):
   yield output.write(b"\x05\x00\x00\x01"+dest)
+
+class SocksHandler:
+  transport=None
+
+  def setTransport(self, transport):
+    self.transport=transport
+
+  @_o
+  def handle(self, conn):
+    print('handle_socks')
+    yield readHandshake(conn)
+    print('read handshake')
+    yield sendHandshake(conn)
+    print('send handshake')
+    dest=yield readRequest(conn)
+    print('read request: '+str(dest))
+    yield sendResponse(dest, conn)
+    print('sent response')
+
+    addr, port=uncompact(dest)
+    print(addr)
+    print(port)
+
+    client = Client()
+    yield client.connect(addr, port)
+    print('connected '+str(addr)+', '+str(port))
+    monocle.launch(pump, conn, client, None)
+    yield pump(client, conn, None)
diff --git a/src/pyptlib/manager/clientManager.py b/src/pyptlib/manager/clientManager.py
index e064db0..a3a901d 100644
--- a/src/pyptlib/manager/clientManager.py
+++ b/src/pyptlib/manager/clientManager.py
@@ -8,4 +8,7 @@ class ClientManager(Manager):
   
     os.environ['TOR_PT_CLIENT_TRANSPORTS']='dummy'
     
-    
\ No newline at end of file
+if __name__=='__main__':
+  manager=ClientManager()
+  manager.launch('pyptlib/framework/client.py')
+  
\ No newline at end of file
diff --git a/src/pyptlib/manager/manager.py b/src/pyptlib/manager/manager.py
index 68c3f12..7f335b1 100644
--- a/src/pyptlib/manager/manager.py
+++ b/src/pyptlib/manager/manager.py
@@ -1,8 +1,14 @@
 import os
+import subprocess
 
 class Manager:
   def __init__(self):
     os.environ['TOR_PT_STATE_LOCATION']='/'
     os.environ['TOR_PT_MANAGED_TRANSPORT_VER']='1'
-    
+
+  def launch(self, str):
+    p=subprocess.Popen(str, stdout=subprocess.PIPE)
+    f=p.stdout
+    b=f.read()
+    print(b)
     
\ No newline at end of file
diff --git a/src/pyptlib/manager/serverManager.py b/src/pyptlib/manager/serverManager.py
index e572d45..cce2214 100644
--- a/src/pyptlib/manager/serverManager.py
+++ b/src/pyptlib/manager/serverManager.py
@@ -11,4 +11,6 @@ class ServerManager(Manager):
     os.environ['TOR_PT_SERVER_BINDADDR']'dummy-127.0.0.1:46466'
     os.environ['TOR_PT_SERVER_TRANSPORTS']='dummy'
     
-    
\ No newline at end of file
+if __name__=='__main__':
+  manager=ServerManager()
+  manager.launch('pyptlib/framework/server.py')
diff --git a/src/pyptlib/transports/dummy.py b/src/pyptlib/transports/dummy.py
index 4a7b5f3..552b7b7 100644
--- a/src/pyptlib/transports/dummy.py
+++ b/src/pyptlib/transports/dummy.py
@@ -1,10 +1,23 @@
-class Dummy:
-  def __init__(self):
-    pass
+def rot13(data):
+  for x in range(len(data)):
+    ascii=ord(data[x])
+    if ascii>=97 and ascii<=122:  # a-z
+      data[x]=(((ascii-97)+13)%26)+97
+    elif ascii>=65 and ascii<=90: # A-Z
+      data[x]=(((ascii-65)+13)%26)+65
+  return data
 
-  def read(self, len):
-    pass
+class Rot13Client:
+  def encode(self, data):
+    return rot13(data)
 
-  def write(self, buf):
-    pass
+  def decode(self, data):
+    return rot13(data)
+
+class Rot13Server:
+  def encode(self, data):
+    return rot13(data)
+
+  def decode(self, data):
+    return rot13(data)
 
diff --git a/src/pyptlib/transports/rot13.py b/src/pyptlib/transports/rot13.py
new file mode 100644
index 0000000..06473f5
--- /dev/null
+++ b/src/pyptlib/transports/rot13.py
@@ -0,0 +1,14 @@
+class DummyClient:
+  def encode(self, data):
+    return data
+
+  def decode(self, data):
+    return data
+
+class DummyServer:
+  def encode(self, data):
+    return data
+
+  def decode(self, data):
+    return data
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git



More information about the Pkg-privacy-commits mailing list