[Pkg-privacy-commits] [pyptlib] 16/136: First draft of framework and some example frameworks, along with numerous bug fixes to get the config library working with the framework

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:01 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 f6ab4d7e0946b5f6c91818b0bce9af29a6ed11cf
Author: Brandon Wiley <brandon at blanu.net>
Date:   Fri Jun 15 12:09:19 2012 -0500

    First draft of framework and some example frameworks, along with numerous bug fixes to get the config library working with the framework
---
 src/pyptlib/config/client.py         |  14 ++--
 src/pyptlib/config/config.py         |  23 +++---
 src/pyptlib/config/server.py         |  56 +++++++--------
 src/pyptlib/framework/client.py      |  12 ++--
 src/pyptlib/framework/daemon.py      |  12 ++--
 src/pyptlib/framework/proxy.py       |   4 +-
 src/pyptlib/framework/server.py      |  39 +++++++---
 src/pyptlib/framework/shared.py      |   2 +-
 src/pyptlib/framework/socks.py       |   4 +-
 src/pyptlib/manager/clientManager.py |   4 +-
 src/pyptlib/manager/manager.py       |  13 ++--
 src/pyptlib/manager/serverManager.py |   9 +--
 src/pyptlib/transports/dummy.py      |  21 ++----
 src/pyptlib/transports/rot13.py      |  22 ++++--
 src/pyptlib/util.py                  | 135 +++++++++++++++++++++++++++++++++++
 15 files changed, 267 insertions(+), 103 deletions(-)

diff --git a/src/pyptlib/config/client.py b/src/pyptlib/config/client.py
index 59898dc..0cc268c 100644
--- a/src/pyptlib/config/client.py
+++ b/src/pyptlib/config/client.py
@@ -1,6 +1,6 @@
 import os
 
-from config import Config
+from pyptlib.config.config import Config
 
 """
 Configuration for a Pluggable Transport client.
@@ -14,19 +14,19 @@ class ClientConfig(Config):
   def __init__(self): # throws EnvError
     Config.__init__(self)
     
-    transports=self.get('TOR_PT_CLIENT_TRANSPORTS').split(',')
-    if '*' in transports:
-      allTransportsEnabled=True
-      transports.remove('*')      
+    self.transports=self.get('TOR_PT_CLIENT_TRANSPORTS').split(',')
+    if '*' in self.transports:
+      self.allTransportsEnabled=True
+      self.transports.remove('*')      
 
   # Returns a list of strings representing the client transports reported by Tor. If present, '*' is stripped from this list and used to set allTransportsEnabled to True.
   def getClientTransports(self):
-    return transports
+    return self.transports
 
   # Write a message to stdout specifying a supported transport
   # Takes: str, int, (str, int), [str], [str]
   def writeMethod(self, name, socksVersion, address, args, optArgs): # CMETHOD
-    methodLine='CMETHOD %s socks%s %s:%s' % (name, socksVersion, address[0], address[1]))
+    methodLine='CMETHOD %s socks%s %s:%s' % (name, socksVersion, address[0], address[1])
     if args and len(args)>0:
       methodLine=methodLine+' ARGS='+args.join(',')
     if optArgs and len(optArgs)>0:
diff --git a/src/pyptlib/config/config.py b/src/pyptlib/config/config.py
index a497d10..148cc7a 100644
--- a/src/pyptlib/config/config.py
+++ b/src/pyptlib/config/config.py
@@ -15,28 +15,28 @@ class Config:
   #Public methods
   
   def __init__(self): # throws EnvError
-    stateLocation=self.get('TOR_PT_STATE_LOCATION')
-    managedTransportVer=self.get('TOR_PT_MANAGED_TRANSPORT_VER').split(',')
+    self.stateLocation=self.get('TOR_PT_STATE_LOCATION')
+    self.managedTransportVer=self.get('TOR_PT_MANAGED_TRANSPORT_VER').split(',')
         
   # Returns a string representing the path to the state storage directory (which may not exist, but should be creatable) reported by Tor
   def getStateLocation(self):
-    return stateLocation
+    return self.stateLocation
 
   # Returns a list of strings representing supported versions as reported by Tor    
   def getManagedTransportVersions(self):
-    return managedTransportVer
+    return self.managedTransportVer
     
   # Checks to see if the specified version is included in those reported by Tor
   # Returns True if the version is included and False if it is not
   def checkManagedTransportVersion(self, version):
-    return version in managedTransportVer
+    return version in self.managedTransportVer
 
   # Returns a bool, True if the transport '*' was specified by Tor, otherwise False.
   def getAllTransportsEnabled(self):
-    return allTransportsEnabled
+    return self.allTransportsEnabled
     
   def checkTransportEnabled(self, transport):
-    return allTransportsEnabled or transport in transports    
+    return self.allTransportsEnabled or transport in self.transports    
 
   # Write a message to stdout specifying that an error parsing the environment variables has occurred
   # Takes: str
@@ -58,8 +58,13 @@ class Config:
     if key in os.environ:
       return os.environ[key]
     else:
-      raise EnvException()    
+      message="Missing environment variable %s" % (key)
+      self.writeEnvError(message)
+      raise EnvException(message)
 
 # Exception thrown when there is an error parsing the configuration parameters provided by Tor in environment variables    
 class EnvException(Exception):
-  pass
+  message=None
+
+  def __init__(self, message):
+    self.message=message
diff --git a/src/pyptlib/config/server.py b/src/pyptlib/config/server.py
index 827c5d0..809de3b 100644
--- a/src/pyptlib/config/server.py
+++ b/src/pyptlib/config/server.py
@@ -1,6 +1,6 @@
 import os
 
-from config import Config
+from pyptlib.config.config import Config
 
 """
 Configuration for a Pluggable Transport server.
@@ -18,34 +18,34 @@ class ServerConfig(Config):
   def __init__(self): # throws EnvError
     Config.__init__(self)
     
-    extendedServerPort=self.get('TOR_PT_EXTENDED_SERVER_PORT')
-    ORPort=self.get('TOR_PT_ORPORT')
+    self.extendedServerPort=self.get('TOR_PT_EXTENDED_SERVER_PORT')
+    self.ORPort=self.get('TOR_PT_ORPORT')
     
-    binds=self.get('TOR_PT_SERVER_BINADDR').split(',')
+    binds=self.get('TOR_PT_SERVER_BINDADDR').split(',')
     for bind in binds:
-      key,value=bind.split(',')
-      serverBindAddr[key]=value
+      key,value=bind.split('-')
+      self.serverBindAddr[key]=value
     
-    transports=self.get('TOR_PT_SERVER_TRANSPORTS').split(',')
-    if '*' in transports:
-      allTransportsEnabled=True
-      transports.remove('*')      
+    self.transports=self.get('TOR_PT_SERVER_TRANSPORTS').split(',')
+    if '*' in self.transports:
+      self.allTransportsEnabled=True
+      self.transports.remove('*')      
     
   # Returns a tuple (str,int) representing the address of the Tor server port as reported by Tor
   def getExtendedServerPort(self):
-    return extendedServerPort
+    return self.extendedServerPort
     
   # Returns a tuple (str,int) representing the address of the Tor OR port as reported by Tor
   def getORPort(self):
-    return ORPort
+    return self.ORPort
     
   # Returns a dict {str: (str,int)} representing the addresses for each transport as reported by Tor
   def getServerBindAddresses(self):
-    return serverBindAddr
+    return self.serverBindAddr
     
   # Returns a list of strings representing the server transports reported by Tor. If present, '*' is stripped from this list and used to set allTransportsEnabled to True.
   def getServerTransports(self):
-    return transports
+    return self.transports
     
   # Write a message to stdout specifying a supported transport
   # Takes: str, (str, int), MethodOptions
@@ -77,39 +77,39 @@ class MethodOptions:
 
   # Sets forward to True    
   def setForward(self):
-    forward=True
+    self.forward=True
   
   # Adds a key-value pair to args
   def addArg(self, key, value):
-    args[key]=value
+    self.args[key]=value
 
   # Adds a key-value pair to declare    
   def addDeclare(self, key, value):
-    declare[key]=value
+    self.declare[key]=value
     
   # Sets useExtendedPort to True
   def setUserExtendedPort(self):
-    useExtendedPort=True
+    self.useExtendedPort=True
 
   def __str__(self):
     options=[]
-    if forward:
+    if self.forward:
       options.append('FORWARD:1')
-    if len(args)>0:
+    if len(self.args)>0:
       argstr='ARGS:'
-      for key in args:
-        value=args[key]
+      for key in self.args:
+        value=self.args[key]
         argstr=argstr+key+'='+value+','
       argstr=argstr[:-1] # Remove trailing comma
       options.append(argstr)
-    if len(declare)>0:
+    if len(self.declare)>0:
       decs='DECLARE:'
-      for key in declare:
-        value=args[key]
-        argstr=argstr+key+'='+value+','
+      for key in self.declare:
+        value=self.declare[key]
+        decs=decs+key+'='+value+','
       decs=decs[:-1] # Remove trailing comma      
       options.append(decs)
-    if useExtendedPort:
+    if self.useExtendedPort:
       options.append('USE-EXTENDED-PORT:1')
 
-    return options.join(' ')      
+    return ' '.join(options)
diff --git a/src/pyptlib/framework/client.py b/src/pyptlib/framework/client.py
old mode 100644
new mode 100755
index c27d09b..77c3b7f
--- a/src/pyptlib/framework/client.py
+++ b/src/pyptlib/framework/client.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python -u
 
 import sys
 import time
@@ -12,17 +12,17 @@ monocle.init('tornado')
 
 from monocle.stack import eventloop
 from monocle.stack.network import add_service, Service, Client, ConnectionLost
-from loopback import FakeSocket
+from pyptlib.framework.loopback import FakeSocket
 
-from socks import SocksHandler
+from pyptlib.framework.socks import SocksHandler
 
-from config.client import ClientConfig
-from daemon import *
+from pyptlib.config.client import ClientConfig
+from pyptlib.framework.daemon import *
 
 class ManagedClient(Daemon):
   def __init__(self):
     try:
-      Daemon.__init__(ClientConfig(), SocksHandler())
+      Daemon.__init__(self, ClientConfig(), SocksHandler())
     except UnsupportedManagedTransportVersionException:
       return
     except NoSupportedTransportsException:
diff --git a/src/pyptlib/framework/daemon.py b/src/pyptlib/framework/daemon.py
index 888ecd4..cc55605 100644
--- a/src/pyptlib/framework/daemon.py
+++ b/src/pyptlib/framework/daemon.py
@@ -12,10 +12,10 @@ monocle.init('tornado')
 
 from monocle.stack import eventloop
 from monocle.stack.network import add_service, Service, Client, ConnectionLost
-from loopback import FakeSocket
+from pyptlib.framework.loopback import FakeSocket
 
-from shared import *
-from socks import *
+from pyptlib.framework.shared import *
+from pyptlib.framework.socks import *
 
 class Daemon:
   config=None
@@ -28,13 +28,13 @@ class Daemon:
     self.config=configManager
     self.handler=handler
     
-    if self.config.checkManagedTransportVersion(supportedTransportVersion):
-      self.config.writeVersion(supportedTransportVersion)
+    if self.config.checkManagedTransportVersion(self.supportedTransportVersion):
+      self.config.writeVersion(self.supportedTransportVersion)
     else:
       self.config.writeVersionError()
       raise UnsupportedManagedTransportVersionException()
         
-    if not self.config.checkTransportEnabled(supportedTransport):
+    if not self.config.checkTransportEnabled(self.supportedTransport):
       raise NoSupportedTransportsException()
   
   def run(self):
diff --git a/src/pyptlib/framework/proxy.py b/src/pyptlib/framework/proxy.py
index 0bb95b0..68d9a16 100644
--- a/src/pyptlib/framework/proxy.py
+++ b/src/pyptlib/framework/proxy.py
@@ -4,9 +4,9 @@ from socket import inet_ntoa
 import monocle
 from monocle import _o, Return
 
-from dust.core.util import encode
+from pyptlib.util import encode
 
-from shared import pump
+from pyptlib.framework.shared import pump
 
 class ProxyHandler:
   transport=None
diff --git a/src/pyptlib/framework/server.py b/src/pyptlib/framework/server.py
old mode 100644
new mode 100755
index 747ebdf..c93a16d
--- a/src/pyptlib/framework/server.py
+++ b/src/pyptlib/framework/server.py
@@ -1,5 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env python -u
 
+import os
 import sys
 import time
 
@@ -13,30 +14,42 @@ monocle.init('tornado')
 from monocle.stack import eventloop
 from monocle.stack.network import add_service, Service, Client
 
-from shared import pump
+from pyptlib.framework.shared import pump
 
-from config.server import ServerConfig
-from daemon import Daemon
+from pyptlib.config.config import EnvException
+from pyptlib.config.server import ServerConfig, MethodOptions
+from pyptlib.framework.daemon import *
 
-from proxy import ProxyHandler
+from pyptlib.framework.proxy import ProxyHandler
+
+from pyptlib.transports.dummy import DummyServer
 
 class ManagedServer(Daemon):
   def __init__(self):
     try:
-      Daemon.__init__(ServerConfig(), ProxyHandler())
+      print('init')
+      Daemon.__init__(self, ServerConfig(), ProxyHandler())
+    except EnvException:
+      print('error 0')
+      return
     except UnsupportedManagedTransportVersionException:
+      print('error 1')
       return
     except NoSupportedTransportsException:
+      print('error 2')
       return
 
     try:
-      self.launchServer(self.supportedTransport)
-      self.config.writeMethod(self.supportedTransport)
+      print('launch')
+      self.launchServer(self.supportedTransport, 8182)
+      self.config.writeMethod(self.supportedTransport, ('127.0.0.1', 8182), MethodOptions())
     except TransportLaunchException as e:
+      print('error 3')
       self.config.writeMethodError(self.supportedTransport, e.message)
 
     self.config.writeMethodEnd()
     
+    print('run')
     self.run()
     
   def launchServer(self, name, port):
@@ -46,3 +59,13 @@ class ManagedServer(Daemon):
     client=DummyServer()
     self.handler.setTransport(client)
     add_service(Service(self.handler, port=port))   
+
+if __name__=='__main__':
+  sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
+  print('main')
+  sys.stdout.flush()
+  import time
+  while True:
+    time.sleep(1000)
+#  server=ManagedServer()
+  
\ No newline at end of file
diff --git a/src/pyptlib/framework/shared.py b/src/pyptlib/framework/shared.py
index 24d35ee..274b825 100644
--- a/src/pyptlib/framework/shared.py
+++ b/src/pyptlib/framework/shared.py
@@ -3,7 +3,7 @@ from monocle import _o, Return
 
 from monocle.stack.network import ConnectionLost
 
-from dust.core.util import encode
+from pyptlib.util import encode
 
 @_o
 def pump(input, output, transform, debug=False):
diff --git a/src/pyptlib/framework/socks.py b/src/pyptlib/framework/socks.py
index 1ef84e9..c5659f0 100644
--- a/src/pyptlib/framework/socks.py
+++ b/src/pyptlib/framework/socks.py
@@ -4,9 +4,9 @@ from socket import inet_ntoa
 import monocle
 from monocle import _o, Return
 
-from dust.core.util import encode
+from pyptlib.util import encode
 
-from shared import pump
+from pyptlib.framework.shared import pump
 
 def uncompact(x):
     ip, port = unpack("!4sH", x)
diff --git a/src/pyptlib/manager/clientManager.py b/src/pyptlib/manager/clientManager.py
index a3a901d..8ce6a0c 100644
--- a/src/pyptlib/manager/clientManager.py
+++ b/src/pyptlib/manager/clientManager.py
@@ -1,6 +1,6 @@
 import os
 
-from manager import manager
+from pyptlib.manager.manager import Manager
 
 class ClientManager(Manager):
   def __init__(self):
@@ -10,5 +10,5 @@ class ClientManager(Manager):
     
 if __name__=='__main__':
   manager=ClientManager()
-  manager.launch('pyptlib/framework/client.py')
+  manager.launch('src/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 7f335b1..948b568 100644
--- a/src/pyptlib/manager/manager.py
+++ b/src/pyptlib/manager/manager.py
@@ -1,4 +1,5 @@
 import os
+import sys
 import subprocess
 
 class Manager:
@@ -6,9 +7,9 @@ class Manager:
     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
+  def launch(self, path):
+    p=subprocess.Popen(path, stdout=subprocess.PIPE)
+    for b in p.stdout:
+      print('b: '+str(b))
+      sys.stdout.flush()
+    print('Done!')
diff --git a/src/pyptlib/manager/serverManager.py b/src/pyptlib/manager/serverManager.py
index cce2214..98df2d8 100644
--- a/src/pyptlib/manager/serverManager.py
+++ b/src/pyptlib/manager/serverManager.py
@@ -1,16 +1,17 @@
 import os
 
-from manager import Manager
+from pyptlib.manager.manager import Manager
 
 class ServerManager(Manager):
   def __init__(self):
     Manager.__init__(self)
   
     os.environ['TOR_PT_EXTENDED_SERVER_PORT']='127.0.0.1:22211'
-    os.environ['TOR_PT_ORPORT']='127.0.0.1:43210'),
-    os.environ['TOR_PT_SERVER_BINDADDR']'dummy-127.0.0.1:46466'
+    os.environ['TOR_PT_ORPORT']='127.0.0.1:43210'
+    os.environ['TOR_PT_SERVER_BINDADDR']='dummy-127.0.0.1:46466'
     os.environ['TOR_PT_SERVER_TRANSPORTS']='dummy'
     
 if __name__=='__main__':
   manager=ServerManager()
-  manager.launch('pyptlib/framework/server.py')
+#  manager.launch('src/pyptlib/framework/server.py')
+  manager.launch(['python', '-u', 'test.py'])
\ No newline at end of file
diff --git a/src/pyptlib/transports/dummy.py b/src/pyptlib/transports/dummy.py
index 552b7b7..06473f5 100644
--- a/src/pyptlib/transports/dummy.py
+++ b/src/pyptlib/transports/dummy.py
@@ -1,23 +1,14 @@
-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
-
-class Rot13Client:
+class DummyClient:
   def encode(self, data):
-    return rot13(data)
+    return data
 
   def decode(self, data):
-    return rot13(data)
+    return data
 
-class Rot13Server:
+class DummyServer:
   def encode(self, data):
-    return rot13(data)
+    return data
 
   def decode(self, data):
-    return rot13(data)
+    return data
 
diff --git a/src/pyptlib/transports/rot13.py b/src/pyptlib/transports/rot13.py
index 06473f5..a050b8e 100644
--- a/src/pyptlib/transports/rot13.py
+++ b/src/pyptlib/transports/rot13.py
@@ -1,14 +1,22 @@
-class DummyClient:
+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
+
+class Rot13Client:
   def encode(self, data):
-    return data
+    return rot13(data)
 
   def decode(self, data):
-    return data
+    return rot13(data)
 
-class DummyServer:
+class Rot13Server:
   def encode(self, data):
-    return data
+    return rot13(data)
 
   def decode(self, data):
-    return data
-
+    return rot13(data)
\ No newline at end of file
diff --git a/src/pyptlib/util.py b/src/pyptlib/util.py
new file mode 100644
index 0000000..e307e5c
--- /dev/null
+++ b/src/pyptlib/util.py
@@ -0,0 +1,135 @@
+import re
+import sys
+import binascii
+
+from struct import pack, unpack
+from socket import inet_aton, inet_ntoa
+
+v3=(sys.version[0]=='3')
+
+def uncompact(x):
+    ip, port = unpack("!4sH", x)
+    return inet_ntoa(ip), port
+
+def encode(s):
+  return binascii.hexlify(s).decode('ascii')
+
+def decode(s):
+  return binascii.unhexlify(s.encode('ascii'))
+
+def encodeAddress(addr):
+  ip=addr[0]
+  if ip=='':
+    ip='::'
+  port=addr[1]
+  if '.' in ip:
+    return ip+':'+str(port)
+  else:
+    return '['+ip+']:'+str(port)
+
+def decodeAddress(s):
+  if '.' in s:
+    parts=s.split(':')
+    return (parts[0], int(parts[1]), False)
+  else:
+    m=re.match('\[([0-9a-f:]+)\]:([0-9]+)', s)
+    return (m.group(1), int(m.group(2)), True)
+
+def getAddress(port):
+  return encodeAddress((getPublicIP(), port))
+
+def splitFields(msg, fields, optionalData=False):
+  values=[]
+  for field in fields:
+    value=msg[:field]
+    msg=msg[field:]
+    values.append(value)
+  if len(msg)>0:
+    values.append(msg)
+  elif optionalData:
+    values.append(None)
+  return values
+
+def splitField(msg, field):
+  return msg[:field], msg[field:]
+
+def decodeFlags(flagsByte):
+  from bitstring import BitString
+  bits=BitString(bytes=flagsByte)
+  bools=[]
+  for x in range(bits.length):
+    bools.append(bits.readbit().uint==1)
+  return bools
+
+def encodeFlags(bools):
+  from bitstring import BitString
+  bits=BitString()
+  for bool in bools:
+    if bool:
+      bits.append(BitString('0b1'))
+    else:
+      bits.append(BitString('0b0'))
+  return bits.bytes
+
+def fill(bs, size):
+  while len(bs)<size:
+    if v3:
+      filler=bytes('\x00', 'ascii')
+    else:
+      filler='\x00'
+    bs=bs+filler
+  return bs
+
+def xor(a, b):
+  if len(a)!=len(b):
+    print('xor parameters must be the same length:', len(a), len(b))
+    return None
+  if v3:
+    c=bytearray()
+    for x in range(len(a)):
+      c.append(a[x] ^ b[x])
+    return bytes(c)
+  else:
+    c=''
+    for x in range(len(a)):
+      c=c+chr(ord(a[x]) ^ ord(b[x]))
+    return c
+
+if v3:
+  from urllib.request import urlopen
+else:
+  from urllib2 import urlopen
+
+def getPublicIP(v6=True):
+  if v6:
+#    try:
+      text=urlopen("http://ipv6.ip6.me/").read()
+      if v3:
+        match=re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
+      else:
+        match=re.search("\+3>([^<]+)<", text)
+      ip=match.group(1)
+      ip=ip.decode('ascii')
+      return ip
+#    except Exception as e:
+#      print(e)
+#      ip=urlopen("http://whatismyv6ip.com/myip").read()
+#      return ip.decode('ascii')
+  else:
+    text=urlopen("http://ip4.me/").read()
+    if v3:
+      match=re.search(bytes("\+3>([^<]+)<", 'ascii'), text)
+    else:
+      match=re.search("\+3>([^<]+)<", text)
+#     ip=urlopen("http://whatismyv6ip.com/myip").read()
+#     return ip.decode('ascii')
+    ip=match.group(1)
+    ip=ip.decode('ascii')
+    return ip
+
+def randomPort():
+  import random
+  minPort=5000
+  maxPort=10000
+  port=random.randint(minPort, maxPort)
+  return port

-- 
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