Created
February 15, 2017 05:40
-
-
Save humbertocastelo/2b5f7817181249995e4919709157e39f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_socket_socks5(proxy_host, proxy_port, proxy_username, proxy_password, host, port): | |
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
sock.connect((proxy_host,proxy_port)) | |
fd = sock.makefile() | |
sock.send("\x05\x01\x02") | |
server_status = sock.recv(8192) | |
if server_status != "\x05\x02": | |
return False | |
sock.send(struct.pack('B', 0x01) + struct.pack('B', len(proxy_username)) + proxy_username + struct.pack('B', len(proxy_password)) + proxy_password) | |
server_buffer = sock.recv(8192) | |
if server_buffer != "\x01\x00": | |
return False | |
sock.send(struct.pack('B', 0x05) + struct.pack('B', 0x01) + struct.pack('B', 0x00) + struct.pack('B', 0x03) + struct.pack('B', len(host)) + host + struct.pack('>H', port)) | |
server_buffer = sock.recv(8192) | |
if server_buffer[0] != "\x05": | |
return False | |
if server_buffer[1] != "\x00": | |
return False | |
if server_buffer[2] != "\x00": | |
return False | |
if server_buffer[3] != "\x01": | |
return False | |
right = server_buffer[4:] | |
proxy_ip = right[0:len(right)-2] | |
proxy_port = right[-2:] | |
proxy_ip = base_convert(proxy_ip[0].encode('hex'), 16, 10) + "." + base_convert(proxy_ip[1].encode('hex'), 16, 10) + "." + base_convert(proxy_ip[2].encode('hex'), 16, 10) + "." + base_convert(proxy_ip[3].encode('hex'), 16, 10) | |
proxy_port = base_convert(proxy_port.encode('hex'), 16, 10) | |
return sock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment