Advertisement
ericpi

Untitled

Feb 9th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.28 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. #credits to  Copyright (C) 2009 Torsten Becker <torsten.becker@gmail.com> for the proxy bases
  4. #credits to &eric for work in progress url parser
  5. require 'socket'
  6. require 'uri'
  7. require "base64"
  8. class Proxy  
  9.   def run port
  10.     begin
  11.       # Start our server to handle connections (will raise things on errors)
  12.       @socket = TCPServer.new port
  13.       s = @socket
  14.      
  15.       # Handle every request in another thread
  16.       loop do
  17.         s = @socket.accept
  18.         Thread.new s, &method(:handle_request)
  19.         puts "Peer connected"
  20.       end
  21.      
  22.     # CTRL-C
  23.     rescue Interrupt
  24.       puts 'Got Interrupt..'
  25.     # Ensure that we release the socket on errors
  26.     ensure
  27.       if @socket
  28.         @socket.close
  29.         puts 'Socked closed..'
  30.       end
  31.       puts 'Quitting.'
  32.     end
  33.   end
  34.   def handle_request to_client
  35.     request_line = to_client.readline
  36.     verb    = request_line[/^\w+/]
  37.     url     = request_line[/^\w+\s+(\S+)/, 1]
  38.     version = request_line[/HTTP\/(1\.\d)\s*$/, 1]
  39.     uri     = URI::parse url
  40.    
  41.     #Show what got requested
  42.    
  43.     puts((" %4s "%verb) + url)
  44.     urlc = url.gsub( /https?:\/\//, "\\1")
  45.     urlp = url.gsub( /https?:\/\/(.+?)(?>\/)(?:\/$)?/, "\\1")
  46.     #############################IRC####################################
  47.         if(verb == "CONNECT")
  48.             host ,ircport = urlp.split(":")
  49.                 contents = File.read('m/hosts/'+host)
  50.                 ircip = Base64.decode64(contents)
  51.                 irc, ircpt = ircip.split(",")
  52.                 puts "Main irc network"
  53.                 p= TCPSocket.new(irc, ircpt)
  54.                 puts "Connecting to #{contents} port: #{ircpt}"
  55.                 loop do
  56.                 msg = p.gets #get everything from irc server
  57.                 to_client.write(msg) #write it to client or Hexchat
  58.                 puts msg
  59.                 request = request_line #get all the info from Hexchat
  60.                 send = p.puts(request)#send the Hexchat info to server
  61.                 end
  62.     ####################################################################            
  63.                     else
  64.                         contents = File.read('m/hosts/'+urlp)
  65.                         hostip = Base64.decode64(contents)
  66.                         ip, port = hostip.split(",")
  67.                         puts "Connecting to #{contents} port: #{port}"
  68.                         to_server = TCPSocket.new(ip, (uri.port.nil? ? 80 : uri.port))
  69.                         to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/#{version}\r\n")
  70.                 end
  71.  
  72.     content_len = 0
  73.    
  74.     loop do      
  75.       line = to_client.readline
  76.       puts line
  77.       if line =~ /^Content-Length:\s+(\d+)\s*$/
  78.         content_len = $1.to_i
  79.       end
  80.      
  81.       # Strip proxy headers
  82.       if line =~ /^proxy/i
  83.         next
  84.       elsif line.strip.empty?
  85.         to_server.write("Connection: close\r\n\r\n")
  86.        
  87.         if content_len >= 0
  88.           to_server.write(to_client.read(content_len))
  89.         end
  90.        
  91.         break
  92.       else
  93.         to_server.write(line)
  94.       end
  95.     end
  96.        
  97.     buff = ""
  98.     loop do
  99.       to_server.read(4048, buff)
  100.       to_client.write(buff)
  101.       break if buff.size < 4048
  102.     end
  103.    
  104.     # Close the sockets
  105.     to_client.close
  106.     to_server.close
  107.   end
  108.  
  109. end
  110.  
  111. # Get parameters and start the server
  112. if ARGV.empty?
  113.   port = 8181
  114. elsif ARGV.size == 1
  115.   port = ARGV[0].to_i
  116. else
  117.   puts 'Usage: proxy.rb [port]'
  118.   exit 1
  119. end
  120.  
  121. Proxy.new.run port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement