1
0

UiRequestPlugin.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, header_length=True):
  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, header_length=header_length) # 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. match = re.match("http[s]{0,1}://(.*?)(/|$)", referer)
  27. if match:
  28. referer_site_address = match.group(1)
  29. else:
  30. referer_site_address = None
  31. else: # Match to request path
  32. match = re.match("/(?P<address>[A-Za-z0-9\.-]+)(?P<inner_path>/.*|$)", referer_path)
  33. if match:
  34. referer_site_address = match.group("address")
  35. else:
  36. referer_site_address = None
  37. if not referer_site_address:
  38. return False
  39. elif referer_site_address == site_address: # Referer site address as simple address
  40. return True
  41. elif self.site_manager.resolveDomain(referer_site_address) == site_address: # Referer site address as dns
  42. return True
  43. else: # Invalid referer
  44. return False
  45. @PluginManager.registerTo("ConfigPlugin")
  46. class ConfigPlugin(object):
  47. def createArguments(self):
  48. group = self.parser.add_argument_group("Zeroname plugin")
  49. group.add_argument('--bit_resolver', help='ZeroNet site to resolve .bit domains', default="1Name2NXVi1RDPDgf5617UoW7xA6YrhM9F", metavar="address")
  50. return super(ConfigPlugin, self).createArguments()