Skip to content
New issue

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

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

Already on GitHub? # to your account

Added port filtering #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions proxybroker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, queue=None, timeout=8, max_conn=200, max_tries=3,
self._server = None
self._limit = 0 # not limited
self._countries = None
self._ports = None

max_concurrent_conn = kwargs.get('max_concurrent_conn')
if max_concurrent_conn:
Expand Down Expand Up @@ -96,21 +97,24 @@ def __init__(self, queue=None, timeout=8, max_conn=200, max_tries=3,
except NotImplementedError:
pass

async def grab(self, *, countries=None, limit=0):
async def grab(self, *, countries=None, ports=None, limit=0):
"""Gather proxies from the providers without checking.

:param list countries: (optional) List of ISO country codes
where should be located proxies
:param list ports: (optional) List of ports indicating
allowed proxy ports
:param int limit: (optional) The maximum number of proxies

:ref:`Example of usage <proxybroker-examples-grab>`.
"""
self._countries = countries
self._ports = ports
self._limit = limit
task = asyncio.ensure_future(self._grab(check=False))
self._all_tasks.append(task)

async def find(self, *, types=None, data=None, countries=None,
async def find(self, *, types=None, data=None, countries=None, ports=None,
post=False, strict=False, dnsbl=None, limit=0, **kwargs):
"""Gather and check proxies from providers or from a passed data.

Expand All @@ -126,6 +130,8 @@ async def find(self, *, types=None, data=None, countries=None,
:param list countries:
(optional) List of ISO country codes where should be located
proxies
:param list ports:
(optional) List of ports indicating allowed proxy ports
:param bool post:
(optional) Flag indicating use POST instead of GET for requests
when checking proxies
Expand Down Expand Up @@ -159,6 +165,7 @@ async def find(self, *, types=None, data=None, countries=None,
real_ext_ip=ip, types=types, post=post,
strict=strict, dnsbl=dnsbl, loop=self._loop)
self._countries = countries
self._ports = ports
self._limit = limit

tasks = [asyncio.ensure_future(self._checker.check_judges())]
Expand Down Expand Up @@ -306,6 +313,10 @@ async def _handle(self, proxy, check=False):
if not self._is_unique(proxy) or not self._geo_passed(proxy):
return

if self._ports and proxy.port not in self._ports:
proxy.log('Proxy port is not in allowed ports list')
return

if check:
await self._push_to_check(proxy)
else:
Expand Down
10 changes: 8 additions & 2 deletions proxybroker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def add_grab_args(group):
nargs='+',
help='List of ISO country codes where should be located proxies')

group.add_argument(
'--ports', '-p',
type=int,
nargs='+',
help='List of ports indicating allowed proxy ports')


def add_serve_args(group):
group.add_argument(
Expand Down Expand Up @@ -332,10 +338,10 @@ def cli(args=sys.argv[1:]):

if ns.command == 'find':
tasks.append(broker.find(
data=ns.data, types=ns.types, countries=ns.countries,
data=ns.data, types=ns.types, countries=ns.countries, ports=ns.ports,
post=ns.post, strict=ns.strict, dnsbl=ns.dnsbl, limit=ns.limit))
elif ns.command == 'grab':
tasks.append(broker.grab(countries=ns.countries, limit=ns.limit))
tasks.append(broker.grab(countries=ns.countries, ports=ns.ports, limit=ns.limit))
elif ns.command == 'serve':
broker.serve(
host=ns.host, port=ns.port, limit=ns.limit,
Expand Down