Skip to content

Instantly share code, notes, and snippets.

@lukespragg
Created September 27, 2014 23:59
Show Gist options
  • Save lukespragg/6325f4a0b6ee2515e810 to your computer and use it in GitHub Desktop.
Save lukespragg/6325f4a0b6ee2515e810 to your computer and use it in GitHub Desktop.
Internet Explorer and Firefox proxy settings script.
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/CurrentVersion/Internet Settings"};
$rkey->SetValue( "ProxyServer", "$server\:port", "REG_SZ" );
$rkey->SetValue( "ProxyEnable",pack("L",$enable), "REG_DWORD" );
$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, and 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment