RichE Posted July 27, 2008 Posted July 27, 2008 Hi All,I haven't post any of my code for a while, So i thought i'd post up some of my latest endeavors.I work in a school (before you ask, I'm not a teacher, LOL), but teachers seem to struggle with instructions (sometimes with pictures) on how to do simple stuff on computers, so... version 1 of proxy switcher gave them two options, Proxy settings on or Proxy settings off. simple I hear you say, not for them... they could quite grasp the concept. so version two and three refined the process to do it all for them.Finally version four added a gui for admins to setup the network/proxy settings (it now should work on any network, and not just where i work) there's a list below of the features, and hey presto they love it.FeaturesLoops five times to give wireless connections time to pickup an IP address.Gui interface on 1st run to configure the settings (which are stored in an .ini file in the program dir)encrypts the username and password (to stop "at a glance" theft.)auto added exceptions and proxy settingsmaps a non persistant drive to a server (recommend the print server with a public share, this helps authentication on AD networks to allow printing)stand local IP's (0,127,169) picked up as off the network (turns off proxy settings)so here's the codeCODE#cs ---------------------------------------------------------------------------- AutoIt Version: 3.2.10.0 Author: Rich Easton Script Function: Automate proxy settings (proxy switcher) #ce ----------------------------------------------------------------------------; Script Start - Add your code below hereOpt("TrayIconHide", 1)#include <GUIConstants.au3>#include <String.au3>#include <Array.au3>#include <FILE.au3>SplashImageOn("", @ScriptDir & "\Splash.jpg", 300, 200, -1, -1, 1)Sleep(3000)SplashOff()Dim $proxy;sets proxy value as 0 by default$proxy = 0;path to PS app and settings.ini$path = @ScriptDir;msgbox(64,"", $path)$appini = $path & "\Settings.ini";check for settings.ini, if not there then run gui to get infoIf FileExists($appini) Then ;run Proxyswitcher from store settings in settings.ini Call("RunPS")Else ;run gui to get settings MsgBox(64, "First run", "Please complete the Proxy Switcher Config Form", 3) Call("PSCF")EndIfExit;functionsFunc PSCF() If FileExists($appini) Then $USR = IniRead($appini, "USER-DETAILS", "USERNAME", "") $PWD = IniRead($appini, "USER-DETAILS", "PASSWORD", "") $DOM = IniRead($appini, "NETWORK-DETAILS", "DOMAIN", "") $PXYSVR = IniRead($appini, "NETWORK-DETAILS", "PROXY-SERVER", "") $PXYPRT = IniRead($appini, "NETWORK-DETAILS", "PROXY-SERVER-PORT", "") $PNTSVR = IniRead($appini, "NETWORK-DETAILS", "PRINT-SERVER", "") $SVRSHR = IniRead($appini, "NETWORK-DETAILS", "SERVER-SHARE", "") $BIP = IniRead($appini, "NETWORK-DETAILS", "BAD-IPS", "") $RNG = IniRead($appini, "NETWORK-DETAILS", "RANGE", "") $EXC = IniRead($appini, "IE-SETTINGS", "EXCEPTIONS", "") ;decrypt username and password $dusr = _StringEncrypt(0, $USR, "mars@12345", 2) $dpwd = _StringEncrypt(0, $PWD, "venus@12345", 2) Else $DOM = "" $PXYSVR = "" $PXYPRT = "" $PNTSVR = "" $SVRSHR = "" $BIP = "" $RNG = "" $EXC = "" ;decrypt username and password $dusr = "" $dpwd = "" EndIf $PSCF = GUICreate("Proxy Switcher Configuration", 467, 295, -1, -1, $WS_EX_DLGMODALFRAME) GUICtrlCreateGroup("User Details", 8, 8, 249, 73) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlCreateLabel("Username", 17, 26, 52, 17) If $dusr > "" Then $username = GUICtrlCreateInput($dusr, 88, 24, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Username") Else $username = GUICtrlCreateInput("", 88, 24, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Username") EndIf GUICtrlCreateLabel("Password", 17, 50, 50, 17) If $dpwd > "" Then $Password = GUICtrlCreateInput($dpwd, 88, 48, 161, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL, $WS_GROUP), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Password") Else $Password = GUICtrlCreateInput("", 88, 48, 161, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL, $WS_GROUP), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Password") EndIf GUICtrlCreateGroup("Network Details", 8, 88, 249, 145) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlCreateLabel("Domain", 17, 111, 40, 17) If $DOM > "" Then $Domain = GUICtrlCreateInput($DOM, 88, 104, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Domain") Else $Domain = GUICtrlCreateInput("", 88, 104, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Domain") EndIf GUICtrlCreateLabel("Proxy Server", 17, 135, 64, 17) If $PXYSVR > "" Then $ProxyServer = GUICtrlCreateInput($PXYSVR, 88, 128, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Proxy server") Else $ProxyServer = GUICtrlCreateInput("", 88, 128, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network Proxy server") EndIf GUICtrlCreateLabel("Proxy Port", 17, 159, 52, 17) If $PXYPRT > "" Then $ProxyPort = GUICtrlCreateInput($PXYPRT, 88, 152, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "The Port the proxy server uses e.g 80") Else $ProxyPort = GUICtrlCreateInput("", 88, 152, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "The Port the proxy server uses e.g 80") EndIf GUICtrlCreateLabel("Print Server", 17, 184, 59, 17) If $PNTSVR > "" Then $PrintServer = GUICtrlCreateInput($PNTSVR, 88, 176, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network print server") Else $PrintServer = GUICtrlCreateInput("", 88, 176, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "Your Network print server") EndIf GUICtrlCreateLabel("Server Share", 16, 207, 66, 17) If $SVRSHR > "" Then $Servershare = GUICtrlCreateInput($SVRSHR, 88, 200, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "A server share so you can be authenticated on the network") Else $Servershare = GUICtrlCreateInput("", 88, 200, 161, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "A server share so you can be authenticated on the network") EndIf GUICtrlCreateGroup("IP Ranges", 264, 9, 193, 105) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlCreateLabel("Local IP Addresses", 272, 24, 95, 17) If $BIP > "" Then $badips = GUICtrlCreateInput($BIP, 272, 40, 177, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "IP Address, that are uses by your computer when it is not on any network") Else $badips = GUICtrlCreateInput("0,127,169", 272, 40, 177, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "IP Address, that are uses by your computer when it is not on any network") EndIf GUICtrlCreateLabel("Network IP Range", 272, 64, 92, 17) If $RNG > "" Then $range = GUICtrlCreateInput($RNG, 272, 80, 177, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "The first three numbers of the IP range used on your network") Else $range = GUICtrlCreateInput("172", 272, 80, 177, 24, -1, BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "The first three numbers of the IP range used on your network") EndIf GUICtrlCreateGroup("IE Exceptions", 264, 120, 193, 113) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") If $EXC > "" Then $exceptions = GUICtrlCreateEdit($EXC, 272, 136, 177, 89, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "IE exceptions used on your network") Else $exceptions = GUICtrlCreateEdit("172.*;192.*;<local>", 272, 136, 177, 89, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE)) GUICtrlSetTip(-1, "IE exceptions used on your network") EndIf $SaveContinue = GUICtrlCreateButton("Save and Continue", 263, 242, 194, 25, 0) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlSetBkColor(-1, 0x4464AC) GUICtrlSetTip(-1, "Saves and runs Proxy Switcher") GUICtrlSetCursor(-1, 0) $Reset = GUICtrlCreateButton("Reset", 8, 242, 124, 25, 0) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlSetTip(-1, "Resets this form back to blank") GUICtrlSetCursor(-1, 0) $Cancel = GUICtrlCreateButton("Cancel", 136, 242, 124, 25, 0) GUICtrlSetFont(-1, 10, 800, 0, "MS Reference Sans Serif") GUICtrlSetCursor(-1, 0) WinSetTrans($PSCF, "", 0) GUISetState(@SW_SHOW) For $T = 0 To 255 Step 1 WinSetTrans($PSCF, "", $T) Next While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE ;fade out effect For $T = 254 To 0 Step -1 WinSetTrans($PSCF, "", $T) Next Exit Case $Cancel ;fade out effect For $T = 254 To 0 Step -1 WinSetTrans($PSCF, "", $T) Next Exit Case $Reset ;Reset form to fields to blank GUICtrlSetData($username, "", "") GUICtrlSetData($Password, "", "") GUICtrlSetData($Domain, "", "") GUICtrlSetData($ProxyServer, "", "") GUICtrlSetData($ProxyPort, "", "") GUICtrlSetData($PrintServer, "", "") GUICtrlSetData($Servershare, "", "") GUICtrlSetData($badips, "0,127,169", "") GUICtrlSetData($range, "172", "") GUICtrlSetData($exceptions, "", "") Case $SaveContinue ;read form fields $USR = GUICtrlRead($username) $PWD = GUICtrlRead($Password) $DOM = GUICtrlRead($Domain) $PXYSVR = GUICtrlRead($ProxyServer) $PXYPRT = GUICtrlRead($ProxyPort) $PNTSVR = GUICtrlRead($PrintServer) $SVRSHR = GUICtrlRead($Servershare) $BIP = GUICtrlRead($badips) $RNG = GUICtrlRead($range) $EXC = GUICtrlRead($exceptions) ;test inputs to check that their are no blank fields $errorstate = 0 If $USR = "" Then MsgBox(48, "Username", "Username missing, please enter", 3) $errorstate = 1 Else ;encrypt username and write to ini $eusr = _StringEncrypt(1, $USR, "mars@12345", 2) IniWrite($appini, "USER-DETAILS", "USERNAME", $eusr) EndIf If $PWD = "" Then MsgBox(48, "Password", "Password missing, please enter", 3) $errorstate = 1 Else ;encrypt password and write to ini $epwd = _StringEncrypt(1, $PWD, "venus@12345", 2) IniWrite($appini, "USER-DETAILS", "PASSWORD", $epwd) EndIf If $DOM = "" Then MsgBox(48, "Domain", "Domain missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "DOMAIN", $DOM) EndIf If $PXYSVR = "" Then MsgBox(48, "Proxy Server", "Proxy Server missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "PROXY-SERVER", $PXYSVR) EndIf If $PXYPRT = "" Then MsgBox(48, "Proxy Server Port", "Proxy Server Port missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "PROXY-SERVER-PORT", $PXYPRT) EndIf If $PNTSVR = "" Then MsgBox(48, "Print Server", "Print Server missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "PRINT-SERVER", $PNTSVR) EndIf If $SVRSHR = "" Then MsgBox(48, "Server Share", "Server Share missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "SERVER-SHARE", $SVRSHR) EndIf If $BIP = "" Then MsgBox(48, "Bad IP's", "Bad IP's missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "BAD-IPS", $BIP) EndIf If $RNG = "" Then MsgBox(48, "IP Range", "IP Range missing, please enter", 3) $errorstate = 1 Else IniWrite($appini, "NETWORK-DETAILS", "RANGE", $RNG) EndIf IniWrite($appini, "IE-SETTINGS", "EXCEPTIONS", $EXC) ;fade out effect If $errorstate = 1 Then Else For $T = 254 To 0 Step -1 WinSetTrans($PSCF, "", $T) Next Call("RunPS") EndIf EndSwitch WEndEndFunc ;==>PSCFFunc RunPS() ;read ini details $USR = IniRead($appini, "USER-DETAILS", "USERNAME", "") $PWD = IniRead($appini, "USER-DETAILS", "PASSWORD", "") $DOM = IniRead($appini, "NETWORK-DETAILS", "DOMAIN", "") $PXYSVR = IniRead($appini, "NETWORK-DETAILS", "PROXY-SERVER", "") $PXYPRT = IniRead($appini, "NETWORK-DETAILS", "PROXY-SERVER-PORT", "") $PNTSVR = IniRead($appini, "NETWORK-DETAILS", "PRINT-SERVER", "") $SVRSHR = IniRead($appini, "NETWORK-DETAILS", "SERVER-SHARE", "") $BIP = IniRead($appini, "NETWORK-DETAILS", "BAD-IPS", "") $RNG = IniRead($appini, "NETWORK-DETAILS", "RANGE", "") $EXC = IniRead($appini, "IE-SETTINGS", "EXCEPTIONS", "") ;decrypt username and password $dusr = _StringEncrypt(0, $USR, "mars@12345", 2) $dpwd = _StringEncrypt(0, $PWD, "venus@12345", 2) If $dusr = "" Or $dpwd = "" Or $DOM = "" Or $PXYSVR = "" Or $PXYPRT = "" Or $PNTSVR = "" Or $SVRSHR = "" Or $BIP = "" Or $RNG = "" Then MsgBox(48, "Warning!!!", "there is an entry or entries missing from the settings.ini, running Configuration Form.") Dim $username Call("PSCF") GUICtrlSetData($username, $dusr, "") Else ProcessClose("iexplore.exe") $PID = ProcessExists("iexplore.exe") ; Will return the PID or 0 if the process isn't found. If $PID Then ProcessClose($PID) EndIf $ip = @IPAddress1 ; split ip address into octet sets $result = StringSplit($ip, ".") ;split local ip ranges into seperate parts $bIPcheck = StringSplit($BIP, ",") ;check ip to see if it equals localips If $result[1] = $bIPcheck[1] Or $result[1] = $bIPcheck[2] Or $result[1] = $bIPcheck[3] Then $i = 0 Do $ip = @IPAddress1 ; split ip address into octet sets $result = StringSplit($ip, ".") If $result[1] = $bIPcheck[1] Or $result[1] = $bIPcheck[2] Or $result[1] = $bIPcheck[3] Then $i = $i + 1 ;if = to local ip then loop MsgBox(48, "Information", "Waiting for IP Address " & @CR & "Attempt " & $i & " of 5", 5) Sleep(2000) Else If $result[1] = $RNG Then MsgBox(64, "IP Address", "Your IP address is " & $ip, 3) $network = Ping($PNTSVR, 500) If $network Then $proxy = 1 Else $proxy = 0 EndIf Else MsgBox(64, "IP Address", "Your IP address is " & $ip, 3) $proxy = 0 EndIf ExitLoop EndIf Until $i = 5 Else ;if not = to local ip, and has ip address then show ip address. If $result[1] = $RNG Then ;MsgBox(64, "IP Address", "Your IP address is " & $ip, 3) $network = Ping($PNTSVR, 500) If $network Then $proxy = 1 Else $proxy = 0 EndIf Else ;MsgBox(64, "IP Address", "Your IP address is " & $ip, 3) $proxy = 0 EndIf EndIf If $proxy = 0 Then ;write IE reg keys for home/external usage RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "REG_DWORD", "0") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "REG_DWORD", "0") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "AutoConfigProxy", "REG_SZ", "") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "AutoConfigURL", "REG_SZ", "") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", "REG_SZ", "") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", "REG_SZ", "") MsgBox(64, "Internet Explorer", "Your IP address is " & $ip & @CR & @CR & "Proxy Server Disabled", 3) Else ;write IE reg keys for School/internal usage RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "REG_DWORD", "1") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyEnable", "REG_DWORD", "1") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "AutoConfigProxy", "REG_SZ", "") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "AutoConfigURL", "REG_SZ", "") RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", "REG_SZ", $PXYSVR & ":" & $PXYPRT) RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", "REG_SZ", $EXC) ;map non persistant drive to print server rmpublic using stored credentials to authenticate DriveMapAdd("*", "\\" & $PNTSVR & "\" & $SVRSHR, 0, $DOM & "\" & $dusr, $dpwd) ; firefox proxy changes MsgBox(64, "Internet Explorer", "Your IP address is " & $ip & @CR & @CR & "Proxy Server Enabled", 3) EndIf ExitEndFunc i've uploaded the Splash screen as well, if anyone wants the compiled version, i've done it with an inno installer, just MSN (rich.easton@gmail.com) or Email me, and i'll send it over. RichE [font="'Arial Narrow';"]Current projects[/font] [font="'Arial Narrow';"]are on my site [/font]Sellostring
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now