BlockIPUtils.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
omschaub.stuffer.main |
![]() |
![]() |
AZCVSUpdater |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Created on Oct 1, 2005
* Created by omschaub
*
*/
package omschaub.stuffer.main;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.regex.*;
import omschaub.stuffer.containers.ClientBlock;
import omschaub.stuffer.containers.IP;
//import omschaub.stuffer.containers.TotalPeerContainer;
import org.eclipse.swt.graphics.RGB;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.download.Download;
import org.gudy.azureus2.plugins.download.DownloadException;
import org.gudy.azureus2.plugins.download.DownloadManager;
import org.gudy.azureus2.plugins.download.DownloadManagerListener;
import org.gudy.azureus2.plugins.download.DownloadPeerListener;
import org.gudy.azureus2.plugins.ipfilter.IPFilter;
import org.gudy.azureus2.plugins.ipfilter.IPRange;
import org.gudy.azureus2.plugins.peers.Peer;
import org.gudy.azureus2.plugins.peers.PeerEvent;
import org.gudy.azureus2.plugins.peers.PeerListener2;
import org.gudy.azureus2.plugins.peers.PeerManager;
import org.gudy.azureus2.plugins.peers.PeerManagerListener;
import org.gudy.azureus2.pluginsimpl.local.ipfilter.IPFilterImpl;
public class BlockIPUtils {
private static int total30;
public static void startMainClientCheck(){
try{
final PluginInterface pluginInterface = Plugin.getPluginInterface();
DownloadManager dm = pluginInterface.getDownloadManager();
//Setting up the main listener
final PeerManagerListener pml = new PeerManagerListener()
{
public void
peerAdded(
final PeerManager manager,
final Peer peer )
{
IP peerIP = new IP(peer.getIp());
/* if(!Plugin.totalPeer_set.isPresent(peerIP)){
TotalPeerContainer tpc = new TotalPeerContainer(peerIP,Plugin.getPluginInterface().getUtilities().getCurrentSystemTime());
Plugin.totalPeer_set.addToMap(tpc);
}else{
TotalPeerContainer tpc = Plugin.totalPeer_set.getTotalPeerContainer(peerIP);
tpc.setConnectionAttemptsPlusOne();
tpc.setLastConnectionTime(Plugin.getPluginInterface().getUtilities().getCurrentSystemTime());
if(tpc.rule1(peer.getPercentDoneInThousandNotation())){
try {
makeIPFilterRule(peer.getIp(),getPeerID(peer.getId()),peer.getClient(),manager.getDownload().getName(),"r255,g255,b255");
} catch (DownloadException e) {
e.printStackTrace();
}
}
}
*/
peer.addListener(new PeerListener2(){
public void eventOccurred(PeerEvent event) {
if(event.getType() == PeerEvent.ET_STATE_CHANGED){
if(Plugin.areRulesPaused) return;
if(((Integer)event.getData()).intValue() == Peer.TRANSFERING){
try{
if(Plugin.getPluginInterface().getPluginconfig().getPluginBooleanParameter("Stuffer_Block_Seeder",false)){
System.out.println(" % done: " + peer.getPercentDoneInThousandNotation());
if(peer.isSeed() || peer.getPercentDoneInThousandNotation() == 1000){
//System.out.println(" % done: " + peer.getPercentDoneInThousandNotation());
String color = Plugin.getPluginInterface().getPluginconfig().getPluginStringParameter("Stuffer_Block_Seeder_Color","r255g0b0");
//System.out.println("Seeder Color: " + color);
if(!color.equalsIgnoreCase("null")){
String peerID = getPeerID(peer.getId());
String clientName = peer.getClient();
// peer.setSnubbed(true);
makeIPFilterRule(peer.getIp(),"Seeder: " + peerID,clientName + " Seeder",manager.getDownload().getName(), color);
Constants.SEEDER_CLIENT_BLOCKS++;
Plugin.getTab2().renumberMOD_Seeder(false, true);
}
}
}
if(Plugin.getPluginInterface().getPluginconfig().getPluginBooleanParameter("Stuffer_Block_Mods",true)){
Pattern p = Pattern.compile("Azureus (?:[^0-2]|[^01]\\.[^0-2]|[^01]\\.[^01]\\.[^0]|[^01]\\.[^01]\\.[\\d]\\.[^0-2])");
Matcher m = p.matcher(peer.getClient());
if(m.find()){
//System.out.println("Inside RegEx Expression: " + peer.getClient());
if(!peer.supportsMessaging()){
String clientName = peer.getClient();
System.out.println("Found MOD client " + clientName + " : " + peer.getIp() + " at " + Utils.getCurrentTime());
try {
if(manager != null){
String color = Plugin.getPluginInterface().getPluginconfig().getPluginStringParameter("Stuffer_Block_Mods_Color","r255g0b0");
//String peerID = Plugin.getPluginInterface().getUtilities().getFormatters().encodeBytesToString(peer.getId());
if(!color.equalsIgnoreCase("null")){
String peerID = getPeerID(peer.getId());
// peer.setSnubbed(true);
makeIPFilterRule(peer.getIp(),"MOD: " + peerID,clientName + " MOD",manager.getDownload().getName(), color);
Constants.MOD_CLIENT_BLOCKS++;
Plugin.getTab2().renumberMOD_Seeder(true,false);
}
}
} catch (DownloadException e) {
e.printStackTrace();
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
try {
if(manager != null){
String clientName = peer.getClient();
String color = mainRuleRun(clientName,manager.isSuperSeeding(), manager.getDownload().getState());
if(!color.equalsIgnoreCase("null")){
String peerID = getPeerID(peer.getId());
/*String peerID;
try {
peerID = new String(peer.getId(),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
peerID = "Normal Peer";
}*/
//String peerID = Plugin.getPluginInterface().getUtilities().getFormatters().encodeBytesToString(peer.getId());
//peer.setSnubbed(true);
makeIPFilterRule(peer.getIp(),peerID,clientName,manager.getDownload().getName(), color);
}
}
} catch (DownloadException e) {
e.printStackTrace();
}
}
}else if(event.getType() == PeerEvent.ET_BAD_CHUNK){
System.out.println(" sentBadChunk listener has been triggered by " + peer.getIp() + " : " + peer.getClient());
/*if(arg1 >= 1){
System.out.println("Peer has sent a bad chunk: " + peer.getClient() + peer.getIp());
if(Plugin.getPluginInterface().getPluginconfig().getPluginBooleanParameter("Stuffer_Block_Bad",true)){
try {
String color = Plugin.getPluginInterface().getPluginconfig().getPluginStringParameter("Stuffer_Block_Bad_Color","r0g255b0");
if(!color.equalsIgnoreCase("null")){
makeIPFilterRule(peer.getIp(),"Bad Data Peer",peer.getClient(),manager.getDownload().getName(), color);
Constants.BAD_CLIENT_BLOCKS++;
Tab2Utilities.renumberMOD_BAD(false,true);
}
} catch (DownloadException e) {
e.printStackTrace();
}
}
}*/
}
}//End of Event Occurred
});
}
public void
peerRemoved(
PeerManager manager,
Peer peer )
{
IP peerIP = new IP(peer.getIp());
/* TotalPeerContainer tpc = Plugin.totalPeer_set.getTotalPeerContainer(peerIP);
if(tpc != null){
tpc.setDisconnectsPlusOne();
tpc.setPercentDone(peer.getPercentDoneInThousandNotation());
}*/
if(peer != null){
if(peer.getState() != 50){
total30++;
System.out.println("peer: " + peer.getIp() + " State: " + peer.getState() + " Total: " + total30);
}
}
}
};
dm.addListener(
new DownloadManagerListener()
{
public void
downloadAdded(
final Download download )
{
download.addPeerListener(
new DownloadPeerListener()
{
public void
peerManagerAdded(
Download download,
final PeerManager peer_manager )
{
peer_manager.addListener(pml);
}
public void
peerManagerRemoved(
Download download,
PeerManager peer_manager )
{
peer_manager.removeListener(pml);
}
});
}
public void
downloadRemoved(
Download download )
{
}
});
}catch( Throwable e ){
e.printStackTrace();
}
}
/**
* Main method to make a IPFilter rule and report it to the GUI if it is running
* **Be sure to snub the peer BEFORE entering this code!
*
* @param peerIP
* @param peerID
* @param peerClient
* @param downloadName
* @param color
*/
public static void makeIPFilterRule(final String peerIP,
final String peerID,
final String peerClient,
final String downloadName,
final String color){
Thread makeFilter = new Thread() {
public void run() {
try{
IPFilter ipf = new IPFilterImpl();
RGB rgb = Utils.getRGB(color);
if (!ipf.isInRange(peerIP)){
//peer.setSnubbed(true);
//System.out.println(Utils.parseString(peerID) + " : Blocked -- Peer: " + peerClient);
IPRange ipr = Plugin.getPluginInterface().getIPFilter().createRange(true);
ipr.setStartIP(peerIP);
ipr.setEndIP(peerIP);
ipr.setDescription("Stuffer - " + Utils.parseString(peerClient) + " Killed"+ " c#" + Utils.getHexfromRGB(rgb));
ipr.checkValid();
ipf.addRange(ipr);
if(Plugin.getTab1() != null){
Plugin.getTab1().addElement(peerIP,peerID,peerClient,downloadName, color);
Plugin.getTab1().totalChange();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
};
makeFilter.setDaemon(true);
makeFilter.start();
}
/**
* Parse out the nasty stuff from the PeerID and return a nice human readable string
* @param peerBytes
* @return
* @throws UnsupportedEncodingException
*/
public static String getPeerID (byte[] peerBytes){
for (int i = 0; i < peerBytes.length; i++) {
int b = (0xFF & peerBytes[i]);
if (b < 32 || b > 127)
peerBytes[i] = '-';
}
String peerID;
try {
//peerID = new String(peerBytes,"ISO-8859-1");
peerID = new String(peerBytes);
return peerID;
} catch (Exception e) {
e.printStackTrace();
return "Error Parsing ID";
}
}
/**
* Main Rule Run.. it will test all the rules from the clientList
* @param peerClientID
* @param downloadName
* @param isSuperSeeding
* @param downloadState
*/
public static String mainRuleRun(final String peerClient,
final boolean isSuperSeeding,
final int downloadState){
if(Plugin.areRulesPaused)
return "null";
try{
Iterator it = Plugin.clientBlock_set.getIterator();
//System.out.println(Plugin.clientBlock_set.getSize());
while(it.hasNext()){
ClientBlock cb = (ClientBlock)it.next();
//check if the clientName is RegEx or not
if(!cb.get_isRegEx()){
//is Regular String
if(stringContains(peerClient.toLowerCase(),cb.getClientName().toLowerCase())){
//match.. so block according to the rules
//check for superseeding block
if(cb.get_isSuperseeding()){
//positive for superseeding only.. now check if torrent is superseeding
if(isSuperSeeding){
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
}
return "null";
}else{
//check for rule for download/upload
//first see if both are postive, and if so, block the peer out right
if(cb.get_isDownloading() && cb.get_isUploading()){
//both positive, so block away
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}else{
//both are not positve.. so need to step through and see which one is
try {
if(downloadState == Download.ST_DOWNLOADING){
//torrent of peer is downloading
//now see if the rule is there to block it
if(cb.get_isDownloading()){
//positive for the rule, so block
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}
return "null";
}else if(downloadState == Download.ST_SEEDING){
//torrent of peer is seeding
//now see if the rule is there to block it
if(cb.get_isUploading()){
//positve for the rule,so block
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}
return "null";
}else{
return "null";
}
} catch (Exception e) {
e.printStackTrace();
return "null";
}//end of single upload or download check
}//end of upload/download check
}//end of major superseed vs up/down check
}//end of regular string match
}else{
//isRegEx
Pattern p = Pattern.compile(cb.getClientName());
Matcher m = p.matcher(peerClient);
//System.out.println("INPUT: " + peer.getClient() + " : " + "Regex: " + p.pattern() + " : " + m.matches());
if(m.find()){
//check for superseeding block
if(cb.get_isSuperseeding()){
//positive for superseeding only.. now check if torrent is superseeding
if(isSuperSeeding){
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}
return "null";
}else{
//check for rule for download/upload
//first see if both are postive, and if so, block the peer out right
if(cb.get_isDownloading() && cb.get_isUploading()){
//both positive, so block away
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}else{
//both are not positve.. so need to step through and see which one is
try {
if(downloadState == Download.ST_DOWNLOADING){
//torrent of peer is downloading
//now see if the rule is there to block it
if(cb.get_isDownloading()){
//positive for the rule, so block
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}else{
return "null";
}
}else if(downloadState == Download.ST_SEEDING){
//torrent of peer is seeding
//now see if the rule is there to block it
if(cb.get_isUploading()){
//positve for the rule,so block
//makeIPFilterRule(peerIP,peerID,peerClient,downloadName,clientList[i][6]);
Utils.changeHits(cb.getIndex(), 1);
return cb.getColor();
}else{
return "null";
}
}else{
return "null";
}
} catch (Exception e) {
e.printStackTrace();
return "null";
}//end of single upload or download check
}//end of upload/download check
}//end of major superseed vs up/down check
}//end of regEx match
}//end of simple string vs regex
}//end of MAIN checking FOR statement
}catch(PatternSyntaxException e){
String message = "RegEx Pattern Error - Check readme for details on how to correctly make a RegEx \n\n" + "Caused by: " + e.getDescription() + "\n\n" + e.getLocalizedMessage();
Plugin.getTab2Utilities().openErrorMessageBox("RegEx Error",message);
e.printStackTrace();
return "null";
}catch(Exception e){
e.printStackTrace();
return "null";
}
return "null";
}
/**
* Java 1.5 alternative to String.contains
*
* @param String inputLine
* @param String searchLine
* @return
*/
public static boolean stringContains(String inputLine, String searchLine){
boolean answer = false;
int nPos = 0;
while(true){
int nIndex = inputLine.indexOf(searchLine,nPos);
if(nIndex < 0){
break;
}
else
nPos = nIndex + searchLine.length();
answer = true;
}
return answer;
}
}//EOF
The table below shows all metrics for BlockIPUtils.java.




