View Single Post
  #1 (permalink)  
Old August 28th, 2006
blahblahblabh blahblahblabh is offline
Novicius
 
Join Date: August 28th, 2006
Posts: 1
blahblahblabh is flying high
Default quick and dirty perl peerguardian to phex IP block list converter

it's not polished at all and frankly I don't know if it's even importing the entirety of the massive peerguardian list, but I made it and figured maybe some others would use it too.
Don't blame me if anything bad happens, this program was written hastily for my own use. It WILL NOT warn you if you attempt to overwrite an existing file! The only potentially dangerous line is the one where you set the output file path. GET IT RIGHT!
For this reason i'm leaving it without a file path prompt, hoping that people smart enough to edit the line of code will be smart enough not to overwrite their files. This program is also not smart enough to keep your existing rules so you will have to redo them. For those adventurous enough to try it, just place the code into a file(.pl extension on windows), set the paths, and run it from DOS prompt(assuming Perl is installed). Linux users will need to put in the shebang line cuz i'm too lazy to. I tried to comment as best I could but the programming is still hackish and kinda hard to understand. Theres also a good chance I created a couple syntax errors while transferring it to the forum. Perhaps a good future Phex feature(haha that rhymes) would be to support importing peerguardian block lists? I would probably do it but I can't program C .
Code:
open (FROM,'C:/documents and settings/bob/desktop/list.p2p');#change this to the path of the exported peerguardian list
open (TO,'>C:/Documents and Settings/bob/phex/security.xml');#change this to the path of phex's security.xml (usually in documents and settings under the current user)
$totalnum=0;
$totalnum++ while(<FROM>);#counts lines in file
seek(FROM,0,0);#resets read pointer
print TO <<HERE;#here document pastes default beginning of file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<phex phex-version="2.8.10.98">
    <security>
HERE
$counter=0;
$time=time();#so I can print status every few seconds
while(<FROM>)#goes line by line
{
	m/(.*):(.*)-(.*)\n/;#seperates PG listing into addresses and description
	@ary=map {uc(sprintf("%x",$_))} split(/\./,$2);#convert starting IP adress to hex(format Phex uses) and makes it uppercase
	foreach(@ary)#adds a 0 if neccesary to adress
	{
		$_="0$_" if(length $_==1);
	}
	$startad=join "",@ary;#completed starting adress
	@ary=map {uc(sprintf("%x",$_))} split(/\./,$3);#convert ending IP adress to hex(format Phex uses) and makes it uppercase
	foreach(@ary)#adds a 0 if neccesary to adress
	{
		$_="0$_" if(length $_==1);
	}
	$endad=join "",@ary;#completed ending adress
	$desc=$1;
	$desc=~y/a-zA-Z0-9. -//cd;#gets rid of chars in the PG descriptions that Phex doesn't like
	push (@adresses,"        <ip-access-rule>
            <description>$desc</description>
            <isDenyingRule>true</isDenyingRule>
            <isDisabled>false</isDisabled>
            <triggerCount>0</triggerCount>
            <expiryDate>9223372036854775807</expiryDate>
            <isDeletedOnExpiry>false</isDeletedOnExpiry>
            <addressType>3</addressType>
            <ip>$startad</ip>
            <compareIP>$endad</compareIP>
        </ip-access-rule>
");#this huge line is what prints completed rule entries
	$counter++;#keeps track of how many lines are done
	if(time()>$time+1)#prints status
	{
		$time=time();
		$cent=int($counter/$totalnum*100);
		print "$cent % done $counter records out of $totalnum\n";
	}
	if(!($counter%4000))#flushes lines to disk every 4000(speed optimization)
	{
		print TO join( "",@adresses);
		@adresses=();
	}
}
print TO <<HERE;#prints default file ending
    </security>
</phex>
HERE
#the newline at the end of HERE was really important so I put a comment here to force you to leave the newline
EDIT::removed default rules lines

Last edited by blahblahblabh; August 29th, 2006 at 03:20 PM.
Reply With Quote