Skip to content

Instantly share code, notes, and snippets.

@guange2015
Forked from druzn3k/proxy.rb
Created October 22, 2012 09:21
Show Gist options
  • Select an option

  • Save guange2015/3930574 to your computer and use it in GitHub Desktop.

Select an option

Save guange2015/3930574 to your computer and use it in GitHub Desktop.

Revisions

  1. @torsten torsten revised this gist Mar 6, 2009. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -17,19 +17,19 @@ def run port
    # Handle every request in another thread
    loop do
    s = @socket.accept
    Thread.new(s, &method(:handle_request))
    Thread.new s, &method(:handle_request)
    end

    # CTRL-C
    rescue Interrupt
    puts 'got interrupt..'
    puts 'Got Interrupt..'
    # Ensure that we release the socket on errors
    ensure
    if @socket
    @socket.close
    puts 'socked closed..'
    puts 'Socked closed..'
    end
    puts 'quiting.'
    puts 'Quitting.'
    end
    end

    @@ -49,20 +49,16 @@ def handle_request to_client

    content_len = 0

    # TODO: keep connection to client alive
    loop do
    line = to_client.readline

    # puts line if line =~ /content|len|agent|proxy|x-/i

    if line =~ /^Content-Length:\s+(\d+)\s*$/
    content_len = $1.to_i
    end

    # Strip proxy headers
    if line =~ /^proxy/i
    next
    # Inject connection: close 'cuz I am lazy
    elsif line.strip.empty?
    to_server.write("Connection: close\r\n\r\n")

    @@ -76,9 +72,12 @@ def handle_request to_client
    end
    end

    # TODO: make it read in 4K blocks or so
    answer = to_server.read
    to_client.write(answer)
    buff = ""
    loop do
    to_server.read(4048, buff)
    to_client.write(buff)
    break if buff.size < 4048
    end

    # Close the sockets
    to_client.close
  2. @torsten torsten revised this gist Mar 5, 2009. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    #!/usr/bin/env ruby

    # A quick and dirty implementation of an HTTP proxy server in Ruby
    # because I did not want to install anything.
    #
    @@ -54,7 +53,7 @@ def handle_request to_client
    loop do
    line = to_client.readline

    puts line if line =~ /content|len|agent|proxy|auth|x-/i
    # puts line if line =~ /content|len|agent|proxy|x-/i

    if line =~ /^Content-Length:\s+(\d+)\s*$/
    content_len = $1.to_i
  3. @torsten torsten revised this gist Mar 5, 2009. 1 changed file with 25 additions and 9 deletions.
    34 changes: 25 additions & 9 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -23,45 +23,61 @@ def run port

    # CTRL-C
    rescue Interrupt
    puts 'Got Interrupt..'
    puts 'got interrupt..'
    # Ensure that we release the socket on errors
    ensure
    if @socket
    @socket.close
    puts 'Socked closed..'
    puts 'socked closed..'
    end
    puts 'Quitting.'
    puts 'quiting.'
    end
    end

    def handle_request to_client
    request_line = to_client.readline

    verb = request_line[/^\w+/]
    url = request_line[/^\w+\s+(\S+)/, 1]
    uri = URI::parse url
    verb = request_line[/^\w+/]
    url = request_line[/^\w+\s+(\S+)/, 1]
    version = request_line[/HTTP\/(1\.\d)\s*$/, 1]
    uri = URI::parse url

    # Show what got requested
    puts(("%6s "%verb) + url)
    puts((" %4s "%verb) + url)

    to_server = TCPSocket.new(uri.host, (uri.port.nil? ? 80 : uri.port))
    to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n")
    to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/#{version}\r\n")

    loop do
    content_len = 0

    # TODO: keep connection to client alive
    loop do
    line = to_client.readline

    puts line if line =~ /content|len|agent|proxy|auth|x-/i

    if line =~ /^Content-Length:\s+(\d+)\s*$/
    content_len = $1.to_i
    end

    # Strip proxy headers
    if line =~ /^proxy/i
    next
    # Inject connection: close 'cuz I am lazy
    elsif line.strip.empty?
    to_server.write("Connection: close\r\n\r\n")

    if content_len >= 0
    to_server.write(to_client.read(content_len))
    end

    break
    else
    to_server.write(line)
    end
    end

    # TODO: make it read in 4K blocks or so
    answer = to_server.read
    to_client.write(answer)

  4. @torsten torsten revised this gist Mar 5, 2009. 1 changed file with 31 additions and 40 deletions.
    71 changes: 31 additions & 40 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env ruby

    # A quick and dirty implementation of a proxy server in Ruby
    # A quick and dirty implementation of an HTTP proxy server in Ruby
    # because I did not want to install anything.
    #
    # Copyright (C) 2009 Torsten Becker <[email protected]>
    @@ -12,84 +12,75 @@
    class Proxy
    def run port
    begin
    # start our server to handle connections (will raise things on errors)
    # Start our server to handle connections (will raise things on errors)
    @socket = TCPServer.new port

    # handle every request with another method
    while true
    # Handle every request in another thread
    loop do
    s = @socket.accept

    Thread.new(s) do |r|
    handle_request r
    end
    Thread.new(s, &method(:handle_request))
    end

    # ctrl-c
    # CTRL-C
    rescue Interrupt
    puts 'got interrupt..'

    # get all possibly errors
    rescue => e
    puts(e.class.to_s + ': ' + e.message)

    # ensure that we release the socket on errors
    puts 'Got Interrupt..'
    # Ensure that we release the socket on errors
    ensure
    if @socket
    @socket.close
    puts 'socked closed..'
    puts 'Socked closed..'
    end

    puts 'quiting.'
    puts 'Quitting.'
    end
    end

    def handle_request socket
    request_line = socket.readline
    def handle_request to_client
    request_line = to_client.readline

    verb = request_line[/^\w+/]
    url = request_line[/^\w+\s+(\S+)/, 1]
    uri = URI::parse url

    # Show what gets requested
    puts(("%-4s "%verb) + url)
    # Show what got requested
    puts(("%6s "%verb) + url)

    client = TCPSocket.new(uri.host, 80)
    client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n")
    to_server = TCPSocket.new(uri.host, (uri.port.nil? ? 80 : uri.port))
    to_server.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n")

    loop do
    line = socket.readline
    line = to_client.readline

    # Strip proxy headers
    if line =~ /^proxy/i
    next
    # Inject connection: close 'cuz I am lazy
    if line.strip.empty?
    client.write("Connection: close\r\n\r\n")
    elsif line.strip.empty?
    to_server.write("Connection: close\r\n\r\n")
    break
    else
    client.write(line)
    to_server.write(line)
    end
    end

    answer = client.read
    socket.write(answer)
    answer = to_server.read
    to_client.write(answer)

    # close the sockets
    client.close
    socket.close
    # Close the sockets
    to_client.close
    to_server.close
    end

    end


    # get parameters and start the server
    # Get parameters and start the server
    if ARGV.empty?
    port = 8008

    elsif ARGV.size == 1
    port = ARGV[0].to_i

    else
    puts 'Call me like this: proxy.rb [port]'
    exit -1

    puts 'Usage: proxy.rb [port]'
    exit 1
    end

    Proxy.new.run port
  5. @torsten torsten revised this gist Mar 5, 2009. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,7 @@
    require 'uri'


    # Its Proxy
    class Proxy

    def run port
    begin
    # start our server to handle connections (will raise things on errors)
    @@ -52,10 +50,11 @@ def handle_request socket
    url = request_line[/^\w+\s+(\S+)/, 1]
    uri = URI::parse url

    puts url
    # Show what gets requested
    puts(("%-4s "%verb) + url)

    client = TCPSocket.new(uri.host, 80)
    client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.0\r\n")
    client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.1\r\n")

    loop do
    line = socket.readline
    @@ -93,4 +92,5 @@ def handle_request socket

    end

    Proxy.new().run port
    Proxy.new.run port

  6. @torsten torsten revised this gist Mar 5, 2009. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,7 @@
    # A quick and dirty implementation of a proxy server in Ruby
    # because I did not want to install anything.
    #
    # Copyright (C) 2009 Torsten Becker <[email protected]>.

    # Copyright (C) 2009 Torsten Becker <[email protected]>

    require 'socket'
    require 'uri'
    @@ -95,4 +94,3 @@ def handle_request socket
    end

    Proxy.new().run port

  7. @torsten torsten created this gist Mar 5, 2009.
    98 changes: 98 additions & 0 deletions proxy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    #!/usr/bin/env ruby

    # A quick and dirty implementation of a proxy server in Ruby
    # because I did not want to install anything.
    #
    # Copyright (C) 2009 Torsten Becker <[email protected]>.


    require 'socket'
    require 'uri'


    # Its Proxy
    class Proxy

    def run port
    begin
    # start our server to handle connections (will raise things on errors)
    @socket = TCPServer.new port

    # handle every request with another method
    while true
    s = @socket.accept

    Thread.new(s) do |r|
    handle_request r
    end
    end

    # ctrl-c
    rescue Interrupt
    puts 'got interrupt..'

    # get all possibly errors
    rescue => e
    puts(e.class.to_s + ': ' + e.message)

    # ensure that we release the socket on errors
    ensure
    if @socket
    @socket.close
    puts 'socked closed..'
    end

    puts 'quiting.'
    end
    end

    def handle_request socket
    request_line = socket.readline

    verb = request_line[/^\w+/]
    url = request_line[/^\w+\s+(\S+)/, 1]
    uri = URI::parse url

    puts url

    client = TCPSocket.new(uri.host, 80)
    client.write("#{verb} #{uri.path}?#{uri.query} HTTP/1.0\r\n")

    loop do
    line = socket.readline

    # Inject connection: close 'cuz I am lazy
    if line.strip.empty?
    client.write("Connection: close\r\n\r\n")
    break
    else
    client.write(line)
    end
    end

    answer = client.read
    socket.write(answer)

    # close the sockets
    client.close
    socket.close
    end

    end


    # get parameters and start the server
    if ARGV.empty?
    port = 8008

    elsif ARGV.size == 1
    port = ARGV[0].to_i

    else
    puts 'Call me like this: proxy.rb [port]'
    exit -1

    end

    Proxy.new().run port