You are not logged in.

#1 2005-07-13 05:23:42

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

http proxy, in 15 lines

Someone in irc was asking about a simple http proxy the other day. I scratched around in some ruby, and came up with a *very* simple one..in 15 lines of code. Yeah, I know..I am prone to verbosity..

Something to fiddle with, in any regard. smile
super simple http-proxy


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#2 2005-07-13 06:34:06

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

A slightly more complete version. Plays a bit nicer with things like wget and links.
simple http-proxy

Lookout! code bloat! feature creep!
wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2005-07-13 07:00:50

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: http proxy, in 15 lines

I raise you one and give you one in python in 4 lines.

https://iphitus.loudas.com/arch/proxy/5lineproxy.py

or the slightly more readable version

https://iphitus.loudas.com/arch/proxy/9lineproxy.py

though it still doesnt do http post. which means you cant do forums, etc, im fiddling on one that does http post now...

Offline

#4 2005-07-13 07:08:05

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

well, like i said, mine was a tad verbose. I actual was outputting useful text. wink
EDIT: Your links dont go anywhere iphitus..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2005-07-13 07:19:06

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: http proxy, in 15 lines

cactus wrote:

well, like i said, mine was a tad verbose. I actual was outputting useful text. wink

meh, didnt have anything else to do smile I'm trying to make mine do POST requests.. which isnt easy.

Offline

#6 2005-07-13 07:23:53

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

yeah. get is pretty easy. post, and cookies...that would be harder.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#7 2005-07-13 10:27:25

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: http proxy, in 15 lines

Hi,

basic post-support:


#!/usr/bin/env ruby
require 'socket'
require 'open-uri'

server = TCPServer.new('127.0.0.1', 9090)

while (session = server.accept)
  request = ""
  response = ""
    
  while (r = session.gets) and r.strip.any?
    request += r
  end
  puts request
  r_type, url_request = (request.split(/ /))
  begin
    case r_type
      when /POST/
        uri = URI.parse(url_request)                
                if uri.is_a?(URI::HTTPS)
                  raise "sorry, no https possiblen"
        end
        content_length = request.scan(/Content-Length: ([0-9]+)rn/i).flatten[0].to_i
        content = session.read(content_length)
                Net::HTTP.start(uri.host, uri.port) do |http|
                  response = http.post(uri.path,content).body
        end
      when /GET/
        open(url_request) do |file|
          response = file.read
        end
      else
        raise "unknown request-type: #{r_type}"
        end
  rescue => e
    response = "Error: "+e.to_s
  end
  session.print response
  session.close
end

Offline

#8 2005-07-13 13:17:07

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: http proxy, in 15 lines

oh, that's just mean, I still havnt got my post support working.

Offline

#9 2005-07-13 13:25:52

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,750

Re: http proxy, in 15 lines

I just want Rails  :cry:


Mr Green

Offline

#10 2005-07-13 13:51:18

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: http proxy, in 15 lines

Mr Green wrote:

I just want Rails  :cry:

In what way would you use rails to write a proxy?  wink

Offline

#11 2005-07-13 14:24:12

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,750

Re: http proxy, in 15 lines

jochen wrote:
Mr Green wrote:

I just want Rails  :cry:

In what way would you use rails to write a proxy?  wink

:oops: threading nappin' sorry


Mr Green

Offline

#12 2005-07-13 16:33:33

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: http proxy, in 15 lines

Btw. this proxy runs great on my linksys wrt54gs with openwrt firmware. I tried to run rails there too. But no chance, it eats way too much resources. Sure it works, but just about 0.2 pages per second  :shock:

Offline

#13 2005-07-13 16:40:06

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: http proxy, in 15 lines

jochen wrote:

Btw. this proxy runs great on my linksys wrt54gs with openwrt firmware. I tried to run rails there too. But no chance, it eats way too much resources. Sure it works, but just about 0.2 pages per second  :shock:

LOL, you running OpenWrt?

It's kinda funny you're using cactus' mini-proxy on your router 8)

Offline

#14 2005-07-13 16:45:54

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: http proxy, in 15 lines

yeah, this little box can be almost everything you want it to be when using openwrt.

Offline

#15 2005-07-13 17:14:55

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

smile


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#16 2005-07-14 04:52:17

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

thanks to jochen, i added post support. Works with both post and get urls..
like post to get type urls even
eg. http://localhost/index.php?type=no

http-proxy.rb
heh..i might try adding cookie support next. wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#17 2005-07-16 08:03:58

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

cookie support is mostly working. Not sure if it allows creating a new cookie, or if it just uses an old cookie.

I may add some threading soon, as it only requests and returns pages sequentially.
heh. nothing like wasting time on silly code. smile


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#18 2005-07-16 13:31:23

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: http proxy, in 15 lines

For threading I would suggest subclassing from GServer http://www.ruby-doc.org/stdlib/libdoc/g … erver.html

Offline

#19 2005-07-16 16:43:06

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: http proxy, in 15 lines

oooh.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

Board footer

Powered by FluxBB