mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-20 01:50:04 +00:00
unstable: fix sync via ssh
This commit is contained in:
parent
40240934c5
commit
75a8348086
5 changed files with 151 additions and 182 deletions
|
@ -28,7 +28,7 @@ module MPW
|
||||||
# @args: password -> the GPG key password
|
# @args: password -> the GPG key password
|
||||||
# @rtrn: true if data has been decrypted
|
# @rtrn: true if data has been decrypted
|
||||||
def decrypt(password=nil)
|
def decrypt(password=nil)
|
||||||
if File.exist?(@file_gpg)
|
if File.exist?(@file_gpg) and not File.zero?(@file_gpg)
|
||||||
crypto = GPGME::Crypto.new(armor: true)
|
crypto = GPGME::Crypto.new(armor: true)
|
||||||
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
|
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
|
||||||
|
|
||||||
|
|
|
@ -1,147 +0,0 @@
|
||||||
#!/usr/bin/ruby
|
|
||||||
# author: nishiki
|
|
||||||
# mail: nishiki@yaegashi.fr
|
|
||||||
# info: a simple script who manage your passwords
|
|
||||||
|
|
||||||
module MPW
|
|
||||||
|
|
||||||
module Sync
|
|
||||||
|
|
||||||
require 'rubygems'
|
|
||||||
require 'i18n'
|
|
||||||
require 'socket'
|
|
||||||
require 'json'
|
|
||||||
require 'timeout'
|
|
||||||
|
|
||||||
class MPWSync
|
|
||||||
|
|
||||||
attr_accessor :error_msg
|
|
||||||
attr_accessor :enable
|
|
||||||
|
|
||||||
# Constructor
|
|
||||||
def initialize
|
|
||||||
@error_msg = nil
|
|
||||||
@enable = false
|
|
||||||
end
|
|
||||||
|
|
||||||
# Connect to server
|
|
||||||
# @args: host -> the server host
|
|
||||||
# port -> ther connection port
|
|
||||||
# gpg_key -> the gpg key
|
|
||||||
# password -> the remote password
|
|
||||||
# suffix -> the suffix file
|
|
||||||
# @rtrn: false if the connection fail
|
|
||||||
def connect(host, user, password, path, port=nil)
|
|
||||||
@host = host
|
|
||||||
@port = !port.instance_of?(Integer) ? 2201 : port
|
|
||||||
@gpg_key = user
|
|
||||||
@password = password
|
|
||||||
@suffix = path
|
|
||||||
|
|
||||||
Timeout.timeout(10) do
|
|
||||||
begin
|
|
||||||
TCPSocket.open(@host, @port) do
|
|
||||||
@enable = true
|
|
||||||
end
|
|
||||||
rescue Errno::ENETUNREACH
|
|
||||||
retry
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue Timeout::Error
|
|
||||||
puts 'timeout'
|
|
||||||
@error_msg = "#{I18n.t('error.timeout')}\n#{e}"
|
|
||||||
@enable = false
|
|
||||||
rescue Exception => e
|
|
||||||
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
|
||||||
@enable = false
|
|
||||||
else
|
|
||||||
return @enable
|
|
||||||
end
|
|
||||||
|
|
||||||
# Get data on server
|
|
||||||
# @args: gpg_password -> the gpg password
|
|
||||||
# @rtrn: nil if nothing data or error
|
|
||||||
def get(gpg_key, gpg_password)
|
|
||||||
return nil if not @enable
|
|
||||||
|
|
||||||
msg = nil
|
|
||||||
TCPSocket.open(@host, @port) do |socket|
|
|
||||||
send_msg = {action: 'get',
|
|
||||||
gpg_key: @gpg_key,
|
|
||||||
password: @password,
|
|
||||||
suffix: @suffix
|
|
||||||
}
|
|
||||||
|
|
||||||
socket.puts send_msg.to_json
|
|
||||||
msg = JSON.parse(socket.gets)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not defined?(msg['error'])
|
|
||||||
@error_msg = I18n.t('error.sync.communication')
|
|
||||||
return nil
|
|
||||||
elsif not msg['error'].nil?
|
|
||||||
@error_msg = I18n.t(msg['error'])
|
|
||||||
return nil
|
|
||||||
elsif msg['data'].nil? or msg['data'].empty?
|
|
||||||
return {}
|
|
||||||
else
|
|
||||||
tmp_file = tmpfile
|
|
||||||
File.open(tmp_file, 'w') do |file|
|
|
||||||
file << msg['data']
|
|
||||||
end
|
|
||||||
|
|
||||||
mpw = MPW.new(tmp_file, gpg_key)
|
|
||||||
if not mpw.decrypt(gpg_password)
|
|
||||||
@error_msg = mpw.error_msg
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
File.unlink(tmp_file)
|
|
||||||
return mpw.search
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Update the remote data
|
|
||||||
# @args: data -> the data to send on server
|
|
||||||
# @rtrn: false if there is a problem
|
|
||||||
def update(data)
|
|
||||||
return true if not @enable
|
|
||||||
|
|
||||||
msg = nil
|
|
||||||
TCPSocket.open(@host, @port) do |socket|
|
|
||||||
send_msg = {action: 'update',
|
|
||||||
gpg_key: @gpg_key,
|
|
||||||
password: @password,
|
|
||||||
suffix: @suffix,
|
|
||||||
data: data
|
|
||||||
}
|
|
||||||
|
|
||||||
socket.puts send_msg.to_json
|
|
||||||
msg = JSON.parse(socket.gets)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not defined?(msg['error'])
|
|
||||||
@error_msg = I18n.t('error.sync.communication')
|
|
||||||
return false
|
|
||||||
elsif msg['error'].nil?
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
@error_msg = I18n.t(msg['error'])
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generate a random string
|
|
||||||
# @rtrn: a random string
|
|
||||||
def tmpfile
|
|
||||||
result = ''
|
|
||||||
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(6).join
|
|
||||||
|
|
||||||
return "/tmp/mpw-#{result}"
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -4,10 +4,9 @@
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
require 'yaml'
|
|
||||||
require 'tempfile'
|
require 'tempfile'
|
||||||
require 'net/ssh'
|
require 'net/ssh'
|
||||||
require 'net/scp'
|
require 'net/sftp'
|
||||||
|
|
||||||
module MPW
|
module MPW
|
||||||
class SyncSSH
|
class SyncSSH
|
||||||
|
@ -34,7 +33,7 @@ module MPW
|
||||||
# Connect to server
|
# Connect to server
|
||||||
# @rtrn: false if the connection fail
|
# @rtrn: false if the connection fail
|
||||||
def connect
|
def connect
|
||||||
Net::SSH.start(@host, @user, password: @password, port: @port) do
|
Net::SSH.start(@host, @user, password: @password, port: @port) do |ssh|
|
||||||
@enable = true
|
@enable = true
|
||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
@ -47,51 +46,36 @@ module MPW
|
||||||
# Get data on server
|
# Get data on server
|
||||||
# @args: gpg_password -> the gpg password
|
# @args: gpg_password -> the gpg password
|
||||||
# @rtrn: nil if nothing data or error
|
# @rtrn: nil if nothing data or error
|
||||||
def get(gpg_key, gpg_password)
|
def get(file_tmp)
|
||||||
return nil if not @enable
|
return false if not @enable
|
||||||
|
|
||||||
file_tmp = Tempfile.new('mpw')
|
puts @path
|
||||||
Net::SCP.start(@host, @user, password: @password, port: @port) do |scp|
|
puts file_tmp
|
||||||
scp.download!(@path, file_tmp.path)
|
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
|
end
|
||||||
|
|
||||||
mpw = MPW.new(file_tmp, gpg_key)
|
|
||||||
raise mpw.error_msg if not mpw.decrypt(gpg_password)
|
|
||||||
|
|
||||||
file_tmp.close(true)
|
|
||||||
|
|
||||||
return mpw.list
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
||||||
file_tmp.close(true)
|
return false
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the remote data
|
# Update the remote data
|
||||||
# @args: items -> the data to send on server
|
# @args: file_gpg -> the data to send on server
|
||||||
# @rtrn: false if there is a problem
|
# @rtrn: false if there is a problem
|
||||||
def update(items)
|
def update(file_gpg)
|
||||||
return true if not @enable
|
return true if not @enable
|
||||||
|
|
||||||
file_tmp = Tempfile.new('mpw')
|
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
|
||||||
|
sftp.upload!(file_gpg, @path)
|
||||||
mpw = MPW.new(file_tmp, gpg_key)
|
|
||||||
items.each do |item|
|
|
||||||
mpw.add(item)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
raise(mpw.error_msg) if not mpw.encrypt
|
|
||||||
|
|
||||||
Net::SCP.start(@host, @user, password: @password, port: @port) do |scp|
|
|
||||||
scp.upload!(file_tmp, @path)
|
|
||||||
end
|
|
||||||
|
|
||||||
file_tmp.close(true)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
||||||
file_tmp.close(true)
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
132
lib/Sync/SyncMPW.rb
Normal file
132
lib/Sync/SyncMPW.rb
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
#!/usr/bin/ruby
|
||||||
|
# author: nishiki
|
||||||
|
|
||||||
|
require 'rubygems'
|
||||||
|
require 'i18n'
|
||||||
|
require 'socket'
|
||||||
|
require 'json'
|
||||||
|
require 'timeout'
|
||||||
|
|
||||||
|
module MPW
|
||||||
|
class SyncMPW
|
||||||
|
|
||||||
|
attr_accessor :error_msg
|
||||||
|
attr_accessor :enable
|
||||||
|
|
||||||
|
# Constructor
|
||||||
|
# @args: host -> the server host
|
||||||
|
# port -> ther connection port
|
||||||
|
# gpg_key -> the gpg key
|
||||||
|
# password -> the remote password
|
||||||
|
# suffix -> the suffix file
|
||||||
|
def initialize(host, user, password, suffix, port=nil)
|
||||||
|
@error_msg = nil
|
||||||
|
@enable = false
|
||||||
|
|
||||||
|
@host = host
|
||||||
|
@port = !port.instance_of?(Integer) ? 2201 : port
|
||||||
|
@gpg_key = user
|
||||||
|
@password = password
|
||||||
|
@suffix = suffix
|
||||||
|
end
|
||||||
|
|
||||||
|
# Connect to server
|
||||||
|
# @rtrn: false if the connection fail
|
||||||
|
def connect
|
||||||
|
Timeout.timeout(10) do
|
||||||
|
begin
|
||||||
|
TCPSocket.open(@host, @port) do
|
||||||
|
@enable = true
|
||||||
|
end
|
||||||
|
rescue Errno::ENETUNREACH
|
||||||
|
retry
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue Timeout::Error
|
||||||
|
@error_msg = "#{I18n.t('error.timeout')}\n#{e}"
|
||||||
|
@enable = false
|
||||||
|
rescue Exception => e
|
||||||
|
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
||||||
|
@enable = false
|
||||||
|
else
|
||||||
|
return @enable
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get data on server
|
||||||
|
# @args: gpg_password -> the gpg password
|
||||||
|
# @rtrn: nil if nothing data or error
|
||||||
|
def get(gpg_key, gpg_password)
|
||||||
|
return nil if not @enable
|
||||||
|
|
||||||
|
msg = nil
|
||||||
|
TCPSocket.open(@host, @port) do |socket|
|
||||||
|
send_msg = {action: 'get',
|
||||||
|
gpg_key: @gpg_key,
|
||||||
|
password: @password,
|
||||||
|
suffix: @suffix
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.puts send_msg.to_json
|
||||||
|
msg = JSON.parse(socket.gets)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not defined?(msg['error'])
|
||||||
|
@error_msg = I18n.t('error.sync.communication')
|
||||||
|
return nil
|
||||||
|
elsif not msg['error'].nil?
|
||||||
|
@error_msg = I18n.t(msg['error'])
|
||||||
|
return nil
|
||||||
|
elsif msg['data'].nil? or msg['data'].empty?
|
||||||
|
return {}
|
||||||
|
else
|
||||||
|
file_tmp = Tempfile.new('mpw-')
|
||||||
|
File.open(file_tmp, 'w') do |file|
|
||||||
|
file << msg['data']
|
||||||
|
end
|
||||||
|
|
||||||
|
mpw = MPW.new(file_tmp, gpg_key)
|
||||||
|
raise mpw.error_msg if not mpw.decrypt(gpg_password)
|
||||||
|
|
||||||
|
file_tmp.close(true)
|
||||||
|
|
||||||
|
puts 'test'
|
||||||
|
return mpw.list
|
||||||
|
end
|
||||||
|
rescue Exception => e
|
||||||
|
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
||||||
|
file_tmp.close(true)
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
# Update the remote data
|
||||||
|
# @args: data -> the data to send on server
|
||||||
|
# @rtrn: false if there is a problem
|
||||||
|
def update(data)
|
||||||
|
return true if not @enable
|
||||||
|
|
||||||
|
msg = nil
|
||||||
|
TCPSocket.open(@host, @port) do |socket|
|
||||||
|
send_msg = {action: 'update',
|
||||||
|
gpg_key: @gpg_key,
|
||||||
|
password: @password,
|
||||||
|
suffix: @suffix,
|
||||||
|
data: data
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.puts send_msg.to_json
|
||||||
|
msg = JSON.parse(socket.gets)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not defined?(msg['error'])
|
||||||
|
@error_msg = I18n.t('error.sync.communication')
|
||||||
|
return false
|
||||||
|
elsif msg['error'].nil?
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
@error_msg = I18n.t(msg['error'])
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -25,13 +25,13 @@ class Cli
|
||||||
# Sync the data with the server
|
# Sync the data with the server
|
||||||
# @rtnr: true if the synchro is finish
|
# @rtnr: true if the synchro is finish
|
||||||
def sync
|
def sync
|
||||||
@sync = MPW::Sync.new(@config, @password, @mpw.list)
|
@sync = MPW::Sync.new(@config, @mpw, @password)
|
||||||
|
|
||||||
raise(@sync.error_msg) if not @sync.get_remote
|
raise(@sync.error_msg) if not @sync.get_remote
|
||||||
raise(@sync.error_msg) if not @sync.sync
|
raise(@sync.error_msg) if not @sync.sync
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
# rescue Exception => e
|
||||||
puts "#{I18n.t('display.error')} #7: #{e}".red
|
puts "#{I18n.t('display.error')} #7: #{e}".red
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue