def test_store_keys(self): from sync.localbox import LocalBox from sync.controllers.login_ctrl import LoginController from sync.auth import Authenticator lox_client = LocalBox(url='https://localhost:5001', label=None) authenticator = Authenticator( authentication_url= 'http://localhost:5000/loauth/?redirect_uri=http%3A%2F%2Flocalhost%3A5001%2F', label=None) authenticator.init_authenticate('loxtest', 'p123') lox_client._authenticator = authenticator result = LoginController().store_keys(lox_client, None, None, 'p123')
def accept_invite(self, index, save=False): """ Delete item from list by 'index' :param index: :param save: :return: """ item = self._list[index] localbox_client = LocalBox(url=item.url, label=item.label, path=item.path) localbox_client.accept_invite(item.id) if save: self.save()
def check_server_connection(self, server): try: getLogger(__name__).debug("Connecting to server %s, %s", server.url, server.label) self.parent.localbox_client = LocalBox(server.url, server.label, None) except (URLError, InvalidURL, ValueError) as error: getLogger(__name__).debug("Can't access server") gui_utils.show_error_dialog( message=_('Can\'t connect to server given by URL'), title=_('Can\'t connect to server')) self.parent.localbox_client = None return except (BadStatusLine, auth.AlreadyAuthenticatedError) as error: getLogger(__name__).debug("error with authentication url thingie") getLogger(__name__).exception(error) gui_utils.show_error_dialog(message=_( 'Can\'t authenticate with given username and password.'), title=_('Can\'t authenticate.')) self.parent.localbox_client = None return except SocketError as e: if e.errno != errno.ECONNRESET: raise # Not the error we are looking for getLogger(__name__).error( 'Failed to connect to server, maybe forgot https? %s', e) self.parent.localbox_client = None return finally: self.parent.box_label = server.label
def get_syncers(): """ returns a list of Syncers. One per configured localbox instance. """ syncs_ctrl = SyncsController() sites = [] for sync_item in syncs_ctrl: getLogger(__name__).info("Syncing %s", sync_item.label) try: url = sync_item.url path = sync_item.path direction = sync_item.direction label = sync_item.label localbox_client = LocalBox(url, label) syncer = Syncer(localbox_client, path, direction, name=sync_item.label) sites.append(syncer) except NoOptionError as error: getLogger(__name__).exception(error) string = "Skipping '%s' due to missing option '%s'" % \ (sync_item, error.option) getLogger(__name__).info(string) except URLError as error: getLogger(__name__).exception(error) string = "Skipping '%s' because it cannot be reached" % \ (sync_item) getLogger(__name__).info(string) return sites
def validate_new_sync_inputs(self, event): # step 1 label = self.label url = self.url path = self.path if gui_utils.is_valid_input(label) and gui_utils.is_valid_input( url) and gui_utils.is_valid_input(path): self.sync_item = SyncItem(label=label, url=url, path=path) try: self.parent.localbox_client = LocalBox(url, label) except (URLError, BadStatusLine, ValueError, auth.AlreadyAuthenticatedError) as error: getLogger(__name__).debug( "error with authentication url thingie") getLogger(__name__).exception(error) except SocketError as e: if e.errno != errno.ECONNRESET: raise # Not error we are looking for getLogger(__name__).error( 'Failed to connect to server, maybe forgot https? %s', e) self.parent.box_label = label self.parent.path = path if not self.parent.localbox_client: getLogger(__name__).error('%s is not a valid URL' % url) gui_utils.show_error_dialog( message=_('%s is not a valid URL') % url, title=_('Invalid URL')) event.Veto() else: event.Veto()
def delete(self, index, save=False): """ Delete item from list by 'index' :param index: :param save: :return: """ item = self._list[index] localbox_client = LocalBox(url=item.url, label=item.label, path=item.path) localbox_client.delete_share(item.id) sql = 'delete from keys where site = ? and user != ?' database_execute(sql, (item.label, item.user)) del self._list[index] if save: self.save()
def load_invites(self): invite_list = [] for item in SyncsController().load(): try: localbox_client = LocalBox(url=item.url, label=item.label, path=item.path) result = localbox_client.get_invite_list(user=item.user) for invite in result: invite_list.append(invite) except URLError: getLogger(__name__).exception( 'failed to get_share_list (%s, %s)' % (item.url, item.label)) return invite_list
def load(self): self._list = [] for item in SyncsController().load(): try: localbox_client = LocalBox(url=item.url, label=item.label) for share in localbox_client.get_share_list(user=item.user): share_item = ShareItem(user=share['user'], path='/' + share['path'], url=item.url, label=item.label, id=share['id']) self._list.append(share_item) except URLError: getLogger(__name__).exception( 'failed to get_share_list (%s, %s)' % (item.url, item.label)) return self._list
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)
def create_watchdog(sync_item): try: url = sync_item.url label = sync_item.label localbox_client = LocalBox(url, label, sync_item.path) event_handler = LocalBoxEventHandler(localbox_client) observer = Observer() observer.setName('th-evt-%s' % sync_item.label) observer.schedule(event_handler, localbox_client.path, recursive=True) observer.start() getLogger(__name__).info('started watchdog for %s' % sync_item.path) except NoOptionError as error: getLogger(__name__).exception(error) string = "Skipping '%s' due to missing option '%s'" % (sync_item, error.option) getLogger(__name__).info(string) except URLError as error: getLogger(__name__).exception(error) string = "Skipping '%s' because it cannot be reached" % sync_item getLogger(__name__).info(string)
def open_file(data_dic, memory=False): # Get passphrase passphrase = LoginController().get_passphrase(data_dic["label"]) if not passphrase: passphrase = gui_utils.get_user_secret_input( "YourLocalBox - Enter Passphrase", "Please provide the passphrase to unlock file.") if not passphrase: gui_utils.show_error_dialog(_('No passphrase provided. aborting'), 'Error', standalone=True) return None # Stat local box instance localbox_client = LocalBox(data_dic["url"], data_dic["label"], "") # Attempt to decode the file # print data_dic, passphrase try: decoded_contents = localbox_client.decode_file( data_dic["localbox_filename"], data_dic["filename"], passphrase) # If there was a failure, answer wit ha 404 to state that the file doesn't exist except Exception as e: gui_utils.show_error_dialog( _('Failed to decode contents. aborting : {}').format(e), 'Error', standalone=True) getLogger(__name__).info( 'failed to decode contents. aborting : {}'.format(e)) return None # If the file was decoded, write it to disk tmp_decoded_filename = \ os_utils.remove_extension(data_dic["filename"], defaults.LOCALBOX_EXTENSION) getLogger(__name__).info('tmp_decoded_filename: %s' % tmp_decoded_filename) if os.path.exists(tmp_decoded_filename): os.remove(tmp_decoded_filename) if not memory: memory = LocalBoxMemoryFS() if not memory: localfile = open(tmp_decoded_filename, 'wb') localfile.write(decoded_contents) localfile.close() else: tmp_decoded_filename = memory.createfile( tmp_decoded_filename.split('/')[-1], decoded_contents) # Keep file in list of opened files openfiles_ctrl.add(tmp_decoded_filename) return tmp_decoded_filename
import logging from sync.controllers.login_ctrl import LoginController from sync.localbox import LocalBox from sync.syncer import Syncer logging.getLogger('sync').setLevel(logging.WARNING) url = 'https://box.yourlocalbox.org' path = '/home/wilson/git-new/LinWin-PySync/lo-wilson-new/' direction = 'potatoes' label = 'lo-wilson' localbox_client = LocalBox(url, label) syncer = Syncer(localbox_client, path, direction, name=label) LoginController().store_passphrase(passphrase='p123', label=label, user='******') syncer.syncsync()
def localbox_client(self): localbox_item = localbox_ctrl.ctrl.get(self.selected_localbox) return LocalBox(url=localbox_item.url, label=localbox_item.label)
def run_file_decryption(filename): 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 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) 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) # get passphrase label = localbox_client.authenticator.label passphrase = LoginController().get_passphrase(label, remote=True) if not passphrase: gui_wx.ask_passphrase(localbox_client.username, label) passphrase = LoginController().get_passphrase(label, remote=False) if not passphrase: gui_utils.show_error_dialog( _('Failed to get passphrase for label: %s.') % label, 'Error', True) getLogger(__name__).error( 'failed to get passphrase for label: %s. Exiting..' % label) exit(1) # decode file try: decoded_contents = localbox_client.decode_file( localbox_filename, filename, passphrase) except URLError: gui_utils.show_error_dialog(_('Failed to decode contents'), 'Error', standalone=True) getLogger(__name__).info('failed to decode contents. aborting') return 1 # write file tmp_decoded_filename = os_utils.remove_extension( filename, defaults.LOCALBOX_EXTENSION) getLogger(__name__).info('tmp_decoded_filename: %s' % tmp_decoded_filename) if os.path.exists(tmp_decoded_filename): os.remove(tmp_decoded_filename) localfile = open(tmp_decoded_filename, 'wb') localfile.write(decoded_contents) localfile.close() # open file open_file_ext(tmp_decoded_filename) openfiles_ctrl.add(tmp_decoded_filename) getLogger(__name__).info('Finished decrypting and opening file: %s', filename) except Exception as ex: getLogger(__name__).exception(ex)
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)