Class: Apiotics::Server
- Inherits:
-
Object
- Object
- Apiotics::Server
- Defined in:
- lib/apiotics/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/apiotics/server.rb', line 11 def initialize @error_msg = nil server_details = Server.lookup rgs = ApioticsSetting.find_by(key: "server") rgs_port = ApioticsSetting.find_by(key: "port") if rgs == nil if server_details["status"] == "ok" rgs = server_details["ip"] c = ApioticsSetting.new c.key = "server" c.value = rgs c.save rgs_port = server_details["port"] c = ApioticsSetting.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 Apiotics.configuration.tls == true socket = TCPSocket.new(rgs, rgs_port) context = OpenSSL::SSL::SSLContext.new context.key = Server.key context.cert = Server.cert if Apiotics.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 Apiotics.configuration.verify_peer == true ca_tempfile.close(true) end else server = TCPSocket.open(rgs, rgs_port) end @server = server @localport = Apiotics.configuration.local_port listen_remote listen_local end |
Class Method Details
.ca_cert ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/apiotics/server.rb', line 219 def self.ca_cert ca_cert = ApioticsSetting.find_by(key: "ca_cert") if ca_cert == nil ca_cert = Apiotics::Portal.ca_certificate c = ApioticsSetting.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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/apiotics/server.rb', line 169 def self.cert cert = ApioticsSetting.find_by(key: "cert") if cert == nil key = Server.key public_key = key.public_key cert = Server.generate_cert(key, public_key) c = ApioticsSetting.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
209 210 211 212 213 214 215 216 217 |
# File 'lib/apiotics/server.rb', line 209 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=#{Apiotics.configuration.public_key}/OU=#{Apiotics.configuration.private_key}" csr.public_key = key.public_key csr.sign key, OpenSSL::Digest::SHA1.new cert = Apiotics::Portal.generate_certificate(csr) return cert end |
.generate_key ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/apiotics/server.rb', line 186 def self.generate_key key = OpenSSL::PKey::RSA.new 2048 pass_phrase = 'simbiotes' cipher = OpenSSL::Cipher.new 'AES-128-CBC' s = ApioticsSetting.new s.key = "key_cipher" s.value = "OpenSSL::Cipher.new 'AES-128-CBC'" s.save s = ApioticsSetting.new s.key = "key_pass_phrase" s.value = 'simbiotes' s.save s = ApioticsSetting.new s.key = "public_key" s.value = key.public_key.to_pem s.save s = ApioticsSetting.new s.key = "key" s.value = key.export(cipher, pass_phrase) s.save return s.value end |
.key ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/apiotics/server.rb', line 157 def self.key key = ApioticsSetting.find_by(key: "key") if key == nil key = Server.generate_key else key = key.value end pass_phrase = ApioticsSetting.find_by(key: "key_pass_phrase").value key = OpenSSL::PKey::RSA.new key, pass_phrase return key end |
.lookup ⇒ Object
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 154 155 |
# File 'lib/apiotics/server.rb', line 128 def self.lookup if Apiotics.configuration.tls == true socket = TCPSocket.new(Apiotics.configuration.server, Apiotics.configuration.server_port) context = OpenSSL::SSL::SSLContext.new context.key = Server.key context.cert = Server.cert if Apiotics.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(Apiotics.configuration.server, Apiotics.configuration.server_port) end server.puts('{"action":"lookup"}') msg = server.gets hash = JSON.parse(msg) server.close if Apiotics.configuration.verify_peer == true ca_tempfile.close(true) end return hash end |
Instance Method Details
#close ⇒ Object
75 76 77 |
# File 'lib/apiotics/server.rb', line 75 def close @server.close end |
#do_at_exit ⇒ Object
234 235 |
# File 'lib/apiotics/server.rb', line 234 def do_at_exit end |
#listen_local ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/apiotics/server.rb', line 105 def listen_local begin server = TCPServer.open(@localport) if Apiotics.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 102 103 |
# File 'lib/apiotics/server.rb', line 79 def listen_remote begin Thread.new do loop do msg = @server.gets puts msg msg_hash = Apiotics::Parse.(msg) r = Apiotics::Insert.new(msg_hash) puts "Message received": msg_hash if r.valid == true if r.action == "set-request-ack" || r.action == "set-complete" || r.action == "get-ack" r.save unless Apiotics.configuration.local_logging == false r.save_log end end end end end rescue => e puts e listen_remote end end |
#send(msg) ⇒ Object
70 71 72 73 |
# File 'lib/apiotics/server.rb', line 70 def send(msg) puts "Message sent: #{msg}" @server.puts( msg ) end |