python - HTTPS proxy CONNECT issue -


i trying make proxy works https requests. proxy want make needs tunnel , packets , don't disturb them. tried make connection between browser , proxy , browser sends connect request google example , made new socket creates own connect request same host , port google example returns bad request method. should do?

i want make simple tunneling proxy https using httplib , sockets. here's code:

import socket 

from packet import * import httplib

class sockclient:

def __init__(self,sock,addr):     print "[*]new client has joined! ["+str(addr)+"]"     self.__sock = sock     self.__addr = str(addr)     self.__target = socket.socket(socket.af_inet,socket.sock_stream)     self.__open = true  def process(self , socklist , clientlist):     print "[*]receiving data. ["+self.__addr+"]"      try:         data = self.__sock.recv(1024)     except:         print "***wtf browser***"         print "[*]closing connection target. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1      print "[*]data received. ["+self.__addr+"]"     print "[*]data:\r\n" + data     print "[*]analyzing data. ["+self.__addr+"]"      if data == "":         print "[*]client closed connection. ["+self.__addr+"]"         print "[*]closing connections target. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1      print "[*]creating packet. ["+self.__addr+"]"     packet = packet(data)     print "[*]packet created. ["+self.__addr+"]"     print "[*]connecting target. ["+self.__addr+"]"      if self.__open:             self.__target.connect((packet.gethost() , packet.getport()))             if packet.getrequest() == "connect":                 #send 200 ok packet client , closes connections.                 self.__sock.send("http/1.1 200 connection established\r\nproxy-agent: gladis\r\n\r\n")                 print "send 200 ok plz"                 print "[*]closing connections client , target. ["+self.__addr+"]"                 print "------------------------------------------------- ["+self.__addr+"]"                 self.__target.close()                 self.__sock.close()                 socklist.remove(self.__sock)                 clientlist.remove(self)                 self.__open = true                 return 1       '''     try:         if self.__open:             if packet.getport() == 443:                 self.__target = httplib.httpsconnection(packet.gethost(),443)                 if packet.getrequest() == "connect":                     #send 200 ok packet client , closes connections.                     self.__target.connect()                     resp = self.__target.getresponse()                     print resp.status , resp.reason                     page = resp.read()                     print page                     self.__sock.send("http/1.1 200 ok\r\ncontent-length: "+str(len(page))+"\r\n\r\n" + page)                     print "send 200 ok plz"                     print "[*]closing connections client , target. ["+self.__addr+"]"                     print "------------------------------------------------- ["+self.__addr+"]"                     self.__target.close()                     self.__sock.close()                     socklist.remove(self.__sock)                     clientlist.remove(self)                     self.__open = true                     return 1              else:                 self.__target.connect((packet.gethost() , packet.getport()))         self.__open = false     except:         print "***couldnt make connection target***"         print "[*]closing connections client. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1     '''      print "[*]connection estabished. ["+self.__addr+"]"     print "[*]sending packet target. ["+self.__addr+"]"      try:         self.__target.send(packet.getpacket())     except:         print "[*]sending packet failed. ["+self.__addr+"]"         print "[*]closing connections client. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1      print "[*]packet sent. ["+self.__addr+"]"     print "[*]waiting response. ["+self.__addr+"]"      try:         reply = self.__target.recv(8192)     except:         print "***error in recv***"         print "[*]closing connections client , server. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1      print "[*]reply received. ["+self.__addr+"]"     print "[*]reply:\r\n"+ reply     print "[*]analyzing reply. ["+self.__addr+"]"      if reply == "":         print "[*]target closed connection. ["+self.__addr+"]"         print "[*]closing connection client. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1       print "[*]sending reply client. ["+self.__addr+"]"      try:         self.__sock.send(reply)     except:         print "***error in send***"         print "[*]closing connection target. ["+self.__addr+"]"         print "------------------------------------------------- ["+self.__addr+"]"         self.__target.close()         self.__sock.close()         socklist.remove(self.__sock)         clientlist.remove(self)         self.__open = true         return 1     print "[*]reply sent. ["+self.__addr+"]"      print "[*]closing connections client , server[done!]. ["+self.__addr+"]"     self.__target.close()     self.__sock.close()     socklist.remove(self.__sock)     clientlist.remove(self)     print "------------------------------------------------- ["+self.__addr+"]"  def getsock(self):     return self.__sock 

the connect method used between client , proxy make proxy create tunnel, i.e specifies final target of tunnel. once tcp connection server given in connect request established proxy sends positive response connect request. after data between client , proxy transferred server , back. connect request not forwarded server.

for exact details please see rfc 2817.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -