/* autoforward.js - Automatic port forwarding setup for PuTTY, Internet Explorer and linux.ox.ac.uk.

This script sets up PuTTY to connect to linux.ox.ac.uk, and sets up port forwarding to allow email and Web access
to Oxford-only sites.  This script can be modified to connect to other SSH servers too, as long as the original author of
the script is acknowledged.

Copyright © Robert Bradley 2/9/2005
*/

// Initialise COM objects needed to run

shell=new ActiveXObject("WScript.Shell");

// Constants

session=escape("linux.ox.ac.uk");
puttyroot="HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\"+session;
inetroot="HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";

flYesNo=4;
flYes=6;

try {
    // Try reading the host name.  This will fail if the session is not setup yet.
    
    var host=shell.RegRead(puttyroot+"\\HostName");
}
catch (e) {
    WScript.Echo("Setting up PuTTY...");
    shell.RegWrite(puttyroot+"\\HostName","linux.ox.ac.uk","REG_SZ");
    shell.RegWrite(puttyroot+"\\Protocol","ssh","REG_SZ");
    shell.RegWrite(puttyroot+"\\Compression",1,"REG_DWORD");
    shell.RegWrite(puttyroot+"\\X11Forward",1,"REG_DWORD");
    shell.RegWrite(puttyroot+"\\PortForwardings","L8080=wwwcache.ox.ac.uk:80,L25=smtp.ox.ac.uk:25,D1080,","REG_SZ");

    // Not really necessary, but included anyway
    shell.RegWrite(puttyroot+"\\LinuxFunctionKeys",1,"REG_DWORD");
}
proxyenable=0;
proxyserver="";
proxyhttp11=0;
try {
    proxyenable=shell.RegRead(inetroot+"\\ProxyEnable");
    proxyserver=shell.RegRead(inetroot+"\\ProxyServer");
    proxyhttp11=shell.RegRead(inetroot+"\\ProxyHttp1.1");
}
catch (e) {}

/* Proxy server detection code

PuTTY is capable of connecting through certain types of proxy.  If these are detected in IE settings, this script assumes
they are necessary for Internet access.
*/

if (proxyenable==1 && (/(socks|https)=/.test(proxyserver) || !(/=/.test(proxyserver)))) {
    // Proxy already in use
    
    usesocks=/socks=(.*):(\d+)/.test(proxyserver);
    usehttps=/https=(.*):(\d+)/.test(proxyserver);
    usegeneric=!(/=/.test(proxyserver)) || usehttps;
    
    if (usesocks) {
        var re=/socks=(.*):(\d+)(;|$)/;
        proxyserver.match(re);
        var server=RegExp.$1;
        var port=RegExp.$2;
        var resp=shell.Popup("You appear to be using a proxy server for all outgoing connections.  Would you like PuTTY to connect through this?\n\n"+server+":"+port,0,"Automatic port forwarding setup",flYesNo);
        
        if (resp==flYes) {
            shell.RegWrite(puttyroot+"\\ProxyMethod",1,"REG_DWORD");
            shell.RegWrite(puttyroot+"\\ProxyHost",server,"REG_SZ");
            shell.RegWrite(puttyroot+"\\ProxyPort",port,"REG_DWORD");
        }
    }
    else {
        if (usegeneric) {
            if (usehttps)
                var re=/https=(.*):(\d+)(;|$)/;
            else
                var re=/(.*):(\d+)/;
            proxyserver.match(re);
            var server=RegExp.$1;
            var port=RegExp.$2;
            var resp=shell.Popup("You appear to be using a proxy server for secure Web access.  Would you like PuTTY to connect through this?\n\n"+server+":"+port,0,"Automatic port forwarding setup",flYesNo);
            
            if (resp==flYes) {
                shell.RegWrite(puttyroot+"\\ProxyMethod",3,"REG_DWORD");
                shell.RegWrite(puttyroot+"\\ProxyHost",server,"REG_SZ");
                shell.RegWrite(puttyroot+"\\ProxyPort",port,"REG_DWORD");
            }
        }
    }
}
else {
    // We are connecting directly using no proxies
    
    shell.RegWrite(puttyroot+"\\ProxyMethod",0,"REG_DWORD");
}

// Modify settings to point at PuTTY

WScript.Echo("Modifying Internet Explorer proxy settings");

shell.RegWrite(inetroot+"\\ProxyEnable",1,"REG_DWORD");
shell.RegWrite(inetroot+"\\ProxyServer","http=localhost:8080;socks=localhost:1080","REG_SZ");
shell.RegWrite(inetroot+"\\ProxyHttp1.1",1,"REG_DWORD");

/* Run PuTTY

This is surrounded by a try/catch block to make sure proxy settings are restored on exit.
*/
try {
    var resp=shell.Popup("Would you like to connect to linux.ox.ac.uk now?",0,"Automatic port forwarding setup",flYesNo);
    shell.Run("iexplore.exe");
    
    if (resp==flYes)
        shell.Run("putty -load "+session,1,true);
    else
        shell.Run("putty",1,true);
}
catch (e) {}
WScript.Echo("Restoring Internet Explorer proxy settings");

//Restore proxy settings
shell.RegWrite(inetroot+"\\ProxyEnable",proxyenable,"REG_DWORD");
shell.RegWrite(inetroot+"\\ProxyServer",proxyserver,"REG_SZ");
shell.RegWrite(inetroot+"\\ProxyHttp1.1",proxyhttp11,"REG_DWORD");