// vpnc-script-win.js
//
// Sets up the Network interface and the routes
// needed by vpnc.

// --------------------------------------------------------------
// Utilities
// --------------------------------------------------------------

function echo(msg)
{
	WScript.echo(msg);
}

function run(cmd)
{   
	echo(cmd); // for debug purposes
	var out = ws.Exec(cmd).StdOut.ReadAll(); 
	echo(out); // for debug purposes
	return out;
}

function getDefaultGateway()
{
    var output =   run( "route print 0.0.0.0"  );
    var pos = output.indexOf("0.0.0.0          0.0.0.0      ") + 30;
    var gw = output.substring(pos,pos+15); // max length of ip address
    gw = gw.substring(0,gw.indexOf(" ")); // trim at first space...
    echo("Default Gateway: [" + gw + "]");
    return gw;   
}

// --------------------------------------------------------------
// Script starts here
// --------------------------------------------------------------

var internal_ip4_netmask = "255.255.255.0"

var ws = WScript.CreateObject("WScript.Shell");
var env = ws.Environment("Process");

switch (env("reason")) {
case "pre-init":
	break;
case "connect":
	
	echo("VPN Gateway: " + env("VPNGATEWAY"));
	echo("Internal Address: " + env("INTERNAL_IP4_ADDRESS"));
  	echo("Internal Netmask: " + env("INTERNAL_IP4_NETMASK"));
	echo("Interface: \"" + env("TUNDEV") + "\"");
	
	var defaultGateway = getDefaultGateway();
	
	if (env("INTERNAL_IP4_NETMASK")) {
	    var internal_ip4_netmask = env("INTERNAL_IP4_NETMASK");
	}

	if (env("TUNDEV")) {
		var interface = env("TUNDEV");
	}
	
	if (env("INTERNAL_IP4_ADDRESS")) {
		var internalAddress = env("INTERNAL_IP4_ADDRESS");
	}
	
	if (env("VPNGATEWAY")) {
		var vpnGateway = env("VPNGATEWAY");
	}
	
	echo("Configuring \"" + interface + "\" interface...");
	
	run("netsh interface ip set address \"" + interface + "\" static " +
	     internalAddress + " " + internal_ip4_netmask);

	//Add direct route for the VPN gateway to avoid routing loops
	run("route add " + vpnGateway +
            " mask 255.255.255.255 " + defaultGateway );

	//Add WINS entries
	if (env("INTERNAL_IP4_NBNS")) {
		var wins = env("INTERNAL_IP4_NBNS").split(/ /);
		for (var i = 0; i < wins.length; i++) {
					run("netsh interface ip add wins \"" +
				interface + "\" " + wins[i]
				+ " index=" + (i+1));
		}
	}

	//Add DNS entries
	if (env("INTERNAL_IP4_DNS")) {
		var dns = env("INTERNAL_IP4_DNS").split(/ /);
						
		// add them back in
		for (var i = 0; i < dns.length; i++) {
					run("netsh interface ip add dns \"" +
				interface + "\" " + dns[i]
				+ " index=" + (i+1));
		}
	}
	echo("Pausing for 4 seconds to allow the adapter to register itself correctly and therefore correct routing inferences made. You may need to supply a longer delay.");
	WScript.Sleep(4000);   
	
	//Add internal network routes
	echo("Configuring networks:");
	if (env("CISCO_SPLIT_INC")) {
		
		for (var i = 0 ; i < parseInt(env("CISCO_SPLIT_INC")); i++) {
		var network = env("CISCO_SPLIT_INC_" + i + "_ADDR");
		var netmask = env("CISCO_SPLIT_INC_" + i + "_MASK");
		run("route add " + network + " mask " + netmask +
					 " " + internalAddress );
		}
			

	
	echo("done.");
	
	} else {
		echo("Gateway did not provide network configuration.");
	}
	
	
	echo("Route configuration done.");
		
	if (env("CISCO_BANNER")) {
		echo("--------------------------------------------------");
		echo(env("CISCO_BANNER"));
		echo("--------------------------------------------------");
	}	
	break;
case "disconnect":
    echo("Deleting previously added routes:"); 
	// Delete direct route for the VPN gateway
	run("route delete " + env("VPNGATEWAY") + " mask 255.255.255.255");
	
	//remove internal network routes
	if (env("CISCO_SPLIT_INC")) {
		for (var i = 0 ; i < parseInt(env("CISCO_SPLIT_INC")); i++) {
			var network = env("CISCO_SPLIT_INC_" + i + "_ADDR");
			run("route delete " + network );
		}
	} else {
		echo("Gateway did not provide network configuration.");
	}
	
	
}


