[Pkg-privacy-commits] [txtorcon] 76/96: Check multiple hidden services with same directory

Jérémy Bobbio lunar at moszumanska.debian.org
Sun Sep 6 18:33:43 UTC 2015


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

lunar pushed a commit to branch master
in repository txtorcon.

commit bb0cd71b220953c98e11287fef9a4ab2bb1330e1
Author: Sambuddha Basu <sambuddhabasu1 at gmail.com>
Date:   Wed Feb 18 08:18:46 2015 +0530

    Check multiple hidden services with same directory
    
    Warns user is same HiddenServiceDir is added
    
    Check multiple hidden services with same directory
    
    Warns user is same HiddenServiceDir is added
    
    raise RuntimeError if there are multiple hidden services with same directory
    
    Does not save new hidden_services list anymore
---
 test/test_torconfig.py | 21 +++++++++++++++++++++
 txtorcon/torconfig.py  | 41 +++++++++++++++++++++++++++--------------
 2 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/test/test_torconfig.py b/test/test_torconfig.py
index a387d3e..4f676f2 100644
--- a/test/test_torconfig.py
+++ b/test/test_torconfig.py
@@ -775,6 +775,27 @@ HiddenServicePort=90 127.0.0.1:2345''')
         conf._setup_hidden_services('HiddenServiceDir=/fake/path/../path')
         self.assertEqual(len(self.flushWarnings()), 1)
 
+    def test_hidden_service_same_directory(self):
+        conf = TorConfig(FakeControlProtocol(['config/names=']))
+        servicelines = '''HiddenServiceDir=/fake/path
+HiddenServiceDir=/fake/path'''
+        self.assertRaises(RuntimeError, conf._setup_hidden_services, servicelines)
+
+        conf = TorConfig()
+        conf.HiddenServices = [HiddenService(conf, '/fake/path', ['80 127.0.0.1:1234'])]
+        conf.HiddenServices.append(HiddenService(conf, '/fake/path', ['80 127.0.0.1:1234']))
+        self.assertTrue(conf.needs_save())
+        self.assertRaises(RuntimeError, conf.save)
+
+        conf = TorConfig()
+        conf.HiddenServices = [HiddenService(conf, '/fake/path', ['80 127.0.0.1:1234'])]
+        conf.HiddenServices.append(HiddenService(conf, '/fake/path/two', ['80 127.0.0.1:1234']))
+        self.assertTrue(conf.needs_save())
+        conf.save()
+        conf.hiddenservices[1].dir = '/fake/path'
+        self.assertTrue(conf.needs_save())
+        self.assertRaises(RuntimeError, conf.save)
+
     def test_multiple_modify_hidden_service(self):
         self.protocol.answers.append('HiddenServiceDir=/fake/path\nHiddenServicePort=80 127.0.0.1:1234\n')
 
diff --git a/txtorcon/torconfig.py b/txtorcon/torconfig.py
index 7e9e5aa..519c0c8 100644
--- a/txtorcon/torconfig.py
+++ b/txtorcon/torconfig.py
@@ -1129,13 +1129,22 @@ class TorConfig(object):
             return defer.succeed(self)
 
         args = []
+        directories = []
         for (key, value) in self.unsaved.items():
             if key == 'HiddenServices':
                 self.config['HiddenServices'] = value
                 for hs in value:
                     for (k, v) in hs.config_attributes():
-                        args.append(k)
-                        args.append(v)
+                        if k == 'HiddenServiceDir':
+                            if v not in directories:
+                                directories.append(v)
+                                args.append(k)
+                                args.append(v)
+                            else:
+                                raise RuntimeError("Trying to add hidden service with same HiddenServiceDir: %s" % v)
+                        else:
+                            args.append(k)
+                            args.append(v)
                 continue
 
             if isinstance(value, types.ListType):
@@ -1225,10 +1234,24 @@ class TorConfig(object):
         defer.returnValue(self)
 
     def _setup_hidden_services(self, servicelines):
+        def maybe_add_hidden_service():
+            if directory is not None:
+                if directory not in directories:
+                    directories.append(directory)
+                    hs.append(
+                        HiddenService(
+                            self, directory, ports, auth, ver, group_read
+                        )
+                    )
+                else:
+                    raise RuntimeError("Trying to add hidden service with same HiddenServiceDir: %s" % directory)
+
         hs = []
         directory = None
+        directories = []
         ports = []
         ver = None
+        group_read = None
         auth = None
         for line in servicelines.split('\n'):
             if not len(line.strip()):
@@ -1238,12 +1261,7 @@ class TorConfig(object):
                 continue
             k, v = line.split('=')
             if k == 'HiddenServiceDir':
-                if directory is not None:
-                    hs.append(
-                        HiddenService(
-                            self, directory, ports, auth, ver, group_read
-                        )
-                    )
+                maybe_add_hidden_service()
                 directory = v
                 _directory = directory
                 directory = os.path.abspath(directory)
@@ -1272,12 +1290,7 @@ class TorConfig(object):
             else:
                 raise RuntimeError("Can't parse HiddenServiceOptions: " + k)
 
-        if directory is not None:
-            hs.append(
-                HiddenService(
-                    self, directory, ports, auth, ver, group_read
-                )
-            )
+        maybe_add_hidden_service()
 
         name = 'HiddenServices'
         self.config[name] = _ListWrapper(

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



More information about the Pkg-privacy-commits mailing list