Class: EncryptDecrypt::EncryptedServer
- Inherits:
-
Object
- Object
- EncryptDecrypt::EncryptedServer
- Defined in:
- lib/accu-encrypt.rb
Overview
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
Instance Method Summary collapse
-
#initialize(server) ⇒ EncryptedServer
constructor
Initializes EncryptedServer object.
-
#interactive ⇒ Object
Opens an interactive session.
-
#listen {|msg| ... } ⇒ Object
Listens for a generic network communication and yields to a passed block (if any).
-
#listen_code ⇒ Object
Listens for a network code.
-
#listen_encrypted ⇒ Object
Listens for encrypted messages.
-
#listen_lines ⇒ Object
Listens for multiple lines, appends them together and stops at END.
-
#send(text) ⇒ Object
Sends a plaintext message.
-
#send_code(code) ⇒ Object
Sends a code.
-
#send_encrypted(lines) ⇒ Object
Send over an encrypted multiline message.
-
#send_lines(lines) ⇒ Object
Sends multiple lines and appends END to end the send.
Constructor Details
#initialize(server) ⇒ EncryptedServer
Initializes EncryptedServer object.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 |
Instance Method Details
#interactive ⇒ Object
Opens an interactive session.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# 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| ... } ⇒ Object
Listens for a generic network communication and yields to a passed block (if any).
104 105 106 107 |
# File 'lib/accu-encrypt.rb', line 104 def listen() msg = @server.gets.chomp yield msg end |
#listen_code ⇒ Object
Listens for a network code.
121 122 123 124 125 126 127 128 129 130 131 |
# 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 ⇒ Object
Listens for encrypted messages.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 ⇒ Object
Listens for multiple lines, appends them together and stops at END.
111 112 113 114 115 116 117 118 |
# 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) ⇒ Object
Sends a plaintext message.
134 135 136 |
# File 'lib/accu-encrypt.rb', line 134 def send(text) @server.puts text end |
#send_code(code) ⇒ Object
Sends a code.
157 158 159 |
# File 'lib/accu-encrypt.rb', line 157 def send_code(code) send("C:#{code}") end |
#send_encrypted(lines) ⇒ Object
Send over an encrypted multiline message.
149 150 151 152 153 154 |
# 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) ⇒ Object
Sends multiple lines and appends END to end the send.
140 141 142 143 144 145 146 |
# 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 |