hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

A File-Sharing Paragraph #

by why in bits

Lately, the Ruby-Talk mailing list has been wonderful. An even mix of good conversation and succulent code. The recent Signatures and one liners thread unearthing some highly entertaining bits of Ruby.

If you read this thread and a few others over the last few months, you can watch Florian Gross dominate the golf scores. The guy is unstoppable. He’s got all the techniques down: utilization of single-character constants, combining overloaded operators, map all over the place, tertiary notation, etc.

Yesterday he polished off a six line peer-to-peer client/server to compete with the fifteen line Python app floating around.

 #!/usr/bin/ruby
 # Server: ruby p2p.rb password server public-uri private-uri merge-servers
 # Sample: ruby p2p.rb foobar server druby://123.123.123.123:1337
 #         druby://:1337 druby://foo.bar:1337
 # Client: ruby p2p.rb password client server-uri download-pattern [list-only]
 # Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
 ################################################################################
 require'drb';F=File;P,M,U,V,*O=ARGV;def c(u)DRbObject.new((),u)end;def x(u);[P,u
 ].hash;end;def s(p);F.basename p[/[^|]+/];end;M["s"]?(DRb.start_service V,Class.
 new{def p(z=O)O.push(*z).uniq;end;new.methods.map{|m|m[/_[_t]/]||private(m)};def
 y;(p(U)+p).map{|u|u!=U&&c(u).f(x(u),p(U))};self;end;def f(c,a=[],t=2)x(U)==c&&t<
 1?Dir[s(a)]:t<2?[*open(s(a),"rb")]:p(a)end}.new.y;sleep):c(U).f(x(U)).map{|n|c(n
 ).f(x(n),V,0).map{|f|s f}.sort.map{|f|O[0]?p(f):open(f,"wb")<<c(n).f(x(n),f,1)}}

Features include:

  • Server shares files from its current working directory.
  • Server is password-protected.
  • Server can connect to other servers and form small peer-to-peer networks.
  • Fixes a vulnerability in the Python version where files outside the working directory can be accessed.
  • Client can search the network for files, using a Regexp.
  • Client can list all files available on the network.

Incidentally, the original author of the Python application has recognized Florian’s version as the record-holder for smallness at the present. And while there’s some dispute as whether one should be allowed to leverage libraries—Florian uses Distributed Ruby above—it’s a great little script, wouldn’t you say? Way to get caught RedHanded, Florian!!

said on 21 Dec 2004 at 03:10
Thank you a lot. But please note that the library-less Perl version is now also down to six lines. I don't think Ruby can win against Perl in the long term, those guys designed the language to be used in Golf. It's still great fun to write short code in Ruby however, even if it's not something you'll find yourself doing every day. :) Oh, and thank you a lot for this weblog. It's a very enjoying read!
said on 30 Nov 2005 at 13:51

Just testing something. Ignore me ;)

said on 30 Nov 2005 at 13:51

Just testing something. Ignore me ;)

said on 30 Nov 2005 at 13:54

via GET

said on 30 Nov 2005 at 13:58

_session_id=1083f0fc315b8d709f0bd34a30175dbf

said on 30 Nov 2005 at 14:00

OK, sorry. This was just a test, I am just trying out how easy anonymous XSS attacks are when public GET postings are allowed.

Whoever reads this, be warned…

said on 19 Feb 2006 at 05:26

Preview the preview

Comments are closed for this entry.