Thanks for the replies! Here is the final code that sets both IE and Firefox proxy settings.
use strict;
use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 );
&setProxy();
###############
sub setProxy{
#Set access to use proxy server (IE)
#http://nscsysop.hypermart.net/setproxy.html
my $server=shift || "localhost";
my $port=shift || 8080;
my $enable=shift || 1;
my $override=shift || "127.0.0.1;<local>";
#set IE Proxy
my $rkey=$Registry->{"CUser/Software/Microsoft/Windows/CurrentVers
+ion/Internet Settings"};
$rkey->SetValue( "ProxyServer" , "$server\:port" , "REG_SZ"
+ );
$rkey->SetValue( "ProxyEnable" ,pack("L",$enable) , "REG_DWOR
+D" );
$rkey->SetValue( "ProxyOverride" , $override , "REG_SZ"
+ );
#Change prefs.js file for mozilla
#http://www.mozilla.org/support/firefox/edit
if(-d "$ENV{APPDATA}\\Mozilla\\Firefox\\Profiles"){
my $mozdir="$ENV{APPDATA}\\Mozilla\\Firefox\\Profiles";
opendir(DIR,$mozdir) || return "opendir Error: $!";
my @pdirs=grep(/\w/,readdir(DIR));
close(DIR);
foreach my $pdir (@pdirs){
next if !-d "$mozdir\\$pdir";
next if $pdir !~/\.default$/is;
my @lines=();
my %Prefs=(
"network\.proxy\.http" => "\"$server\"",
"network\.proxy\.http_port" => $port,
"network\.proxy\.type" => $enable,
);
if(open(FH,"$mozdir\\$pdir\\prefs.js")){
@lines=<FH>;
close(FH);
my $cnt=@lines;
#Remove existing proxy settings
for(my $x=0;$x<$cnt;$x++){
my $line=strip($lines[$x]);
next if $line !~/^user_pref/is;
foreach my $key (%Prefs){
if($line=~/\"$key\"/is){
delete($lines[$x]);
}
}
}
}
if(open(FH,">$mozdir\\$pdir\\prefs.js")){
binmode(FH);
#print "Writing $mozdir\\$pdir\\prefs.js\n";
foreach my $line (@lines){
$line=strip($line);
print FH "$line\r\n";
}
foreach my $key (sort(keys(%Prefs))){
print FH qq|user_pref("$key",$Prefs{$key});\r\n|;
}
close(FH);
return 1;
}
}
}
return 0;
}
###############
sub strip{
#usage: $str=strip($str);
#info: strips off beginning and endings returns, newlines, tabs, a
+nd spaces
my $str=shift;
if(length($str)==0){return;}
$str=~s/^[\r\n\s\t]+//s;
$str=~s/[\r\n\s\t]+$//s;
return $str;
}
|