Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def connect():
if args.proxy:
proto, _ = args.proxy.split("://")
if proto == "http":
default_headers = urllib3.make_headers(
proxy_basic_auth='%s:%s' % (args.username, args.password),
)
http_proxy = urllib3.ProxyManager(
args.proxy,
headers=default_headers
)
http = http_proxy.connection_from_url(args.uri["uri"])
elif proto in ["socks4", "socks5"]:
http = SOCKSProxyManager(args.proxy)
else:
logging.error("Invalid proxy protocol. It must start with 'http://' or "
"'socks[45]://'.")
exit(1)
else:
http = urllib3.connection_from_url(args.uri["uri"])
return http
# cert_file
cert_file = Configuration().cert_file
# key file
key_file = Configuration().key_file
# ssl_context
ssl_context = Configuration().ssl_context
# proxy
proxy = Configuration().proxy
# https pool manager
if proxy:
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=cert_file,
key_file=key_file,
ssl_context=ssl_context,
proxy_url=proxy
)
else:
self.pool_manager = urllib3.PoolManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=cert_file,
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()
addition_pool_args = {}
if configuration.assert_hostname is not None:
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
if maxsize is None:
if configuration.connection_pool_maxsize is not None:
maxsize = configuration.connection_pool_maxsize
else:
maxsize = 4
# https pool manager
if configuration.proxy:
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file,
proxy_url=configuration.proxy,
**addition_pool_args
)
else:
self.pool_manager = urllib3.PoolManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
def get_PoolManager():
"""Creates a pool manager with proxy support, if applicable.
Returns
-------
ProxyManager or PoolManager
ProxyManager if https_proxy is defined, PoolManager otherwise.
"""
if myproxy:
return urllib3.ProxyManager(myproxy, num_pools=1, headers=myheaders)
return urllib3.PoolManager(num_pools=1, headers=myheaders)
tmp_file = os.path.join(self.nvd_db_dir, 'temp_' + cve_year + '.zip')
# Download zip file (include cve list) and uncompress zip file.
target_json_name = ''
self.utility.write_log(20, 'Accessing : {}'.format(target_url))
self.utility.print_message(OK, 'Get {} CVE list from {}'.format(cve_year, target_url))
http = None
ctx = ssl.create_default_context()
ctx.set_ciphers('DEFAULT')
# ctx.set_ciphers('DEFAULT@SECLEVEL=1')
if self.utility.proxy != '':
self.utility.print_message(WARNING, 'Set proxy server: {}'.format(self.utility.proxy))
if self.utility.proxy_user != '':
headers = urllib3.make_headers(proxy_basic_auth=self.utility.proxy_user + ':' + self.utility.proxy_pass)
http = urllib3.ProxyManager(timeout=self.con_timeout,
headers=self.http_req_header,
proxy_url=self.utility.proxy,
proxy_headers=headers)
else:
http = urllib3.ProxyManager(timeout=self.con_timeout,
headers=self.http_req_header,
proxy_url=self.utility.proxy)
else:
http = urllib3.PoolManager(timeout=self.con_timeout,
headers=self.http_req_header,
ssl_version=ssl.PROTOCOL_TLS,
ssl_context=ctx)
try:
with http.request('GET', target_url, preload_content=False) as res, open(tmp_file, 'wb') as fout:
shutil.copyfileobj(res, fout)
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()
addition_pool_args = {}
if configuration.assert_hostname is not None:
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
if maxsize is None:
if configuration.connection_pool_maxsize is not None:
maxsize = configuration.connection_pool_maxsize
else:
maxsize = 4
# https pool manager
if configuration.proxy:
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file,
proxy_url=configuration.proxy,
**addition_pool_args
)
else:
self.pool_manager = urllib3.PoolManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()
addition_pool_args = {}
if configuration.assert_hostname is not None:
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
if maxsize is None:
if configuration.connection_pool_maxsize is not None:
maxsize = configuration.connection_pool_maxsize
else:
maxsize = 4
# https pool manager
if configuration.proxy:
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file,
proxy_url=configuration.proxy,
**addition_pool_args
)
else:
self.pool_manager = urllib3.PoolManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()
addition_pool_args = {}
if configuration.assert_hostname is not None:
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
if maxsize is None:
if configuration.connection_pool_maxsize is not None:
maxsize = configuration.connection_pool_maxsize
else:
maxsize = 4
# https pool manager
if configuration.proxy:
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file,
proxy_url=configuration.proxy,
**addition_pool_args
)
else:
self.pool_manager = urllib3.PoolManager(
num_pools=pools_size,
maxsize=maxsize,
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
self.utility.print_message(OK, 'Get {} CVE list from {}'.format(cve_year, target_url))
http = None
ctx = ssl.create_default_context()
ctx.set_ciphers('DEFAULT')
# ctx.set_ciphers('DEFAULT@SECLEVEL=1')
if self.utility.proxy != '':
self.utility.print_message(WARNING, 'Set proxy server: {}'.format(self.utility.proxy))
if self.utility.proxy_user != '':
headers = urllib3.make_headers(proxy_basic_auth=self.utility.proxy_user + ':' + self.utility.proxy_pass)
http = urllib3.ProxyManager(timeout=self.con_timeout,
headers=self.http_req_header,
proxy_url=self.utility.proxy,
proxy_headers=headers)
else:
http = urllib3.ProxyManager(timeout=self.con_timeout,
headers=self.http_req_header,
proxy_url=self.utility.proxy)
else:
http = urllib3.PoolManager(timeout=self.con_timeout,
headers=self.http_req_header,
ssl_version=ssl.PROTOCOL_TLS,
ssl_context=ctx)
try:
with http.request('GET', target_url, preload_content=False) as res, open(tmp_file, 'wb') as fout:
shutil.copyfileobj(res, fout)
except Exception as e:
self.utility.print_exception(e, 'Access is failure : {}'.format(target_url))
self.utility.write_log(30, 'Accessing is failure : {}'.format(target_url))
with zipfile.ZipFile(tmp_file, 'r') as downloaded_zip:
if req['proxy_type'] == 'socks5':
if req['verify']:
pool = SOCKSProxyManager(
proxy_url,
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
)
else:
pool = SOCKSProxyManager(proxy_url)
elif req['proxy_type'] == 'http':
if req['proxy_auth']:
proxy_headers = make_headers(proxy_basic_auth=req['proxy_auth'])
else:
proxy_headers = None
if req['verify']:
pool = ProxyManager(
proxy_url,
proxy_headers=proxy_headers,
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
)
else:
pool = ProxyManager(
proxy_url,
proxy_headers=proxy_headers,
)
else:
raise error.IowebConfigError(
'Invalid value of request option `proxy_type`: %s'
% req['proxy_type']
)
if use_cache: