Class: Simbiotes::Server
- Inherits:
-
Object
- Object
- Simbiotes::Server
- Defined in:
- lib/simbiotes/server.rb
Class Method Summary collapse
- .ca_cert ⇒ Object
- .cert ⇒ Object
- .generate_cert(key, public_key) ⇒ Object
- .generate_key ⇒ Object
- .key ⇒ Object
- .lookup ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #do_at_exit ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #listen_local ⇒ Object
- #listen_remote ⇒ Object
- #send(msg) ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simbiotes/server.rb', line 11 def initialize @error_msg = nil server_details = Server.lookup rgs = SimbiotesSetting.find_by(key: "server") rgs_port = SimbiotesSetting.find_by(key: "port") if rgs == nil if server_details["status"] == "ok" rgs = server_details["ip"] c = SimbiotesSetting.new c.key = "server" c.value = rgs c.save rgs_port = server_details["port"] c = SimbiotesSetting.new c.key = "port" c.value = rgs_port c.save else @error_msg = server_details["status_msg"] end else if server_details["status"] == "ok" rgs.value = server_details["ip"] rgs.save rgs = rgs.value rgs_port.value = server_details["port"] rgs_port.save rgs_port = rgs_port.value else @error_msg = server_details["status_msg"] end end if Simbiotes.configuration.tls == true socket = TCPSocket.new(rgs, rgs_port) context = OpenSSL::SSL::SSLContext.new context.key = Server.key context.cert = Server.cert if Simbiotes.configuration.verify_peer == true ca_tempfile = Tempfile.new ca_tempfile.write Server.ca_cert.to_pem ca_tempfile.rewind context.ca_file = ca_tempfile.path context.verify_mode = OpenSSL::SSL::VERIFY_PEER end server = OpenSSL::SSL::SSLSocket.new socket, context server.sync_close = true server.connect if Simbiotes.configuration.verify_peer == true ca_tempfile.close(true) end else server = TCPSocket.open(rgs, rgs_port) end @server = server @localport = Simbiotes.configuration.local_port listen_remote listen_local end |
Class Method Details
.ca_cert ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/simbiotes/server.rb', line 217 def self.ca_cert ca_cert = SimbiotesSetting.find_by(key: "ca_cert") if ca_cert == nil ca_cert = Simbiotes::Portal.ca_certificate c = SimbiotesSetting.new c.key = "ca_cert" c.value = ca_cert c.save else ca_cert = ca_cert.value end ca_cert = OpenSSL::X509::Certificate.new ca_cert return ca_cert end |
.cert ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/simbiotes/server.rb', line 167 def self.cert cert = SimbiotesSetting.find_by(key: "cert") if cert == nil key = Server.key public_key = key.public_key cert = Server.generate_cert(key, public_key) c = SimbiotesSetting.new c.key = "cert" c.value = cert c.save else cert = cert.value end cert = OpenSSL::X509::Certificate.new cert return cert end |
.generate_cert(key, public_key) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/simbiotes/server.rb', line 207 def self.generate_cert(key, public_key) csr = OpenSSL::X509::Request.new csr.version = 0 csr.subject = OpenSSL::X509::Name.parse "CN=simbiotes.com/O=#{Simbiotes.configuration.public_key}/OU=#{Simbiotes.configuration.private_key}" csr.public_key = key.public_key csr.sign key, OpenSSL::Digest::SHA1.new cert = Simbiotes::Portal.generate_certificate(csr) return cert end |
.generate_key ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/simbiotes/server.rb', line 184 def self.generate_key key = OpenSSL::PKey::RSA.new 2048 pass_phrase = 'simbiotes' cipher = OpenSSL::Cipher.new 'AES-128-CBC' s = SimbiotesSetting.new s.key = "key_cipher" s.value = "OpenSSL::Cipher.new 'AES-128-CBC'" s.save s = SimbiotesSetting.new s.key = "key_pass_phrase" s.value = 'simbiotes' s.save s = SimbiotesSetting.new s.key = "public_key" s.value = key.public_key.to_pem s.save s = SimbiotesSetting.new s.key = "key" s.value = key.export(cipher, pass_phrase) s.save return s.value end |
.key ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/simbiotes/server.rb', line 155 def self.key key = SimbiotesSetting.find_by(key: "key") if key == nil key = Server.generate_key else key = key.value end pass_phrase = SimbiotesSetting.find_by(key: "key_pass_phrase").value key = OpenSSL::PKey::RSA.new key, pass_phrase return key end |
.lookup ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/simbiotes/server.rb', line 126 def self.lookup if Simbiotes.configuration.tls == true socket = TCPSocket.new(Simbiotes.configuration.server, Simbiotes.configuration.server_port) context = OpenSSL::SSL::SSLContext.new context.key = Server.key context.cert = Server.cert if Simbiotes.configuration.verify_peer == true ca_tempfile = Tempfile.new ca_tempfile.write Server.ca_cert.to_pem ca_tempfile.rewind context.ca_file = ca_tempfile.path context.verify_mode = OpenSSL::SSL::VERIFY_PEER end server = OpenSSL::SSL::SSLSocket.new socket, context server.sync_close = true server.connect else server = TCPSocket.open(Simbiotes.configuration.server, Simbiotes.configuration.server_port) end server.puts('{"action":"lookup"}') msg = server.gets hash = JSON.parse(msg) server.close if Simbiotes.configuration.verify_peer == true ca_tempfile.close(true) end return hash end |
Instance Method Details
#close ⇒ Object
75 76 77 |
# File 'lib/simbiotes/server.rb', line 75 def close @server.close end |
#do_at_exit ⇒ Object
232 233 |
# File 'lib/simbiotes/server.rb', line 232 def do_at_exit end |
#listen_local ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/simbiotes/server.rb', line 103 def listen_local begin server = TCPServer.open(@localport) if Simbiotes.configuration.handshake == true self.send('{"action":"connect"}') end loop do Thread.fork(server.accept) do |client| s = client.gets if @error_msg != nil string = '{"error":"' + error_msg + '"}' client.puts(string) end #puts s self.send(s) end end rescue => e puts e listen_local end end |
#listen_remote ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/simbiotes/server.rb', line 79 def listen_remote begin Thread.new do loop do msg = @server.gets puts msg msg_hash = Simbiotes::Parse.(msg) r = Simbiotes::Insert.new(msg_hash) puts "Message received": msg_hash if r.action == "set-request-ack" || r.action == "set-complete" || r.action == "get-ack" r.save unless Simbiotes.configuration.local_logging == false r.save_log end end end end rescue => e puts e listen_remote end end |
#send(msg) ⇒ Object
70 71 72 73 |
# File 'lib/simbiotes/server.rb', line 70 def send(msg) puts "Message sent: #{msg}" @server.puts( msg ) end |