Version 4 of ProxySwitch

Updated 2005-10-15 09:52:17

HZe I use a Windows notebook at work and at home. At work, there is internet access through a proxy server while at home I can access internet directly without proxy through a DSL router.

DHCP works fine to get the IP-addresses without reconfiguration, but everytime I switched the networks I had to enable/disable the proxy settings. So I came up with this little script, which does the same based on the current network connection. I added it to my windows AutoStart group and now I'm fine.


HZe - 11-OCT-2004: now it also supports Mozilla Firefox


    # Switch to enable/disable Proxy depending on Company net or HOME net
    # Tested on Windows XP

    proc usage {} {
        tk_messageBox -message {

    ProxySwitch

        sets the proxy in your Internet Explorer depending the network connection

    Author:
        Holger Zeinert (holger DOT zeinert AT gmx DOT de)

    usage:
        tclsh proxyswitch.tcl <re-expr> <proxyhost:port> ...
        proxyswitch.exe <re-expr> <proxyhost:port> ...

    Description:
        Depending on the output of ipconfig, the setting of the Proxy is changed. This
        is done in the registry for Internet Explorer and in the file prefs.js for
        Mozilla Firefox

        To do so, the output of ipconfig is matched against the regular expressions. The first
        fit determines the proxy host and port to be used. Since the whole output of ipconfig
        is base for regexp, you can make a decision on IP-Address, DNS suffix or whatever.

        If no match fits, the default is to disable the proxy.

    Examples:
        tclsh proxyswitch.tcl ":[ \t]*10.2." "gate:3128"
        proxyswitch.exe ":[ \t]*192.168." "gate:8888" "DNS-Suffix: home" "world.home.de:3200"

    Appendix:
        An output of ipconfig looks e.g. like this:

            Windows-IP-Konfiguration


            Ethernetadapter WLAN-Verbindung :

                    Verbindungsspezifisches DNS-Suffix: home
                    IP-Adresse. . . . . . . . . . . . : 192.168.1.2
                    Subnetzmaske. . . . . . . . . . . : 255.255.255.0
                    Standardgateway . . . . . . . . . : 192.168.1.1
        }
    }

    package require registry

    # no main window
    wm withdraw .

    if {$argv == ""} {
        usage
        exit
    }

    if {[catch {set ipconfig [exec ipconfig]}]} {
        tk_messageBox -message "could not start ipconfig! Not running Windows?"
        exit
    }

    set server "off"
    foreach {re proxy} $argv {
        if {[regexp $re $ipconfig]} {
            set server $proxy
            break
        }
    }

    # depending on proxy, set override proxy for local addresses
    if {$server == "off"} {
        set server ""
        set override ""
        set switch 0
    } else {
        set override <local>
        set switch 1
    }

    #
    # Internet Explorer 6
    #

    # set the entries in the registry
    set keyName {HKEY_CURRENT_USER\SOFTWARE\microsoft\windows\currentversion\internet settings}
    registry set $keyName "ProxyEnable" $switch dword
    registry set $keyName "ProxyServer" $server
    registry set $keyName "ProxyOverride" $override

    if {$switch} {
        tk_messageBox -message "Proxy set to $server!" -icon info
    } else {
        tk_messageBox -message "Proxy disabled!" -icon warning
    }

    #
    # FireFox 0.9.3, 1.0PR
    #

    set tmp [split $server ":"]
    set host [lindex $tmp 0]
    set port [lindex $tmp 1]

    foreach pref [glob -nocomplain [file join $env(APPDATA) Mozilla Firefox Profiles * prefs.js]] {
        set in [open $pref r]
        set out [open $pref.tmp w]
        while {![eof $in]} {
            gets $in line
            if {![string match "user_pref(\"network.proxy*\",*" $line]} {
                puts $out $line
            }
        }
        # TODO: deal with other proxy settings as well.
        if {$switch} {
            puts $out "user_pref(\"network.proxy.http\", \"$host\");"
            puts $out "user_pref(\"network.proxy.http_port\", $port);"
            puts $out "user_pref(\"network.proxy.type\", 1);"
        }
        close $in
        close $out
        file rename -force $pref.tmp $pref
    }

    exit