[Pkg-privacy-commits] [pyptlib] 103/136: Parse the TOR_PT_SERVER_TRANSPORT_OPTIONS environment variable.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:15 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 5c1478642c542681a19d226fb1ae31ba45eb58cd
Author: George Kadianakis <desnacked at riseup.net>
Date:   Sun Sep 8 18:09:51 2013 +0300

    Parse the TOR_PT_SERVER_TRANSPORT_OPTIONS environment variable.
---
 pyptlib/server_config.py    | 49 +++++++++++++++++++++++++++++++++++++++++++--
 pyptlib/test/test_server.py | 19 ++++++++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/pyptlib/server_config.py b/pyptlib/server_config.py
index ac7ce5b..5f66f4a 100644
--- a/pyptlib/server_config.py
+++ b/pyptlib/server_config.py
@@ -10,6 +10,33 @@ import pyptlib.util as util
 
 from pyptlib.config import env_has_k, get_env, SUPPORTED_TRANSPORT_VERSIONS
 
+def validate_transport_options(string):
+    """
+    Parse transport options.
+    :param str optstring: Example input: 'scramblesuit:k=v;scramblesuit:k2=v2;obs3fs:k=v'
+    :returns: {'obfs3': {'k':'v'}, 'scramblesuit': {'k2' : 'v2', 'k' : 'v'} }
+    """
+    transport_args = {}
+
+    params = string.split(';')
+    for param in params:
+        try:
+            (name, kv_string) = param.split(':')
+        except ValueError:
+            raise ValueError("Invalid options string (%s)" % param)
+
+        if name not in transport_args:
+            transport_args[name] = {}
+
+        try:
+            (key, value) = kv_string.split('=')
+        except ValueError:
+            raise ValueError("Not a k=v value (%s)" % kv_string)
+
+        transport_args[name][key] = value
+
+    return transport_args
+
 class ServerConfig(config.Config):
     """
     A client-side pyptlib configuration.
@@ -18,6 +45,8 @@ class ServerConfig(config.Config):
     :var tuple extendedORPort: (ip,port) pointing to Tor's Extended ORPort. None if Extended ORPort is not supported.
     :var dict serverBindAddr: A dictionary {<transport> : [<addr>, <port>]}, where <transport> is the name of the transport that must be spawned, and [<addr>, <port>] is a list containing the location where that transport should bind. The dictionary can be empty.
     :var string authCookieFile: String representing the filesystem path where the Extended ORPort Authentication cookie is stored. None if Extended ORPort authentication is not supported.
+    :var dict serverTransportOptions: Dictionary containing user-provided parameters that must be passed to the pluggable transports.
+        Example: {'obfs3': {'k':'v'}, 'scramblesuit': {'k2' : 'v2', 'k' : 'v'} }
     """
 
     @classmethod
@@ -73,6 +102,13 @@ class ServerConfig(config.Config):
             return transports
         transports = get_env('TOR_PT_SERVER_TRANSPORTS', validate_transports)
 
+        def validate_transport_options(k, v):
+            if v is None:
+                return None
+            serverTransportOptions = env_has_k(k, v)
+            return validate_transport_options(serverTransportOptions)
+        transport_options = get_env('TOR_PT_SERVER_TRANSPORT_OPTIONS', validate_transport_options)
+
         return cls(
             stateLocation = get_env('TOR_PT_STATE_LOCATION'),
             managedTransportVer = get_env('TOR_PT_MANAGED_TRANSPORT_VER').split(','),
@@ -80,7 +116,8 @@ class ServerConfig(config.Config):
             serverBindAddr = serverBindAddr,
             ORPort = ORPort,
             extendedORPort = extendedORPort,
-            authCookieFile = authCookieFile
+            authCookieFile = authCookieFile,
+            serverTransportOptions = transport_options
             )
 
     def __init__(self, stateLocation,
@@ -89,7 +126,8 @@ class ServerConfig(config.Config):
                  serverBindAddr=None,
                  ORPort=None,
                  extendedORPort=None,
-                 authCookieFile=None):
+                 authCookieFile=None,
+                 serverTransportOptions=None):
         config.Config.__init__(self, stateLocation,
             managedTransportVer or SUPPORTED_TRANSPORT_VERSIONS,
             transports or [])
@@ -97,6 +135,7 @@ class ServerConfig(config.Config):
         self.ORPort = ORPort
         self.extendedORPort = extendedORPort
         self.authCookieFile = authCookieFile
+        self.serverTransportOptions = serverTransportOptions
 
     def getExtendedORPort(self):
         """
@@ -115,3 +154,9 @@ class ServerConfig(config.Config):
         :returns: :attr:`pyptlib.server_config.ServerConfig.authCookieFile`
         """
         return self.authCookieFile
+
+    def getServerTransportOptions(self):
+        """
+        :returns: :attr:`pyptlib.server_config.ServerConfig.serverTransportOptions`
+        """
+        return self.serverTransportOptions
diff --git a/pyptlib/test/test_server.py b/pyptlib/test/test_server.py
index 0497741..3e9b217 100644
--- a/pyptlib/test/test_server.py
+++ b/pyptlib/test/test_server.py
@@ -2,6 +2,7 @@ import os
 import unittest
 
 from pyptlib.config import EnvError, Config
+from pyptlib.server_config import validate_transport_options
 from pyptlib.server import ServerTransportPlugin
 from pyptlib.test.test_core import PluginCoreTestMixin
 from pyptlib.core import SUPPORTED_TRANSPORT_VERSIONS
@@ -142,6 +143,24 @@ class testServer(PluginCoreTestMixin, unittest.TestCase):
         self.assertEquals(bindaddr["boom"], ('127.0.0.1', 6666))
         self.assertOutputLinesStartWith("VERSION ")
 
+class testUtils(unittest.TestCase):
+    def test_validate_transport_options_wrong(self):
+        """Invalid options string"""
+        to_parse = "trebuchet_secret=nou"
+        self.assertRaises(ValueError, validate_transport_options, to_parse)
+
+    def test_validate_transport_options_wrong_2(self):
+        """No k=v value"""
+        to_parse = "trebuchet:secret~nou"
+        self.assertRaises(ValueError, validate_transport_options, to_parse)
+
+    def test_validate_transport_options_correct(self):
+        to_parse = "trebuchet:secret=nou;trebuchet:cache=/tmp/cache;ballista:secret=yes;ballista:fun=no;archer:bow=yes"
+        expected = {"trebuchet" : {"secret" : "nou", "cache" : "/tmp/cache"} , "ballista" : {"secret" : "yes", "fun" : "no"}, "archer" : {"bow" : "yes" } }
+
+        result = validate_transport_options(to_parse)
+        self.assertEquals(result, expected) # XXX does this check iteratables?
+
 if __name__ == '__main__':
     unittest.main()
 

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