Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/http'
- require 'mysql2'
- require_relative 'thread_pool.rb'
- ##########################################################################
- #
- # Checks if all proxies marked working in database are actually working
- #
- # Multithreaded to 10 threads at once using thread pool with worker queue
- # and Mutex locks to ensure 1 thread per worker block
- #
- ##########################################################################
- def proxy_checker()
- ######## First let's connect to that sexy ass MySQL database ##############
- $proxy_database = Mysql2::Client.new(:host => 'localhost', :username => 'carpanda', :password => 'alexcarpanda')
- $proxy_database.query("USE proxy_database")
- #CREATE TABLE proxy_table (id INTEGER PRIMARY KEY NOT NULL, ip TEXT, port TEXT, alive_boolean BOOL);
- #inet_aton and inet_ntoa to convert ip to integer
- if $proxy_database
- puts "Successfully connected to proxy database"
- else
- puts "Bad connection to proxy databse, exiting..."
- return
- end
- ############## Define a single proxy checking instance that checks a few proxies at once ###########
- def proxy_checking_instance(ip, port)
- #puts "Checking #{ip}:#{port}"
- attempt = 1
- begin
- Net::HTTP::Proxy(ip, port).start('makeabank.com') { |http|
- response = http.request_get('/faq.cgi');
- response.body.split(/\n/).each do |line|
- if (line.index('AnonyLevel')) then print "GOOD PROXY: ",ip,":",port,"\n\n" end
- end
- }
- rescue Errno::ECONNREFUSED
- puts "connection refused (attempt ##{attempt})"
- if attempt < 3 then
- attempt+=1
- retry
- end
- rescue
- print "BAD PROXY: ",ip,":",port,"\n\n"
- $proxy_database.query("UPDATE proxy_table SET alive_boolean=0 WHERE id=inet_aton('#{ip}')")
- end
- end
- ############## Setup Threads for concurrent proxy checking #######################
- proxy_thread_pool = ThreadPool.new(10)
- proxyList = $proxy_database.query("SELECT * FROM proxy_table where alive_boolean=true")
- proxyList.each do |proxy|
- print "Currently checking: ", proxy['ip'],"\n"
- proxy_thread_pool.process{proxy_checking_instance(proxy['ip'],proxy['port'])}
- end
- proxy_thread_pool.join()
- end
- proxy_checker()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement