Пример #1
0
    def test_attrs(self):
        self.db.add_socks5("8.8.8.8",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=True,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        self.db.set_approx_bandwidth(1, 10.55)
        self.db.set_connect_time(1, 0.07)
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)

        assert s.id == 1
        assert s.host == "8.8.8.8"
        assert s.port == 1337
        assert s.country == "germany"
        assert s.city == "Frankfurt"
        assert s.country_code == "DE"
        assert s.username == "doge"
        assert s.password == "wow"
        assert s.description == "Such wow, many socks5"
        assert s.operational
        assert s.bandwidth == 10.55
        assert s.connect_time == 0.07
        assert s.last_use is None
        assert s.last_check is None
        assert isinstance(s.added_on, datetime.datetime)
Пример #2
0
    def test_measure_conn_time(self, ms):
        create_cwd(cwd())
        socksocket = mock.MagicMock()
        ms.socksocket.return_value = socksocket
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.measure_connection_time()

        assert isinstance(res, float)
        socksocket.set_proxy.assert_called_once_with(ms.SOCKS5,
                                                     "example.com",
                                                     1337,
                                                     username="******",
                                                     password="******")
        socksocket.settimeout.assert_called_once_with(3)
        socksocket.connect.assert_called_once_with(("api.ipify.org", 80))
        assert self.db.view_socks5(1).connect_time == res
Пример #3
0
    def acquire(self, country=None, country_code=None, city=None,
                min_mbps_down=None, max_connect_time=None, update_usage=True):
        """
        Acquire a socks5 server that was tested to be operational. The
        returned socks5 server will automatically be marked as used.
        Acquiring is in a round-robin fashion.

        :param country: Country the socks5 server should be in.
        :param country_code: 2-letter country code (ISO 3166-1 alpha-2).
        :param city: City the socks5 server should be in.
        :param min_mbps_down: The minimum average download speed in  mbits
            (float).
        :param max_connect_time: The maximum average connection time in seconds
            a socks5 server should have (float).
        :param update_usage: Mark retrieved socks5 as used. (bool).
        :return: A Socks5 object containing information about the server. None
            if no matching Socks5 server was found.
        :rtype: Socks5

        :Example:

        >>> from socks5man.manager import Manager
        >>> Manager().acquire(country="Germany")
        """
        db_socks5 = db.find_socks5(
            country=country, country_code=country_code, city=city,
            min_mbps_down=min_mbps_down, max_connect_time=max_connect_time,
            update_usage=update_usage
        )
        if db_socks5:
            return Socks5(db_socks5[0])
        else:
            return None
Пример #4
0
 def test_repr_nonauth(self):
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        description="Such wow, many socks5")
     s = self.db.view_socks5(1)
     socks5 = Socks5(s)
     assert repr(
         socks5
     ) == "<Socks5(host=example.com, port=1337, country=germany, authenticated=False)>"
Пример #5
0
    def list_socks5(self,
                    country=None,
                    country_code=None,
                    city=None,
                    host=None,
                    operational=None,
                    description=None):
        """Retrieve list of existing socks5 servers using the specified
        filters. This does not mark them as used. It only retrieves a list of
        matching servers. Returns an empty list if no matches were found.
        Returns all servers if no filters were provided.

        :param country: Country of a socks5 server
        :param country_code: 2-letter country code (ISO 3166-1 alpha-2)
        :param city: City of a socks5 server
        :param host: The host/IP of a socks5 server
        :param operational: Operational or not (bool).
            Is ignored if value is None
        :param description: socks server description
        :returns: A list of Socks5 objects containing the information of the
            matching servers.
        :rtype: list

        :example:

        >>> from socks5man.manager import Manager
        >>> Manager().list_socks5(country="united states")
        [
            <Socks5(host=example.com, port=1234, country=United States, authenticated=False)>,
            <Socks5(host=example.org, port=1234, country=United States, authenticated=False)>
        ]

        >>> Manager().list_socks5()
        [
            <Socks5(host=example.com, port=1234, country=United States, authenticated=False)>,
            <Socks5(host=example.org, port=1234, country=United States, authenticated=False)>,
            <Socks5(host=example.net, port=1234, country=Germany, authenticated=False)>
        ]
        """
        socks5s = db.list_socks5(
            country=country,
            country_code=country_code,
            city=city,
            host=host,
            operational=operational,
            description=description,
        )
        return [Socks5(s) for s in socks5s]
Пример #6
0
 def test_verify_hostname(self, mg):
     create_cwd(cwd())
     mg.return_value = b"93.184.216.34"
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     assert s.verify()
     db_socks5_2 = self.db.view_socks5(1)
     assert db_socks5_2.operational
Пример #7
0
 def test_verify_fail(self, mg):
     create_cwd(cwd())
     mg.return_value = None
     self.db.add_socks5("8.8.8.8",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=True,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     assert not s.verify()
     db_socks5_2 = self.db.view_socks5(1)
     assert not db_socks5_2.operational
Пример #8
0
 def test_verify_private(self, mg):
     create_cwd(cwd())
     mg.return_value = "8.8.8.8"
     self.db.add_socks5("192.168.0.50",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     assert s.verify()
     db_socks5_2 = self.db.view_socks5(1)
     assert db_socks5_2.operational
Пример #9
0
 def test_measure_conn_time_fail(self, ms):
     create_cwd(cwd())
     socksocket = mock.MagicMock()
     ms.socksocket.return_value = socksocket
     socksocket.connect.side_effect = socket.error
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     s.measure_connection_time() is None
     self.db.view_socks5(1).connect_time is None
Пример #10
0
 def test_approx_bandwidth_fail(self, ma):
     create_cwd(cwd())
     ma.return_value = None
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     res = s.approx_bandwidth()
     assert res == None
     db_socks5_2 = self.db.view_socks5(1)
     assert db_socks5_2.bandwidth is None
Пример #11
0
 def test_verify(self, mg):
     create_cwd(cwd())
     mg.return_value = b"8.8.8.8"
     self.db.add_socks5("8.8.8.8",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     assert s.verify()
     mg.assert_called_once_with("http://api.ipify.org",
                                "8.8.8.8",
                                1337,
                                username="******",
                                password="******",
                                timeout=3)
     db_socks5_2 = self.db.view_socks5(1)
     assert db_socks5_2.operational
Пример #12
0
 def test_socks5_to_dict(self):
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     s = self.db.view_socks5(1)
     socks5 = Socks5(s)
     d = socks5.to_dict()
     assert d["host"] == b"example.com"
     assert d["port"] == 1337
     assert d["country"] == b"germany"
     assert d["country_code"] == b"DE"
     assert d["city"] == b"Frankfurt"
     assert not d["operational"]
     assert d["username"] == b"doge"
     assert d["password"] == b"wow"
     assert d["description"] == b"Such wow, many socks5"
     assert d["added_on"] == socks5.added_on.strftime("%Y-%m-%d %H:%M:%S")
Пример #13
0
 def test_attrs_invalid(self):
     self.db.add_socks5("8.8.8.8",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=True,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     self.db.set_approx_bandwidth(1, 10.55)
     self.db.set_connect_time(1, 0.07)
     db_socks5 = self.db.view_socks5(1)
     db_socks5.host = ""
     db_socks5.country = ""
     db_socks5.city = ""
     db_socks5.username = ""
     db_socks5.description = ""
     s = Socks5(db_socks5)
     assert s.host is None
     assert s.country is None
     assert s.city is None
     assert s.username is None
     assert s.description is None
Пример #14
0
 def test_approx_bandwidth(self, ma):
     create_cwd(cwd())
     ma.return_value = 15.10
     self.db.add_socks5("example.com",
                        1337,
                        "germany",
                        "DE",
                        city="Frankfurt",
                        operational=False,
                        username="******",
                        password="******",
                        description="Such wow, many socks5")
     db_socks5 = self.db.view_socks5(1)
     s = Socks5(db_socks5)
     res = s.approx_bandwidth()
     assert res == 15.10
     ma.assert_called_once_with("example.com",
                                1337,
                                username="******",
                                password="******",
                                times=2,
                                timeout=10)
     db_socks5_2 = self.db.view_socks5(1)
     assert db_socks5_2.bandwidth == 15.10
Пример #15
0
def verify_all(repeated=False, operational=None, unverified=None):

    last_bandwidth = None
    bandwidth_checked = False
    download_verified = False
    if repeated:
        log.info("Starting continuous verification")

    while True:
        socks5_list = db.list_socks5(
            operational=operational, unverified=unverified
        )
        log.debug("Verifying %s socks5 servers", len(socks5_list))
        for socks5 in socks5_list:
            socks5 = Socks5(socks5)

            log.info(
                "Testing socks5 server: '%s:%s'", socks5.host, socks5.port
            )
            if socks5.verify():
                log.info("Operationality check: OK")
            else:
                log.warning(
                    "Operationality check (%s:%s): FAILED",
                    socks5.host, socks5.port
                )
                continue

            if cfg("connection_time", "enabled"):
                con_time = socks5.measure_connection_time()
                if con_time:
                    log.debug("Measured connection time: %s", con_time)
                else:
                    log.warning(
                        "Connection time measurement failed for: '%s:%s'",
                        socks5.host, socks5.port
                    )
                    continue

            if cfg("bandwidth", "enabled"):
                print "BLABLA 2"
                if last_bandwidth:
                    waited = time.time() - last_bandwidth
                    if waited < cfg("socks5man", "bandwidth_interval"):
                        continue

                if not download_verified:
                    download_url = cfg("bandwidth", "download_url")
                    try:
                        urllib2.urlopen(download_url, timeout=5)
                        download_verified = True
                    except (socket.error, urllib2.URLError) as e:
                        log.error(
                            "Failed to download speed test file: '%s'. Please"
                            " verify the configured file is still online!"
                            " Without this file, it is not possible to"
                            " approximate the bandwidth of a socsk5 server."
                            " Error: %s", download_url, e
                        )
                        continue

                bandwidth_checked = True
                bandwidth = socks5.approx_bandwidth()
                if bandwidth:
                    log.debug(
                        "Approximate bandwidth: %s Mbit/s down", bandwidth
                    )
                else:
                    log.warning(
                        "Bandwidth approximation test failed for: '%s:%s'",
                        socks5.host, socks5.port
                    )

        if bandwidth_checked:
            bandwidth_checked = False
            last_bandwidth = time.time()

        if not repeated:
            break

        time.sleep(cfg("socks5man", "verify_interval"))