Debugging PAC File Issues in Chrome: myIpAddress() Challenges and Alternative Solutions


2 views

When working with Proxy Auto-Config (PAC) files, many developers encounter inconsistent behavior with the myIpAddress() function across different browsers. Chrome's implementation has particular quirks that differ from IE and Firefox:

// Example PAC file that works in IE/Firefox but fails in Chrome
function FindProxyForURL(url, host) {
    var localIP = myIpAddress();
    if (isInNet(host, "192.168.1.0", "255.255.255.0")) {
        return "DIRECT";
    }
    return "PROXY proxy.example.com:8080";
}

Chrome removed several traditional debugging methods for PAC files:

  • No more alert() function support
  • Limited logging in chrome://net-internals
  • Inconsistent PROXY_SCRIPT_DECIDER event reporting

Here are practical approaches to debug PAC issues in Chrome:

1. Using the JavaScript Console

Access chrome://net-internals/#events and filter for PAC-related events:

// In Chrome's address bar:
chrome://net-internals/#events?q=type:PROXY_SCRIPT_DECIDER%20is:active

2. Testing myIpAddress() Behavior

Create a test PAC file to understand your IP detection:

function FindProxyForURL(url, host) {
    // Return the detected IP as proxy address (will show in net-internals)
    return "PROXY " + myIpAddress() + ":9999";
}

3. Alternative IP Detection Methods

Consider these more reliable approaches than myIpAddress():

function getLocalIP() {
    try {
        // Try DNS resolution of local hostname
        var hostname = dnsResolve('hostname.local');
        if (hostname) return hostname;
    } catch(e) {}
    
    // Fallback to getting first non-local IP
    var ips = [
        dnsResolve(''),
        dnsResolve('localhost'),
        myIpAddress()
    ].filter(Boolean);
    
    return ips.find(ip => !/^(127\.|::1|fe80::)/.test(ip)) || '127.0.0.1';
}

Launch Chrome with special flags for deeper proxy debugging:

chrome.exe --log-net-log="C:\temp\net_log.json" --v=1

This generates detailed network logs including PAC script execution.

Always validate your PAC file across multiple browsers:

// Example cross-browser compatible PAC file
function FindProxyForURL(url, host) {
    var localIP = getLocalIP(); // Using our custom function
    if (isInNet(host, "192.168.1.0", "255.255.255.0") || 
        shExpMatch(host, "*.corp.internal")) {
        return "DIRECT";
    }
    return "PROXY proxy.example.com:8080";
}

While Chrome's PAC debugging capabilities are limited compared to other browsers, combining these techniques should help diagnose most proxy configuration issues. The key is to understand Chrome's specific implementation of myIpAddress() and work around its limitations with custom IP detection logic.


When working with Proxy Auto-Configuration (PAC) files in Chrome, developers often hit roadblocks due to the browser's restrictive debugging environment. Unlike IE/Firefox which support alert() debugging, Chrome requires alternative approaches.

The core issue stems from Chrome's IPv6-first resolution strategy. The myIpAddress() function frequently returns IPv6 addresses or incorrect interfaces:

function FindProxyForURL(url, host) {
    // Problematic implementation:
    var ip = myIpAddress();
    if (isInNet(ip, "10.0.0.0", "255.0.0.0")) {
        return "PROXY internal-proxy:8080";
    }
    return "DIRECT";
}

1. Chrome Net Internals Logging

Access these debugging URLs:

  • chrome://net-export/ - Capture network logs
  • chrome://net-internals/#proxy - Verify active PAC script

2. PAC Script Validation

Test your PAC logic with this enhanced diagnostic function:

function debugPAC() {
    var debugInfo = "";
    debugInfo += "URL: " + url + "\n";
    debugInfo += "Host: " + host + "\n";
    
    try {
        var ip = myIpAddress();
        debugInfo += "myIpAddress(): " + ip + "\n";
        // Add more diagnostics as needed
    } catch(e) {
        debugInfo += "Error: " + e.message + "\n";
    }
    
    // Output to console via DNS trick (works with --log-net-log)
    dnsResolve("debug-" + debugInfo.length + ".pac");
    return debugInfo;
}

3. Alternative IP Detection

Use this workaround for reliable IP detection:

function getMyIP() {
    try {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://ifconfig.me/ip", false);
        xhr.send();
        return xhr.responseText.trim();
    } catch(e) {
        return myIpAddress(); // Fallback
    }
}

Enable verbose logging when launching Chrome:

chrome.exe --log-net-log=netlog.json --v=1

Key log filters to apply:

cat netlog.json | grep -E 'PROXY_SCRIPT|PAC_JAVASCRIPT'

Create a test harness for PAC validation:

function testPacScript(pacScript, testCases) {
    var sandbox = {
        myIpAddress: function() { return "192.168.1.1"; },
        dnsResolve: function(host) { return "8.8.8.8"; },
        // Mock other PAC functions
    };
    
    var proxyForURL = new Function('url', 'host', pacScript);
    
    testCases.forEach(function(test) {
        var result = proxyForURL.call(sandbox, test.url, test.host);
        console.log(test.url + " => " + result);
    });
}

Example test case:

testPacScript(
    'function FindProxyForURL(url,host){return myIpAddress().startsWith("192")?"PROXY valid":"DIRECT";}',
    [{url:"http://example.com", host:"example.com"}]
);