class Saphir::BNCServer
Public Class Methods
new(password="nopassword", port=7789, useSSL=true, enableLog=true)
click to toggle source
Set server properties
# File lib/Saphir/BNCServer.rb, line 7 def initialize(password="nopassword", port=7789, useSSL=true, enableLog=true) @port = port @password = password @useSSL = useSSL @enableLog = enableLog #Set the default certificate path @certFile = "#{File.dirname(__FILE__)}/../ssl.cert" @keyFile = "#{File.dirname(__FILE__)}/../ssl.key" end
Public Instance Methods
setSSLCertificate(certFile)
click to toggle source
Change the default certificate
# File lib/Saphir/BNCServer.rb, line 18 def setSSLCertificate(certFile) if File.exist?(certFile) @certFile = certFile else puts "ERROR: File does not exist!" end end
setSSLKey(keyFile)
click to toggle source
Change the default key
# File lib/Saphir/BNCServer.rb, line 27 def setSSLKey(keyFile) if File.exist?(keyFile) @keyFile = keyFile else puts "ERROR: File does not exist!" end end
start() { |request| ... }
click to toggle source
Start the server
# File lib/Saphir/BNCServer.rb, line 36 def start #Create a basic tcp server s_server = TCPServer.open(@port) #Check if ssl was enabled if @useSSL #If enabled create an ssl tunnel for the tcp server sslContext = OpenSSL::SSL::SSLContext.new sslContext.cert = OpenSSL::X509::Certificate.new(File.open(@certFile)); sslContext.key = OpenSSL::PKey::RSA.new(File.open(@keyFile)); ssl_server = OpenSSL::SSL::SSLServer.new(s_server, sslContext) ssl_server.start_immediately = true #Use the tunnel for communication server = ssl_server else #If disabled use the basic tcp server server = s_server end loop do #Try to start a new thread for each incoming request begin Thread.start(server.accept) do |client| #Get client info sock_domain, remote_port, remote_hostname, remote_ip = client.peeraddr puts "Accepted request from #{remote_hostname} (#{remote_ip})" if @enableLog #Start processing the request while request = client.gets #Read the password from the request password = request[0..request.index('$$$')-1] #Set request to be the content of the actual request request = request[request.index('$$$')+3..-2] #Verify the password if @password == password #Try to process the command from the request begin #Check if the command will be processed by BCPServer case request[0..2] when "HLO" #Send 'Hello' to the client client.puts "Hello\000" when "EXT" #Close the connection puts "#{remote_ip}$ Closing connection..." if @enableLog client.close else #If no BCPServer command is found forward the request to the application that implemented BCPServer begin puts "#{remote_ip}$ Request: #{request}" if @enableLog response = yield request[0..-1] client.puts "#{response}\000" rescue client.puts "Request received\000" end end rescue #Through an error if the request cannot be processed puts "Invalid request from #{remote_ip}" if @enableLog client.puts "ERROR: INVALID REQUEST\000" end else #If the password is wrong return an error puts "Invalid password from #{remote_ip}" if @enableLog client.puts "ERROR: INVALID PASSWORD\000" end end end rescue #If the request is invalid through an error puts "ERROR: Invalid connection request" if @enableLog end end end