carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 18 forks source link

PyPAC does not work with file url that uses IP and does not use domain #60

Closed carmomelo closed 2 years ago

carmomelo commented 3 years ago

Hello, I work in a company where the security team uses IP in the url of the proxy file instead of the domain, the justification for it is that it becomes more difficult to track the domain that is best known.

In this step above I came across a problem, when the pypac searches for the domain and does not find it, generating an error, with this I will prepare a workaround solution to be used when you have the name of the url in IP, however it would be good to embed this in my own lib, I am creating some libs to work safely in python and I would like to leave here my contribution to pypac, remembering that I will be able to use this function in other projects of mine on github.

this causes an error: pac = get_pac (url = 'http: //10.1.1.1/fileName.pac')

this works: pac = get_pac (url = 'http: //domain.com/fileName.pac')

Workaround for the error:

url_domain_pac = exchange_ip_by_domain ('http://10.1.1.1/fileName.pac') pac = get_pac (url = url_domain_pac)

Function of the solution described below: def exchange_ip_by_domain (proxy_url): import socket import re "" " Function that receives the wpac url with ip and after dns search translates to hostname with domain "" " proxy_ip = None pattern1 = re.compile ("(http | https): // (. *?) + /") match = pattern1.match (proxy_url) url_ip = match.group () pattern2 = re.compile (r '(\ d {1,3} . \ d {1,3} . \ d {1,3} . \ d {1,3})') try: proxy_ip = pattern2.search (url_ip) [0] except (TypeError, AttributeError): proxy_ip = None proxy_name = None try: data = socket.gethostbyaddr (proxy_ip) proxy_name = str (data [0]) except (socket.gaierror, socket.herror): proxy_name = None

print ("host_name", proxy_name)

if proxy_ip is not None and proxy_name is not None:
    proxy_url = proxy_url.replace (proxy_ip, proxy_name)
return proxy_url
carsonyl commented 3 years ago

When you call get_pac() and give it a URL with an IP host, what is the error you get? In this situation, I don't expect there to be any difference between an IP host and a domain host.

carmomelo commented 3 years ago

Hello, when I do this: pac = get_pac (url = 'http: //10.1.1.1/fileName.pac') result the value "pac" is None

When I continue the process I get this error in the request: requests.exceptions.ConnectionError: HTTPConnectionPool (host = '10 .1.1.1 ', port = 80) Max retries exceeded

carsonyl commented 2 years ago

This is a tricky issue to diagnose, but I suspect the problem is that http://10.1.1.1/fileName.pac is not serving the file with the required Content-Type header. By default, PyPAC requires the PAC-specific mimetype specified by the PAC standard. Check the doc for details.