コード例 #1
0
ファイル: test_httputil.py プロジェクト: bogdanm13/tunapy
	def test_makeRelative_no_subpages(self):
		httpRequest = HttpRequest.buildFromBuffer("""GET HTTP://www.google.ro HTTP/1.1\r
User-Agent: curl/7.29.0\r
Host: www.google.ro\r
Accept: */*\r
Proxy-Connection: Keep-Alive\r\n\r\n""")
		httpRequest.makeRelative()
		self.assertEquals("/", httpRequest.requestedResource)
コード例 #2
0
ファイル: test_httputil.py プロジェクト: bogdanm13/tunapy
    def test_makeRelative_no_subpages(self):
        httpRequest = HttpRequest.buildFromBuffer(
            """GET HTTP://www.google.ro HTTP/1.1\r
User-Agent: curl/7.29.0\r
Host: www.google.ro\r
Accept: */*\r
Proxy-Connection: Keep-Alive\r\n\r\n""")
        httpRequest.makeRelative()
        self.assertEquals("/", httpRequest.requestedResource)
コード例 #3
0
    def work(self):
        Worker.work(self)
        while self.running:
            client = self.connectRequestsQueue.get()
            self.say("New client: %s " % client)
            proxy = self.__getProxy()
            if proxy:
                self.say("Forwarding to next proxy: %s" % str(proxy))
                self.forwardingQueue.put(Connection(client, proxy).reduce())
                continue
            else:  # direct connection
                self.say("No proxy found, making direct connection")
                client.rebuild()
                buf = client.socket.recv(BUFFER_SIZE)
                self.say("Received %s from %s " % (buf, client))
                if not buf:
                    client.shutdown()
                    continue
                httpRequest = HttpRequest.buildFromBuffer(buf)
                if httpRequest.requestType == "CONNECT":
                    host, port = httpRequest.requestedResource.split(":")
                    self.say("Tunneling to: %s:%s" % (host, port))
                    try:
                        server = Endpoint.connectTo(Address(host, port))
                    except socket.error, why:
                        sys.stderr.write(why.message + "\n")
                        client.shutdown()
                        continue

                    client.socket.sendall(
                        "HTTP/1.1 200 Connection established\r\nProxy-Agent: TunaPy/0.1\r\n\r\n"
                    )
                    self.forwardingQueue.put(
                        Connection(client, server).reduce())
                    continue
                else:
                    httpRequest.makeRelative()
                    host = httpRequest.options['Host']
                    port = 80
                    address = Address(host, port)
                    self.say('Sending to %s' % address)
                    try:
                        server = Endpoint.connectTo(address)
                        # resend the client HTTP request to the server
                        self.say("Sending: %s" % httpRequest.toBuffer())
                        server.socket.sendall(httpRequest.toBuffer())
                    except socket.error, why:
                        sys.stderr.write('An error occurred:\n%s\n' %
                                         why.message)
                        client.shutdown()
                        continue

                    self.say("Proxying queue size: %d" %
                             self.proxyingQueue.qsize())
                    conn = Connection(client, server).reduce()
                    self.proxyingQueue.put(conn)
コード例 #4
0
ファイル: test_httputil.py プロジェクト: bogdanm13/tunapy
    def setUp(self):
        self.buffer = """GET http://www.xulescu.ro/new/proiectele-mele HTTP/1.1\r
Host: www.serbanradulescu.ro\r
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0\r
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r
Accept-Language: en-US,en;q=0.5\r
Accept-Encoding: gzip, deflate\r
Connection: keep-alive\r
Cookie: __utma=256668681.8295899.1354958517.1354961422.1354992154.3; __utmz=256668681.1354958517.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=256668681.6.10.1354992154; __utmc=256668681\r
Cache-Control: max-age=0\r\n\r\n"""

        self.httpRequest = HttpRequest.buildFromBuffer(self.buffer)
コード例 #5
0
ファイル: test_httputil.py プロジェクト: bogdanm13/tunapy
	def setUp(self):
		self.buffer ="""GET http://www.xulescu.ro/new/proiectele-mele HTTP/1.1\r
Host: www.serbanradulescu.ro\r
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0\r
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r
Accept-Language: en-US,en;q=0.5\r
Accept-Encoding: gzip, deflate\r
Connection: keep-alive\r
Cookie: __utma=256668681.8295899.1354958517.1354961422.1354992154.3; __utmz=256668681.1354958517.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=256668681.6.10.1354992154; __utmc=256668681\r
Cache-Control: max-age=0\r\n\r\n"""

		self.httpRequest = HttpRequest.buildFromBuffer(self.buffer)
コード例 #6
0
ファイル: workers.py プロジェクト: bogdanm13/tunapy
	def work(self):
		Worker.work(self)
		while self.running:
			client = self.connectRequestsQueue.get()
			self.say("New client: %s " % client)
			proxy = self.__getProxy()
			if proxy:
				self.say("Forwarding to next proxy: %s" % str(proxy))
				self.forwardingQueue.put( Connection(client, proxy).reduce())
				continue
			else: # direct connection
				self.say("No proxy found, making direct connection")
				client.rebuild()
				buf = client.socket.recv(BUFFER_SIZE)
				self.say("Received %s from %s " % (buf ,client))
				if not buf:
					client.shutdown()
					continue
				httpRequest = HttpRequest.buildFromBuffer(buf)
				if httpRequest.requestType == "CONNECT":
					host, port = httpRequest.requestedResource.split(":")
					self.say("Tunneling to: %s:%s" % (host, port))
					try:
						server = Endpoint.connectTo(Address(host, port))
					except socket.error, why:
						sys.stderr.write(why.message + "\n")
						client.shutdown()
						continue

					client.socket.sendall("HTTP/1.1 200 Connection established\r\nProxy-Agent: TunaPy/0.1\r\n\r\n")
					self.forwardingQueue.put( Connection(client, server).reduce())
					continue
				else:
					httpRequest.makeRelative()
					host = httpRequest.options['Host']
					port = 80
					address = Address(host, port)
					self.say('Sending to %s' % address)
					try:
						server = Endpoint.connectTo(address)
						# resend the client HTTP request to the server
						self.say("Sending: %s" % httpRequest.toBuffer())
						server.socket.sendall(httpRequest.toBuffer())
					except socket.error, why:
						sys.stderr.write('An error occurred:\n%s\n' % why.message)
						client.shutdown()
						continue

					self.say("Proxying queue size: %d" % self.proxyingQueue.qsize())
					conn = Connection(client, server).reduce()
					self.proxyingQueue.put(conn)
コード例 #7
0
ファイル: workers.py プロジェクト: bogdanm13/tunapy
	def _processBuffer(self, readable, buf):
		conn = self.socket2conn[readable]
		if conn.client.socket is readable:
			httpRequest= HttpRequest.buildFromBuffer(buf)
			address = Address(httpRequest.options['Host'], 80)

			if address.host is not None and (address.host, address.port) != conn.server.address:
				# observed behaviour was that a client may try to reuse a connection but with a different server
				# when this is the case the old server connection is replaced with the new one
				self.say("New connection requested to %s from %s" % (address,conn))
				self._removeConnection(conn)
				conn.server.shutdown()
				try:
					conn.server= Endpoint.connectTo(address)
				except socket.error, why:
					sys.stderr.write("Failed to setup connection to %s, reason: %s" % (address,why) )
					return
				self._addConnection(conn)
			httpRequest.makeRelative()
			conn.server.socket.sendall(httpRequest.toBuffer())
コード例 #8
0
    def _processBuffer(self, readable, buf):
        conn = self.socket2conn[readable]
        if conn.client.socket is readable:
            httpRequest = HttpRequest.buildFromBuffer(buf)
            address = Address(httpRequest.options['Host'], 80)

            if address.host is not None and (
                    address.host, address.port) != conn.server.address:
                # observed behaviour was that a client may try to reuse a connection but with a different server
                # when this is the case the old server connection is replaced with the new one
                self.say("New connection requested to %s from %s" %
                         (address, conn))
                self._removeConnection(conn)
                conn.server.shutdown()
                try:
                    conn.server = Endpoint.connectTo(address)
                except socket.error, why:
                    sys.stderr.write(
                        "Failed to setup connection to %s, reason: %s" %
                        (address, why))
                    return
                self._addConnection(conn)
            httpRequest.makeRelative()
            conn.server.socket.sendall(httpRequest.toBuffer())