Skip to content
This repository has been archived by the owner on Sep 29, 2022. It is now read-only.

Commit

Permalink
Merge 00eca15 into da7d12d
Browse files Browse the repository at this point in the history
  • Loading branch information
fakenine committed Mar 8, 2018
2 parents da7d12d + 00eca15 commit 722270c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/openvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
module Openvas
extend self

class OpenvasError < StandardError; end
class ConfigError < OpenvasError; end
class AuthError < OpenvasError; end
class QueryError < OpenvasError; end

def configure
block_given? ? yield(Config) : Config
%w[url username password].each do |key|
next unless Openvas::Config.instance_variable_get("@#{key}").nil?
raise Openvas::Config::RequiredOptionMissing,
raise ConfigError,
"Configuration parameter missing: '#{key}'. " \
'Please add it to the Openvas.configure block'
end
Expand Down
10 changes: 7 additions & 3 deletions lib/openvas/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class InvalidLogin < StandardError; end

# Do Login
def self.login
raise InvalidLogin, 'Please configure the username' unless Openvas::Config.username
raise InvalidLogin, 'Please configure the password' unless Openvas::Config.password
raise ConfigError, 'Username not configured' unless Openvas::Config.username
raise ConfigError, 'Password not configured' unless Openvas::Config.password

content = Nokogiri::XML::Builder.new do |xml|
xml.authenticate do
Expand All @@ -20,7 +20,11 @@ def self.login
end
end

query(content)
begin
query(content)
rescue QueryError
raise AuthError, 'Invalid login or password'
end

true
end
Expand Down
31 changes: 19 additions & 12 deletions lib/openvas/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,40 @@

module Openvas
class Client
class InvalidUrlConfigError < StandardError; end
class QueryError < StandardError; end

# buffer size for socket
BLOCK_SIZE = 1024 * 16

# Connect the websocket
def self.connect
# Retrieve URI
raise InvalidUrlConfigError, 'Please Configure the client before' unless Openvas::Config.url
raise ConfigError, 'URL not configured' unless Openvas::Config.url

uri = URI.parse(Openvas::Config.url)
begin
uri = URI.parse(Openvas::Config.url)

plain_socket = TCPSocket.open(uri.host, uri.port)
self.socket = OpenSSL::SSL::SSLSocket.new(plain_socket, OpenSSL::SSL::SSLContext.new)
plain_socket = TCPSocket.open(uri.host, uri.port)
self.socket = OpenSSL::SSL::SSLSocket.new(plain_socket, OpenSSL::SSL::SSLContext.new)

# Enable to close socket and SSL layer together
socket.sync_close = true
socket.connect
# Enable to close socket and SSL layer together
socket.sync_close = true
socket.connect
rescue Errno::ECONNREFUSED, OpenSSL::SSL::SSLError, SocketError,
URI::InvalidURIErro => e
raise ConfigError, e
end

true
end

def self.disconnect
return unless socket
socket.close
self.socket = nil

begin
socket.close
self.socket = nil
rescue SocketError => e
raise ConfigError, e
end
end

def self.version
Expand Down

0 comments on commit 722270c

Please sign in to comment.