def create_compressed_shares_message(self, sharestype): """ Create a message that will later contain a compressed list of our shares """ if sharestype == "normal": self.compressed_shares_normal = slskmessages.SharedFileList( None, self.config.sections["transfers"]["sharedfilesstreams"]) elif sharestype == "buddy": self.compressed_shares_buddy = slskmessages.SharedFileList( None, self.config.sections["transfers"]["bsharedfilesstreams"])
def create_compressed_shares_message(self, sharestype): """ Create a message that will later contain a compressed list of our shares """ if sharestype == "normal": self.compressed_shares_normal = slskmessages.SharedFileList( None, self.share_dbs["streams"]) elif sharestype == "buddy": self.compressed_shares_buddy = slskmessages.SharedFileList( None, self.share_dbs["buddystreams"])
def load_shares_list_from_disk(self, filename): try: try: # Try legacy format first import bz2 with bz2.BZ2File(filename) as file_handle: shares_list = RestrictedUnpickler(file_handle, encoding='utf-8').load() except Exception: # Try new format with open(filename, encoding="utf-8") as file_handle: shares_list = json.load(file_handle) # Basic sanity check for _folder, files in shares_list: for _file_data in files: pass except Exception as msg: log.add(_("Loading Shares from disk failed: %(error)s"), {'error': msg}) return username = filename.replace('\\', os.sep).split(os.sep)[-1] self.show_user(username) msg = slskmessages.SharedFileList(None) msg.list = shares_list self.shared_file_list(username, msg)
def load_local_shares_list(self, username, shares_list): self.show_user(username) msg = slskmessages.SharedFileList(None) msg.list = shares_list self.shared_file_list(username, msg)
def __init__(self, np, config, queue, ui_callback=None, connected=False): self.np = np self.ui_callback = ui_callback self.config = config self.queue = queue self.connected = connected self.translatepunctuation = str.maketrans( dict.fromkeys(string.punctuation, ' ')) self.share_dbs = {} self.public_rescanning = False self.buddy_rescanning = False self.newbuddyshares = False self.newnormalshares = False self.compressed_shares_normal = slskmessages.SharedFileList(None, None) self.compressed_shares_buddy = slskmessages.SharedFileList(None, None) self.convert_shares() self.public_share_dbs = [ ("files", os.path.join(self.config.data_dir, "files.db")), ("streams", os.path.join(self.config.data_dir, "streams.db")), ("wordindex", os.path.join(self.config.data_dir, "wordindex.db")), ("fileindex", os.path.join(self.config.data_dir, "fileindex.db")), ("mtimes", os.path.join(self.config.data_dir, "mtimes.db")) ] self.buddy_share_dbs = [ ("buddyfiles", os.path.join(self.config.data_dir, "buddyfiles.db")), ("buddystreams", os.path.join(self.config.data_dir, "buddystreams.db")), ("buddywordindex", os.path.join(self.config.data_dir, "buddywordindex.db")), ("buddyfileindex", os.path.join(self.config.data_dir, "buddyfileindex.db")), ("buddymtimes", os.path.join(self.config.data_dir, "buddymtimes.db")) ] if ui_callback: # Slight delay to prevent minor performance hit when compressing large file share timer = threading.Timer(0.75, self.init_shares) timer.name = "InitSharesTimer" timer.daemon = True timer.start() else: self.init_shares()
def __init__(self, core, config, queue, network_callback=None, ui_callback=None, init_shares=True): self.core = core self.network_callback = network_callback self.ui_callback = ui_callback self.config = config self.queue = queue self.translatepunctuation = str.maketrans( dict.fromkeys(PUNCTUATION, ' ')) self.share_dbs = {} self.pending_network_msgs = [] self.rescanning = False self.should_compress_shares = False self.compressed_shares_normal = slskmessages.SharedFileList(None, None) self.compressed_shares_buddy = slskmessages.SharedFileList(None, None) self.convert_shares() self.share_db_paths = [ ("files", os.path.join(self.config.data_dir, "files.db")), ("streams", os.path.join(self.config.data_dir, "streams.db")), ("wordindex", os.path.join(self.config.data_dir, "wordindex.db")), ("fileindex", os.path.join(self.config.data_dir, "fileindex.db")), ("mtimes", os.path.join(self.config.data_dir, "mtimes.db")), ("buddyfiles", os.path.join(self.config.data_dir, "buddyfiles.db")), ("buddystreams", os.path.join(self.config.data_dir, "buddystreams.db")), ("buddywordindex", os.path.join(self.config.data_dir, "buddywordindex.db")), ("buddyfileindex", os.path.join(self.config.data_dir, "buddyfileindex.db")), ("buddymtimes", os.path.join(self.config.data_dir, "buddymtimes.db")) ] if not init_shares: return self.init_shares()
def create_compressed_shares_message(self, share_type): """ Create a message that will later contain a compressed list of our shares """ if share_type == "normal": streams = self.share_dbs.get("streams") else: streams = self.share_dbs.get("buddystreams") compressed_shares = slskmessages.SharedFileList(None, streams) compressed_shares.make_network_message() compressed_shares.list = None compressed_shares.type = share_type self.queue.put(compressed_shares)
def compress_shares(self, sharestype): if sharestype == "normal": streams = self.config.sections["transfers"]["sharedfilesstreams"] elif sharestype == "buddy": streams = self.config.sections["transfers"]["bsharedfilesstreams"] if streams is None: log.add_warning(_("ERROR: No %(type)s shares database available") % {"type": sharestype}) return m = slskmessages.SharedFileList(None, streams) _thread.start_new_thread(m.make_network_message, (0, True)) if sharestype == "normal": self.compressed_shares_normal = m elif sharestype == "buddy": self.compressed_shares_buddy = m
def get_shared_file_list(self, msg): """ Peer code: 4 """ log.add_msg_contents(msg) user = msg.init.target_user request_time = time.time() if user in self.requested_share_times and request_time < self.requested_share_times[ user] + 0.4: # Ignoring request, because it's less than half a second since the # last one by this user return self.requested_share_times[user] = request_time log.add(_("User %(user)s is browsing your list of shared files"), {'user': user}) ip_address, _port = msg.init.addr checkuser, reason = self.network_filter.check_user(user, ip_address) if not checkuser: message = self.ban_message % reason self.privatechats.send_automatic_message(user, message) shares_list = None if checkuser == 1: # Send Normal Shares shares_list = self.shares.get_compressed_shares_message("normal") elif checkuser == 2: # Send Buddy Shares shares_list = self.shares.get_compressed_shares_message("buddy") if not shares_list: # Nyah, Nyah shares_list = slskmessages.SharedFileList(msg.init, {}) shares_list.init = msg.init self.queue.append(shares_list)
def CompressShares(self, sharestype): if sharestype == "normal": streams = self.config.sections["transfers"]["sharedfilesstreams"] elif sharestype == "buddy": streams = self.config.sections["transfers"]["bsharedfilesstreams"] if streams is None: message = _("ERROR: No %(type)s shares database available") % {"type": sharestype} print(message) self.logMessage(message, None) return m = slskmessages.SharedFileList(None, streams) m.makeNetworkMessage(nozlib=0, rebuild=True) if sharestype == "normal": self.CompressedSharesNormal = m elif sharestype == "buddy": self.CompressedSharesBuddy = m
def browse_local_public_shares(self, folder=None, new_request=None): """ Browse your own public shares """ username = self.config.sections["server"]["login"] if username not in self.users or new_request: # Deactivate if we only share with buddies if self.config.sections["transfers"]["friendsonly"]: msg = slskmessages.SharedFileList(None, {}) else: msg = self.core.shares.get_compressed_shares_message("normal") thread = threading.Thread(target=self.parse_local_shares, args=(username, msg)) thread.name = "LocalShareParser" thread.daemon = True thread.start() self.show_user(username, folder=folder, local_shares_type="normal", indeterminate_progress=True)
def GetSharedFileList(self, msg): self.logMessage("%s %s" % (msg.__class__, vars(msg)), 4) user = ip = port = None # Get peer's username, ip and port for i in self.np.peerconns: if i.conn is msg.conn.conn: user = i.username if i.addr is not None: if len(i.addr) != 2: break ip, port = i.addr break if user is None: # No peer connection return # Check address is spoofed, if possible # if self.CheckSpoof(user, ip, port): # # Message IS spoofed # return if user == self.config.sections["server"]["login"]: if ip is not None and port is not None: self.logMessage( _("%(user)s is making a BrowseShares request, blocking possible spoofing attempt from IP %(ip)s port %(port)s" ) % { 'user': user, 'ip': ip, 'port': port }, 1) else: self.logMessage( _("%(user)s is making a BrowseShares request, blocking possible spoofing attempt from an unknown IP & port" ) % {'user': user}, None) if msg.conn.conn is not None: self.queue.put(slskmessages.ConnClose(msg.conn.conn)) return self.logMessage( _("%(user)s is making a BrowseShares request") % {'user': user}, 1) addr = msg.conn.addr[0] checkuser, reason = self.np.CheckUser(user, addr) if checkuser == 1: # Send Normal Shares if self.newnormalshares: self.CompressShares("normal") self.newnormalshares = False m = self.CompressedSharesNormal elif checkuser == 2: # Send Buddy Shares if self.newbuddyshares: self.CompressShares("buddy") self.newbuddyshares = False m = self.CompressedSharesBuddy else: # Nyah, Nyah m = slskmessages.SharedFileList(msg.conn.conn, {}) m.makeNetworkMessage(nozlib=0) m.conn = msg.conn.conn self.queue.put(m)