class EncryptDecrypt::EncryptedServer
A class which allows for the creation of an encrypted session over a server, with the client acting as the server.
Codes: 0 - Connection refused, 1 - Connection accepted, 2 - Ready, 3 - Sending encrypted message, 4 - End communications, 5 - Verification message inbound
Public Class Methods
new( server )
click to toggle source
Initializes EncryptedServer
object.
# File lib/accu-encrypt.rb, line 53 def initialize( server ) @server = server print "Connection attempt from |" + @server.peeraddr[3] + "|. Would you like to connect? " response = gets.chomp.downcase if response == "y" then puts "Connection accepted." send_code(1) @password = EncryptDecrypt.get_password "Please enter password for communications: " @listen_thread, @interactive_thread = nil, nil @mode = EncryptDecrypt.get_mode if listen_code() == 2 then send_code(2) puts "Ready code received.\nSuccessfully connected!" listen_encrypted() interactive() @listen_thread.join @interactive_thread.join puts "Ending connection." else puts "Connection refused." end else send_code 0 puts "Connection refused." end server.close end
Public Instance Methods
interactive()
click to toggle source
Opens an interactive session.
# File lib/accu-encrypt.rb, line 162 def interactive() @interactive_thread = Thread.new { loop { print "What would you like to do? |S Q| " response = gets.chomp.downcase while response != "q" and response != "s" do puts "Incorrect input: |" + response + "|" print "What would you like to do? |S Q| " response = gets.chomp.downcase end if response == "q" then puts "Sending close message to client." send_code(4) @listen_thread.exit @interactive_thread.exit else print "Type message and end with END:\n\n" lines = "" while line = gets and line.chomp != "END" do #puts "|" + line + "|" lines << line end lines = lines.chomp puts "Message read.\nEncrypting now." send_encrypted(lines) puts "Message sent." end } } end
listen() { |msg| ... }
click to toggle source
Listens for a generic network communication and yields to a passed block (if any).
# File lib/accu-encrypt.rb, line 104 def listen() msg = @server.gets.chomp yield msg end
listen_code()
click to toggle source
Listens for a network code.
# File lib/accu-encrypt.rb, line 121 def listen_code() listen { |msg| if msg.slice(0..1) == "C:" then #puts "Code present." return msg.slice(2..msg.length).to_i else #puts "Code not present." return msg.slice(0..1) end } end
listen_encrypted()
click to toggle source
Listens for encrypted messages.
# File lib/accu-encrypt.rb, line 82 def listen_encrypted() @listen_thread = Thread.new { loop { code = listen_code() if code == 3 then lines = "" while line = @server.gets and line != "END\n" do lines << line end secret,text = EncryptDecrypt.decrypt(@password,lines,@mode) puts "Message received:\n\n" + text.chomp + "\n\nMessage end." elsif code == 4 then puts "Client closed connection." @interactive_thread.exit @listen_thread.exit end } } end
listen_lines()
click to toggle source
Listens for multiple lines, appends them together and stops at END.
# File lib/accu-encrypt.rb, line 111 def listen_lines() lines = [] while line = @server.gets.chomp and line != "END" do #puts "|" + line + "|" lines << line end lines end
send(text)
click to toggle source
Sends a plaintext message.
# File lib/accu-encrypt.rb, line 134 def send(text) @server.puts text end
send_code(code)
click to toggle source
Sends a code.
# File lib/accu-encrypt.rb, line 157 def send_code(code) send("C:#{code}") end
send_encrypted(lines)
click to toggle source
Send over an encrypted multiline message.
# File lib/accu-encrypt.rb, line 149 def send_encrypted(lines) secret,text = EncryptDecrypt.encrypt(@password,lines,@mode) send_code(3) send(text) send("END") end
send_lines(lines)
click to toggle source
Sends multiple lines and appends END to end the send.
# File lib/accu-encrypt.rb, line 140 def send_lines(lines) array = lines.split("\n") array.each do |line| send(line) end send("END") end