commit 925e63eece07920e319f8eb196b91142d30053d3 Author: zachary Date: Mon Jul 6 10:21:09 2015 -0400 inital diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..840bd98 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +Dockerfile +.dockerignore +.git +.gitignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..34117fc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM ubuntu:15.04 + +VOLUME /var/lib/docker + +RUN bash /usr/local/bin/install_docker + +ADD ./dind/wrapdocker /usr/local/bin/wrapdocker + +RUN chmod +x /usr/local/bin/wrapdocker + +COPY ./env.sh /usr/local/bin/set_env + +COPY ./shutdown.sh /usr/local/bin/stop-tor-router + +COPY ./startup.sh /usr/local/bin/stop-tor-router + +COPY ./tor-router.sh /usr/local/bin/tor-router + +COPY ./new_ip.sh /usr/local/bin/new-ip + +RUN chmod -v +x /usr/local/bin/set_env + +RUN chmod -v +x /usr/local/bin/stop-tor-router + +RUN chmod -v +x /usr/local/bin/start-tor-router + +RUN chmod -v +x /usr/local/bin/tor-router + +RUN chmod -v +x /usr/local/bin/new-ip + +EXPOSE 9050 + +ENV TOR_INSTANCES 5 + +CMD ["/usr/local/bin/tor-router"] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e1009c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: dind .drone.yml + +clean: + rm -f .drone.yml + +dind: + git subtree add --prefix dind https://github.com/jpetazzo/dind.git master --squash + +.drone.yml: + wget -qO- http://bit.ly/drone-yml-php | php > .drone.yml \ No newline at end of file diff --git a/env.sh b/env.sh new file mode 100755 index 0000000..e379412 --- /dev/null +++ b/env.sh @@ -0,0 +1,5 @@ +export TOR_INSTANCES=${TOR_INSTANCES:=5} +export TOR_PORT=${TOR_PORT:=9050} +export INSTANCE_PREFIX="tor-" +export IP_ADDRESS=$1 +export CONTROL_PORT_DIR=$2 \ No newline at end of file diff --git a/new_ip.sh b/new_ip.sh new file mode 100755 index 0000000..f0de374 --- /dev/null +++ b/new_ip.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Sends a HUP signal to Tor instances, generating a new IP + +source env.sh + +index="0" + +while [ $index -lt $TOR_INSTANCES ] +do + current_instance="$INSTANCE_PREFIX$index" + + echo "sending signal to $current_instance..." + docker exec -t $current_instance /bin/bash -c 'pgrep -f tor | xargs kill -HUP' + index=$[$index+1] +done + +sleep 1 + +exit 0 diff --git a/package.json b/package.json new file mode 100644 index 0000000..752eb9c --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "tor-router", + "version": "0.0.1", + "author": [ + { + "name": "Zachary Boyd", + "email": "zacharyboyd@zacharyboyd.nyc" + } + ], + "repository": [ + { + "type": "docker", + "url": "http://docker.io/znetstar" + }, + { + "type": "git", + "url": "https://bitbucket.org/znetstar/tor-router" + } + ], + "homepage": "", + "dependencies": { + + }, + "scripts": { + "postinstall": "make" + }, + "devDependencies": { + + } +} diff --git a/shutdown.sh b/shutdown.sh new file mode 100755 index 0000000..7b9e04c --- /dev/null +++ b/shutdown.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +source env.sh + +index="0" + +while [ $index -lt $TOR_INSTANCES ] +do + current_instance=$INSTANCE_PREFIX$index + echo "shutting down $current_instance" + docker rm -f $current_instance + index=$[index+1] +done + +echo "stop haproxy" +docker rm -f haproxy + +echo "closing port" +sudo iptables -A INPUT -p tcp --dport 9050 -j REJECT + +#sleep 5 + +exit 0 diff --git a/startup.sh b/startup.sh new file mode 100755 index 0000000..7045440 --- /dev/null +++ b/startup.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +source env.sh + +TEMP_HA_CONFIG=$(mktemp) +TOR_CMD='tor --MaxCircuitDirtiness 60 --RunAsDaemon 0 --CookieAuthentication 0 --controlport 0.0.0.0:9051 --HashedControlPassword 16:4E9480609FC7089F604C83E788481164C25C205288E17D9E5E73EB050B --PidFile tor.pid --SocksPort 0.0.0.0:9150 --DataDirectory /data/tor --ExcludeSingleHopRelays 0 --NewCircuitPeriod 30 --EnforceDistinctSubnets 0 --AllowDotExit 1' + +index="0" + +#docker -d & + +while [ $index -lt $TOR_INSTANCES ] +do + current_instance="$INSTANCE_PREFIX$index" + echo "removing instance $current_instance..." + docker kill $current_instance + docker rm -f $current_instance + echo "instance $current_instance removed" + +# control_port=$(cat $2/$current_instance) + echo "instnce $current_instance will be assigned control port control port $control_port" + + echo "creating instance $current_instance..." + docker run --name $current_instance -d -v /data --restart="on-failure" nagev/tor $TOR_CMD + echo "instance $current_instance created" + index=$[$index+1] +done + +echo "removing haproxy..." +docker kill haproxy +docker rm -f haproxy +echo "writing config..." +cat << EOF > $TEMP_HA_CONFIG +global + user root + group root + +defaults + log global + mode http + option dontlognull + timeout connect 5000ms + timeout client 50000ms + timeout server 50000ms + errorfile 400 /usr/local/etc/haproxy/errors/400.http + errorfile 403 /usr/local/etc/haproxy/errors/403.http + errorfile 408 /usr/local/etc/haproxy/errors/408.http + errorfile 500 /usr/local/etc/haproxy/errors/500.http + errorfile 502 /usr/local/etc/haproxy/errors/502.http + errorfile 503 /usr/local/etc/haproxy/errors/503.http + errorfile 504 /usr/local/etc/haproxy/errors/504.http +EOF + +echo "listen socks :$TOR_PORT" > $TEMP_HA_CONFIG + +cat <<-EOF >> $TEMP_HA_CONFIG + mode tcp + balance roundrobin +EOF + +index="0" +instances="" +while [ $index -lt $TOR_INSTANCES ] +do + current_instance=$INSTANCE_PREFIX$index + instances=$instances" --link $current_instance:$current_instance" + cat <<-EOF >> $TEMP_HA_CONFIG + server $current_instance $current_instance:9150 check + EOF + index=$[$index+1] +done + +echo "starting haproxy..." +docker run -d -p 9050:9050 --name haproxy $instances -v $TEMP_HA_CONFIG:/usr/local/etc/haproxy/haproxy.cfg:ro haproxy:1.5.9 +echo "tor server setup is complete" + +exit 0 \ No newline at end of file diff --git a/tor-router.sh b/tor-router.sh new file mode 100644 index 0000000..5d3ec6f --- /dev/null +++ b/tor-router.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +wrapdocker & +sleep 5 + +echo 'starting tor router...' +/usr/local/bin/start-tor-router + +docker kill haproxy +docker start -a -i haproxy + +echo 'stopping tor router...' +/usr/local/bin/stop-tor-router +start-stop-daemon --stop --pidfile "/var/run/docker.pid" + +exit 0 \ No newline at end of file