Esempio n. 1
0
    def connectionMade(self):
        self.transport.pauseProducing()

        client = self.clientProtocolFactory()
        client.setServer(self)

        if self.factory.use_relay == True:
            self.factory.reactor.connectTCP(self.factory.hop_host, self.factory.hop_port, client)
        else:
            self.factory.reactor.connectTCP(self.factory.final_host, self.factory.final_port, client)
Esempio n. 2
0
    def dataReceived(self, data):
        if not self.connected_to_end:
            # Counter
            global relayed_queries
            relayed_queries += 1

            # print data
            host = ""
            if data[0:2] == "##":
                host_start = 2
                host_end = host_start + data[host_start:].find("##")
                headers = data[host_start:host_end].split(",")
                final_host = headers[0]
                if self.check_autorized_host(final_host):
                    final_port = int(headers[1])
                    desired_hops = int(headers[2]) - 1
                    data = data[host_end + 2 :]
                    if desired_hops == 0:
                        host = final_host
                        port = final_port
                    else:
                        header = "##%s,%i,%i##" % (final_host, final_port, desired_hops)
                        data = header + data
                        host, port = get_relay_address()
                else:
                    self.logger.error("%s is not an autorized host", final_host)
                    self.transport.loseConnection()
                    return

            if host != "":
                self.logger.info("Relay to destination %s:%i with %i hop remaining", host, port, desired_hops)
                # Don't read anything from the connecting client until we have
                # somewhere to send it to.
                self.transport.pauseProducing()

                client = self.clientProtocolFactory()
                client.setServer(self)

                if self.reactor is None:
                    from twisted.internet import reactor

                    self.reactor = reactor
                self.reactor.connectTCP(host, port, client)
                self.first_data += data
                self.connected_to_end = True
            else:
                print "Error, invalid destination host defined"
                self.transport.loseConnection()

            data = ""

        Proxy.dataReceived(self, data)
        self.data_received_count += 1