Describe EliezerCroitoru/SessionHelper here.

   1 #!/usr/bin/env ruby
   2 # license note
   3 # Copyright (c) 2014, Eliezer Croitoru
   4 # All rights reserved.
   5 #
   6 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
   7 #
   8 # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
   9 #
  10 # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11 #
  12 # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  13 #
  14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  15 
  16 require "dalli"
  17 
  18 active_login = true
  19 $debug = false
  20 $minutes = 30
  21 
  22 class Db
  23 
  24   def initialize()
  25     begin
  26        @db = Dalli::Client.new('localhost:11211')
  27     rescue =>e
  28       puts "printing backtrace"
  29       puts e.inspect
  30       puts e
  31     end
  32   end
  33 
  34   def writable?()
  35     begin
  36       @db.set("8.8.8.8", Time.now.to_i)
  37       @db.delete("8.8.8.8")
  38     rescue => e
  39       puts "printing backtrace"
  40       puts e.inspect
  41       puts e
  42       return false
  43     end
  44       return true
  45   end
  46 
  47   def login(ip)
  48     @db.set(ip,Time.now.to_i)
  49   end
  50 
  51   def logout(ip)
  52     @db.delete(ip)
  53   end
  54 
  55   def gettime(ip_address)
  56     result = @db.get(ip_address)
  57     if result
  58       return Time.at(result)
  59     end
  60     return Time.at(0)
  61   end
  62 end
  63 
  64 $database = Db.new()
  65 if !$database.writable?
  66   puts "database is not writable exiting.."
  67   exit 1
  68 end
  69 STDOUT.sync = true
  70 
  71 while line = STDIN.gets
  72   id , ip , login = line.chomp.split
  73   STDERR.puts "request details: {id=> \" #{id}\", ip=> \"#{ip}\", login=> \"#{login == "LOGIN"}\"}" if $debug
  74   if login && login == "LOGIN"
  75     $database.login(ip)
  76     STDOUT.puts "#{id} OK message=\"Welcome\""
  77   elsif login && login == "LOGOUT"
  78     $database.logout(ip)
  79     STDOUT.puts "#{id} OK message=\"ByeBye\""
  80   else
  81     current = $database.gettime(ip)
  82     calc = (Time.now- current).to_i
  83     if  calc > ($minutes*60)
  84       STDOUT.puts "#{id} ERR message=\"No session available\""
  85     else
  86       STDOUT.puts "#{id} OK message=\"passed: #{calc} seconds\""
  87     end
  88   end
  89 end

EliezerCroitoru/SessionHelper (last edited 2014-11-16 09:54:29 by Eliezer Croitoru)