mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 22:03:05 +00:00
fix FTP sync
This commit is contained in:
parent
cdf2de0fb4
commit
68062fe728
2 changed files with 71 additions and 102 deletions
|
@ -1,41 +1,37 @@
|
||||||
#!/usr/bin/ruby
|
#!/usr/bin/ruby
|
||||||
# author: nishiki
|
# author: nishiki
|
||||||
# mail: nishiki@yaegashi.fr
|
# mail: nishiki@yaegashi.fr
|
||||||
# info: a simple script who manage your passwords
|
|
||||||
|
|
||||||
module MPW
|
|
||||||
|
|
||||||
module Sync
|
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
require 'net/ftp'
|
require 'net/ftp'
|
||||||
|
|
||||||
|
module MPW
|
||||||
class FTP
|
class FTP
|
||||||
|
|
||||||
attr_accessor :error_msg
|
attr_accessor :error_msg
|
||||||
attr_accessor :enable
|
attr_accessor :enable
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def initialize
|
|
||||||
@error_msg = nil
|
|
||||||
@enable = false
|
|
||||||
end
|
|
||||||
|
|
||||||
# Connect to server
|
|
||||||
# @args: host -> the server host
|
# @args: host -> the server host
|
||||||
# port -> ther connection port
|
# port -> ther connection port
|
||||||
# gpg_key -> the gpg key
|
# gpg_key -> the gpg key
|
||||||
# password -> the remote password
|
# password -> the remote password
|
||||||
# suffix -> the suffix file
|
# suffix -> the suffix file
|
||||||
# @rtrn: false if the connection fail
|
def initialize(host, user, password, path, port=nil)
|
||||||
def connect(host, user, password, path, port=nil)
|
@error_msg = nil
|
||||||
|
@enable = false
|
||||||
|
|
||||||
@host = host
|
@host = host
|
||||||
@user = user
|
@user = user
|
||||||
@password = password
|
@password = password
|
||||||
@path = path
|
@path = path
|
||||||
@port = port.instance_of?(Integer) ? 21 : port
|
@port = port.instance_of?(Integer) ? 21 : port
|
||||||
|
end
|
||||||
|
|
||||||
|
# Connect to server
|
||||||
|
# @rtrn: false if the connection fail
|
||||||
|
def connect
|
||||||
Net::FTP.open(@host) do |ftp|
|
Net::FTP.open(@host) do |ftp|
|
||||||
ftp.login(@user, @password)
|
ftp.login(@user, @password)
|
||||||
@enable = true
|
@enable = true
|
||||||
|
@ -50,62 +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
|
||||||
|
|
||||||
tmp_file = tmpfile
|
|
||||||
Net::FTP.open(@host) do |ftp|
|
Net::FTP.open(@host) do |ftp|
|
||||||
ftp.login(@user, @password)
|
ftp.login(@user, @password)
|
||||||
ftp.gettextfile(@path, tmp_file)
|
ftp.gettextfile(@path, file_tmp)
|
||||||
end
|
end
|
||||||
|
|
||||||
mpw = MPW.new(tmp_file, gpg_key)
|
return true
|
||||||
if not mpw.decrypt(gpg_password)
|
|
||||||
@error_msg = mpw.error_msg
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
File.unlink(tmp_file)
|
|
||||||
return mpw.search
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
||||||
return nil
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the remote data
|
# Update the remote data
|
||||||
# @args: data -> the data to send on server
|
# @args: data -> the data to send on server
|
||||||
# @rtrn: false if there is a problem
|
# @rtrn: false if there is a problem
|
||||||
def update(data)
|
def update(file_gpg)
|
||||||
return true if not @enable
|
return true if not @enable
|
||||||
|
|
||||||
tmp_file = tmpfile
|
|
||||||
File.open(tmp_file, "w") do |file|
|
|
||||||
file << data
|
|
||||||
end
|
|
||||||
|
|
||||||
Net::FTP.open(@host) do |ftp|
|
Net::FTP.open(@host) do |ftp|
|
||||||
ftp.login(@user, @password)
|
ftp.login(@user, @password)
|
||||||
ftp.puttextfile(tmp_file, @path)
|
ftp.puttextfile(file_gpg, @path)
|
||||||
end
|
end
|
||||||
|
|
||||||
File.unlink(tmp_file)
|
|
||||||
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}"
|
||||||
return false
|
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
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
require 'tempfile'
|
|
||||||
require 'net/ssh'
|
require 'net/ssh'
|
||||||
require 'net/sftp'
|
require 'net/sftp'
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue