MindSlap Posted March 15, 2010 Posted March 15, 2010 Hi all... This is my first post (Tho I've been lurking for a while.) I poked around trying to find something to change Firefox's proxy settings. No dice. Its need to remotely change the setting. (I'm figuring on running this on all workstations via SMS) So..I figured I'd give it a whack myself. This is my first script..so please dont beat me when you see how ugly it is. This seems to work just fine...with requirements. You have to have FF 3.6 because 3.6 includes the option for USE SYSTEM PROXY SETTINGS" (This works out nicely because I can set the proxy via GPO, and simply have FF go by that setting) I'm tossing this up because: 1) Get a few eyeballs on it. Get some suggestions how to improve it, if any. 2) Did I miss any considerations regarding the FF prefs.js file and how FF handles it. 3) It may be useful for others. expandcollapse popup; ; Firefox Proxy Changer Ver 1.2 ; ; ; JVS March 2010 ; ; Requires Firefox 3.6 or higher, and Firefox.exe MUST be installed in its default directory C:\program files\Mozilla Firefox ; #Include <File.au3> #Include <Array.au3> Dim $validprefsdirs[10] Dim $PrefStrings, $AllPrefsFiles GLOBAL $elementcount, $validprefsdirs, $profdir, $stringtochange, $stringchangeto, $FlagForFileWrite Global $filelist, $Rsearch, $Searchroot, $linecount global $RecusiveSearchResults, $FFversion ;Begin this ugly thing msgbox (0, "FIREFOX PROXY CONFIGURATION", "This Operation will Configure your FireFox Proxy Settings for use within xxxx's Network." & @lf & @lf & "Before the Changes are Made, a Conditional Check will Be Perfomed. Click OK to Continue") CheckFFVersion() ;MUST BE INSTALLED IN PROGRAM FILES AND HAVE AT LEAST VERSION 3.6!!! ;Exit KillFF() ;Whack FF findallprefs() ;Discover all prefs.js files in Documents and settings. ;_ArrayDisplay($RecusiveSearchResults, "recusrive results") $AllPrefsFiles = $RecusiveSearchResults ;_arraydisplay($allPrefsFiles, "These are the files being modified") $elementcount = $AllPrefsFiles[0] changeproxy() ;Change proxy settings msgbox (0, "FIREFOX PROXY CONFIGURATION", "The Operations is Complete." & @lf & "If you Encounter any Problems, Contact EUC at xHELP" & @lf & @lf & " Click OK To Close") Exit ; Old main dont use. Only for per user searches. ;getprofilepaths() ;GetAllProfileDirs() ;BuildPrefsTargets() ;changeproxy() ;============ FUNCTIONS ================================= func CheckFFVersion() $ffversion = FileGetVersion("C:\Program Files\Mozilla Firefox\firefox.exe","ProductVersion") ;productversion productname ;msgbox(0,"FF Version", "Version " & $ffversion) if $ffversion < 3.6 Then msgbox(0,"FireFox Product Check", "Conditional Check: FAILED" & @lf & @lf & "Your instance of Firefox is either not installed correctly" & @lf & "or is older than Version 3.6" & @lf & @lf & "Please Contact....") exit endif msgbox(0,"FireFox Product Check", "Conditional Check: SUCCESS" & @lf & @lf & "Your instance of FireFox is Installed Correctly and is at least Version 3.6" & @lf & @lf & " Click OK to Update Your Proxy Settings" & @lf & " If Firefox Is Currently Open it will be Shutdown!") EndFunc ;Whack the fox! func killff() ;msgbox(0,"Changing Proxy Settings", "Killing FireFox") while winexists("[class:MozillaUIWindowClass]","") winkill("[class:MozillaUIWindowClass]","") WEnd EndFunc func changeproxy() $StringtoChange = 'user_pref("network.proxy.type",' $StringChangeto = 'user_pref("network.proxy.type",5);' ;msgbox(0,"changing to", "changing from " & $StringtoChange & "to " & $StringChangeto) ;msgbox(0,"elementcount", $elementcount) for $i = 0 to $elementcount -1 $FlagForFileWrite = 0 ;msgbox(0,"loopy", $i) if FileExists($AllPrefsFiles[$i]) Then ;msgbox (0,"found file", $allprefsfiles[$i]) _FileReadToArray($AllPrefsFiles[$i],$PrefStrings) $linecount = $PrefStrings[0] ;_arraydisplay($PrefStrings, "pref strings file") ;Search for proxy setting for $j = 1 to $PrefSTrings[0]-1 if stringleft($PrefStrings[$j],stringlen($stringtochange)) = $stringtochange Then $PrefStrings [$j] = $StringChangeTo $FlagForFileWrite = 1 EndIf ;MSGBOX(0,"STRING TO CHANGE NOT FOUND", "String " & $StringtoChange & " not found") if $FlagForFileWrite = 0 Then ;check file again for proxy varables and ADD type because moz deletes the damn thing if set to noproxy!!! for $j = 1 to $PrefSTrings[0]-1 if stringleft($PrefStrings[$j],29) = 'user_pref("network.proxy.http' Then ;MsgBox(0,"Found conditional forced line", "found conditional subserch for proxy entry") ;msgbox(0,"LINECOUNT", $linecount) redim $PrefStrings[$linecount + 2] $PrefStrings[$linecount+1] = $StringChangeto $FlagForFileWrite = 1 ;_ArrayDisplay($PrefStrings, "Appended file with Proxy type") EndIf Next endif Next ;_ArrayDisplay($PrefStrings,"New Prefs file!") If $FlagForFileWrite = 1 Then Filecopy ($AllPrefsFiles[$i],$AllPrefsFiles[$i] & ".bkup" & Random (1111,9999,1)) ;backup the dang file just in Case Filedelete($AllPrefsFiles[$i]) sleep(100) ;msgbox(0,"gonna write to", $AllPrefsFiles[$i] & " " & $PrefStrings[$i]) _FileWriteFromArray($AllPrefsFiles[$i],$PrefStrings,1) $FlagForFileWrite = 0 endif EndIf Next EndFunc func FindAllprefs() $searchroot = "c:\documents and settings" ;$Rsearch="\*.js" ; old dont need $RecusiveSearchResults = _Search($searchroot, "prefs.js", "True", -1) ;msgbox(0,"searching for ", "Root dir =" & $searchroot & " file = " & $Rsearch) EndFunc ;Lifted this recursive code from autoIT forums. ; ;### SEARCH FUNCTION ### ;Returns array containing full path of matched files, with [0] containing the count of returned elements. ; ;$s_Path = start location of search, default is "" (current working directory) ; ;$s_Filter = expression to use for file matching (e.g. *.txt), default is "*" ; ;$f_Directories = set to true if directories are to be included in the SEARCH results ; Default set to True ; ;$i_Depth = folder depth to limit search ; -1 -> (Default) no limit, SEARCH in all subfolders ; 0 -> SEARCH current folder only ; n -> SEARCH up to n folders deep ; ;Sample Call ;_Main() ; ;Func _Main() ; Local $iTimer = TimerInit() ; Local $aRET = _Search("c:\documents and settings", "prefs.js", "True", -1) ; $iTimer = TimerDiff($iTimer) ; _ArrayDisplay($aRET, "Time = " & Round($iTimer / 1000, 3) & "sec") ;EndFunc ;==>_Main Func _Search($s_Path = "", $s_Filter = "*", $f_Directories = True, $i_Depth = -1) Local $h_Search, $s_Found, $s_Results = "", $a_Results[1] = [0], $s_RecursePath, $a_Recurse[1] = [0] ; Parameter checks If $s_Path Then If StringRight($s_Path, 1) <> "\" Then $s_Path &= "\" Else $s_Path = @WorkingDir & "\" EndIf ; Get SEARCH handle $h_Search = FileFindFirstFile($s_Path & $s_Filter) If $h_Search <> -1 Then ; Perform SEARCH of this directory While 1 $s_Found = FileFindNextFile($h_Search) If @error Then ExitLoop ; Check for not found If @extended = 1 Then ; Check for directory ; Directory If $f_Directories Then $s_Results &= $s_Path & $s_Found & "\|" Else ; File $s_Results &= $s_Path & $s_Found & "|" EndIf WEnd FileClose($h_Search) EndIf ; check recursion If $i_Depth <> 0 Then $h_Search = FileFindFirstFile($s_Path & "*") While 1 $s_Found = FileFindNextFile($h_Search) If @error Then ExitLoop If @extended Then $a_Recurse = _Search($s_Path & $s_Found, $s_Filter, $f_Directories, $i_Depth - 1) For $n = 1 To $a_Recurse[0] $s_Results &= $a_Recurse[$n] & "|" Next EndIf WEnd FileClose($h_Search) EndIf ; Create return array and return If StringRight($s_Results, 1) = "|" Then $s_Results = StringTrimRight($s_Results, 1) $a_Results = StringSplit($s_Results, "|") EndIf Return $a_Results EndFunc ;==>_Search
MindSlap Posted March 16, 2010 Author Posted March 16, 2010 (edited) I guess I can't edit my own posts... But this needs an update. There was a bug when ripping thru the array. If you have only one prefs.js it would fail. (HEY! I'm new at this!! ) The working code is here. expandcollapse popup; ; Firefox Proxy Changer Ver 1.3 ; ; ; Mindslap March 2010 ; ; Requires Firefox 3.6 or higher, and Firefox.exe MUST be installed in its default directory C:\program files\Mozilla Firefox ; #Include <File.au3> #Include <Array.au3> Dim $validprefsdirs[10] Dim $PrefStrings, $AllPrefsFiles GLOBAL $elementcount, $validprefsdirs, $profdir, $stringtochange, $stringchangeto, $FlagForFileWrite Global $filelist, $Rsearch, $Searchroot, $linecount global $RecusiveSearchResults, $FFversion Opt("TrayAutoPause",0) ;Begin this ugly thing msgbox (0, "FIREFOX PROXY CONFIGURATION", "This Operation will Configure your FireFox Proxy Settings for use within xxxx Network." & @lf & @lf & "Before the Changes are Made, a Conditional Check will Be Perfomed. Click OK to Continue") CheckFFVersion() ;MUST BE INSTALLED IN PROGRAM FILES AND HAVE AT LEAST VERSION 3.6!!! ;Exit KillFF() ;Whack FF SplashTextOn("FIREFOX PROXY CONFIGURATION", "Please Wait...." & @LF & "Searching for profiles and Configuring Proxy", 300, 100, -1, -1, 16, "tahoma",10) sleep(1000) findallprefs() ;Discover all prefs.js files in Documents and settings. ;_ArrayDisplay($RecusiveSearchResults, "recusrive results") $AllPrefsFiles = $RecusiveSearchResults ;_arraydisplay($allPrefsFiles, "These are the files being modified") $elementcount = $AllPrefsFiles[0] changeproxy() ;Change proxy settings splashoff() msgbox (0, "FIREFOX PROXY CONFIGURATION", "The Operation is Complete." & @lf & "If you Encounter any Problems, Contact xxx" & @lf & @lf & " Click OK To Close",15) Exit ; Old main dont use. Only for per user searches. ;getprofilepaths() ;GetAllProfileDirs() ;BuildPrefsTargets() ;changeproxy() ;============ FUNCTIONS ================================= func CheckFFVersion() $ffversion = FileGetVersion("C:\Program Files\Mozilla Firefox\firefox.exe","ProductVersion") ;productversion productname ;msgbox(0,"FF Version", "Version " & $ffversion) if $ffversion < 3.6 Then msgbox(0,"FireFox Product Check -- ", "Conditional Check: FAILED" & @lf & @lf & "Your instance of Firefox is either not installed correctly" & @lf & "or is older than Version 3.6" & @lf & @lf & "Please Contact ") exit endif msgbox(0,"FireFox Product Check -- ", "Conditional Check: SUCCESS" & @lf & @lf & "Your instance of FireFox is Installed Correctly and is at least Version 3.6" & @lf & @lf & " Click OK to Update Your Proxy Settings" & @lf & " If Firefox Is Currently Open it will be Shutdown!") EndFunc ;Whack the fox! func killff() ;msgbox(0,"Changing Proxy Settings", "Killing FireFox") while winexists("[class:MozillaUIWindowClass]","") winkill("[class:MozillaUIWindowClass]","") WEnd while ProcessExists("firefox.exe") ;The double whammy!!! This will work when workstation is locked. ProcessClose("firefox.exe") WEnd EndFunc func changeproxy() $StringtoChange = 'user_pref("network.proxy.type"' $StringChangeto = 'user_pref("network.proxy.type",5);' ;msgbox(0,"changing to", "changing from " & $StringtoChange & "to " & $StringChangeto) ;msgbox(0,"elementcount", $elementcount) for $i = 1 to $elementcount $FlagForFileWrite = 0 ;msgbox(0,"loopy", $i) if FileExists($AllPrefsFiles[$i]) Then ;msgbox (0,"found file", $allprefsfiles[$i]) _FileReadToArray($AllPrefsFiles[$i],$PrefStrings) $linecount = $PrefStrings[0] ;_arraydisplay($PrefStrings, "pref strings file") ;Search for proxy setting for $j = 1 to $PrefSTrings[0]-1 ;MSGBOX(0,"LOOP THRU PRE FILE", "LOOP THRU PRE FILE" & $j) if stringleft($PrefStrings[$j],stringlen($stringtochange)) = $stringtochange Then ;msgbox(0,"FOUND NETWORK PROXY TYPE..WHOO HOO", "FOUND NETWORK PROXY TYPE..WHOO HOO") $PrefStrings [$j] = $StringChangeTo $FlagForFileWrite = 1 EndIf next ;MSGBOX(0,"STRING TO CHANGE NOT FOUND", "String " & $StringtoChange & " not found") if $FlagForFileWrite = 0 Then ;check file again for proxy varables and ADD type because moz deletes the damn thing if set to noproxy!!! for $j = 1 to $PrefSTrings[0]-1 if stringleft($PrefStrings[$j],26) = '# Mozilla User Preferences' Then ;old lookup string user_pref("network.proxy.http ;MsgBox(0,"Found conditional forced line", "found conditional subserch for proxy entry") ;msgbox(0,"LINECOUNT", $linecount) redim $PrefStrings[$linecount + 2] $PrefStrings[$linecount+1] = $StringChangeto $FlagForFileWrite = 1 ;_ArrayDisplay($PrefStrings, "Appended file with Proxy type") EndIf Next endif ;Next ;_ArrayDisplay($PrefStrings,"New Prefs file!") If $FlagForFileWrite = 1 Then KillFF() ; The search can take a while. Whack it AGAIN, if user fired it up while waiting Filecopy ($AllPrefsFiles[$i],$AllPrefsFiles[$i] & ".bkup" & Random (1111,9999,1)) ;backup the dang file just in Case Filedelete($AllPrefsFiles[$i]) sleep(100) ;msgbox(0,"gonna write to", $AllPrefsFiles[$i] & " " & $PrefStrings[$i]) _FileWriteFromArray($AllPrefsFiles[$i],$PrefStrings,1) $FlagForFileWrite = 0 endif EndIf Next EndFunc func FindAllprefs() $searchroot = "c:\documents and settings" ;$Rsearch="\*.js" ; old dont need $RecusiveSearchResults = _Search($searchroot, "prefs.js", "True", -1) ;msgbox(0,"searching for ", "Root dir =" & $searchroot & " file = " & $Rsearch) EndFunc ;Lifted this recursive code from autoIT forums. ; ;### SEARCH FUNCTION ### ;Returns array containing full path of matched files, with [0] containing the count of returned elements. ; ;$s_Path = start location of search, default is "" (current working directory) ; ;$s_Filter = expression to use for file matching (e.g. *.txt), default is "*" ; ;$f_Directories = set to true if directories are to be included in the SEARCH results ; Default set to True ; ;$i_Depth = folder depth to limit search ; -1 -> (Default) no limit, SEARCH in all subfolders ; 0 -> SEARCH current folder only ; n -> SEARCH up to n folders deep ; ;Sample Call ;_Main() ; ;Func _Main() ; Local $iTimer = TimerInit() ; Local $aRET = _Search("c:\documents and settings", "prefs.js", "True", -1) ; $iTimer = TimerDiff($iTimer) ; _ArrayDisplay($aRET, "Time = " & Round($iTimer / 1000, 3) & "sec") ;EndFunc ;==>_Main Func _Search($s_Path = "", $s_Filter = "*", $f_Directories = True, $i_Depth = -1) Local $h_Search, $s_Found, $s_Results = "", $a_Results[1] = [0], $s_RecursePath, $a_Recurse[1] = [0] ; Parameter checks If $s_Path Then If StringRight($s_Path, 1) <> "\" Then $s_Path &= "\" Else $s_Path = @WorkingDir & "\" EndIf ; Get SEARCH handle $h_Search = FileFindFirstFile($s_Path & $s_Filter) If $h_Search <> -1 Then ; Perform SEARCH of this directory While 1 $s_Found = FileFindNextFile($h_Search) If @error Then ExitLoop ; Check for not found If @extended = 1 Then ; Check for directory ; Directory If $f_Directories Then $s_Results &= $s_Path & $s_Found & "\|" Else ; File $s_Results &= $s_Path & $s_Found & "|" EndIf WEnd FileClose($h_Search) EndIf ; check recursion If $i_Depth <> 0 Then $h_Search = FileFindFirstFile($s_Path & "*") While 1 $s_Found = FileFindNextFile($h_Search) If @error Then ExitLoop If @extended Then $a_Recurse = _Search($s_Path & $s_Found, $s_Filter, $f_Directories, $i_Depth - 1) For $n = 1 To $a_Recurse[0] $s_Results &= $a_Recurse[$n] & "|" Next EndIf WEnd FileClose($h_Search) EndIf ; Create return array and return If StringRight($s_Results, 1) = "|" Then $s_Results = StringTrimRight($s_Results, 1) $a_Results = StringSplit($s_Results, "|") EndIf Return $a_Results EndFunc ;==>_Search ; My search functions. JVS ; Written to seek out profiles only withing the users' profile. ; Not good enough..but neat. Leaving this in here for future use if I need to only look in @appdatadir per user ;Get all directories in user's profile path ;@Appdatadir\Mozilla\Profiles\default old ;@Appdatadir\Mozilla\Firefox\Profiles\default new func getprofilepaths() ;Pull profile directories $FileList=_FileListToArray(@AppDataDir & "\Mozilla\Firefox\Profiles\") ;msgbox(0,"profile path",@appdatadir & "\mozilla\firefox\profiles") If @Error=1 Then MsgBox (0,"","No Files\Folders Found.") Exit EndIf ;_ArrayDisplay($FileList,"$FileList") $prefs = "C:\Documents and Settings\" & @UserName & "\Application Data\Mozilla\Firefox\Profiles\" & $filelist[1] & "\prefs.js" EndFunc ;Validate profile directories .default .slt whatever func GetAllProfileDirs () $elementcount = $filelist[0] Dim $validprefsdirs[$elementcount] For $i = 0 to $filelist[0] Select case StringInStr($filelist[$i],"default") $validprefsdirs[$i-1] = $filelist[$i] case StringInStr($filelist[$i],"slt") $validprefsdirs[$i-1] = $filelist[$i] EndSelect Next _arraydisplay($validprefsdirs,"valid prefs directories") ;_ArrayDisplay($filelist,$validprefs) ;msgbox(0,"",$filelist[$validprefsDirs]) endfunc func BuildPrefsTargets() Dim $AllPrefsFiles[$elementcount] ;_arraydisplay($validprefsdirs,"valid prefs building targets") ;msgbox (0,$elementcount, $elementcount) For $i = 0 to $elementcount -1 $AllPrefsFiles[$i]= @AppDataDir & "\Mozilla\Firefox\Profiles\" & $ValidPrefsDirs[$i] & "\prefs.js" Next _ArrayDisplay($AllPrefsFiles,"prefs targets") EndFunc Edited March 29, 2010 by MindSlap
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