diff --git a/identproxy.rb b/identproxy.rb index 612960d1aa72dad9f51e3a0f1a5522ae340595d6..2f649adb8b3036e5a75cbe6155ecf239f6b6aabc 100644 --- a/identproxy.rb +++ b/identproxy.rb @@ -41,59 +41,65 @@ end # Main server loop loop do - # New client connected - client = server.accept - - # Read request from socket - input = client.gets - input.strip! - - sane = true - p1 = 0 - p2 = 0 - - # Check if it's something that looks like an ident response - if( input =~ /^(\d+)(|\s),(|\s)(\d+)$/ ) - p1 = $1.to_i - p2 = $3.to_i - else - sane = false - end - - # Sanity check on port number - if( p1 < 1 || p1 > 65535 || p2 < 1 || p2 > 65535 ) - sane = false - end - - # Send generic error for stuff that does not look like ident requests - if(!sane) - client.puts "#{input}:ERROR:NO-USER" - client.close - next - end - - # Do NAT table lookup - n = natlookup( p1, p2 ) + begin + # New client connected + client = server.accept + + # Read request from socket + input = client.gets + input.strip! + + puts "Request: #{input}" + + sane = true + p1 = 0 + p2 = 0 + + # Check if it's something that looks like an ident response + if( input =~ /^(\d+)(|\s),(|\s)(\d+)$/ ) + p1 = $1.to_i + p2 = $4.to_i + else + sane = false + end + + # Sanity check on port number + if( p1 < 1 || p1 > 65535 || p2 < 1 || p2 > 65535 ) + sane = false + end + + # Send generic error for stuff that does not look like ident requests + if(!sane) + client.puts "#{input}:ERROR:NO-USER" + client.close + next + end + + # Do NAT table lookup + n = natlookup( p1, p2 ) - # Check if there was a NAT table entry - if( n.nil? ) - client.puts "#{input}:ERROR:NO-USER" + # Check if there was a NAT table entry + if( n.nil? ) + client.puts "#{input}:ERROR:NO-USER" + client.close + next + end + + # Forward request to NAT'ed server + result = forwardrequest( n[0], n[1], n[2] ) + + # Show result to client + if( result.nil? ) + client.puts "#{input}:ERROR:NO-USER" + client.close + next + end + + puts "Response: #{result}" + client.puts result + + # Close connection and wait for the next request client.close - next - end - - # Forward request to NAT'ed server - result = forwardrequest( n[0], n[1], n[2] ) - - # Show result to client - if( result.nil? ) - client.puts "#{input}:ERROR:NO-USER" - client.close - next + rescue Exception => e end - - client.puts result - - # Close connection and wait for the next request - client.close end