Skip to content

Instantly share code, notes, and snippets.

@tokibito
Created September 25, 2011 06:24
Show Gist options
  • Save tokibito/1240297 to your computer and use it in GitHub Desktop.
Save tokibito/1240297 to your computer and use it in GitHub Desktop.
import socket
def get_host_name(s):
for line in s.splitlines():
if line.startswith('Host:'):
return line[6:]
def host_to_ip(s):
return socket.getaddrinfo(s, 80)[0][4][0]
def main():
s_srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s_srv.bind(('127.0.0.1', 4000))
s_srv.listen(1)
try:
while True:
conn, addr = s_srv.accept()
print '---connected---'
data = conn.recv(1024)
print data
print '-----client------'
# proxy client
hostname = get_host_name(data)
if ':' in hostname:
host, port = hostname.split(':')
port = int(port)
else:
host = hostname
port = 80
ip = host_to_ip(host)
s_cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print (ip, port)
s_cli.connect((ip, port))
s_cli.send(data)
content = s_cli.recv(10240)
s_cli.close()
print content
print '-----proxy-------'
conn.send(content.replace('test page', 'hack page'))
conn.close()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment