diff options
| author | manuel <manuel@mausz.at> | 2013-12-26 13:13:59 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2013-12-26 13:13:59 +0100 |
| commit | 4c8a650943a0e619526937543a6e4a45e12d0427 (patch) | |
| tree | b046ac0e89ac484e713e55cc00620f0e053e2b84 | |
| parent | 70d41a270bfd8cb96718cd6a46f6b04ced94eed7 (diff) | |
| download | webiopi-4c8a650943a0e619526937543a6e4a45e12d0427.tar.gz webiopi-4c8a650943a0e619526937543a6e4a45e12d0427.tar.bz2 webiopi-4c8a650943a0e619526937543a6e4a45e12d0427.zip | |
add support for ip whitelist
| -rw-r--r-- | python/webiopi/protocols/http.py | 8 | ||||
| -rw-r--r-- | python/webiopi/server/__init__.py | 5 |
2 files changed, 11 insertions, 2 deletions
diff --git a/python/webiopi/protocols/http.py b/python/webiopi/protocols/http.py index aea6d82..00d811d 100644 --- a/python/webiopi/protocols/http.py +++ b/python/webiopi/protocols/http.py | |||
| @@ -22,6 +22,7 @@ from webiopi.utils.version import VERSION_STRING, PYTHON_MAJOR | |||
| 22 | from webiopi.utils.logger import info, exception | 22 | from webiopi.utils.logger import info, exception |
| 23 | from webiopi.utils.crypto import encrypt | 23 | from webiopi.utils.crypto import encrypt |
| 24 | from webiopi.utils.types import str2bool | 24 | from webiopi.utils.types import str2bool |
| 25 | from netaddr import IPNetwork, IPAddress | ||
| 25 | 26 | ||
| 26 | if PYTHON_MAJOR >= 3: | 27 | if PYTHON_MAJOR >= 3: |
| 27 | import http.server as BaseHTTPServer | 28 | import http.server as BaseHTTPServer |
| @@ -36,7 +37,7 @@ except: | |||
| 36 | WEBIOPI_DOCROOT = "/usr/share/webiopi/htdocs" | 37 | WEBIOPI_DOCROOT = "/usr/share/webiopi/htdocs" |
| 37 | 38 | ||
| 38 | class HTTPServer(BaseHTTPServer.HTTPServer, threading.Thread): | 39 | class HTTPServer(BaseHTTPServer.HTTPServer, threading.Thread): |
| 39 | def __init__(self, host, port, handler, context, docroot, index, auth=None): | 40 | def __init__(self, host, port, handler, context, docroot, index, auth=None, allowfrom=[]): |
| 40 | BaseHTTPServer.HTTPServer.__init__(self, ("", port), HTTPHandler) | 41 | BaseHTTPServer.HTTPServer.__init__(self, ("", port), HTTPHandler) |
| 41 | threading.Thread.__init__(self, name="HTTPThread") | 42 | threading.Thread.__init__(self, name="HTTPThread") |
| 42 | self.host = host | 43 | self.host = host |
| @@ -60,6 +61,7 @@ class HTTPServer(BaseHTTPServer.HTTPServer, threading.Thread): | |||
| 60 | 61 | ||
| 61 | self.handler = handler | 62 | self.handler = handler |
| 62 | self.auth = auth | 63 | self.auth = auth |
| 64 | self.allowfrom = allowfrom | ||
| 63 | 65 | ||
| 64 | self.running = True | 66 | self.running = True |
| 65 | self.start() | 67 | self.start() |
| @@ -98,6 +100,10 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |||
| 98 | if self.server.auth == None or len(self.server.auth) == 0: | 100 | if self.server.auth == None or len(self.server.auth) == 0: |
| 99 | return True | 101 | return True |
| 100 | 102 | ||
| 103 | for cidr in self.server.allowfrom: | ||
| 104 | if IPAddress(self.client_address[0]) in IPNetwork(cidr): | ||
| 105 | return True | ||
| 106 | |||
| 101 | authHeader = self.headers.get('Authorization') | 107 | authHeader = self.headers.get('Authorization') |
| 102 | if authHeader == None: | 108 | if authHeader == None: |
| 103 | return False | 109 | return False |
diff --git a/python/webiopi/server/__init__.py b/python/webiopi/server/__init__.py index 68fdbe6..11fe7d7 100644 --- a/python/webiopi/server/__init__.py +++ b/python/webiopi/server/__init__.py | |||
| @@ -80,6 +80,7 @@ class Server(): | |||
| 80 | http_port = config.getint("HTTP", "port", port) | 80 | http_port = config.getint("HTTP", "port", port) |
| 81 | http_enabled = config.getboolean("HTTP", "enabled", http_port > 0) | 81 | http_enabled = config.getboolean("HTTP", "enabled", http_port > 0) |
| 82 | http_passwdfile = config.get("HTTP", "passwd-file", passwdfile) | 82 | http_passwdfile = config.get("HTTP", "passwd-file", passwdfile) |
| 83 | http_allowfrom = config.get("HTTP", "allow-from", None) | ||
| 83 | context = config.get("HTTP", "context", None) | 84 | context = config.get("HTTP", "context", None) |
| 84 | docroot = config.get("HTTP", "doc-root", None) | 85 | docroot = config.get("HTTP", "doc-root", None) |
| 85 | index = config.get("HTTP", "welcome-file", None) | 86 | index = config.get("HTTP", "welcome-file", None) |
| @@ -112,8 +113,10 @@ class Server(): | |||
| 112 | if auth == None or len(auth) == 0: | 113 | if auth == None or len(auth) == 0: |
| 113 | logger.warn("Access unprotected") | 114 | logger.warn("Access unprotected") |
| 114 | 115 | ||
| 116 | allowfrom = http_allowfrom.split(" ") if http_allowfrom != None else [ ] | ||
| 117 | |||
| 115 | if http_enabled: | 118 | if http_enabled: |
| 116 | self.http_server = http.HTTPServer(self.host, http_port, self.restHandler, context, docroot, index, auth) | 119 | self.http_server = http.HTTPServer(self.host, http_port, self.restHandler, context, docroot, index, auth, allowfrom) |
| 117 | else: | 120 | else: |
| 118 | self.http_server = None | 121 | self.http_server = None |
| 119 | 122 | ||
