Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

uP2P — micro P2P file sharing application

   1  
   2  #!/bin/sh
   3  # uP2P.sh 0.0.1, 436 characters (excluding comments)
   4  [ $3 ]&&export W=$1 H="$2 $3" K=`mktemp`;Z=/dev/null;e(){ echo "$*";};n(){
   5  nc $* 2>$Z;};x(){ nc -lp ${H#* } -e $1 &>$Z <$Z&};f(){ cat $K|while read h;do
   6  e $W $1 "$2"|n $h;done };case $# in 4)e $W s "$4"|n $H|while read h p f; do
   7  e $W g "$f"|n $h $p>"$f";done;;5)e $H>$K;e $W d $H|n $4 $5>>$K;x $0;;0)x $0
   8  read w c r;[ $W = $w ]&&case $c in s)f l "$r";;g)cat "$r";;a)e $r>>$K;;d)cat $K
   9  f a "$r";;l)ls|grep "$r"|sed "s/^/$H /";;esac;;esac


Source: uP2P, mentioned here

six line peer-to-peer client/server in ruby


slonik AZ wrote:
> slashdot published an article on someone's
> 15 lines long Peer-2-Peer application
> http://developers.slashdot.org/article.pl?sid=04/12/15/1953227

> Another person followed up with a 9 line equivalent Perl code.

> I wonder what an equivalent Ruby program would look like?

I did this 9.5 hours ago. Compared to the python one it is not
vulnerable to File stealing attacks (a client can request a file
../foobar and ~/foobar from the python server and will get it back
AFAIK) and 6 lines long. It is however vulnerable to the DRb style
.instance_eval exploits. I will fix this shortly, but I might have to
use 7 lines then.


   1  
   2  # Server: ruby p2p.rb password server server-uri merge-servers
   3  # Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
   4  # Client: ruby p2p.rb password client server-uri download-pattern
   5  # Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
   6  require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1
   7  ]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&&c(U).f(
   8  x(U)).each{|n|p,c=x(n),c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).each{|f|F.open(f,
   9  "w"){|o|o<<c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&&(
  10  t==0&&D[s(a)]||t==1&&F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),
  11  p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep) 


Source: p2p.rb (Florian Gross), mentioned here
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS