Example #1
0
 def do_heartbeat(self, force_gui_notif=False):
     getLogger(__name__).debug("Do heartbeat syncer.py")
     if self.localbox.do_heartbeat():
         self.online = True
         Notifs().syncHeartbeatUp(self.name, force_gui_notif)
     else:
         self.online = False
         Notifs().syncHeartbeatDown(self.name, force_gui_notif)
Example #2
0
    def upload_file(self, path, fs_path, passphrase, remove=True):
        """
        upload a file to localbox

        :param path: path relative to localbox location. eg: /some_folder/image.jpg
        :param fs_path: file system path. eg: /home/user/localbox/some_folder/image.jpg
        :param passphrase: used to encrypt file
        :param remove: whether or not to remove the plain text file
        :return:
        """
        metapath = path.encode('utf8')

        try:
            # read plain file
            stats = stat(fs_path)
            openfile = open(fs_path, 'rb')
            contents = openfile.read(stats.st_size)
            openfile.flush()

            # encrypt file
            contents = gpg.add_pkcs7_padding(contents)
            clen = len(contents)
            contents = self.encode_file(path, contents, passphrase)

            # save encrypted file
            encrypted_file = open(fs_path + defaults.LOCALBOX_EXTENSION, 'wb')
            encrypted_file.write(contents)
            encrypted_file.close()

            openfile.close()

            # remove plain file
            if remove:
                getLogger(__name__).debug('Deleting old plain file: %s' %
                                          fs_path)
                os_utils.shred(fs_path)

            # upload encrypted file
            getLogger(__name__).info(
                "Uploading %s: Statsize: %d, readsize: %d cryptosize: %d",
                fs_path, stats.st_size, clen, len(contents))

            request = Request(url=self.url + 'lox_api/files',
                              data=dumps({
                                  'contents': b64encode(contents),
                                  'path': metapath
                              }))

            res = self._make_call(request)
            Notifs().uploadedFile(os.path.basename(fs_path))
            return res

        except (BadStatusLine, HTTPError, OSError) as error:
            getLogger(__name__).error('Failed to upload file: %s, error=%s' %
                                      (path, error))

        return None
Example #3
0
    def do_heartbeat(self, labels=None, force_gui_notif=False):
        syncs_ctrl = SyncsController()

        for sync_item in syncs_ctrl:
            if labels is None or labels == [] or sync_item.label in labels:
                url = sync_item.url
                path = sync_item.path
                direction = sync_item.direction
                label = sync_item.label

                try:
                    localbox_client = LocalBox(url, label, path)
                    results = localbox_client.do_heartbeat()

                    if results:
                        Notifs().syncHeartbeatUp(label, force_gui_notif)
                    else:
                        Notifs().syncHeartbeatDown(label, force_gui_notif)

                except URLError as error:
                    Notifs().syncHeartbeatDown(sync_item.label, force_gui_notif)
Example #4
0
    def run(self):
        location = SITESINI_PATH
        configparser = ConfigParser()
        configparser.read(location)
        try:
            configparser.read(SYNCINI_PATH)
            try:
                delay = int(configparser.get('sync', 'delay'))
            except (NoSectionError, NoOptionError) as error:
                getLogger(__name__).warning("%s in '%s'", error.message, SYNCINI_PATH)
                delay = 3600
            while self.keep_running:
                getLogger(__name__).debug("starting loop")
                Notifs().syncStarted()
                self.waitevent.set()
                for syncer in get_syncers():
                    if len(self._labels_to_sync) > 0 and syncer.name not in self._labels_to_sync:
                        continue
                    runner = SyncRunner(syncer=syncer)
                    getLogger(__name__).debug("starting thread %s", runner.name)
                    syncer.stop_event = Event()
                    runner.start()
                    self.thread_pool[syncer.name] = runner
                    getLogger(__name__).debug("started thread %s", runner.name)
                for runner in self.thread_pool.values():
                    getLogger(__name__).debug("joining thread %s", runner.name)
                    runner.join()
                    runner.syncer.stop_event.clear()
                    getLogger(__name__).debug("joined thread %s", runner.name)
                Notifs().syncEnded()

                self.waitevent.clear()
                getLogger(__name__).debug("Cleared Event")
                if self.waitevent.wait(delay):
                    getLogger(__name__).debug("stopped waiting")
                else:
                    getLogger(__name__).debug("done waiting")
        except Exception as error:  # pylint: disable=W0703
            getLogger(__name__).exception(error)
Example #5
0
 def delete(self, localbox_path):
     """
     do the delete call
     """
     metapath = urlencode({'path': localbox_path.encode('utf8')})
     request = Request(url=self.url + 'lox_api/operations/delete/',
                       data=metapath)
     try:
         res = self._make_call(request)
         Notifs().deletedFile(os.path.basename(localbox_path))
         return res
     except HTTPError:
         getLogger(__name__).error("Error remote deleting '%s'",
                                   localbox_path)
Example #6
0
    def OnTaskBarClose(self, event):  # pylint: disable=W0613
        """
        Destroy the taskbar icon and frame from the taskbar icon itself
        """
        self.frame.Close()
        self.delete_decrypted()

        for observer in self.observers:
            observer.stop()
            observer.join()

        Notifs().stop()

        self.Destroy()

        app = wx.GetApp()
        app.ExitMainLoop()
Example #7
0
def remove_decrypted_files():
    import os, sync.controllers.openfiles_ctrl as ctrl

    getLogger(__name__).info('removing decrypted files')

    files = ctrl.load()

    if files is None:
        return

    for filename in files:
        try:
            os.remove(filename)
        except Exception as ex:
            getLogger(__name__).error('could not remove file %s, %s' %
                                      (filename, ex))

    ctrl.save([])
    Notifs().openfilesCtrl()
Example #8
0
def run_file_decryption(filename, memory=False):
    try:
        getLogger(__name__).info('Decrypting and opening file: %s', filename)

        # verify if the file belongs to any of the configured syncs
        sync_list = sync_ctrl.list

        localbox_client = None
        localbox_filename = None
        try:
            filename = filename.decode('utf-8')
        except UnicodeEncodeError:
            # on purpose, it's already an utf-8 string
            pass

        for sync_item in sync_list:
            getLogger(__name__).debug('sync path: %s' % sync_item.path)
            sync_path = sync_item.path if sync_item.path.endswith(
                '/') else sync_item.path + os.path.sep
            if filename.startswith(sync_path):
                localbox_filename = os_utils.remove_extension(
                    filename.replace(sync_item.path, ''),
                    defaults.LOCALBOX_EXTENSION)
                localbox_client = LocalBox(sync_item.url, sync_item.label,
                                           sync_item.path)
                break

        if not localbox_client or not localbox_filename:
            gui_utils.show_error_dialog(
                _('%s does not belong to any configured localbox') % filename,
                'Error', True)
            getLogger(__name__).error(
                '%s does not belong to any configured localbox' % filename)
            #exit(1)

        # Request file to be opened
        data_dic = {
            "url": sync_item.url,
            "label": localbox_client.authenticator.label,
            "filename": filename,
            "localbox_filename": localbox_filename
        }

        name = "LocalBoxApp-{}".format(wx.GetUserId())
        instance = wx.SingleInstanceChecker(name)
        app_is_running = instance.IsAnotherRunning()

        if app_is_running:
            file_to_open = Notifs().openFileReq(data_dic)
        else:
            file_to_open = open_file(data_dic, memory)

        if file_to_open is not None and os.path.exists(file_to_open):
            open_file_ext(file_to_open)
            getLogger(__name__).info(
                'Finished decrypting and opening file: %s', filename)

        else:
            gui_utils.show_error_dialog(_('Failed to decode contents'),
                                        'Error',
                                        standalone=True)
            getLogger(__name__).info('failed to decode contents. aborting')

    except Exception as ex:
        getLogger(__name__).exception(ex)
        gui_utils.show_error_dialog(_('Exception {}').format(ex),
                                    'Error',
                                    standalone=True)