class Apiotics::Server
Public Class Methods
ca_cert()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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
new()
click to toggle source
# 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
Public Instance Methods
close()
click to toggle source
# File lib/apiotics/server.rb, line 75 def close @server.close end
do_at_exit()
click to toggle source
# File lib/apiotics/server.rb, line 234 def do_at_exit end
listen_local()
click to toggle source
# 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()
click to toggle source
# 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.message(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)
click to toggle source
# File lib/apiotics/server.rb, line 70 def send(msg) puts "Message sent: #{msg}" @server.puts( msg ) end