1

I'm tryig to make a proxy pac file for my Squid. I suppose to change redirection if the user is in my network or if he is at home for example, and I try to make it whith the myAdress() function.

I have test this PAC, whith most of function that we can use in a PAC : http://findproxyforurl.com/debug-pac-file/ .

function FindProxyForURL(url, host) {

   debugPAC ="PAC Debug Information\n";
   debugPAC +="-----------------------------------\n";
   debugPAC +="Machine IP: " + myIpAddress() + "\n";        <-----|
   debugPAC +="Hostname: " + host + "\n";
   if (isResolvable(host)) {resolvableHost = "True"} else {resolvableHost = "False"};
    debugPAC +="Host Resolvable: " + resolvableHost + "\n";
    debugPAC +="Hostname IP: " + dnsResolve(host) + "\n";
    if (isPlainHostName(host)) {plainHost = "True"} else {plainHost = "False"};
    debugPAC +="Plain Hostname: " + plainHost + "\n";
    debugPAC +="Domain Levels: " + dnsDomainLevels(host) + "\n";
    debugPAC +="URL: " + url + "\n";

    // Protocol can only be determined by reading the entire URL.
    if (url.substring(0,5)=="http:") {protocol="HTTP";} else
        if (url.substring(0,6)=="https:") {protocol="HTTPS";} else
           if (url.substring(0,4)=="ftp:") {protocol="FTP";}
                else {protocol="Unknown";}
    debugPAC +="Protocol: " + protocol + "\n";

    // Reduce volume of alerts to a useable level, e.g. only alert on static text pages.
    if (!shExpMatch(url,"*.(js|xml|ico|gif|png|jpg|jpeg|css|swf)*")) {alert(debugPAC);}

   return "DIRECT";
}

But on the output, I have ipv6 address ?!

PAC-alert: PAC Debug Information
-----------------------------------
Machine IP: fe80::xxx:xxx:xxxx:xxxx        <-----|
Hostname: download.cdn.mozilla.net
Host Resolvable: True
Hostname IP: 93.184.221.133
Plain Hostname: False
Domain Levels: 3
URL:     http://download.cdn.mozilla.net/pub/firefox/releases/37.0.2/update/win32/fr/firefox-37.0.2.complete.mar
Protocol: HTTP

Is that normal ? Or there another method for getting ipv4 address of the user ? If so, I can not make test like :

if ( isInNet(myAddress, "10.0.0.0","255.0.0.0") )   ?

Thanks for your help

user63946
  • 15
  • 2
  • 6

1 Answers1

1

The myIpAddress function is based on the assumption that a host has only a single address. This has never been a valid assumption.

A better alternative would be a function returning a list of IP addresses. It appears Microsoft has introduced their own extension doing exactly that.

It would make sense for myIpAddress to return the address providing the most useful information. However you cannot rely on that. There are reports about myIpAddress sometimes returning 127.0.0.1 which is mostly useless.

In your case it clearly didn't make an optimal choice either, because a link-local address contains less useful information for a PAC script than a local or global address would. And I am guessing that in your case the host does have at least one local or global address it could be returning instead.

Overall my best recommendation is to write FindProxyForURL such that it doesn't need to know the IP address of the host (or have the server serving the PAC script embed the IP address of the client into the script through server side scripting).

If a significant fraction of your users run a browser with support for Microsoft's extension you can also add a FindProxyForURLEx function, which takes advantage of myIPAddressEx

It is also not advisable to use dnsResolve in a PAC script due to possibly blocking the browser while a DNS resolution is happening.

kasperd
  • 29,894
  • 16
  • 72
  • 122
  • Thanks a lot. Did that mean _myAddress_ can't know with interface use ? And how can I identify if a user is in my network or not ? – user63946 May 12 '15 at 11:19
  • @user63946 A host can have multiple physical interfaces as well as multiple virtual interfaces, and each interface can have multiple IP addresses. That's a lot of addresses to chose from, and `myIpAddress` has no information telling it which of the addresses is relevant to the PAC script. However the webserver serving the PAC script will know which IP address the client connected from, and it can dynamically serve different script contents based on that. – kasperd May 12 '15 at 11:25
  • ok, so _myAddress()_ is a little usseless :/. And PAC file too in my use case. Thanks again. I'm going to look for another solution from client side with js. – user63946 May 12 '15 at 11:52
  • 1
    If you are running newer browsers there are new IPv6 capable functions that return a list of IPs myIPAddressEx() See http://blogs.msdn.com/b/wndp/archive/2006/07/18/ipv6-wpad-for-winhttp-and-wininet.aspx – Zoredache May 12 '15 at 16:47
  • 1
    @Zoredache That's useful information. So far it seems to be an IE specific extension. I found open bugs against both [Chrome](https://code.google.com/p/chromium/issues/detail?id=162721) and [Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=558253) to implement support for it. – kasperd May 12 '15 at 17:36