Link to home
Start Free TrialLog in
Avatar of Yashy
YashyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Can you help me understand this script?

Hi guys,

This is a great code that I found on the internet. I'm having difficulty undrestanding what happens from where it says //Actions? I know there are some comments, but I'd like to really understand it by line?

Any help would be awesome.


function FindProxyForURL(url, host) {

    // SETTINGS

    // your proxy hostname (myproxy.mynetwork.local)

    var proxyHostname = "${asg_hostname}";

    var proxyPort = 8080;



    // regex patterns to exclude from proxy (put your internal networks here)

    var directRegexPatterns = [

        "*.local/*",

        "*.lan/*",

        "*192.168.0.*",

        "*172.16.*",

        "*172.30.0.*"

    ];



    // networks that should use proxies with optional proxy to use override (put your internal networks that should be proxied here)

    var nets = [

        { addr: "172.16.0.0", subnet: "255.255.0.0" },

        { addr: "172.30.0.0", subnet: "255.255.255.0", proxy: "172.30.0.1:" + proxyPort },

        { addr: "192.168.0.0", subnet: "255.255.255.0" }

    ];



    var p = "DIRECT";

    var defaultproxyurl = "PROXY " + proxyHostname + ":" + proxyPort;





    // ACTIONS



    //Don't proxy connections to the proxy web interface

    if (shExpMatch(url, "https://${asg_hostname}*")) { p = "DIRECT"; }

    else if (shExpMatch(url, "https://" + dnsResolve(host) + "*")) { p = "DIRECT"; }

    //Exclude non-fqdn hosts from being proxied

    else if (isPlainHostName(host)) { p = "DIRECT"; }

    else {

        var hasRegexMatch = false;



        // check proxy exclusion regex patterns

        for (var i = 0; i < directRegexPatterns.length; i++) {

            var pattern = directRegexPatterns[i];

            if (shExpMatch(url, pattern)) {

                p = "DIRECT";

                hasRegexMatch = true;

                break;

            }

        }



        if (!hasRegexMatch) {

            // check if client is in proxy network

            var ipstr = "";

            if (typeof myIpAddressEx === "undefined") {

                alert("myIpAddressEx is undefined!"); // this will print to a specific log in the browser

                ipstr = myIpAddress(); // only one ip, not all, but FF does not support the "Ex" version...

            } else {

                ipstr = myIpAddressEx(); // IP1;IP2;IP3

            }



            var ips = ipstr.split(";");

            for (var j = 0; j < nets.length; j++) {

                var net = nets[j];

                for (var i = 0; i < ips.length; i++) {

                    var ip = ips[i];

                    if (isInNet(ip, net.addr, net.subnet)) {

                        var proxyToUse = defaultproxyurl;

                        if(net.proxy){

                            proxyToUse = "PROXY " + net.proxy;

                        }

                        p = proxyToUse;

                        // alert("found " + proxyToUse + " because: " + ip + " is in net " + net.addr + " / " + net.subnet);

                        break;

                    }

                }

            }

        }

    }



    return p;

}
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yashy

ASKER

As always Michel - thank you. Really helpful. 
Avatar of Yashy

ASKER

Michel - if I wanted to have two proxies rather than one, are you able to help me add that? For example, imagine if I wanted to add the below (this is just a small snippet but a lot of my code uses either one proxy or the other) to the code above (one has a port 8080, another with port 3128, with different networks. I know that I would have to define a variable for that, like proxyPort2 = 3128. I just don't know how to make it part of the code?). I'm happy to create a new question.

if (isInNet(myIpAddress(), "10.200.40.128","255.255.255.128"))
return "PROXY proxy-gs2:8080";

if (isInNet(myIpAddress(), "10.19.0.0","255.255.0.0"))
return "PROXY 10.19.0.250:3128;



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, your are correct
Avatar of Yashy

ASKER

Michel - thanks again for the above. If I had a load of other network ranges, instead of entering them manually, can the IP ranges be defined first, such as in the 'var nets' section? 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yashy

ASKER

Legend, thank you again. 
Sorry, missed a comma between the } {