Esempio n. 1
0
    def connect(self):
        self.type = "out"
        if self.ip_type == "onion":
            if not self.server.tor_manager or not self.server.tor_manager.enabled:
                raise Exception("Can't connect to onion addresses, no Tor controller present")
            self.sock = self.server.tor_manager.createSocket(self.ip, self.port)
        elif config.tor == "always" and helper.isPrivateIp(self.ip) and self.ip not in config.ip_local:
            raise Exception("Can't connect to local IPs in Tor: always mode")
        elif config.trackers_proxy != "disable" and self.is_tracker_connection:
            if config.trackers_proxy == "tor":
                self.sock = self.server.tor_manager.createSocket(self.ip, self.port)
            else:
                from lib.PySocks import socks
                self.sock = socks.socksocket()
                proxy_ip, proxy_port = config.trackers_proxy.split(":")
                self.sock.set_proxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, int(proxy_port))
        else:
            self.sock = helper.createSocket(self.ip)

        if "TCP_NODELAY" in dir(socket):
            self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        timeout_before = self.sock.gettimeout()
        self.sock.settimeout(30)
        if self.ip_type == "ipv6":
            sock_address = (self.ip, self.port, 1, 1)
        else:
            sock_address = (self.ip, self.port)

        self.sock.connect(sock_address)

        # Implicit SSL
        should_encrypt = not self.ip_type == "onion" and self.ip not in self.server.broken_ssl_ips and self.ip not in config.ip_local
        if self.cert_pin:
            self.sock = CryptConnection.manager.wrapSocket(self.sock, "tls-rsa", cert_pin=self.cert_pin)
            self.sock.do_handshake()
            self.crypt = "tls-rsa"
            self.sock_wrapped = True
        elif should_encrypt and "tls-rsa" in CryptConnection.manager.crypt_supported:
            try:
                self.sock = CryptConnection.manager.wrapSocket(self.sock, "tls-rsa")
                self.sock.do_handshake()
                self.crypt = "tls-rsa"
                self.sock_wrapped = True
            except Exception, err:
                if not config.force_encryption:
                    self.log("Crypt connection error: %s, adding ip %s as broken ssl." % (err, self.ip))
                    self.server.broken_ssl_ips[self.ip] = True
                self.sock.close()
                self.sock = helper.createSocket(self.ip_type)
                self.sock.settimeout(30)
                self.sock.connect(sock_address)
Esempio n. 2
0
    def actionCheckport(self, params):
        if helper.getIpType(self.connection.ip) == "ipv6":
            sock_address = (self.connection.ip, params["port"], 0, 0)
        else:
            sock_address = (self.connection.ip, params["port"])

        with closing(helper.createSocket(self.connection.ip)) as sock:
            sock.settimeout(5)
            if sock.connect_ex(sock_address) == 0:
                self.response({"status": "open", "ip_external": self.connection.ip})
            else:
                self.response({"status": "closed", "ip_external": self.connection.ip})
Esempio n. 3
0
 def getRandomPort(self, ip, port_range_from, port_range_to):
     self.log.info("Getting random port in range %s-%s..." % (port_range_from, port_range_to))
     tried = []
     for bind_retry in range(100):
         port = random.randint(port_range_from, port_range_to)
         if port in tried:
             continue
         tried.append(port)
         sock = helper.createSocket(ip)
         try:
             sock.bind((ip, port))
             success = True
         except Exception as err:
             self.log.warning("Error binding to port %s: %s" % (port, err))
             success = False
         sock.close()
         if success:
             self.log.info("Found unused random port: %s" % port)
             return port
         else:
             time.sleep(0.1)
     return False