From 871b1dcbed1490d47b8bab40cd22e12aae6eb879 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 27 Mar 2017 21:47:43 +0200 Subject: [PATCH] remove all sync --- README.md | 6 --- bin/mpw-add | 9 +--- bin/mpw-copy | 7 +-- bin/mpw-delete | 11 ++--- bin/mpw-export | 11 ++--- bin/mpw-import | 9 +--- bin/mpw-list | 11 ++--- bin/mpw-update | 11 ++--- bin/mpw-wallet | 34 +------------- i18n/en.yml | 26 ----------- i18n/fr.yml | 26 ----------- lib/mpw/cli.rb | 32 +------------ lib/mpw/item.rb | 7 --- lib/mpw/mpw.rb | 106 -------------------------------------------- lib/mpw/sync/ftp.rb | 68 ---------------------------- lib/mpw/sync/ssh.rb | 67 ---------------------------- 16 files changed, 20 insertions(+), 421 deletions(-) delete mode 100644 lib/mpw/sync/ftp.rb delete mode 100644 lib/mpw/sync/ssh.rb diff --git a/README.md b/README.md index 7ac7d47..fd2534a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ mpw is a little software which stores your passwords in [GnuPG](http://www.gnupg * generate OTP code * copy your login, password or otp in clipboard * manage many wallets - * synchronize your passwords with SSH or FTP. * share a wallet with others GPG keys ## Install @@ -105,11 +104,6 @@ Remove a GPG key: mpw wallet --delete-gpg-key test42@localhost.com ``` -Add synchronize: -``` -mpw wallet --protocol ssh --host example.com --user test --path /remote/path --password -``` - ### Export and import data You can export your data in yaml file with your passwords in clear text: diff --git a/bin/mpw-add b/bin/mpw-add index 5322cc1..fc688a4 100644 --- a/bin/mpw-add +++ b/bin/mpw-add @@ -24,8 +24,7 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true +options = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw add [options]" @@ -39,10 +38,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-r', '--random', I18n.t('option.random_password')) do options[:password] = true end @@ -53,7 +48,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-copy b/bin/mpw-copy index 0a14ed7..1e5804b 100644 --- a/bin/mpw-copy +++ b/bin/mpw-copy @@ -25,7 +25,6 @@ require 'mpw/cli' # --------------------------------------------------------- # options = {} -options[:sync] = true options[:clipboard] = true values = {} @@ -49,10 +48,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| values[:pattern] = pattern end @@ -63,7 +58,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-delete b/bin/mpw-delete index 2d79e6a..53c8074 100644 --- a/bin/mpw-delete +++ b/bin/mpw-delete @@ -24,9 +24,8 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true -values = {} +options = {} +values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw delete [options]" @@ -44,10 +43,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| values[:pattern] = pattern end @@ -58,7 +53,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-export b/bin/mpw-export index f891873..7913040 100644 --- a/bin/mpw-export +++ b/bin/mpw-export @@ -24,9 +24,8 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true -values = {} +options = {} +values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" @@ -48,10 +47,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| values[:pattern] = pattern end @@ -62,7 +57,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-import b/bin/mpw-import index c740252..a827783 100644 --- a/bin/mpw-import +++ b/bin/mpw-import @@ -24,8 +24,7 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true +options = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw import [options]" @@ -43,17 +42,13 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-list b/bin/mpw-list index 9d18b0d..3ebc2b8 100644 --- a/bin/mpw-list +++ b/bin/mpw-list @@ -24,9 +24,8 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true -values = {} +options = {} +values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw list [options]" @@ -48,17 +47,13 @@ OptionParser.new do |opts| values[:pattern] = pattern end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-update b/bin/mpw-update index 221c632..fd207c5 100644 --- a/bin/mpw-update +++ b/bin/mpw-update @@ -24,9 +24,8 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = true -values = {} +options = {} +values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw update [options]" @@ -44,10 +43,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| values[:pattern] = pattern end @@ -62,7 +57,7 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) diff --git a/bin/mpw-wallet b/bin/mpw-wallet index fbf0389..56e96ba 100644 --- a/bin/mpw-wallet +++ b/bin/mpw-wallet @@ -25,9 +25,7 @@ require 'mpw/cli' # --------------------------------------------------------- # options = {} -options[:sync] = {} options[:delete] = false -values = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" @@ -50,10 +48,6 @@ OptionParser.new do |opts| exit 0 end - opts.on('--host NAME', I18n.t('option.host')) do |host| - values[:host] = host - end - opts.on('-l', '--list', I18n.t('option.list')) do options[:list] = true end @@ -62,37 +56,13 @@ OptionParser.new do |opts| options[:list_keys] = true end - opts.on('-n', '--no-sync', I18n.t('option.no_sync')) do - options[:sync] = false - end - - opts.on('--password', I18n.t('option.password')) do - values[:password] = true - end - - opts.on('--path PATH', I18n.t('option.path')) do |path| - values[:path] = path - end - - opts.on('--port NUMBER', I18n.t('option.port')) do |port| - values[:port] = port - end - - opts.on('--protocol NAME', I18n.t('option.protocol')) do |protocol| - values[:protocol] = protocol - end - - opts.on('--user NAME', I18n.t('option.user')) do |user| - values[:user] = user - end - opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:sync]) +cli = MPW::Cli.new(config) cli.load_config @@ -110,7 +80,5 @@ else else cli.add_key(options[:gpg_key]) end - elsif not values.empty? - cli.setup_wallet_config(values) end end diff --git a/i18n/en.yml b/i18n/en.yml index 5ee9868..162f6b3 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -23,15 +23,6 @@ en: import: "Can't import, unable to read %{file}!" update: host_empty: "You must define a host!" - sync: - general: "An error has appeared during the sync" - connection: "Connection fail!" - communication: "A communication problem with the server is appeared!" - download: "Can't download the file!" - not_authorized: "You haven't the access to remote file!" - upload: "Can't upload the file on the server!" - unknown: "An unknown error is occured!" - unknown_type: "The sync type is unknown" warning: select: 'Your choice is not a valid element!' @@ -68,7 +59,6 @@ en: gpg_key: "Specify a GPG key (ex: user@example.com)" group: "Search the items with specified group" help: "Show this help message" - host: "Specify the server for the synchronization" init: "Initialize mpw" import: "Import item since a yaml file" key: "Specify the key name" @@ -76,13 +66,8 @@ en: length: "Size of the password" list: "List the wallets" list_keys: "List the GPG keys in wallet" - no_sync: "Disable synchronization with the server" numeric: "Use number to generate a password" - password: "Change the password for the synchronization" - path: "Specify the remote path" pattern: "Given search pattern" - port: "Specify the connection port" - protocol: "Specify the protocol for the connection" random_password: "Generate a random password" setup: "Create a new configuration file" setup_wallet: "Create a new configuration file for a wallet" @@ -90,7 +75,6 @@ en: show: "Search and show the items" show_all: "List all items" usage: "Usage" - user: "Specify the user for the connection" wallet: "Specify a wallet to use" wallet_dir: "Set the wallets folder" @@ -141,16 +125,6 @@ en: gpg_exe: "Enter the executable GPG path (optional): " wallet_dir: "Enter the wallets's folder path [default=%{home}/wallets]: " valid: "The config file has been created!" - setup_wallet: - password: "Sync password: " - title: "Wallet setup" - sync_type: "Synchronization type (ssh, ftp): " - sync_host: "Synchronization server: " - sync_port: "Port of the synchronization server: " - sync_user: "Username for the synchronization: " - sync_pwd: "Password for the synchronization: " - sync_path: "File path for the synchronization : " - valid: "The wallet config file has been created!" setup_gpg_key: title: "Setup a GPG key" ask: "Do you want create your GPG key ? (Y/n)" diff --git a/i18n/fr.yml b/i18n/fr.yml index d9bd279..b2e3db4 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -23,15 +23,6 @@ fr: import: "Impossible d'importer le fichier %{file}, car il n'est pas lisible!" update: host_empty: "Vous devez définir un host!" - sync: - general: "Une erreur est survenue durant la synchronisation" - connection: "La connexion n'a pu être établie!" - communication: "Un problème de communication avec le serveur est apparu!" - download: "Impossible de télécharger le fichier!" - not_authorized: "Vous n'avez pas les autorisations d'accès au fichier distant!" - upload: "Impossible d'envoyer le fichier sur le serveur!" - unknown: "Une erreur inconnue est survenue!" - unknown_type: "Le type de synchronisation est inconnu" warning: select: "Votre choix n'est pas un élément valide!" @@ -68,7 +59,6 @@ fr: gpg_key: "Spécifie une clé GPG (ex: user@example.com)" group: "Recherche les éléments appartenant au groupe spécifié" help: "Affiche ce message d'aide" - host: "Spécifie le serveur pour la synchronisation" import: "Importe des éléments depuis un fichier yaml" init: "Initialise mpw" key: "Spécifie le nom d'une clé" @@ -76,13 +66,8 @@ fr: length: "Taille du mot de passe" list: "Liste les portefeuilles" list_keys: "Liste les clés GPG dans le portefeuille" - no_sync: "Désactive la synchronisation avec le serveur" numeric: "Utilise des chiffre dans la génération d'un mot de passe" - password: "Changer le mot de passe de connexion" - path: "Spécifie le chemin distant" pattern: "Motif de donnée à chercher" - port: "Spécifie le port de connexion" - protocol: "Spécifie le protocol utilisé pour la connexion" random_password: "Génére un mot de passe aléatoire" setup: "Création d'un nouveau fichier de configuration" setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille" @@ -90,7 +75,6 @@ fr: show: "Recherche et affiche les éléments" show_all: "Liste tous les éléments" usage: "Utilisation" - user: "Spécifie l'identifiant de connection" wallet: "Spécifie le portefeuille à utiliser" wallet_dir: "Spécifie le répertoire des portefeuilles" @@ -141,16 +125,6 @@ fr: gpg_exe: "Entrez le chemin de l'exécutable GPG (optionnel): " wallet_dir: "Entrez le chemin du répertoire qui contiendra les porte-feuilles de mot de passe [défaut=%{home}/wallets]: " valid: "Le fichier de configuration a bien été créé!" - setup_wallet: - password: "Mot de passe de synchronisation: " - title: "Configuration du porte-feuille" - sync_type: "Type de synchronisation (ssh, ftp): " - sync_host: "Serveur: " - sync_port: "Port: " - sync_user: "Utilisateur: " - sync_pwd: "Mot de passe: " - sync_path: "Chemin du fichier: " - valid: "Le fichier de configuration du porte-feuille a bien été créé!" setup_gpg_key: title: "Configuration d'une nouvelle clé GPG" ask: "Voulez vous créer votre clé GPG ? (O/n)" diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 90fe00d..0eb294e 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -31,10 +31,8 @@ class Cli # Constructor # @args: config -> the config - # sync -> boolean for sync or not - def initialize(config, sync=true) - @config = config - @sync = sync + def initialize(config) + @config = config end # Change a parameter int the config after init @@ -89,26 +87,6 @@ class Cli exit 2 end - # Setup wallet config for sync - # @args: options -> value to change - def setup_wallet_config(options={}) - if not options[:password].nil? - options[:password] = ask(I18n.t('form.setup_wallet.password')) {|q| q.echo = false} - end - - #wallet_file = wallet.nil? ? "#{@config.wallet_dir}/default.mpw" : "#{@config.wallet_dir}/#{wallet}.mpw" - - @mpw = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe) - @mpw.read_data - @mpw.set_config(options) - @mpw.write_data - - puts "#{I18n.t('form.setup_wallet.valid')}".green - rescue Exception => e - puts "#{I18n.t('display.error')} #10: #{e}".red - exit 2 - end - # List gpg keys in wallet def list_keys table_list('keys', @mpw.list_keys) @@ -131,7 +109,6 @@ class Cli end @mpw.read_data - @mpw.sync if @sync rescue Exception => e puts "#{I18n.t('display.error')} #11: #{e}".red exit 2 @@ -369,7 +346,6 @@ class Cli def add_key(key) @mpw.add_key(key) @mpw.write_data - @mpw.sync(true) if @sync puts "#{I18n.t('form.add_key.valid')}".green rescue Exception => e @@ -381,7 +357,6 @@ class Cli def delete_key(key) @mpw.delete_key(key) @mpw.write_data - @mpw.sync(true) if @sync puts "#{I18n.t('form.delete_key.valid')}".green rescue Exception => e @@ -431,7 +406,6 @@ class Cli @mpw.set_password(item.id, options[:password]) if options.has_key?(:password) @mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key) @mpw.write_data - @mpw.sync(true) if @sync puts "#{I18n.t('form.add_item.valid')}".green rescue Exception => e @@ -457,7 +431,6 @@ class Cli @mpw.set_password(item.id, options[:password]) if options.has_key?(:password) @mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key) @mpw.write_data - @mpw.sync(true) if @sync puts "#{I18n.t('form.update_item.valid')}".green end @@ -484,7 +457,6 @@ class Cli item.delete @mpw.write_data - @mpw.sync(true) if @sync puts "#{I18n.t('form.delete_item.valid')}".green end diff --git a/lib/mpw/item.rb b/lib/mpw/item.rb index d3783b9..a4f730b 100644 --- a/lib/mpw/item.rb +++ b/lib/mpw/item.rb @@ -30,7 +30,6 @@ class Item attr_accessor :otp attr_accessor :comment attr_accessor :last_edit - attr_accessor :last_sync attr_accessor :created # Constructor @@ -72,11 +71,6 @@ class Item @last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit) end - # Update last_sync - def set_last_sync - @last_sync = Time.now.to_i - end - # Delete all data def delete @id = nil @@ -89,7 +83,6 @@ class Item @comment = nil @created = nil @last_edit = nil - @last_sync = nil end def empty? diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index fe46108..8f4b42e 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -229,7 +229,6 @@ class MPW @config['user'] = options[:user] if options.has_key?(:user) @config['password'] = options[:password] if options.has_key?(:password) @config['path'] = options[:path] if options.has_key?(:path) - @config['last_sync'] = @config['last_sync'].nil? ? 0 : @config['last_sync'] end # Add a new item @@ -281,111 +280,6 @@ class MPW return nil end - # Get last sync - def get_last_sync - return @config['last_sync'].to_i - rescue - return 0 - end - - # Sync data with remote file - # @args: force -> force the sync - def sync(force=false) - return if @config.empty? or @config['protocol'].to_s.empty? - return if get_last_sync + 300 > Time.now.to_i and not force - - tmp_file = "#{@wallet_file}.sync" - - case @config['protocol'] - when 'sftp', 'scp', 'ssh' - require "mpw/sync/ssh" - sync = SyncSSH.new(@config) - when 'ftp' - require 'mpw/sync/ftp' - sync = SyncFTP.new(@config) - else - raise I18n.t('error.sync.unknown_type') - end - - sync.connect - sync.get(tmp_file) - - remote = MPW.new(@key, tmp_file, @gpg_pass, @gpg_exe) - remote.read_data - - File.unlink(tmp_file) if File.exist?(tmp_file) - - return if remote.get_last_sync == @config['last_update'] - - if not remote.to_s.empty? - @data.each do |item| - next if item.empty? - - update = false - - remote.list.each do |r| - next if item.id != r.id - - # Update item - if item.last_edit < r.last_edit - item.update(group: r.group, - host: r.host, - protocol: r.protocol, - user: r.user, - port: r.port, - comment: r.comment - ) - set_password(item.id, remote.get_password(item.id)) - end - - r.delete - update = true - - break - end - - # Remove an old item - if not update and item.last_sync.to_i < get_last_sync and item.last_edit < get_last_sync - item.delete - end - end - end - - # Add item - remote.list.each do |r| - next if r.last_edit <= get_last_sync - - item = Item.new(id: r.id, - group: r.group, - host: r.host, - protocol: r.protocol, - user: r.user, - port: r.port, - comment: r.comment, - created: r.created, - last_edit: r.last_edit - ) - - set_password(item.id, remote.get_password(item.id)) - add(item) - end - - remote = nil - - @data.each do |item| - item.set_last_sync - end - - @config['last_sync'] = Time.now.to_i - - write_data - sync.update(@wallet_file) - rescue Exception => e - File.unlink(tmp_file) if File.exist?(tmp_file) - - raise "#{I18n.t('error.sync.general')}\n#{e}" - end - # Set an opt key # args: id -> the item id # key -> the new key diff --git a/lib/mpw/sync/ftp.rb b/lib/mpw/sync/ftp.rb deleted file mode 100644 index d64e629..0000000 --- a/lib/mpw/sync/ftp.rb +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/ruby -# MPW is a software to crypt and manage your passwords -# Copyright (C) 2016 Adrien Waksberg -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -require 'i18n' -require 'net/ftp' - -module MPW -class FTP - - # Constructor - # @args: config -> the config - def initialize(config) - @host = config['host'] - @user = config['user'] - @password = config['password'] - @path = config['path'] - @port = config['port'].instance_of?(Integer) ? 22 : config['port'] - end - - - # Connect to server - def connect - Net::FTP.open(@host) do |ftp| - ftp.login(@user, @password) - break - end - rescue Exception => e - raise "#{I18n.t('error.sync.connection')}\n#{e}" - end - - # Get data on server - # @args: file_tmp -> the path where download the file - def get(file_tmp) - Net::FTP.open(@host) do |ftp| - ftp.login(@user, @password) - ftp.gettextfile(@path, file_tmp) - end - rescue Exception => e - raise "#{I18n.t('error.sync.download')}\n#{e}" - end - - # Update the remote data - # @args: file_gpg -> the data to send on server - def update(file_gpg) - Net::FTP.open(@host) do |ftp| - ftp.login(@user, @password) - ftp.puttextfile(file_gpg, @path) - end - rescue Exception => e - raise "#{I18n.t('error.sync.upload')}\n#{e}" - end -end -end diff --git a/lib/mpw/sync/ssh.rb b/lib/mpw/sync/ssh.rb deleted file mode 100644 index 4471949..0000000 --- a/lib/mpw/sync/ssh.rb +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/ruby -# MPW is a software to crypt and manage your passwords -# Copyright (C) 2016 Adrien Waksberg -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -require 'i18n' -require 'net/ssh' -require 'net/sftp' - -module MPW -class SyncSSH - - # Constructor - # @args: config -> the config - def initialize(config) - @host = config['host'] - @user = config['user'] - @password = config['password'] - @path = config['path'] - @port = config['port'].instance_of?(Integer) ? 22 : config['port'] - end - - # Connect to server - def connect - Net::SSH.start(@host, @user, password: @password, port: @port) do - break - end - rescue Exception => e - raise "#{I18n.t('error.sync.connection')}\n#{e}" - end - - # Get data on server - # @args: file_tmp -> the path where download the file - def get(file_tmp) - Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| - sftp.lstat(@path) do |response| - sftp.download!(@path, file_tmp) if response.ok? - end - end - rescue Exception => e - raise "#{I18n.t('error.sync.download')}\n#{e}" - end - - # Update the remote data - # @args: file_gpg -> the data to send on server - def update(file_gpg) - Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| - sftp.upload!(file_gpg, @path) - end - rescue Exception => e - raise "#{I18n.t('error.sync.upload')}\n#{e}" - end -end -end