1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-28 16:13:04 +00:00
mpw/lib/Sync/MPWSync.rb

148 lines
3.3 KiB
Ruby
Raw Normal View History

2014-01-29 19:49:39 +00:00
#!/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'
2014-08-31 08:59:33 +00:00
require 'timeout'
2014-01-29 19:49:39 +00:00
2014-01-30 22:08:38 +00:00
class MPWSync
2014-01-29 19:49:39 +00:00
attr_accessor :error_msg
attr_accessor :enable
# Constructor
2014-01-29 22:10:35 +00:00
def initialize
2014-01-29 19:49:39 +00:00
@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
2014-01-30 22:08:38 +00:00
def connect(host, user, password, path, port=nil)
2014-02-02 16:34:05 +00:00
@host = host
2014-12-06 15:25:03 +00:00
@port = !port.instance_of?(Integer) ? 2201 : port
2014-01-30 22:08:38 +00:00
@gpg_key = user
2014-01-29 19:49:39 +00:00
@password = password
2014-01-30 22:08:38 +00:00
@suffix = path
2014-01-29 19:49:39 +00:00
2014-08-31 08:59:33 +00:00
Timeout.timeout(10) do
begin
TCPSocket.open(@host, @port) do
@enable = true
end
rescue Errno::ENETUNREACH
retry
end
2014-02-02 09:16:10 +00:00
end
2014-08-31 08:59:33 +00:00
rescue Timeout::Error
puts 'timeout'
@error_msg = "#{I18n.t('error.timeout')}\n#{e}"
@enable = false
2014-01-29 21:49:13 +00:00
rescue Exception => e
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
@enable = false
else
2014-01-29 19:49:39 +00:00
return @enable
end
# Get data on server
# @args: gpg_password -> the gpg password
# @rtrn: nil if nothing data or error
def get(gpg_password)
2014-11-16 18:50:09 +00:00
return nil if not @enable
2014-12-06 20:37:18 +00:00
2014-02-02 16:34:05 +00:00
msg = nil
TCPSocket.open(@host, @port) do |socket|
2014-08-31 11:07:06 +00:00
send_msg = {action: 'get',
gpg_key: @gpg_key,
password: @password,
suffix: @suffix
}
2014-02-02 09:16:10 +00:00
socket.puts send_msg.to_json
msg = JSON.parse(socket.gets)
end
2014-12-06 20:37:18 +00:00
2014-11-16 18:50:09 +00:00
if not defined?(msg['error'])
@error_msg = I18n.t('error.sync.communication')
2014-01-29 19:49:39 +00:00
return nil
2014-11-16 18:50:09 +00:00
elsif not msg['error'].nil?
@error_msg = I18n.t(msg['error'])
return nil
2014-11-16 18:50:09 +00:00
elsif msg['data'].nil? or msg['data'].empty?
2014-12-06 20:37:18 +00:00
return {}
else
2014-01-30 22:08:38 +00:00
tmp_file = tmpfile
2014-01-29 19:49:39 +00:00
File.open(tmp_file, 'w') do |file|
file << msg['data']
end
2015-01-16 23:05:40 +00:00
mpw = MPW.new(tmp_file, @gpg_key)
2014-11-16 18:50:09 +00:00
if not mpw.decrypt(gpg_password)
2014-01-30 22:08:38 +00:00
@error_msg = mpw.error_msg
2014-01-29 19:49:39 +00:00
return nil
end
2014-01-30 22:08:38 +00:00
2014-01-29 19:49:39 +00:00
File.unlink(tmp_file)
2014-01-30 22:08:38 +00:00
return mpw.search
2014-01-29 19:49:39 +00:00
end
end
# Update the remote data
# @args: data -> the data to send on server
# @rtrn: false if there is a problem
def update(data)
2014-11-16 18:50:09 +00:00
return true if not @enable
2014-01-29 19:49:39 +00:00
2014-02-02 16:34:05 +00:00
msg = nil
TCPSocket.open(@host, @port) do |socket|
2014-08-31 11:07:06 +00:00
send_msg = {action: 'update',
gpg_key: @gpg_key,
password: @password,
suffix: @suffix,
data: data
}
2014-02-02 09:16:10 +00:00
socket.puts send_msg.to_json
msg = JSON.parse(socket.gets)
end
2014-01-29 19:49:39 +00:00
2014-11-16 18:50:09 +00:00
if not defined?(msg['error'])
2014-01-29 19:49:39 +00:00
@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
2014-01-30 22:08:38 +00:00
# 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
2014-01-29 19:49:39 +00:00
end
end
end