diff --git a/bind-input b/bind-input new file mode 100755 index 0000000..15dfac5 --- /dev/null +++ b/bind-input @@ -0,0 +1,62 @@ +#!/bin/bash + + +# (1) Init. +#--------------------------------------------------------# +# (1) Get current absolute dir +ROOT=$(dirname `realpath $0`); + +# (2) Check argc +test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tbind-input \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the input (buffer name)\n\t\tTo port to listen to\n" && exit 1; + +# (3) Check @PORT range # +MIN_PORT=49152; +MAX_PORT=65535; +test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: must be between $MIN_PORT and $MAX_PORT" && exit 1; + +# (4) Set argument explicit names # +IN_ID="$1"; +IN_PT="$2"; + + + +# (2) Prepare buffer +#--------------------------------------------------------# +# (1) Create buffer path +BUFFER="/tmp/inbuf_$IN_ID"; + +# (2) Create/Flush buffer +test -f $BUFFER && rm $BUFFER; +touch $BUFFER; + + + + +# (3) Launch INPUT server +#--------------------------------------------------------# +( + + trap "kill -9 %1 2>/dev/null" INT KILL; + infail=0; + + # (1) kill script after 10 failures + while [ $infail -lt 10 ]; do + + echo "failures: $infail"; + + # (2) bind socket to buffer + echo "(.) binding :$IN_PT to $BUFFER"; + + ( nc -lp $IN_PT >> $BUFFER 2> /dev/null && infail="0" || ( infail=`expr $infail + 1`; exit 1 ) || echo " * cannot access :$IN_PT" )& + wait $!; + + done; + +)& +LOOP_PID=$!; + + +# (2) Remove buffer + kill loop # +trap "rm $BUFFER 2>/dev/null; kill -9 $LOOP_PID 2>/dev/null" INT KILL; + +wait $LOOP_PID; \ No newline at end of file diff --git a/bind-output b/bind-output new file mode 100755 index 0000000..2107717 --- /dev/null +++ b/bind-output @@ -0,0 +1,63 @@ +#!/bin/bash + + +# (1) Init. +#--------------------------------------------------------# +# (1) Get current absolute dir +ROOT=$(dirname `realpath $0`); + +# (2) Check argc +test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tbind-output \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the output (buffer name)\n\t\tThe ip or host of the target\n\t\tThe port to connect to\n" && exit 1; + +# (3) Check @PORT range # +MIN_PORT=49152; +MAX_PORT=65535; +test "$3" -gt "$MAX_PORT" -o "$3" -lt "$MIN_PORT" && echo "error: must be between $MIN_PORT and $MAX_PORT" && exit 1; + +# (4) Set argument explicit names # +OUT_ID="$1"; +OUT_IP="$2"; +OUT_PT="$3"; + + + +# (2) Prepare buffer +#--------------------------------------------------------# +# (1) Create buffer path +BUFFER="/tmp/outbuf_$OUT_ID"; + +# (2) Create/Flush buffer +test -f $BUFFER && rm $BUFFER; +touch $BUFFER; + + + + +# (3) Launch INPUT server +#--------------------------------------------------------# +( + + trap "kill -9 %1 2>/dev/null" INT KILL; + outfail=0; + + # (1) kill script after 10 failures + while [ $outfail -lt 10 ]; do + + echo "failures: $outfail"; + + # (2) bind socket to buffer + echo "(.) binding $BUFFER to $OUT_IP:$OUT_PT"; + + ( tail -f $BUFFER | nc $OUT_IP $OUT_PT 2> /dev/null && outfail="0" || ( outfail=`expr $outfail + 1`; exit 1 ) || echo " * cannot access $OUT_IP:$OUT_PT" )& + wait $!; + + done; + +)& +LOOP_PID=$!; + + +# (2) Remove buffer + kill loop # +trap "rm $BUFFER 2>/dev/null; kill -9 $LOOP_PID 2>/dev/null" INT KILL; + +wait $LOOP_PID; \ No newline at end of file diff --git a/pool b/pool new file mode 100755 index 0000000..0200962 --- /dev/null +++ b/pool @@ -0,0 +1,65 @@ +#!/bin/bash + + +# (1) Init. +#--------------------------------------------------------# +# (1) Get current absolute dir +ROOT=$(dirname `realpath $0`); + +# (2) Check argc +test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tpool \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the pool (client must match it)\n\t\tTo port to listen to\n" && exit 1; + +# (3) Check @PORT range # +MIN_PORT=49152; +MAX_PORT=65535; +test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: must be between $MIN_PORT and $MAX_PORT" && exit 1; + +# (4) Set argument explicit names # +POOL_ID="$1"; +POOL_PT="$2"; + + +# (2) Launch pool listener +#--------------------------------------------------------# +# (1) Start the script in background +listfail=0; + +# infinite listener +while true; do + + # Listen on port @2 + echo "(.) listen $POOL_PT"; + MSG=`nc -lp $POOL_PT`; + + # Do nothing if empty msg + test -z "$MSG" && continue; + + # Extract ID - PORT + CLI_ID="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\)$/\1/'`"; + CLI_PT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\)$/\2/'`"; + + # if ID does not match + test "$CLI_ID" != "$POOL_ID" && echo " /!\\ wrong id" && continue; + + # create new listening socket + echo " [x] creating socket"; + + # Find an available port + PORT="$MIN_PORT"; + + while true; do + + test "$PORT" = "$POOL_PT" && PORT="`expr $PORT + 1`" && continue; + + echo -n " ?? port $PORT -> "; + ss -tl4 "( sport = :$PORT )" | grep "$PORT" >/dev/null 2>&1 || (echo "free"; exit 1) || break; + + echo "used"; + PORT="`expr $PORT + 1`"; + + done; + + echo " > listen $PORT"; + +done; +