class Saphir::BNCClient

Public Class Methods

new(server, password = "nopassword", port=7789, useSSL=true) click to toggle source

Set client properties

# File lib/Saphir/BNCClient.rb, line 7
def initialize(server, password = "nopassword", port=7789, useSSL=true)
  @port = port
  @server = server
  @password = password
  @useSSL = useSSL
  #Set the default certificate path
  @certFile = "#{File.dirname(__FILE__)}/../ssl.cert"
end

Public Instance Methods

close() click to toggle source

Close the server connection

# File lib/Saphir/BNCClient.rb, line 65
def close()
  @socket.puts("EXT\000")
  @socket.close
end
sendMessage(msg) click to toggle source

Send a message to the server

# File lib/Saphir/BNCClient.rb, line 55
def sendMessage(msg)
  @socket.puts("#{@password}$$$#{msg}\000")
 
  #Get the server response and forward it to the application using BCPClient
  response = @socket.gets()
  response = response.chomp[0..-2]
  response
end
setSSLCertificate(certFile) click to toggle source

Change the default certificate

# File lib/Saphir/BNCClient.rb, line 17
def setSSLCertificate(certFile)
  if File.exist?(certFile)
    @certFile = certFile
  else
    puts "ERROR: File does not exist!"
  end
end
start() click to toggle source

Start the client

# File lib/Saphir/BNCClient.rb, line 26
def start()
  #Create a basic tcp socket
  socket = TCPSocket.open(@server, @port)
  
  #Check if ssl is enabled
  if @useSSL 
    #Load certificate
    expectedCert = OpenSSL::X509::Certificate.new(File.open(@certFile))
    #Create a ssl tunnel for the tcp socket
    s_socket = OpenSSL::SSL::SSLSocket.new(socket)
    s_socket.sync_close = true
    #Connect to the server
    s_socket.connect
    
    #If the server and client certificate differ through an error and quit the script
    if s_socket.peer_cert.to_s != expectedCert.to_s
      stderrr.puts "Unexpected certificate"
      exit(1)
    end
    
    #Use the tunnel for communication
    @socket = s_socket
  else
    #Use the basic socket for communication
    @socket = socket
  end
end