1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-20 01:50:04 +00:00

clean code

This commit is contained in:
nishiki 2014-01-15 22:49:01 +01:00
parent d5c2af0867
commit 0598ec134f

View file

@ -22,29 +22,29 @@ class Server
loop do loop do
Thread.start(server.accept) do |client| Thread.start(server.accept) do |client|
while true do while true do
msg = self.getClientMessage(client) msg = getClientMessage(client)
if !msg if !msg
next next
end end
if msg['gpg_key'].nil? || msg['gpg_key'].empty? || msg['password'].nil? || msg['password'].empty? if msg['gpg_key'].nil? || msg['gpg_key'].empty? || msg['password'].nil? || msg['password'].empty?
self.closeConnection(client) closeConnection(client)
next next
end end
case msg['action'] case msg['action']
when 'get' when 'get'
client.puts self.getFile(msg) client.puts getFile(msg)
when 'update' when 'update'
client.puts self.updateFile(msg) client.puts updateFile(msg)
when 'delete' when 'delete'
client.puts self.deleteFile(msg) client.puts deleteFile(msg)
when 'close' when 'close'
self.closeConnection(client) closeConnection(client)
else else
client.puts 'Unknown command' client.puts 'Unknown command'
self.closeConnection(client) closeConnection(client)
end end
end end
end end
@ -68,12 +68,10 @@ class Server
salt = gpg_data['gpg']['salt'] salt = gpg_data['gpg']['salt']
hash = gpg_data['gpg']['hash'] hash = gpg_data['gpg']['hash']
data = gpg_data['gpg']['data'] data = gpg_data['gpg']['data']
last_update = gpg_data['gpg']['last_update']
if self.isAuthorized?(msg['password'], salt, hash) if isAuthorized?(msg['password'], salt, hash)
send_msg = {:action => 'get', send_msg = {:action => 'get',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:last_update => last_update,
:msg => 'done', :msg => 'done',
:data => data} :data => data}
else else
@ -85,7 +83,6 @@ class Server
else else
send_msg = {:action => 'get', send_msg = {:action => 'get',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:last_update => 0,
:data => '', :data => '',
:msg => 'fail', :msg => 'fail',
:error => 'file_not_exist'} :error => 'file_not_exist'}
@ -126,12 +123,10 @@ class Server
hash = Digest::SHA256.hexdigest(salt + msg['password']) hash = Digest::SHA256.hexdigest(salt + msg['password'])
end end
if self.isAuthorized?(msg['password'], salt, hash) if isAuthorized?(msg['password'], salt, hash)
begin begin
last_update = Time.now.to_i
config = {'gpg' => {'salt' => salt, config = {'gpg' => {'salt' => salt,
'hash' => hash, 'hash' => hash,
'last_update' => last_update,
'data' => data}} 'data' => data}}
File.open(file_gpg, 'w+') do |file| File.open(file_gpg, 'w+') do |file|
@ -140,7 +135,6 @@ class Server
send_msg = {:action => 'update', send_msg = {:action => 'update',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:last_update => last_update,
:msg => 'done'} :msg => 'done'}
rescue Exception => e rescue Exception => e
send_msg = {:action => 'update', send_msg = {:action => 'update',
@ -183,7 +177,7 @@ class Server
salt = gpg_data['gpg']['salt'] salt = gpg_data['gpg']['salt']
hash = gpg_data['gpg']['hash'] hash = gpg_data['gpg']['hash']
if self.isAuthorized?(msg['password'], salt, hash) if isAuthorized?(msg['password'], salt, hash)
begin begin
File.unlink(file_gpg) File.unlink(file_gpg)
@ -227,8 +221,7 @@ class Server
msg = client.gets msg = client.gets
return JSON.parse(msg) return JSON.parse(msg)
rescue rescue
client.puts "Communication it's bad" closeConnection(client)
self.closeConnection(client)
return false return false
end end
end end