Javascript PAC file and variables


 
Thread Tools Search this Thread
Top Forums Programming Javascript PAC file and variables
# 1  
Old 09-08-2010
Javascript PAC file and variables

Guys i'm new to javascript but have pulled together a PAC file based on requirements.

Problem is i'm not sure how javascript handles variables. I'm proficient with shell script, and a bit of perl.

I can't find any examples on the net of what i'm trying to do.

I want to return proxies which are set in variables depending on different conditions.

e.g

Code:
function FindProxyForURL(url,host)
{

// set chp1 and chp1 to the 2 xxxx proxies VIPS
// xxxx VIP
var chpv1="aa.bb.cc.dd:8080";
var chpv2="aa.bb.cc.dd:8080";

// Also set the real proxy P1 interfaces just in case
var chrp1="aa.bb.cc.dd:8080";
var chrp2="aa.bb.cc.dd:8080";
var shrp1="aa.bb.cc.dd:8080";

// Declare variables we use later
var proxyone;
var proxytwo;
var proxythree;
var proxyfour;

// Get client ip and store in var
var myip=myIpAddress();

// If client is unable to communicate with proxies, indicating out of office location,
// still allow connection to *.gov.uk sites by adding a fourth proxy as direct.
// 
// This does have the effect that if all proxies are unavailable client will attempt
// direct connection.  This will be denied by FW policy when in office and only
// allow connection to *.gov.uk sites if not.

// Not ideal due to PAC file timeouts, plus relies on office security to deny direct internet.
//
// Not done *.gov.uk* incase of *.gov.uk.scamdomain.com instances
if (shExpMatch(url, "*.gov.uk")) 
{
    var proxyfour="DIRECT";
}

// If ip is xxxxx range use proxy xxxxx first
// *** Correct IP range needed. Demo only ***
if ( (isInNet(myip, "10.10.10.0", "255.255.255.0")) || (isInNet(myip, "10.10.5.0", "255.255.255.0")) ) 
{
    var proxyone=shrp1;    // P1 Interface of xxxxx
    var proxytwo=chpv1;
    var proxythree=chpv2;
   
    // Return proxy order to stop processing

    // If 4th proxy defined (indicating *.gov.uk sites)
    if ( proxyfour != null ) {
        return "PROXY proxyone; PROXY proxytwo; PROXY proxythree; PROXY proxyfour";
    } else {
    // Not allowing direct attempt for any site
        return "PROXY proxyone; PROXY proxytwo; PROXY proxythree";
    } 
} 

// Now use main proxies based on odd/even

// find the 4th octet - 
var ipbits=myip.split(".");
var myseg=parseInt(ipbits[3]);

// If even use proxy1 first
if(myseg==Math.floor(myseg/2)*2) {
   var proxyone=chpv1;
   var proxytwo=chpv2;
   var proxythree=shrp1;
}
else {
// Must be odd use proxy2 first
   var proxyone=chpv2;
   var proxytwo=chpv1;
   var proxythree=shrp1;
}

// Return proxy order

// If 4th proxy defined 
if ( proxyfour == null ) {
    return "PROXY proxyone; PROXY proxytwo; PROXY proxythree; PROXY proxyfour";
} else {
    // Not allowing direct attempt for any site
    return "PROXY proxyone; PROXY proxytwo; PROXY proxythree";
}

} // End of FindProxyURL function.
// End

Problem is when testing using pacparser i'm just getting back a string "PROXY proxyone; PROXY proxytwo; PROXY proxythree" instead of it resolving the variables and replacing with the correct order aa.bb.cc.dd:8080.

I basically want to set a proxy order based on conditions and then just return it at the end.

Anyone any ideas?

---------- Post updated at 03:13 PM ---------- Previous update was at 12:09 PM ----------

I've fixed this with :-

Code:
// Return proxy order

// If 4th proxy defined
if ( proxyfour != null ) {
        //Need to expose vars outside quotes
        return "PROXY "+proxyone+"\; PROXY "+proxytwo+"\; PROXY "+proxythree+"\; PROXY "+proxyfour;
} else {
        // Not allowing direct attempt for any site
        //return "PROXY proxyone; PROXY proxytwo; PROXY proxythree";
        //Need to expose vars outside quotes
        return "PROXY "+proxyone+"\; PROXY "+proxytwo+"\; PROXY "+proxythree;
}

But there may be a better way to do it?? I'm still not sure if need to return the string PROXY or not as i've seen it with and without in some pac examples.

Arr well.
# 2  
Old 10-18-2010
Lavascript,

the following code will fail when using Firefox in wondows 7 and I think windows vista:

Code:
// Now use main proxies based on odd/even

// find the 4th octet - 
var ipbits=myip.split(".");
var myseg=parseInt(ipbits[3]);

// If even use proxy1 first
if(myseg==Math.floor(myseg/2)*2) {
   var proxyone=chpv1;
   var proxytwo=chpv2;
   var proxythree=shrp1;
}
else {
// Must be odd use proxy2 first
   var proxyone=chpv2;
   var proxytwo=chpv1;
   var proxythree=shrp1;
}

It fails because myip.split(".") will result in "ipbits" not being an array with 4 elements in it because Windows 7 (and I think Vista) returns an IPv6 address that has colons (Smilie as separators and the values are hex strings.

Try this instead:

Code:
	// split the IPv4 address into a 4 element array 
	var ipbits = myip.split(".");

	// make sure this is an IPv4 address, which is > 1
	if (ipbits.length > 1)
	{
		// we think we have an IPv4 address here
		// grab the 4th octet and convert it to a decimal
		var myseg = parseInt(ipbits[3]);
	} else {		
		// we have a potential IPv6 address here
		// split the IPv6 address into a 6 element array
		var ipbits = myip.split(":");

		if (ipbits.length > 1)
		{
			// looks like we have an IPv6 address here
			// grab the 6th octet and convert it from hex to decimal
			var myseg = parseInt(ipbits[5],16);
		} else {
			// NOT AN IPv6 either, so ODD it will be by us forcing it to a 1!!!
			var myseg = 1;
		}
	}


	// take the modulus 2 of the myseg where 1 = odd, 0 = even
	var mycalcseg = myseg % 2;

	if (mycalcseg == 1) {
		// ODD number (mycalcseg = 1)
		var proxyone = proxyONEip;
		var proxytwo = proxyTWOip;
	} else {
		// EVEN number (mycalcseg = 0)
		var proxyone = proxyTWOip;
		var proxytwo = proxyONEip;
	}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to call variables from a file to the scripting file?

Let say I have a file with variables (Test1.txt) In Test1.txt file, it consists of Tom is a boy Jim is a dog In the other scripting file (RunTest1.sh), I have #!/bin/ksh filename = /directory/Test1.txt cat $filename for i in $filename do print $i done I managed to call... (1 Reply)
Discussion started by: TestKing
1 Replies

2. IP Networking

Help with PAC file

Hello All , I need hello all , i need help in creating a pac file (to use in IE ). I browsed and wrote something , which is not meeting my conditions 1. in office i have to use one proxy , say PROXY1:7070 to access internet and to use client network need to add in exclusion list (advanced... (2 Replies)
Discussion started by: radha254
2 Replies

3. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

5. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

6. Programming

Need help with Javascript

Hi guys, Ok first, let me explain what I want to do. I'm making a theme for the iphone, and I found a nice wallpaper slideshow script. Here is how it knows which wallpapers to use: <script type="text/javascript"> // SLIDE ROTATION FREQUENCY (in minutes) var slideRotation = 0.4;... (3 Replies)
Discussion started by: kicker75
3 Replies

7. Programming

Help in javascript

Hi , I wanted to know if its possible to execute a javascript function like this E:- function js1(){ alert ("this is js1"); } function js2(){ alert ("this is js2"); } function js3(){ .... execthisscript(js1); execthisscript(js2); } ... (1 Reply)
Discussion started by: daptal
1 Replies

8. Shell Programming and Scripting

reading from a file and pass as variables and ignore # in the file

file.txt contains ------------------ sat1 1300 #sat2 2400 sat3 sat4 500 sat5 I need to write a shell script that will output like the below #output sat1.ksh 1300 sat3.ksh sat4.ksh 500 sat5.ksh my try ------- (4 Replies)
Discussion started by: konark
4 Replies

9. Shell Programming and Scripting

variables from a file

Hi I am trying to write a shell script that will read 4 lines from a file and put them in variables which will be used in the script. Can someone tell me how can I read a line and put it in a variable? I have tried this... but this does not work. #! /bin/sh cat yest | while read line do... (2 Replies)
Discussion started by: skotapal
2 Replies
Login or Register to Ask a Question