8

In Firefox internet connection is made through a proxy auto configuration file something.pac.

How do I know for a certain URL which proxy server is being used?

yivi
  • 42,438
  • 18
  • 116
  • 138
KirdApe
  • 83
  • 1
  • 1
  • 3
  • Sorry for not being clearer. It didn't worked because javascript errors isInNet is not defined. – KirdApe Jun 22 '10 at 12:48
  • Just a note, I had to find the proxy server from the pac file, and this tool: http://code.google.com/p/pacparser/ worked just fine. – Ayusman May 09 '12 at 00:52
  • The google-link now just forwards to [this github page](https://github.com/manugarg/pacparser). You can also consider this (online!) tool: https://app.thorsen.pm/proxyforurl. Furthermore, you can also just view the contents of `something.pac`, it's just plain text. – Cadoiz Dec 02 '21 at 08:00

2 Answers2

7

.pac file is just an ECMAscript - aka JavaScript. Check out the wikipedia article on the file format.

If you copy the PAC code you can process it to see what proxy is being used based on the target url. If you are feeling fancy, you can wrap the script into a web page (locally) to create a tool to evaluate locally.

Edit:

As an alternative to the method I started recommending, you might check out PACTester, available on Google Code. This will allow you to quickly test a range of options.

If you have .Net available and are interested in playing with C# then you can check out this article on MSDN which has code you can use in a similar fashion to the above.

To expand on the original method outlined above, there are a number of functions which may (and typically are) provided by the host browser. The basic function which must be implemented in a pac file is FindProxyForUrl(). This accepts two parameters: the url and the host (the host derived from the name of url). The "provided" functions include: isInNet(), localHostOrDomainIs(), isPlainHostName(), isResolvable(), etc.

If you are working in a Microsoft environment then you can check out this page on Technet which describes the .pac format with some useful examples.

Per the Microsoft documentation for isInNet():

The isInNet(host, pattern, mask) function returns TRUE if the host IP address matches the specified pattern. The mask indicates which part of the IP address to match (255=match, 0=ignore).

If you want to get technical, here is the Mozilla source code for the implementation of proxy auto-config related services. It specifies the JS code for isInNet() as:

200 function isInNet(ipaddr, pattern, maskstr) {
201     var test = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/(ipaddr);
202     if (test == null) {
203         ipaddr = dnsResolve(ipaddr);
204         if (ipaddr == null)
205             return false;
206     } else if (test[1] > 255 || test[2] > 255 ||
207                test[3] > 255 || test[4] > 255) {
208         return false;    // not an IP address
209     }
210     var host = convert_addr(ipaddr);
211     var pat  = convert_addr(pattern);
212     var mask = convert_addr(maskstr);
213     return ((host & mask) == (pat & mask));
214     
215 }

Hope that helps!

nullp01nter
  • 522
  • 4
  • 18
AJ.
  • 3,062
  • 2
  • 24
  • 32
  • 2
    I've downloaded it and added the .pac file to a local web page. I tried to debug with Firebug but there are certain unknown functions like myIpAddress() (which I created to return my ip) or isInNet(). – KirdApe Jun 21 '10 at 16:48
  • Hope the expanded answer helps - you might read more of the source on the mozilla link I've included - it will give you the other related functions (or links to find them). – AJ. Jun 24 '10 at 08:59
6

I've created simple HTML page resolving proxy:

<html>
<head>
    <script type="text/javascript">
    function myIpAddress() {
        return "192.168.1.2"; // Your IP
    }

    function isInNet(ipaddr, pattern, maskstr) {
        var test = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/(ipaddr);
        if (test == null) {
            ipaddr = dnsResolve(ipaddr);
            if (ipaddr == null) return false;
        } else if (test[1] > 255 || test[2] > 255 || test[3] > 255 || test[4] > 255) {
            return false;    // not an IP address
        }
        var host = convert_addr(ipaddr);
        var pat  = convert_addr(pattern);
        var mask = convert_addr(maskstr);
        return ((host & mask) == (pat & mask));
    }

    function dnsResolve(host) {
        try {
            return dns.resolve(host, 0).getNextAddrAsString();
        } catch (e) {
            return null;
        }
    }

    function convert_addr(ipchars) {
        var bytes = ipchars.split('.');
        var result = ((bytes[0] & 0xff) << 24) |
                     ((bytes[1] & 0xff) << 16) |
                     ((bytes[2] & 0xff) <<  8) |
                      (bytes[3] & 0xff);
        return result;
    }

    function isPlainHostName(host) {
        return host.search('\\\\.') == -1;
    }

    function shExpMatch(url, pattern) {
       pattern = pattern.replace(/\\./g, '\\\\.');
       pattern = pattern.replace(/\\*/g, '.*');
       pattern = pattern.replace(/\\?/g, '.');
       var newRe = new RegExp('^' + pattern + '$');
       return newRe.test(url);
    }

    function dnsDomainIs(host, domain) {
        return host.length >= domain.length && host.substring(host.length - domain.length) == domain;
    }
    </script>

    <!-- Your proxy script -->
    <script type="text/javascript" src="webproxy.js"></script>
</head>
<body>
    Host: <input id="host"/><br/>
    URL: <input id="url"/><br/>
    Proxy: <input id="proxy" disabled="disabled"/><br/>
    <input type="button" value="Resolve"
           onclick="document.getElementById('proxy').value = FindProxyForURL(document.getElementById('host').value, document.getElementById('url').value)"/><br/>
</body>
</html>

Code for myIpAddress etc I've got from mozilla sources.

Vitaliy Ulantikov
  • 10,157
  • 3
  • 61
  • 54
  • This looks great if I can get it to work...however putting an alert before and after this line `var test = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/(ipaddr);` it seem that this line breaks it...and its the same line that appears in the actual Mozilla file, so I guess its right but looking at this http://www.w3schools.com/js/js_obj_regexp.asp it seems incorrect. Changing the line to this `var test1 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i; var test = test1.exec(ipaddr);` seemed to do the trick...your thoughts? – user1135469 May 09 '13 at 06:34
  • On Windows the value for `myIpAddress()` is the IP returned by DOS command `ipconfig`? – Peter Krauss Jul 29 '19 at 18:31
  • Why it is a "Host and URL" form? I need to navigate over all Internet with the proxy... I use my client-IP, **how to fill Host and URL?** – Peter Krauss Jul 29 '19 at 18:56