diff --git a/.dockerignore b/.dockerignore index ea26410..d55d55e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,4 +3,5 @@ Dockerfile .git .gitignore .drone.yml -Makefile \ No newline at end of file +Makefile +run.sh \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 59243a5..f7f3552 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ -FROM ubuntu:15.04 +FROM php:5.4.42-cli VOLUME /var/lib/docker +VOLUME /tmp + COPY ./install_docker.sh /usr/local/bin/install_docker RUN chmod +x /usr/local/bin/install_docker @@ -10,7 +12,7 @@ RUN bash /usr/local/bin/install_docker COPY docker /usr/bin/docker -ADD ./dind/wrapdocker /usr/local/bin/wrapdocker +COPY ./dind/wrapdocker /usr/local/bin/wrapdocker RUN chmod +x /usr/local/bin/wrapdocker @@ -22,6 +24,8 @@ COPY ./tor-router.sh /usr/local/bin/tor-router COPY ./new_ip.sh /usr/local/bin/new-ip +COPY ./haproxy-config.php /opt/haproxy-config.php + RUN chmod -v +x /usr/local/bin/stop-tor-router RUN chmod -v +x /usr/local/bin/start-tor-router diff --git a/Makefile b/Makefile index e015275..0857212 100644 --- a/Makefile +++ b/Makefile @@ -12,8 +12,5 @@ dind: docker: cp -aruv /usr/bin/docker docker -build: docker all - docker build -t znetstar/tor-router:0.0.1 . - install: - bash install.sh \ No newline at end of file + docker build -t znetstar/tor-router:0.0.1 . \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0267656 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +Creates multiple instances of Tor and switches between them in a round-robin fashion. + +Based on Docker, HAProxy and of course Tor + +Run using + ./run.sh + +Use the enviornment variable TOR_INSTANCES to set how many instances of Tor you'd like to run +Use the enviornment variable TOR_PORT to set the port you'd like to connect to. The TOR_PORT variable can also be an ip address and port (TOR_PORT=127.0.0.1:9050) + +By default TOR_INSTANCES is set to 5 and TOR_PORT is set to 0.0.0.0:9050 \ No newline at end of file diff --git a/haproxy-config.php b/haproxy-config.php new file mode 100644 index 0000000..ce602cc --- /dev/null +++ b/haproxy-config.php @@ -0,0 +1,28 @@ +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 + +listen socks *: + server :9050 check + mode tcp + balance roundrobin diff --git a/install.sh b/run.sh similarity index 62% rename from install.sh rename to run.sh index 9f4bdc2..70ae0df 100755 --- a/install.sh +++ b/run.sh @@ -1,7 +1,10 @@ #!/bin/bash +export TOR_INSTANCES=${TOR_INSTANCES:-5} +export TOR_PORT=${TOR_PORT:-9050} + apt-get update -y -apt-get install -yqq curl git tar +apt-get install -yqq curl git tar btrfs-tools echo 'installing docker...' @@ -27,7 +30,7 @@ apt-get install -y lxc-docker-1.5.0 # END: docker installer -echo 'running tor-router...' -/usr/bin/docker run --privileged -v /var/run/docker.sock:/var/run/docker.sock -p 9050:9050 --rm -it znetstar/tor-router:0.0.1 +echo 'starting up tor-router...' +/usr/bin/docker run --rm -it -e TOR_INSTANCES=$TOR_INSTANCES -e TOR_PORT=$TOR_PORT --name tor-router --privileged -v /tmp:/tmp -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker znetstar/tor-router:0.0.1 exit 0 \ No newline at end of file diff --git a/shutdown.sh b/shutdown.sh index 8d6ab5b..11624d6 100755 --- a/shutdown.sh +++ b/shutdown.sh @@ -10,12 +10,15 @@ do index=$[index+1] done -echo "stop haproxy" +echo "shutting down haproxy..." docker rm -f haproxy -echo "closing port" -iptables -A INPUT -p tcp --dport 9050 -j REJECT +echo 'removing files...' +rm -rf /tmp/haproxy.cfg +rm -rf /tmp/tor #sleep 5 +echo 'tor router has shut down' + exit 0 diff --git a/startup.sh b/startup.sh index 1eb9e13..5e93614 100755 --- a/startup.sh +++ b/startup.sh @@ -1,10 +1,9 @@ #!/bin/bash -export TEMP_HA_CONFIG=$(mktemp) -export 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" +export 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:9050 --DataDirectory /data/tor --ExcludeSingleHopRelays 0 --NewCircuitPeriod 30 --EnforceDistinctSubnets 0 --AllowDotExit 1' +index=0 +instances='' #docker -d & while [ $index -lt $TOR_INSTANCES ] @@ -15,61 +14,22 @@ do 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" znetstar/tor $TOR_CMD - echo "instance $current_instance created" + docker run -d -v /tmp/tor/$current_instance:/data --name $current_instance --restart="on-failure" znetstar/tor $TOR_CMD + echo 'created $current_instance' + instances="$instances --link $current_instance:$current_instance" + 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 +php /opt/haproxy-config.php > /tmp/haproxy.cfg -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:9050 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" +echo "started tor-router" +clear; +docker run --name haproxy -d -p $TOR_PORT:9050 $instances -v /tmp/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro haproxy:1.5.9 exit 0 \ No newline at end of file diff --git a/tor-router.sh b/tor-router.sh index 5d3ec6f..8a3e069 100755 --- a/tor-router.sh +++ b/tor-router.sh @@ -6,8 +6,8 @@ sleep 5 echo 'starting tor router...' /usr/local/bin/start-tor-router -docker kill haproxy -docker start -a -i haproxy +docker logs -f haproxy & +docker wait haproxy echo 'stopping tor router...' /usr/local/bin/stop-tor-router