Advertisement
ericpi

Untitled

Feb 9th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.11 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.         if(verb == "CONNECT")
  47.             host ,ircport = urlp.split(":")
  48.                 contents = File.read('m/hosts/'+host)
  49.                 ircip = Base64.decode64(contents)
  50.                 irc, ircpt = ircip.split(",")
  51.                 puts "Main irc network"
  52.                 p= TCPSocket.new(irc, ircpt)
  53.                 puts "Connecting to #{contents} port: #{ircpt}"
  54.                 loop do
  55.                 msg = p.gets #get everything from server
  56.                 to_client.write(msg) #write it to client
  57.                 puts msg
  58.                 request = request_line #get all the info from client
  59.                 send = p.puts(request)#send the client info to server
  60.                 end
  61.                     else
  62.                         contents = File.read('m/hosts/'+urlp)
  63.                         hostip = Base64.decode64(contents)
  64.                         ip, port = hostip.split(",")
  65.                         puts "Connecting to #{contents} port: #{port}"
  66.                         to_server = TCPSocket.new(ip, (uri.port.nil? ? 80 : uri.port))
  67.                         to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/#{version}\r\n")
  68.                 end
  69.  
  70.     content_len = 0
  71.    
  72.     loop do      
  73.       line = to_client.readline
  74.       puts line
  75.       if line =~ /^Content-Length:\s+(\d+)\s*$/
  76.         content_len = $1.to_i
  77.       end
  78.      
  79.       # Strip proxy headers
  80.       if line =~ /^proxy/i
  81.         next
  82.       elsif line.strip.empty?
  83.         to_server.write("Connection: close\r\n\r\n")
  84.        
  85.         if content_len >= 0
  86.           to_server.write(to_client.read(content_len))
  87.         end
  88.        
  89.         break
  90.       else
  91.         to_server.write(line)
  92.       end
  93.     end
  94.        
  95.     buff = ""
  96.     loop do
  97.       to_server.read(4048, buff)
  98.       to_client.write(buff)
  99.       break if buff.size < 4048
  100.     end
  101.    
  102.     # Close the sockets
  103.     to_client.close
  104.     to_server.close
  105.   end
  106.  
  107. end
  108.  
  109. # Get parameters and start the server
  110. if ARGV.empty?
  111.   port = 8181
  112. elsif ARGV.size == 1
  113.   port = ARGV[0].to_i
  114. else
  115.   puts 'Usage: proxy.rb [port]'
  116.   exit 1
  117. end
  118.  
  119. Proxy.new.run port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement