class Simbiotes::Server

Public Class Methods

ca_cert() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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
new() click to toggle source
# 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

Public Instance Methods

close() click to toggle source
# File lib/simbiotes/server.rb, line 75
def close
  @server.close
end
do_at_exit() click to toggle source
# File lib/simbiotes/server.rb, line 232
def do_at_exit
end
listen_local() click to toggle source
# 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() click to toggle source
# 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.message(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) click to toggle source
# File lib/simbiotes/server.rb, line 70
def send(msg)
  puts "Message sent: #{msg}"
  @server.puts( msg )
end