Skip to content

Instantly share code, notes, and snippets.

@brutuscat
Last active July 29, 2025 16:05
Show Gist options
  • Save brutuscat/2519840 to your computer and use it in GitHub Desktop.
Save brutuscat/2519840 to your computer and use it in GitHub Desktop.

Revisions

  1. brutuscat revised this gist Nov 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion haproxy.cfg.erb
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,4 @@ backend tors
    <% tors_count.times.each do |c| %>
    server tor<%= c %> localhost:<%= delegated_base_port + c %>
    <% end %>
    balance roundrobin
    balance leastconn
  2. brutuscat revised this gist Apr 28, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    1 - Install monit, haproxy, tor and delegated
    0 - Read http://blog.databigbang.com/running-your-own-anonymous-rotating-proxies/
    1 - Install monit, haproxy, tor and delegated.
    2 - Setup your environment in the setup.rb file
    3 - Just run > ruby setup.rb
    4 - ...........
  3. brutuscat revised this gist Apr 28, 2012. 2 changed files with 6 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    1 - Install monit, haproxy, tor and delegated
    2 - Setup your environment in the setup.rb file
    3 - Just run > ruby setup.rb
    4 - ...........
    5 - PROFIT! > http://www.southparkstudios.com/clips/151040/the-underpants-business
    1 change: 1 addition & 0 deletions monitrc.erb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # monitrc template file
    set daemon 60
    set pidfile <%= install_path %>/pids/monit.pid
    # gmail or google apps account
  4. brutuscat revised this gist Apr 28, 2012. 2 changed files with 83 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions monitrc.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    set daemon 60
    set pidfile <%= install_path %>/pids/monit.pid
    # gmail or google apps account

    <% if gmail_username && gmail_password %>
    set mailserver smtp.gmail.com port 587
    username "<%= gmail_username %>" password "<%= gmail_password %>"
    using tlsv1 with timeout 60 seconds
    <% end %>
    set mail-format { from: <%= email %> }
    set alert <%= email %> with reminder on 10 cycles
    # Monit web panel
    set httpd port 44102
    allow admin:"password"

    # tor
    <% tors_count.times.each do |c| %>
    check process tor<%= c %> with pidfile <%= install_path %>/pids/tor<%= c %>.pid
    group proxy
    start program = "/bin/sh -c '<%= tor_path %> --RunAsDaemon 1 --CookieAuthentication 0 --PidFile <%= install_path %>/pids/tor<%= c %>.pid --SocksPort <%= tor_base_port + c %> --DataDirectory <%= install_path %>/caches/tor<%= c %>'" as uid <%= user %> and gid <%= user %>
    stop program = "/bin/sh -c '/bin/kill -s SIGTERM `cat <%= install_path %>/pids/tor<%= c %>.pid` && rm \"<%= install_path %>/pids/tor<%= c %>.pid\"'" as uid <%= user %> and gid <%= user %>
    if mem > 32 MB for 3 cycles then restart
    if cpu > 30% for 5 cycles then restart
    <% end %>

    # delegated
    <% tors_count.times.each do |c| %>
    check process delegated<%= c %> with pidfile <%= install_path %>/pids/delegated<%= c %>.pid
    group proxy
    start program = "/bin/sh -c '<%= delegated_path %> -P<%= delegated_base_port + c %> SERVER=http SOCKS=localhost:<%= tor_base_port + c %> PIDFILE=<%= install_path %>/pids/delegated<%= c %>.pid LOGFILE=<%= install_path %>/log/delegated.log ADMIN=<%= email %>'" as uid <%= user %> and gid <%= user %>
    stop program = "/bin/sh -c '/bin/kill -s SIGTERM `cat <%= install_path %>/pids/delegated<%= c %>.pid` && rm \"<%= install_path %>/pids/delegated<%= c %>.pid\"'" as uid <%= user %> and gid <%= user %>
    if mem > 8 MB for 3 cycles then restart
    if cpu > 30% for 5 cycles then restart
    depends on tor<%= c %>
    <% end %>

    # haproxy
    check process haproxy with pidfile <%= install_path %>/pids/haproxy.pid
    group proxy
    start program = "/bin/sh -c '<%= haproxy_path %> -f <%= install_path %>/etc/haproxy.cfg -p <%= install_path %>/pids/haproxy.pid'" as uid <%= user %> and gid <%= user %>
    stop program = "/bin/sh -c '/bin/kill `cat <%= install_path %>/pids/haproxy.pid` && rm \"<%= install_path %>/pids/haproxy.pid\"'" as uid <%= user %> and gid <%= user %>
    if mem > 8 MB for 3 cycles then restart
    if cpu > 40% for 5 cycles then restart
    depends on <%= tors_count.times.map{ |c| "delegated#{c}" }.join(', ') %>

    # monitrc to restart monit itself
    check file monitrc path <%= install_path %>/etc/monitrc
    if changed md5 checksum
    then exec "<%= monit_path %> reload"
    34 changes: 34 additions & 0 deletions setup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    require 'erb'

    # Setup variables
    monit_path = "/usr/bin/monit"
    tor_path = "/usr/bin/tor"
    delegated_path = "/usr/local/bin/delegated"
    haproxy_path = "/usr/local/bin/haproxy"
    install_path = "/Users/mauroasprea/rotating-tors"
    user = "username"
    email = "[email protected]" # where moint and delegated will send mails.
    tors_count = 4
    haproxy_port = 3128
    delegated_base_port = 3129
    tor_base_port = 9050
    tor_control_base_port = 8118

    # Optional: setup gmail smtp for monit outgoing alerts
    gmail_username = "[email protected]"
    gmail_password = "password"

    # Prepare paths
    `mkdir -p #{install_path}/etc`
    `mkdir -p #{install_path}/caches`
    `mkdir -p #{install_path}/pids`

    # haproxy.cfg
    c = ERB.new(File.read("haproxy.cfg.erb"))
    File.write("#{install_path}/etc/haproxy.cfg", c.result(binding))
    `chmod 600 #{install_path}/etc/haproxy.cfg`

    # monitrc
    c = ERB.new(File.read('monitrc.erb'))
    File.write("#{install_path}/etc/monitrc", c.result(binding))
    `chmod 600 #{install_path}/etc/monitrc`
  5. brutuscat created this gist Apr 28, 2012.
    21 changes: 21 additions & 0 deletions haproxy.cfg.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    global
    daemon
    maxconn 256

    defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

    frontend rotatingproxies
    bind *:<%= haproxy_port %>
    default_backend tors
    option http_proxy

    backend tors
    option http_proxy
    <% tors_count.times.each do |c| %>
    server tor<%= c %> localhost:<%= delegated_base_port + c %>
    <% end %>
    balance roundrobin