2009
05.12
05.12
To check if a HTTP proxy works, there’s a need, not only to check if it’s on-line, but also if it actually works as a proxy. To do that I developed a Python script that will check the user external IP address with a direct connection, and then through all the proxies in the list.txt file. If the script receives a valid response and that response reports a different IP address, then the proxy works.
Some proxies to test the script with.
119.70.40.101:8080
203.110.240.22:80
200.31.42.3:80
200.31.42.3:80
201.26.212.10:6588
124.53.159.169:80
The service that the script uses is provided by ShowMyIP, please do not abuse.
#!/usr/bin/python3.0
"""
Copyright (c) 2009 Duarte Silva
All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from threading import Thread
from queue import Queue
from http.client import HTTPConnection
from xml.dom.minidom import parseString
class HTTPProxyConnection(HTTPConnection):
def __init__(self, proxy, target, proxyport = None, strict = None, timeout = None):
HTTPConnection.__init__(self, proxy, proxyport, strict, timeout)
self.target = target
def request(self, method, url = None, body = None, headers = {}):
if url != None:
url = ''.join([ self.target, url ])
else:
url = self.target
HTTPConnection.request(self, method, url, body, headers)
class ShowMyIP:
def __init__(self):
self.information = {
'ip' : '',
'host' : '',
'country' : '',
}
def feed(self, data):
try:
dom = parseString(data)
for info in self.information:
elements = dom.getElementsByTagName(info)
if len(elements) == 0:
continue
data = ''
for node in elements[0].childNodes:
if node.nodeType == node.TEXT_NODE:
data = '{0}{1.data}'.format(data, node)
self.information[info] = data
except Exception as error:
raise Exception('An error occured while processing ShowMyIP {0}'.format(error))
class ProxyThread(Thread):
def run(self):
while True:
proxy = proxyqueue.get()
if proxy != None:
try:
conn = HTTPProxyConnection(proxy, 'http://www.showmyip.com', timeout = 60)
conn.request('GET', '/xml/')
response = conn.getresponse()
if response.status == 200:
showmyip = ShowMyIP()
showmyip.feed(response.read())
if showmyip.information['ip'] != '' and showmyip.information['ip'] != directip:
print('Proxy {0} seems to work:'.format(proxy))
for info in showmyip.information:
print('- {0}: {1}'.format(info, showmyip.information[info]))
goodfile.write("{0}n".format(proxy))
else:
raise Exception('Transparent proxy')
else:
raise Exception('Wrong response {0.status} {0.reason}'.format(response))
except Exception as error:
rejectfile.write('{0} - {1}n'.format(proxy, error))
finally:
conn.close()
proxyqueue.task_done()
directip = None
goodfile = open('good.txt', 'w', 1)
rejectfile = open('rejected.txt', 'w', 1)
proxyqueue = Queue()
def main():
try:
print('Retrieving current connection info...')
conn = HTTPConnection('www.showmyip.com', timeout = 60)
conn.request('GET', '/xml/')
response = conn.getresponse()
showmyip = ShowMyIP()
showmyip.feed(response.read())
conn.close()
print('Current connection info:')
for info in showmyip.information:
print('- {0}: {1}'.format(info, showmyip.information[info]))
directip = showmyip.information['ip']
print('Reading proxy list...')
proxyfile = open('list.txt', 'r')
proxylist = proxyfile.readlines()
proxyfile.close()
print('Creating worker threads...')
for i in range(100):
t = ProxyThread()
t.setDaemon(True)
t.start()
print('Enqueue proxies...')
for proxy in proxylist:
proxyqueue.put(proxy.strip('n'))
proxyqueue.join()
goodfile.close()
rejectfile.close()
except Exception as error:
print('Error in the main thread {0}'.format(error))
if __name__ == '__main__':
main()