Version 7 of ProxySwitch

Updated 2005-12-18 12:03:23

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


HZe - 18-DEC-2005: now also supports a silent mode and optionally a start of a command after the proxy setting was done. On my Windows machine I use this as short cut to start FireFox:

   proxyswitch.tcl -silent 1 -exec "c:\Programme\Mozilla Firefox\firefox.exe" "DNS-Suffix:[ \t]*atwork.com" "proxyhost:3128"

so, every time I start FireFox using this short cut, it checks in which network the machine is connected and sets the proxy accordingly.


    # 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 [-silent 0|1] [-exec <cmd>] <re-expr> <proxyhost:port> ...
        proxyswitch.exe [-silent 0|1] [-exec <cmd>] <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.

        -silent 0|1:
            if set to 1, there will be no message box used to report the action
        -exec <cmd>:
            call a command after the setting was made

    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"
    set silent 0
    set cmd ""
    foreach {key value} $argv {
        # options
        switch -- $key {
            -silent {
                set silent $value
            }
            -exec {
                set cmd $value
            }
            default {
                if {[regexp $key $ipconfig]} {
                    set server $value
                    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 {!$silent} {
        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 {![regexp {user_pref\("network\.proxy\.(http|type).*",.*} $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
    }

    # start a command after the configuration setting
    if {$cmd != ""} {
        exec $cmd &
    }

    exit