ramesh.mju has asked for the wisdom of the Perl Monks concerning the following question:

Hi Team, Can you help me to understand the logic behind the below script. ===========

#!/usr/bin/perl use lib "/opt/proxypac"; # # Use mod_perl and Net::Netmask # Strict and warnings are recommended. #use strict; use warnings; use Net::Netmask; print "Content-type: application/x-ns-proxy-autoconfig\n\n"; # initialisation - only need to do it once. Skip it if the data is alr +eady resident in memory if (!$specialRoutingFile) { $base = "/opt/proxypac/"; #ln either cross-use-n.txt or cross-use-a.txt to cross-use.txt based o +n which side this runs on $crossusefile = $base."cross-use.txt"; $defaultfile = $base."default.txt"; $specialRoutingFile = $base."special-routing.txt"; $netRoutingFile = $base."net-routing.txt"; $dataXFile = $base."data-x.txt"; $proxyXFile = $base."proxy-x.txt"; $version = "2.1"; #loading and parsing default.txt open($file, "< $defaultfile") or die "Could not open file default.txt $!"; $DefaultProxy=<$file>; chomp $DefaultProxy; # $block = new Net::Netmask ("0.0.0.0/0"); # $block->tag("region1", $DefaultProxy); # $block->storeNetblock(); close($file); #loading and parsing proxy-x.txt open($file, "< $proxyXFile") or die "Could not open file proxy-x.txt $!"; while (<$file>){ chomp; chop if (m/\r$/); (my $region, my $proxy, my $proxy2) = split("#"); if ($proxy2) {$Proxies{"$region"}=$proxy.";PROXY ".$proxy2;} else {$Proxies{"$region"}=$proxy;} } #while close($file); #loading and parsing data-x.txt open($file, "< $dataXFile") or die "Could not open file data-x.txt $!"; while (<$file>){ chomp; chop if (m/\r$/); (my $dataXNet, my $region1, my $region2) = split("#"); # use default if a proxy reference is invalid $region1 = $DefaultProxy unless ($Proxies{"$region1"}); if ($region2 && !$Proxies{"$region2"}) {$region2 = $DefaultProxy;} $block = new Net::Netmask ($dataXNet); $block->tag("region1", $region1); $block->tag("region2", $region2); $block->storeNetblock(); } #while close($file); # build the exception header $pac = <<"EOF"; if ( shExpMatch(host, "localhost") ) { return "DIRECT"; } if ( shExpMatch(host, "localhost.*") ) { return "DIRECT"; } if ( shExpMatch(host, "127.0.0.*") ) { return "DIRECT"; } if ( isPlainHostName(host) ) { return "DIRECT"; } if ( isInNet(myIpAddress(), "10.255.32.0", "255.255.240.0")) { return + "DIRECT"; } EOF { open(my $file, "< $crossusefile") or die "Could not open file $crossusefile $!"; local $/; $pac.=<$file>; } #loading and parsing special-routing.txt open($file, "< $specialRoutingFile") or die "Could not open file special-routing.txt $!"; while (<$file>){ chomp; chop if /\r$/; (my $webServer, my $dnsq, my $proxy, my $proxy2) = split(" "); if(!($proxy eq "DIRECT")){ if ($proxy eq "PROXY") { $proxy="proxy";} else { if ($proxy2) { $proxy="\"PROXY $proxy;PROXY $proxy2\"";} else { $proxy="\"PROXY $proxy\"";} } } else {$proxy="\"".$proxy."\"";} if ($dnsq eq "Y") { $dnsq="dnsResolve(host+'.prx.nsn-intra.net');"; } else { $dnsq=""; } if ( $webServer =~ /^\..*/) { $pac .= <<EOF; if ( dnsDomainIs(host, "$webServer") ) { $dnsq return $proxy; } EOF } elsif ( $webServer =~ /.*\*.*/) { $pac .= <<EOF; if (shExpMatch(host,"$webServer") ) { $dnsq return $proxy; } EOF } else{ $pac .= <<EOF; if (host == "$webServer") { $dnsq return $proxy; } EOF } } close($file); $pac .= "\n"; #---------------------------------- #loading and parsing net-routing.txt open($file, "< $netRoutingFile") or die "Could not open file net-routing.txt $!"; $pac .= <<EOF; if (isResolvable(host)) { resolved_host = dnsResolve(host); EOF while (<$file>){ chomp; chop if /\r$/; (my $webHost, my $webHostMask, my $proxy) = split(";"); if ($proxy eq "PROXY") { $proxy="proxy";} elsif(!($proxy eq "DIRECT")){ $proxy= "\"PROXY ".$proxy."\""; } else {$proxy="\"DIRECT\"";} $pac .= <<EOF; if ( isInNet(resolved_host, "$webHost","$webHostMask") ) { return $pr +oxy; } EOF } close($file); } #if - end of initialisation ### main code ### $ipAddr = $ENV{'REMOTE_ADDR'}; # Check for test IP in query if ( $ENV{'QUERY_STRING'} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) +{ $ipAddr = $ENV{'QUERY_STRING'}; } # expire headers are handled by Apache #print "Cache-Control: max-age=1800\n"; #------------------ # print out the PAC print <<EOF; /* ** automatic proxy configuration V$version for $ipAddr from $ENV{'SERV +ER_NAME'} ($ENV{'SERVER_ADDR'}) */ EOF print "function FindProxyForURL(url, host) {\n var proxy=\""; $block = findNetblock($ipAddr); if ($block->base() eq "0.0.0.0") { print STDERR "IPAM:$ENV{'REMOTE_ADDR'}\n"; } if ($block->tag("region1") eq "DIRECT") { print "DIRECT"; } else { $proxy1 = $Proxies{$block->tag("region1")}; print "PROXY $proxy1;"; if (!($block->tag("region1") =~ /^GLB /) && ($block->tag("region2"))){ $proxy2 = $Proxies{$block->tag("region2")}; print " PROXY $proxy2;"; } } print "\";\n$pac}\n return proxy;\n}\n"; #for pilot only print STDERR "PILOT:$ENV{'REMOTE_ADDR'},$proxy1\n"; #close($file); exit 0;

============

Replies are listed 'Best First'.
Re: Know the logic behind a script.
by daxim (Curate) on Jul 25, 2019 at 12:30 UTC
Re: Know the logic behind a script.
by poj (Abbot) on Jul 25, 2019 at 12:30 UTC

    The script uses data from text files in folder /opt/proxypac

    cross-use.txt
    data-x.txt
    default.txt
    net-routing.txt
    proxy-x.txt
    special-routing.txt
    

    to dynamically build a javascript function FindProxyForURL(url, host) something like this

    Content-type: application/x-ns-proxy-autoconfig /* ** automatic proxy configuration V$version for $ipAddr from $ENV{'SERV +ER_NAME'} ($ENV{'SERVER_ADDR'}) */ function FindProxyForURL(url, host) { var proxy="PROXY $proxy1; PROXY $proxy2"; if ( shExpMatch(host, "localhost") ) { return "DIRECT"; } if ( shExpMatch(host, "localhost.*") ) { return "DIRECT"; } if ( shExpMatch(host, "127.0.0.*") ) { return "DIRECT"; } if ( isPlainHostName(host) ) { return "DIRECT"; } if ( isInNet(myIpAddress(), "10.255.32.0", "255.255.240.0")) { retu +rn "DIRECT"; } if ( dnsDomainIs(host, "$webServer") ) { $dnsq return $proxy; } if (shExpMatch(host,"$webServer") ) { $dnsq return $proxy; } if (host == "$webServer") { $dnsq return $proxy; } if (isResolvable(host)) { resolved_host = dnsResolve(host); if ( isInNet(resolved_host, "$webHost","$webHostMask") ) { return + $proxy; } } return proxy }

    see Proxy_auto-config. Which part do you not understand ?

    poj
Re: Know the logic behind a script.
by Eily (Monsignor) on Jul 25, 2019 at 12:10 UTC

    No.

    But if you have a more specific question, and can point to which part you don't understand we might be able to help. Granted you actually have the right to share that script.