Browse Source

support returning shadow HS from /info

Matthew Hodgson 5 years ago
parent
commit
fa6a5e9818
3 changed files with 22 additions and 2 deletions
  1. 1 0
      setup.py
  2. 10 2
      sydent/http/servlets/infoservlet.py
  3. 11 0
      sydent/sydent.py

+ 1 - 0
setup.py

@@ -40,6 +40,7 @@ setup(
         "pyyaml",
         "daemonize",
         "phonenumbers",
+        "netaddr",
     ],
     setup_requires=[
         "setuptools_trial",

+ 10 - 2
sydent/http/servlets/infoservlet.py

@@ -43,9 +43,9 @@ class InfoServlet(Resource):
             # medium:
             #   email:
             #     entries:
-            #       matthew@matrix.org: { hs: 'matrix.org' }
+            #       matthew@matrix.org: { hs: 'matrix.org', shadow_hs: 'shadow-matrix.org' }
             #     patterns:
-            #       - .*@matrix.org: { hs: 'matrix.org' }
+            #       - .*@matrix.org: { hs: 'matrix.org', shadow_hs: 'shadow-matrix.org' }
 
         except Exception as e:
             logger.error(e)
@@ -85,6 +85,14 @@ class InfoServlet(Resource):
                     break
 
         result = copy.deepcopy(result)
+
+        if self.sydent.nonshadow_ips:
+            ip = IPAddress(self.sydent.ip_from_request(request))
+            if (ip not in self.sydent.nonshadow_ips):
+                result['hs'] = result['shadow_hs']
+
+        result.pop('shadow_hs', None)
+
         result['invited'] = True if pendingJoinTokens else False
         return json.dumps(result)
 

+ 11 - 0
sydent/sydent.py

@@ -25,6 +25,8 @@ from twisted.python import log
 
 from db.sqlitedb import SqliteDatabase
 
+from netaddr import IPSet, IPNetwork
+
 from http.httpcommon import SslComponents
 from http.httpserver import ClientApiHttpServer, ReplicationHttpsServer
 from http.httpsclient import ReplicationHttpsClient
@@ -70,6 +72,7 @@ class Sydent:
         'server.name': '',
         'log.path': '',
         'pidfile.path': 'sydent.pid',
+        'ips.nonshadow': '',  # \n separated list of CIDR ranges which /info will return non-shadow HS to.
         # db
         'db.file': 'sydent.db',
         # http
@@ -128,6 +131,14 @@ class Sydent:
 
         self.pidfile = self.cfg.get('general', "pidfile.path");
 
+        self.nonshadow_ips = None
+        ips = self.cfg.get('general', "ips.nonshadow");
+        if ips:
+            self.nonshadow_ips = IPSet()
+            ips = ips.splitlines()
+            for ip in ips:
+                self.nonshadow_ips.add(IPNetwork(ip))
+
         observer = log.PythonLoggingObserver()
         observer.start()