1
0

UiRequestPlugin.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import re
  2. from Plugin import PluginManager
  3. @PluginManager.registerTo("UiRequest")
  4. class UiRequestPlugin(object):
  5. def __init__(self, *args, **kwargs):
  6. from Site import SiteManager
  7. self.site_manager = SiteManager.site_manager
  8. super(UiRequestPlugin, self).__init__(*args, **kwargs)
  9. # Media request
  10. def actionSiteMedia(self, path):
  11. match = re.match("/media/(?P<address>[A-Za-z0-9-]+\.[A-Za-z0-9\.-]+)(?P<inner_path>/.*|$)", path)
  12. if match: # Its a valid domain, resolve first
  13. domain = match.group("address")
  14. address = self.site_manager.resolveDomain(domain)
  15. if address:
  16. path = "/media/" + address + match.group("inner_path")
  17. return super(UiRequestPlugin, self).actionSiteMedia(path) # Get the wrapper frame output
  18. # Is mediarequest allowed from that referer
  19. def isMediaRequestAllowed(self, site_address, referer):
  20. referer_path = re.sub("http[s]{0,1}://.*?/", "/", referer).replace("/media", "") # Remove site address
  21. referer_path = re.sub("\?.*", "", referer_path) # Remove http params
  22. if not re.sub("^http[s]{0,1}://", "", referer).startswith(self.env["HTTP_HOST"]): # Different origin
  23. return False
  24. if self.isProxyRequest(): # Match to site domain
  25. referer = re.sub("^http://zero[/]+", "http://", referer) # Allow /zero access
  26. referer_site_address = re.match("http[s]{0,1}://(.*?)(/|$)", referer).group(1)
  27. else: # Match to request path
  28. referer_site_address = re.match("/(?P<address>[A-Za-z0-9\.-]+)(?P<inner_path>/.*|$)", referer_path).group("address")
  29. if referer_site_address == site_address: # Referer site address as simple address
  30. return True
  31. elif self.site_manager.resolveDomain(referer_site_address) == site_address: # Referer site address as dns
  32. return True
  33. else: # Invalid referer
  34. return False
  35. @PluginManager.registerTo("ConfigPlugin")
  36. class ConfigPlugin(object):
  37. def createArguments(self):
  38. group = self.parser.add_argument_group("Zeroname plugin")
  39. group.add_argument('--bit_resolver', help='ZeroNet site to resolve .bit domains', default="1Name2NXVi1RDPDgf5617UoW7xA6YrhM9F", metavar="address")
  40. return super(ConfigPlugin, self).createArguments()