the 6-lines p2p system.
I was surfing the internet looking for some inspiration to develop my undergrad project (and no, I wasn't looking for porn!) and found this AMAZING p2p system written by Dr. Pascal Felber, he called it uP2P (micro peer-to-peer). Here is the code:
-
#!/bin/sh
-
# uP2P.sh 0.0.1, 436 characters (excluding comments)
-
[ $3 ]&&export W=$1 H="$2 $3" K=`mktemp`;Z=/dev/null;e(){ echo "$*";};n(){
-
nc $* 2>$Z;};x(){ nc -lp ${H#* } -e $1 &>$Z <$Z&};f(){ cat $K|while read h;do
-
e $W $1 "$2"|n $h;done };case $# in 4)e $W s "$4"|n $H|while read h p f; do
-
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
-
read w c r;[ $W = $w ]&&case $c in s)f l "$r";;g)cat "$r";;a)e $r>>$K;;d)cat $K
-
f a "$r";;l)ls|grep "$r"|sed "s/^/$H /";;esac;;esac
ok, it is a mess... (somehow, reminds me the worpress source-code hehehehehe) but that's because it hasn't any comment lines. There is a long version available as well which explains how it works (you will see that everything is made using standard Unix tools and the whole communication is made using netcat ("nc").
usage is pretty simple though:
To start a server, change to the directory that you would like to share and start the script as follows:
uP2P.sh password local-ip local-port remote-ip remote-port
where 'password' is the network's password; 'local-ip' and 'local-port' are the IP address and port used by the server for listening to remote requests; 'remote-ip' and 'remote-port' are the IP address and port of some other peer in the network. The first server can be started with any remote address (even its own address) since connections to non-existing endpoints will fail silently. All 5 parameters must be specified.
To start a client, change to the directory that you would like to download files to and start the script as follows:
uP2P.sh password remote-ip remote-port pattern
where 'password' is the network's password; 'remote-ip' and 'remote-port' are the IP address and port of some server peer in the network; 'pattern' is a filter that specifies the files to download using the 'grep' regular expression syntax. Note that, because of message forwarding, redundant escape characters must be used; for instance, "\." (dot) must be specified as "\\\\.".
To end the script, kill all instances of 'nc'.
Example:
Start 4 servers on 4 hosts (or in different directories on the same host):
uP2P.sh abc 192.168.1.100 9000 192.168.1.100 9000
uP2P.sh abc 192.168.1.101 9000 192.168.1.100 9000
uP2P.sh abc 192.168.1.102 9000 192.168.1.101 9000
uP2P.sh abc 192.168.1.103 9000 192.168.1.101 9000
Download all files containing 'pdf' in their name (here using third server as entry point to the network):
uP2P.sh abc 192.168.1.102 9000 "pdf"
Amazing huh? Use it to impress your teacher (please keep the credits for Dr. Felber). And remember: this is justa PoC. There are many limitations (and possibilities!).
-bigo
No comments yet. Be the first.
Leave a reply
