diff --git a/bin/netsoul-ruby b/bin/netsoul-ruby index 8666aa4..fd2794d 100755 --- a/bin/netsoul-ruby +++ b/bin/netsoul-ruby @@ -51,10 +51,13 @@ module Netsoul @sock = TCPSocket.new(@config.server_host, @config.server_port) fail Netsoul::SocketError, 'Could not open a socket. Connection is unavailable.'.freeze unless @sock _cmd, _socket_num, md5_hash, client_ip, client_port, _server_timestamp = sock_get.split + @config.build_user_connection_info md5_hash: md5_hash, client_ip: client_ip, client_port: client_port + auth_ag auth_method auth_status + @started = true end @@ -75,8 +78,8 @@ module Netsoul sock, = IO.select([@sock], nil, nil, SOCKET_READ_TIMEOUT) fail Netsoul::SocketError, 'Timeout or fail on read socket' if sock.nil? || sock.empty? res = sock.first.gets - log :info, "[get ] #{res.chomp}" - res + log :info, "[get ] #{res.chomp}" if res + res || '' end def sock_close @@ -99,12 +102,7 @@ OptionParser.new do |opts| opts.separator 'Netsoul-Ruby options:'.freeze opts.on('-c'.freeze, '--config FILE'.freeze, 'Configuration file in YAML'.freeze) do |file| - options[:config] = file - unless File.file?(options[:config]) - puts '[ERROR] Configuration is not a valid file' - exit - end - options[:user_opts] = YAML.load_file(options[:config]) + options[:user_opts] = YAML.load_file(file) if File.file?(file) end opts.on('-h', '--help', 'Display this screen') do @@ -113,8 +111,9 @@ OptionParser.new do |opts| end end.parse! -unless options.include?(:config) - puts '[ERROR] Configuration file is not provided' +unless ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_SOCKS_PASSWORD).include?(k) } == 2 || + ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_UNIX_PASSWORD NETSOUL_AUTH_METHOD).include?(k) } == 3 + puts '[ERROR] You have to specify a configuration file or environment variables' exit end diff --git a/lib/netsoul/config.rb b/lib/netsoul/config.rb index 7a41b60..9776fea 100644 --- a/lib/netsoul/config.rb +++ b/lib/netsoul/config.rb @@ -17,20 +17,33 @@ class Config attr_reader :client_name - # rubocop:disable Metrics/AbcSize + # Supported environment variables: + # + # +NETSOUL_SERVER_HOST+: Netsoul server host, default 'ns-server.epita.fr' + # +NETSOUL_SERVER_PORT+: Netsoul server port, default 4242 + # +NETSOUL_LOGIN+: IONIS account name + # +NETSOUL_SOCKS_PASSWORD+: IONIS socks password + # +NETSOUL_UNIX_PASSWORD+: IONIS unix password + # +NETSOUL_AUTH_METHOD+: Authentication method, default :std. Valid options are => @see +Config::AUTH_METHODS+ + # +NETSOUL_STATE+: User status, default is :none. Valid options are => @see +Config::USER_STATES+ + # +NETSOUL_LOCATION+: User location is free text of your position. If you ar in IONIS network an automatic mapping is proceed to detect your location + # +NETSOUL_USER_GROUP+: Free text specifying your promo or whatever else + # +NETSOUL_CLIENT_NAME+: Redefine the client name exposed to the Netsoul server + # + # rubocop:disable all def initialize(opts = {}) - @server_host = opts.fetch(:server_host, 'ns-server.epita.fr'.freeze) - @server_port = Integer(opts.fetch(:server_port, 4242)) - @login = opts.fetch(:login, 'ionis'.freeze) - @socks_password = opts.fetch(:socks_password, 'socks_password'.freeze) - @unix_password = opts.fetch(:unix_password, 'unix_password'.freeze) - @auth_method = AUTH_METHODS.include?(opts[:auth_method]) ? opts[:auth_method] : :std - @state = USER_STATES.include?(opts[:state]) ? opts[:state] : :none - @location = opts.fetch(:location, 'Home'.freeze) - @user_group = opts.fetch(:user_group, 'ETNA_2008'.freeze) + @server_host = ENV['NETSOUL_SERVER_HOST'] || opts.fetch(:server_host, 'ns-server.epita.fr'.freeze) + @server_port = Integer(ENV['NETSOUL_SERVER_PORT'] || opts.fetch(:server_port, 4242)) + @login = ENV['NETSOUL_LOGIN'] || opts.fetch(:login, 'ionis'.freeze) + @socks_password = ENV['NETSOUL_SOCKS_PASSWORD'] || opts.fetch(:socks_password, 'socks_password'.freeze) + @unix_password = ENV['NETSOUL_UNIX_PASSWORD'] || opts.fetch(:unix_password, 'unix_password'.freeze) + @auth_method = (ENV['NETSOUL_AUTH_METHOD'] || (AUTH_METHODS.include?(opts[:auth_method]) ? opts[:auth_method] : :std)).to_sym + @state = (ENV['NETSOUL_STATE'] || (USER_STATES.include?(opts[:state]) ? opts[:state] : :none)).to_sym + @location = ENV['NETSOUL_LOCATION'] || opts.fetch(:location, 'Home'.freeze) + @user_group = ENV['NETSOUL_USER_GROUP'] || opts.fetch(:user_group, 'ETNA_2008'.freeze) @user_connection_info = {} - @client_name = opts.fetch(:client_name, '(Netsoul-Ruby) -> { Christian Kakesa, since 2009}'.freeze) + @client_name = ENV['NETSOUL_CLIENT_NAME'] || opts.fetch(:client_name, '(Netsoul-Ruby) -> { Christian Kakesa, since 2009}'.freeze) end def build_user_connection_info(opts = {}) diff --git a/lib/netsoul/version.rb b/lib/netsoul/version.rb index 0c83001..6642a77 100644 --- a/lib/netsoul/version.rb +++ b/lib/netsoul/version.rb @@ -1,3 +1,3 @@ module Netsoul - VERSION = '1.6.0'.freeze + VERSION = '1.7.0'.freeze end