Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding modular support for custom validators #128

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Next Next commit
adding modular support for validators
lmst2 committed Sep 28, 2020
commit 136143f5d6b0df5fea3a6d75f9f687edfbd38a12
13 changes: 13 additions & 0 deletions scylla/validator.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

from .loggings import logger
from .tcpping import ping
from .validators import *

IP_CHECKER_API = 'http://api.ipify.org/?format=json'
IP_CHECKER_API_SSL = 'https://api.ipify.org/?format=json'
@@ -83,9 +84,21 @@ def validate_proxy(self):
logger.debug('Catch requests.RequestException for proxy ip: {}'.format(self._host))
logger.debug(e.__str__())

def validate_proxy_through_validators(self):
protocol = 'https' if self._using_https else 'http'
proxy_str = '{}://{}:{}'.format(protocol, self._host, self._port)
validators = [validator() for validator in all_validators]

is_valid = all([_validator.validate(proxy_str) for _validator in validators])
if is_valid:
self._valid = True
else:
self._valid = False

def validate(self):
self.validate_latency()
self.validate_proxy()
self.validate_proxy_through_validators()

@property
def latency(self):
3 changes: 3 additions & 0 deletions scylla/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .bilibili_validator import BilibiliValidator

all_validators = [BilibiliValidator]
13 changes: 13 additions & 0 deletions scylla/validators/base_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


class BaseValidator(object):

def __init__(self):
pass

def __str__(self):
return self.__class__.__name__

def validate(self, proxy_str: str) -> bool:
raise NotImplementedError

23 changes: 23 additions & 0 deletions scylla/validators/bilibili_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
from .base_validator import BaseValidator
from ..loggings import logger


class BilibiliValidator(BaseValidator):

check_url: str = 'https://api.bilibili.com/'

def validate(self, proxy_str):
try:
r = requests.get(self.check_url, proxies={'https': proxy_str, 'http': proxy_str}, verify=False, timeout=15)
if not r.ok:
logger.debug('Catch {} for proxy ip: {} when connecting to {}'.format(r.status_code, proxy_str.split(':')[0], self.check_url))
return False
return True
except requests.Timeout:
logger.debug('Catch requests.Timeout for proxy ip: {} when connecting to {}'.format(proxy_str.split(':')[0], self.check_url))
return False
except requests.RequestException as e:
logger.debug('Catch requests.RequestException for proxy ip: {} when connecting to {}'.format(proxy_str.split(':')[0], self.check_url))
logger.debug(e.__str__())
return False