mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
delete sync close function
This commit is contained in:
parent
b1c281f318
commit
71fb489958
7 changed files with 53 additions and 68 deletions
|
@ -30,7 +30,7 @@ module MPW
|
||||||
# @args: file_config -> the specify config file
|
# @args: file_config -> the specify config file
|
||||||
def initialize(file_config=nil)
|
def initialize(file_config=nil)
|
||||||
@error_msg = nil
|
@error_msg = nil
|
||||||
@file_config = "#{Dir.home()}/.mpw.cfg"
|
@file_config = "#{Dir.home}/.mpw.cfg"
|
||||||
|
|
||||||
if !file_config.nil? && !file_config.empty?
|
if !file_config.nil? && !file_config.empty?
|
||||||
@file_config = file_config
|
@file_config = file_config
|
||||||
|
@ -57,7 +57,7 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
if file_gpg.empty?
|
if file_gpg.empty?
|
||||||
file_gpg = "#{Dir.home()}/.mpw.gpg"
|
file_gpg = "#{Dir.home}/.mpw.gpg"
|
||||||
end
|
end
|
||||||
|
|
||||||
timeout_pwd = timeout_pwd.empty? ? 60 : timeout_pwd.to_i
|
timeout_pwd = timeout_pwd.empty? ? 60 : timeout_pwd.to_i
|
||||||
|
@ -86,7 +86,7 @@ module MPW
|
||||||
|
|
||||||
# Check the config file
|
# Check the config file
|
||||||
# @rtrn: true if the config file is correct
|
# @rtrn: true if the config file is correct
|
||||||
def checkconfig()
|
def checkconfig
|
||||||
config = YAML::load_file(@file_config)
|
config = YAML::load_file(@file_config)
|
||||||
@key = config['config']['key']
|
@key = config['config']['key']
|
||||||
@lang = config['config']['lang']
|
@lang = config['config']['lang']
|
||||||
|
|
|
@ -35,8 +35,9 @@ module MPW
|
||||||
@password = password
|
@password = password
|
||||||
@suffix = path
|
@suffix = path
|
||||||
|
|
||||||
@socket = TCPSocket.new(host, port.to_i)
|
TCPSocket.new(host, port.to_i) do
|
||||||
@enable = true
|
@enable = true
|
||||||
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
||||||
@enable = false
|
@enable = false
|
||||||
|
@ -51,17 +52,19 @@ module MPW
|
||||||
if !@enable
|
if !@enable
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
send_msg = {:action => 'get',
|
|
||||||
:gpg_key => @gpg_key,
|
|
||||||
:password => @password,
|
|
||||||
:suffix => @suffix}
|
|
||||||
|
|
||||||
@socket.puts send_msg.to_json
|
TCPSocket.new(host, port.to_i) do |socket|
|
||||||
msg = JSON.parse(@socket.gets)
|
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 !defined?(msg['error'])
|
if !defined?(msg['error'])
|
||||||
@error_msg = I18n.t('error.sync.communication')
|
error_msg = I18n.t('error.sync.communication')
|
||||||
return nil
|
return nil
|
||||||
elsif msg['error'].nil?
|
elsif msg['error'].nil?
|
||||||
tmp_file = tmpfile
|
tmp_file = tmpfile
|
||||||
|
@ -81,6 +84,7 @@ module MPW
|
||||||
@error_msg = I18n.t(msg['error'])
|
@error_msg = I18n.t(msg['error'])
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the remote data
|
# Update the remote data
|
||||||
|
@ -91,14 +95,16 @@ module MPW
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
send_msg = {:action => 'update',
|
TCPSocket.new(host, port.to_i) do |socket|
|
||||||
:gpg_key => @gpg_key,
|
send_msg = {:action => 'update',
|
||||||
:password => @password,
|
:gpg_key => @gpg_key,
|
||||||
:suffix => @suffix,
|
:password => @password,
|
||||||
:data => data}
|
:suffix => @suffix,
|
||||||
|
:data => data}
|
||||||
@socket.puts send_msg.to_json
|
|
||||||
msg = JSON.parse(@socket.gets)
|
socket.puts send_msg.to_json
|
||||||
|
msg = JSON.parse(socket.gets)
|
||||||
|
end
|
||||||
|
|
||||||
if !defined?(msg['error'])
|
if !defined?(msg['error'])
|
||||||
@error_msg = I18n.t('error.sync.communication')
|
@error_msg = I18n.t('error.sync.communication')
|
||||||
|
@ -111,16 +117,6 @@ module MPW
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Close the connection
|
|
||||||
def close
|
|
||||||
if !@enable
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
send_msg = {:action => 'close'}
|
|
||||||
@socket.puts send_msg.to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generate a random string
|
# Generate a random string
|
||||||
# @rtrn: a random string
|
# @rtrn: a random string
|
||||||
def tmpfile
|
def tmpfile
|
||||||
|
|
|
@ -97,10 +97,6 @@ module MPW
|
||||||
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Close the connection
|
|
||||||
def close
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generate a random string
|
# Generate a random string
|
||||||
# @rtrn: a random string
|
# @rtrn: a random string
|
||||||
|
|
|
@ -23,11 +23,6 @@ class Cli
|
||||||
@config = config
|
@config = config
|
||||||
end
|
end
|
||||||
|
|
||||||
# Close sync
|
|
||||||
def sync_close
|
|
||||||
@sync.close
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -77,7 +72,7 @@ class Cli
|
||||||
puts '--------------------'
|
puts '--------------------'
|
||||||
language = ask(I18n.t('form.setup.lang', :lang => lang)).to_s
|
language = ask(I18n.t('form.setup.lang', :lang => lang)).to_s
|
||||||
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
||||||
file_gpg = ask(I18n.t('form.setup.gpg_file', :home => Dir.home())).to_s
|
file_gpg = ask(I18n.t('form.setup.gpg_file', :home => Dir.home)).to_s
|
||||||
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
|
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
|
||||||
sync_type = ask(I18n.t('form.setup.sync_type')).to_s
|
sync_type = ask(I18n.t('form.setup.sync_type')).to_s
|
||||||
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
|
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
|
||||||
|
@ -101,7 +96,7 @@ class Cli
|
||||||
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
if not @config.checkconfig()
|
if not @config.checkconfig
|
||||||
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
||||||
exit 2
|
exit 2
|
||||||
end
|
end
|
||||||
|
@ -171,7 +166,7 @@ class Cli
|
||||||
end
|
end
|
||||||
|
|
||||||
# Form to add a new item
|
# Form to add a new item
|
||||||
def add()
|
def add
|
||||||
row = []
|
row = []
|
||||||
puts I18n.t('form.add.title')
|
puts I18n.t('form.add.title')
|
||||||
puts '--------------------'
|
puts '--------------------'
|
||||||
|
@ -185,8 +180,8 @@ class Cli
|
||||||
comment = ask(I18n.t('form.add.comment')).to_s
|
comment = ask(I18n.t('form.add.comment')).to_s
|
||||||
|
|
||||||
if @mpw.update(name, group, server, protocol, login, passwd, port, comment)
|
if @mpw.update(name, group, server, protocol, login, passwd, port, comment)
|
||||||
if @mpw.encrypt()
|
if @mpw.encrypt
|
||||||
sync()
|
sync
|
||||||
puts I18n.t('form.add.valid')
|
puts I18n.t('form.add.valid')
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
||||||
|
@ -214,8 +209,8 @@ class Cli
|
||||||
comment = ask(I18n.t('form.update.comment' , :comment => row[MPW::MPW::COMMENT])).to_s
|
comment = ask(I18n.t('form.update.comment' , :comment => row[MPW::MPW::COMMENT])).to_s
|
||||||
|
|
||||||
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
||||||
if @mpw.encrypt()
|
if @mpw.encrypt
|
||||||
sync()
|
sync
|
||||||
puts I18n.t('form.update.valid')
|
puts I18n.t('form.update.valid')
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
||||||
|
@ -249,8 +244,8 @@ class Cli
|
||||||
|
|
||||||
if force
|
if force
|
||||||
if @mpw.remove(id)
|
if @mpw.remove(id)
|
||||||
if @mpw.encrypt()
|
if @mpw.encrypt
|
||||||
sync()
|
sync
|
||||||
puts I18n.t('form.delete.valid', :id => id)
|
puts I18n.t('form.delete.valid', :id => id)
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
||||||
|
@ -294,8 +289,8 @@ class Cli
|
||||||
end
|
end
|
||||||
|
|
||||||
if force
|
if force
|
||||||
if @mpw.import(file) && @mpw.encrypt()
|
if @mpw.import(file) && @mpw.encrypt
|
||||||
sync()
|
sync
|
||||||
puts I18n.t('form.import.valid')
|
puts I18n.t('form.import.valid')
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
|
||||||
|
@ -304,7 +299,7 @@ class Cli
|
||||||
end
|
end
|
||||||
|
|
||||||
# Interactive mode
|
# Interactive mode
|
||||||
def interactive()
|
def interactive
|
||||||
group = nil
|
group = nil
|
||||||
last_access = Time.now.to_i
|
last_access = Time.now.to_i
|
||||||
|
|
||||||
|
@ -331,7 +326,7 @@ class Cli
|
||||||
display(command[1], group, command[2])
|
display(command[1], group, command[2])
|
||||||
end
|
end
|
||||||
when 'add', 'a'
|
when 'add', 'a'
|
||||||
add()
|
add
|
||||||
when 'update', 'u'
|
when 'update', 'u'
|
||||||
if !command[1].nil? && !command[1].empty?
|
if !command[1].nil? && !command[1].empty?
|
||||||
update(command[1])
|
update(command[1])
|
||||||
|
|
|
@ -11,12 +11,12 @@ class Server
|
||||||
attr_accessor :error_msg
|
attr_accessor :error_msg
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def initialize()
|
def initialize
|
||||||
YAML::ENGINE.yamler='syck'
|
YAML::ENGINE.yamler='syck'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
def start()
|
def start
|
||||||
server = TCPServer.open(@host, @port)
|
server = TCPServer.open(@host, @port)
|
||||||
@log.info("The server is started on #{@host}:#{@port}")
|
@log.info("The server is started on #{@host}:#{@port}")
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ class Server
|
||||||
hash = gpg_data['gpg']['hash']
|
hash = gpg_data['gpg']['hash']
|
||||||
|
|
||||||
else
|
else
|
||||||
salt = salt()
|
salt = salt
|
||||||
hash = Digest::SHA256.hexdigest(salt + msg['password'])
|
hash = Digest::SHA256.hexdigest(salt + msg['password'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
12
mpw
12
mpw
|
@ -110,7 +110,7 @@ end.parse!
|
||||||
# --------------------------------------------------------- #
|
# --------------------------------------------------------- #
|
||||||
|
|
||||||
config = MPW::Config.new(options[:config])
|
config = MPW::Config.new(options[:config])
|
||||||
check_error = config.checkconfig()
|
check_error = config.checkconfig
|
||||||
|
|
||||||
cli = Cli.new(lang, config)
|
cli = Cli.new(lang, config)
|
||||||
|
|
||||||
|
@ -119,8 +119,8 @@ if !check_error || !options[:setup].nil?
|
||||||
cli.setup(lang)
|
cli.setup(lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.decrypt()
|
cli.decrypt
|
||||||
cli.sync()
|
cli.sync
|
||||||
|
|
||||||
# Display the item's informations
|
# Display the item's informations
|
||||||
if not options[:display].nil?
|
if not options[:display].nil?
|
||||||
|
@ -136,7 +136,7 @@ elsif not options[:update].nil?
|
||||||
|
|
||||||
# Add a new item
|
# Add a new item
|
||||||
elsif not options[:add].nil?
|
elsif not options[:add].nil?
|
||||||
cli.add()
|
cli.add
|
||||||
|
|
||||||
# Export
|
# Export
|
||||||
elsif not options[:export].nil?
|
elsif not options[:export].nil?
|
||||||
|
@ -149,15 +149,13 @@ elsif not options[:import].nil?
|
||||||
# Interactive mode
|
# Interactive mode
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
cli.interactive()
|
cli.interactive
|
||||||
rescue SystemExit, Interrupt
|
rescue SystemExit, Interrupt
|
||||||
cli.sync_close()
|
|
||||||
cli = nil
|
cli = nil
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.sync_close()
|
|
||||||
cli = nil
|
cli = nil
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
8
mpw-ssh
8
mpw-ssh
|
@ -63,7 +63,7 @@ end.parse!
|
||||||
# --------------------------------------------------------- #
|
# --------------------------------------------------------- #
|
||||||
|
|
||||||
config = MPW::Config.new(options[:config])
|
config = MPW::Config.new(options[:config])
|
||||||
check_error = config.checkconfig()
|
check_error = config.checkconfig
|
||||||
|
|
||||||
cli = CliSSH.new(lang, config)
|
cli = CliSSH.new(lang, config)
|
||||||
cli.login = options[:login]
|
cli.login = options[:login]
|
||||||
|
@ -80,11 +80,11 @@ elsif ARGV.length < 1
|
||||||
puts "#{I18n.t('ssh.option.usage')}: mpw-ssh SEARCH [options]"
|
puts "#{I18n.t('ssh.option.usage')}: mpw-ssh SEARCH [options]"
|
||||||
exit 2
|
exit 2
|
||||||
else
|
else
|
||||||
cli.decrypt()
|
cli.decrypt
|
||||||
cli.sync()
|
cli.sync
|
||||||
cli.ssh(search)
|
cli.ssh(search)
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.sync_close()
|
cli = nil
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue