6 |
7 |
8 | This is a python script that allow you to:
9 |
10 | * Check if a proxy is alive.
11 | * Check if it's a socks4/5 proxy.
12 | * Save runtime any working proxy.
13 |
14 | ***
15 |
16 | # Requirements #
17 |
18 | * Python 2.7 or higher ( Not tested with Python3 it may require some edits).
19 |
20 | # How to Run it #
21 |
22 | * Open terminal and move to the directory where you saved it.
23 | * Type 'python checker.py'.
24 | * Fill in required fields and you're good to go.
25 |
26 | # More #
27 |
28 | You can find me on:
29 |
30 | * [Holeboards](www.holeboards.eu)
31 | * [Telegram](www.telegram.me/eigoog)
32 |
--------------------------------------------------------------------------------
/checker.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # -*- coding: utf-8 -*-
3 | import urllib2 # Network lib
4 | import threading # Threading lib
5 | import socket # Socket lib
6 | import sys # System lib
7 | import time # Time lib
8 | import os # Os lib
9 | #Don't create .pyc
10 | sys.dont_write_bytecode = True
11 |
12 | #Colours
13 | red="\033[1;31m"
14 | green="\033[1;32m"
15 | yellow="\033[1;33m"
16 | blue="\033[1;34m"
17 | defcol = "\033[0m"
18 |
19 | def error(msg):
20 | print (red+"["+yellow+"!"+red+"] - "+defcol+msg)
21 |
22 | def alert(msg):
23 | print (red+"["+blue+"#"+red+"] - "+defcol+ msg)
24 |
25 | def action(msg):
26 | print (red+"["+green+"+"+red+"] - "+defcol+msg)
27 |
28 | def errorExit(msg):
29 | sys.exit(red+"["+yellow+"!"+red+"] - "+defcol+"Fatal - "+ msg)
30 |
31 | def get(text):
32 | return raw_input(red+"["+blue+"#"+red+"] - "+defcol+text)
33 |
34 | def saveToFile(proxy):
35 | with open(outputfile,'a') as file:
36 | file.write(proxy+"\n")
37 |
38 | def isSocks(host, port, soc):
39 | proxy = host+":"+port
40 | try:
41 | if(socks5(host,port,soc)):
42 | action("%s is socks5." % proxy)
43 | return True
44 | if(socks4(host,port,soc)):
45 | action("%s is socks4." % proxy)
46 | return True
47 |
48 | except socket.timeout:
49 | alert("Timeout during socks check: " + proxy)
50 | return False
51 | except socket.error:
52 | alert("Connection refused during socks check: " + proxy)
53 | return False
54 |
55 | def socks4(host, port, soc): # Check if a proxy is Socks4 and alive
56 | ipaddr = socket.inet_aton(host)
57 | packet4 = "\x04\x01" + pack(">H",port) + ipaddr + "\x00"
58 | soc.sendall(packet4)
59 | data = soc.recv(8)
60 | if(len(data)<2):
61 | # Null response
62 | return False
63 | if data[0] != "\x00":
64 | # Bad data
65 | return False
66 | if data[1] != "\x5A":
67 | # Server returned an error
68 | return False
69 | return True
70 | def socks5(host, port, soc): # Check if a proxy is Socks5 and alive
71 | soc.sendall("\x05\x01\x00")
72 | data = soc.recv(2)
73 | if(len(data)<2):
74 | # Null response
75 | return False
76 | if data[0] != "\x05":
77 | # Not socks5
78 | return False
79 | if data[1] != "\x00":
80 | # Requires authentication
81 | return False
82 | return True
83 |
84 | def isAlive(pip,timeout): # Check if a proxy is alive
85 | try:
86 | proxy_handler = urllib2.ProxyHandler({'http': pip}) # Setup proxy handler
87 | opener = urllib2.build_opener(proxy_handler)
88 | opener.addheaders = [('User-agent', 'Mozilla/5.0')] # Some headers
89 | urllib2.install_opener(opener) # Install the opener
90 | req=urllib2.Request('http://www.google.com') # Make the request
91 | sock=urllib2.urlopen(req,None,timeout=timeout) # Open url
92 | except urllib2.HTTPError, e: # Catch exceptions
93 | error(pip+" throws: "+str(e.code))
94 | return False
95 | except Exception, details:
96 | error(pip+" throws: "+str(details))
97 | return False
98 | return True
99 |
100 | def checkProxies():
101 | while len(toCheck) > 0:
102 | proxy = toCheck[0]
103 | toCheck.pop(0)
104 | alert("Checking %s" % proxy)
105 |
106 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
107 | s.settimeout(timeout)
108 |
109 | host = proxy.split(":")[0]
110 | port = proxy.split(":")[1]
111 | if int(port) < 0 or int(port) > 65536:
112 | error("Invalid port for " + proxy)
113 | continue
114 | if(isSocks(host, port, s)):
115 | socks.append(proxy)
116 | saveToFile(proxy)
117 | else:
118 | alert("%s not a working socks 4/5." % proxy)
119 | if(isAlive(proxy,timeout)):
120 | action("Working http/https proxy found (%s)!" % proxy)
121 | working.append(proxy)
122 | saveToFile(proxy)
123 | else:
124 | error("%s not working.")
125 | s.close()
126 |
127 |
128 |
129 |
130 |
131 | socks = []
132 | working = []
133 | toCheck = []
134 | threads = []
135 | checking = True
136 |
137 | proxiesfile = get("Proxy list: ")
138 | outputfile = get("Output file: ")
139 | threadsnum = int(get("Number of threads: "))
140 | timeout = int(get("Timeout(seconds): "))
141 | try:
142 | proxiesfile = open(proxiesfile,"r")
143 | except:
144 | errorExit(" Unable to open file: %s" % proxiesfile)
145 |
146 | for line in proxiesfile.readlines():
147 | toCheck.append(line.strip('\n'))
148 | proxiesfile.close()
149 |
150 | if(os.path.isfile(outputfile)):
151 | check = ''
152 | while check != 'yes' and check != 'y':
153 | error("Output file already exist, content will be overwritten!")
154 | check = get("Are you sure you would like to continue(y/n)?").lower()
155 | if check == 'n' or check == 'no':
156 | errorExit("Quitting...")
157 |
158 | for i in xrange(threadsnum):
159 | threads.append(threading.Thread(target=checkProxies))
160 | threads[i].setDaemon(True)
161 | action("Starting thread n: "+str(i+1))
162 | threads[i].start()
163 | time.sleep(0.25)
164 |
165 | action(str(threadsnum)+" threads started...")
166 | while checking:
167 |
168 | time.sleep(5)
169 | if len(threading.enumerate())-1 == 0:
170 | alert("All threads done.")
171 | action(str(len(working))+" alive proxies.")
172 | action(str(len(socks))+" socks proxies.")
173 | action(str(len(socks)+len(working))+" total alive proxies.")
174 | checking = False
175 | else:
176 | alert(str(len(working))+" alive proxies until now.")
177 | alert(str(len(socks))+" socks proxies until now.")
178 | alert(str(len(toCheck))+" remaining proxies.")
179 | alert(str(len(threading.enumerate())-1)+" active threads...")
180 |
--------------------------------------------------------------------------------