import re import os import cgi import sys import math import time import json try: import cStringIO as StringIO except: import StringIO from Config import config from Plugin import PluginManager from Debug import Debug plugin_dir = "plugins/Sidebar" media_dir = plugin_dir + "/media" sys.path.append(plugin_dir) # To able to load geoip lib loc_cache = {} @PluginManager.registerTo("UiRequest") class UiRequestPlugin(object): # Inject our resources to end of original file streams def actionUiMedia(self, path): if path == "/uimedia/all.js" or path == "/uimedia/all.css": # First yield the original file and header body_generator = super(UiRequestPlugin, self).actionUiMedia(path) for part in body_generator: yield part # Append our media file to the end ext = re.match(".*(js|css)$", path).group(1) plugin_media_file = "%s/all.%s" % (media_dir, ext) if config.debug: # If debugging merge *.css to all.css and *.js to all.js from Debug import DebugMedia DebugMedia.merge(plugin_media_file) for part in self.actionFile(plugin_media_file, send_header=False): yield part elif path.startswith("/uimedia/globe/"): # Serve WebGL globe files file_name = re.match(".*/(.*)", path).group(1) plugin_media_file = "%s-globe/%s" % (media_dir, file_name) if config.debug and path.endswith("all.js"): # If debugging merge *.css to all.css and *.js to all.js from Debug import DebugMedia DebugMedia.merge(plugin_media_file) for part in self.actionFile(plugin_media_file): yield part else: for part in super(UiRequestPlugin, self).actionUiMedia(path): yield part @PluginManager.registerTo("UiWebsocket") class UiWebsocketPlugin(object): def sidebarRenderPeerStats(self, body, site): connected = len([peer for peer in site.peers.values() if peer.connection and peer.connection.connected]) connectable = len([peer_id for peer_id in site.peers.keys() if not peer_id.endswith(":0")]) onion = len([peer_id for peer_id in site.peers.keys() if ".onion" in peer_id]) peers_total = len(site.peers) if peers_total: percent_connected = float(connected) / peers_total percent_connectable = float(connectable) / peers_total percent_onion = float(onion) / peers_total else: percent_connectable = percent_connected = percent_onion = 0 body.append("""
  • """.format(**locals())) def sidebarRenderTransferStats(self, body, site): recv = float(site.settings.get("bytes_recv", 0)) / 1024 / 1024 sent = float(site.settings.get("bytes_sent", 0)) / 1024 / 1024 transfer_total = recv + sent if transfer_total: percent_recv = recv / transfer_total percent_sent = sent / transfer_total else: percent_recv = 0.5 percent_sent = 0.5 body.append("""
  • """.format(**locals())) def sidebarRenderFileStats(self, body, site): body.append("
  • ") def getFreeSpace(self): free_space = 0 if "statvfs" in dir(os): # Unix statvfs = os.statvfs(config.data_dir) free_space = statvfs.f_frsize * statvfs.f_bavail else: # Windows try: import ctypes free_space_pointer = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW( ctypes.c_wchar_p(config.data_dir), None, None, ctypes.pointer(free_space_pointer) ) free_space = free_space_pointer.value except Exception, err: self.log.debug("GetFreeSpace error: %s" % err) return free_space def sidebarRenderSizeLimit(self, body, site): free_space = self.getFreeSpace() / 1024 / 1024 size = float(site.settings["size"]) / 1024 / 1024 size_limit = site.getSizeLimit() percent_used = size / size_limit body.append("""
  • MB Set
  • """.format(**locals())) def sidebarRenderOptionalFileStats(self, body, site): size_total = 0.0 size_downloaded = 0.0 res = site.content_manager.contents.execute("SELECT inner_path FROM content WHERE size_files_optional > 0 AND site_id = :site_id") for row in res: inner_path = row["inner_path"] content = site.content_manager.contents[inner_path] for file_name, file_details in content["files_optional"].items(): size_total += file_details["size"] if site.content_manager.hashfield.hasHash(file_details["sha512"]): size_downloaded += file_details["size"] if not size_total: return False percent_downloaded = size_downloaded / size_total size_formatted_total = size_total / 1024 / 1024 size_formatted_downloaded = size_downloaded / 1024 / 1024 body.append("""
  • """.format(**locals())) return True def sidebarRenderOptionalFileSettings(self, body, site): if self.site.settings.get("autodownloadoptional"): checked = "checked='checked'" else: checked = "" body.append("""
  • """.format(**locals())) def sidebarRenderBadFiles(self, body, site): body.append("""
  • """) def sidebarRenderDbOptions(self, body, site): if site.storage.db: inner_path = site.storage.getInnerPath(site.storage.db.db_path) size = float(site.storage.getSize(inner_path)) / 1024 feeds = len(site.storage.db.schema.get("feeds", {})) else: inner_path = "No database found" size = 0.0 feeds = 0 body.append(u"""
  • Reload Rebuild
  • """.format(**locals())) def sidebarRenderIdentity(self, body, site): auth_address = self.user.getAuthAddress(self.site.address) rules = self.site.content_manager.getRules("data/users/%s/content.json" % auth_address) if rules and rules.get("max_size"): quota = rules["max_size"] / 1024 try: content = site.content_manager.contents["data/users/%s/content.json" % auth_address] used = len(json.dumps(content)) + sum([file["size"] for file in content["files"].values()]) except: used = 0 used = used / 1024 else: quota = used = 0 body.append("""
  • {auth_address} Change
  • """.format(**locals())) def sidebarRenderControls(self, body, site): auth_address = self.user.getAuthAddress(self.site.address) if self.site.settings["serving"]: class_pause = "" class_resume = "hidden" else: class_pause = "hidden" class_resume = "" body.append("""
  • Update Pause Resume Delete
  • """.format(**locals())) site_address = self.site.address body.append("""

  • {site_address} Donate
  • """.format(**locals())) def sidebarRenderOwnedCheckbox(self, body, site): if self.site.settings["own"]: checked = "checked='checked'" else: checked = "" body.append("""

    This is my site

    """.format(**locals())) def sidebarRenderOwnSettings(self, body, site): title = cgi.escape(site.content_manager.contents.get("content.json", {}).get("title", ""), True) description = cgi.escape(site.content_manager.contents.get("content.json", {}).get("description", ""), True) privatekey = cgi.escape(self.user.getSiteData(site.address, create=False).get("privatekey", "")) body.append(u"""
  • Save site settings
  • """.format(**locals())) def sidebarRenderContents(self, body, site): body.append("""
  • """) # Choose content you want to sign contents = ["content.json"] contents += site.content_manager.contents.get("content.json", {}).get("includes", {}).keys() if len(contents) > 1: body.append("
    Choose: ") for content in contents: content = cgi.escape(content, True) body.append("{content} ".format(**locals())) body.append("
    ") body.append(""" Sign Publish
  • """) def actionSidebarGetHtmlTag(self, to): site = self.site body = [] body.append("
    ") body.append("

    %s

    " % cgi.escape(site.content_manager.contents.get("content.json", {}).get("title", ""), True)) body.append("
    ") body.append("") body.append("
    ") self.response(to, "".join(body)) def downloadGeoLiteDb(self, db_path): import urllib import gzip import shutil from util import helper self.log.info("Downloading GeoLite2 City database...") self.cmd("notification", ["geolite-info", "Downloading GeoLite2 City database (one time only, ~20MB)...", 0]) db_urls = [ "https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz", "https://raw.githubusercontent.com/texnikru/GeoLite2-Database/master/GeoLite2-City.mmdb.gz" ] for db_url in db_urls: try: # Download response = helper.httpRequest(db_url) data = StringIO.StringIO() while True: buff = response.read(1024 * 512) if not buff: break data.write(buff) self.log.info("GeoLite2 City database downloaded (%s bytes), unpacking..." % data.tell()) data.seek(0) # Unpack with gzip.GzipFile(fileobj=data) as gzip_file: shutil.copyfileobj(gzip_file, open(db_path, "wb")) self.cmd("notification", ["geolite-done", "GeoLite2 City database downloaded!", 5000]) time.sleep(2) # Wait for notify animation return True except Exception, err: self.log.error("Error downloading %s: %s" % (db_url, err)) pass self.cmd("notification", [ "geolite-error", "GeoLite2 City database download error: %s!
    Please download and unpack to data dir:
    %s" % (err, db_urls[0]), 0 ]) def actionSidebarGetPeers(self, to): permissions = self.getPermissions(to) if "ADMIN" not in permissions: return self.response(to, "You don't have permission to run this command") try: import maxminddb db_path = config.data_dir + '/GeoLite2-City.mmdb' if not os.path.isfile(db_path) or os.path.getsize(db_path) == 0: if not self.downloadGeoLiteDb(db_path): return False geodb = maxminddb.open_database(db_path) peers = self.site.peers.values() # Find avg ping ping_times = [ peer.connection.last_ping_delay for peer in peers if peer.connection and peer.connection.last_ping_delay and peer.connection.last_ping_delay ] if ping_times: ping_avg = sum(ping_times) / float(len(ping_times)) else: ping_avg = 0 # Place bars globe_data = [] placed = {} # Already placed bars here for peer in peers: # Height of bar if peer.connection and peer.connection.last_ping_delay: ping = min(0.20, math.log(1 + peer.connection.last_ping_delay / ping_avg, 300)) else: ping = -0.03 # Query and cache location if peer.ip in loc_cache: loc = loc_cache[peer.ip] else: try: loc = geodb.get(peer.ip) except: loc = None loc_cache[peer.ip] = loc if not loc or "location" not in loc: continue # Create position array lat, lon = (loc["location"]["latitude"], loc["location"]["longitude"]) latlon = "%s,%s" % (lat, lon) if latlon in placed: # Dont place more than 1 bar to same place, fake repos using ip address last two part lat += float(128 - int(peer.ip.split(".")[-2])) / 50 lon += float(128 - int(peer.ip.split(".")[-1])) / 50 latlon = "%s,%s" % (lat, lon) placed[latlon] = True globe_data += (lat, lon, ping) # Append myself loc = geodb.get(config.ip_external) if loc: lat, lon = (loc["location"]["latitude"], loc["location"]["longitude"]) globe_data += (lat, lon, -0.135) self.response(to, globe_data) except Exception, err: self.log.debug("sidebarGetPeers error: %s" % Debug.formatException(err)) self.response(to, {"error": err}) def actionSiteSetOwned(self, to, owned): permissions = self.getPermissions(to) if "Multiuser" in PluginManager.plugin_manager.plugin_names: self.cmd("notification", ["info", "This function is disabled on this proxy"]) return False if "ADMIN" not in permissions: return self.response(to, "You don't have permission to run this command") self.site.settings["own"] = bool(owned) def actionSiteSetAutodownloadoptional(self, to, owned): permissions = self.getPermissions(to) if "Multiuser" in PluginManager.plugin_manager.plugin_names: self.cmd("notification", ["info", "This function is disabled on this proxy"]) return False if "ADMIN" not in permissions: return self.response(to, "You don't have permission to run this command") self.site.settings["autodownloadoptional"] = bool(owned) self.site.update(check_files=True) self.site.worker_manager.removeGoodFileTasks() def actionDbReload(self, to): permissions = self.getPermissions(to) if "ADMIN" not in permissions: return self.response(to, "You don't have permission to run this command") self.site.storage.closeDb() self.site.storage.getDb() return self.response(to, "ok") def actionDbRebuild(self, to): permissions = self.getPermissions(to) if "ADMIN" not in permissions: return self.response(to, "You don't have permission to run this command") self.site.storage.rebuildDb() return self.response(to, "ok")