📄 ipfilterimpl.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: IpFilterImpl.java
package org.gudy.azureus2.core3.ipfilter.impl;
import com.aelitis.azureus.core.util.CopyOnWriteList;
import java.io.*;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.config.ParameterListener;
import org.gudy.azureus2.core3.ipfilter.*;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.tracker.protocol.PRHelpers;
import org.gudy.azureus2.core3.util.*;
// Referenced classes of package org.gudy.azureus2.core3.ipfilter.impl:
// BannedIpImpl, BlockedIpImpl, IPAddressRangeManager, IpFilterAutoLoaderImpl,
// IpRangeImpl
public class IpFilterImpl
implements IpFilter
{
private static final LogIDs LOGID;
private static final long BAN_IP_PERSIST_TIME = 0x240c8400L;
private static final int MAX_BLOCKS_TO_REMEMBER = 500;
private static IpFilterImpl ipFilter;
private static AEMonitor class_mon = new AEMonitor("IpFilter:class");
private IPAddressRangeManager range_manager;
private Map bannedIps;
private LinkedList ipsBlocked;
private int num_ips_blocked;
private int num_ips_blocked_loggable;
private long last_update_time;
private List listeners;
private CopyOnWriteList external_handlers;
FrequencyLimitedDispatcher blockedListChangedDispatcher;
private IpFilterAutoLoaderImpl ipFilterAutoLoader;
private boolean ip_filter_enabled;
private boolean ip_filter_allow;
private IpFilterImpl()
{
range_manager = new IPAddressRangeManager();
num_ips_blocked = 0;
num_ips_blocked_loggable = 0;
listeners = new ArrayList();
external_handlers = new CopyOnWriteList();
COConfigurationManager.addAndFireParameterListeners(new String[] {
"Ip Filter Allow", "Ip Filter Enabled"
}, new ParameterListener() {
final IpFilterImpl this$0;
public void parameterChanged(String parameterName)
{
ip_filter_enabled = COConfigurationManager.getBooleanParameter("Ip Filter Enabled");
ip_filter_allow = COConfigurationManager.getBooleanParameter("Ip Filter Allow");
}
{
this$0 = IpFilterImpl.this;
super();
}
});
ipFilter = this;
bannedIps = new HashMap();
ipsBlocked = new LinkedList();
blockedListChangedDispatcher = new FrequencyLimitedDispatcher(new AERunnable() {
final IpFilterImpl this$0;
public void runSupport()
{
Object listenersArray[] = listeners.toArray();
for (int i = 0; i < listenersArray.length; i++)
try
{
IPFilterListener l = (IPFilterListener)listenersArray[i];
l.IPBlockedListChanged(IpFilterImpl.this);
}
catch (Exception e)
{
Debug.out(e);
}
}
{
this$0 = IpFilterImpl.this;
super();
}
}, 10000);
ipFilterAutoLoader = new IpFilterAutoLoaderImpl(this);
try
{
loadBannedIPs();
}
catch (Throwable e)
{
Debug.printStackTrace(e);
}
try
{
loadFilters(true, true);
}
catch (Exception e)
{
Debug.printStackTrace(e);
}
COConfigurationManager.addParameterListener(new String[] {
"Ip Filter Allow", "Ip Filter Enabled"
}, new ParameterListener() {
final IpFilterImpl this$0;
public void parameterChanged(String parameterName)
{
markAsUpToDate();
}
{
this$0 = IpFilterImpl.this;
super();
}
});
}
public static IpFilter getInstance()
{
IpFilterImpl ipfilterimpl;
class_mon.enter();
if (ipFilter == null)
ipFilter = new IpFilterImpl();
ipfilterimpl = ipFilter;
class_mon.exit();
return ipfilterimpl;
Exception exception;
exception;
class_mon.exit();
throw exception;
}
public File getFile()
{
return FileUtil.getUserFile("filters.config");
}
public void reload()
throws Exception
{
reload(true);
}
public void reload(boolean allowAsyncDownloading)
throws Exception
{
range_manager.clearAllEntries();
markAsUpToDate();
loadFilters(allowAsyncDownloading, false);
}
public void save()
throws Exception
{
Map map;
FileOutputStream fos;
class_mon.enter();
map = new HashMap();
List filters = new ArrayList();
map.put("ranges", filters);
List entries = range_manager.getEntries();
Iterator iter = entries.iterator();
do
{
if (!iter.hasNext())
break;
IpRange range = (IpRange)iter.next();
if (range.isValid() && !range.isSessionOnly())
{
String description = range.getDescription();
String startIp = range.getStartIp();
String endIp = range.getEndIp();
Map mapRange = new HashMap();
mapRange.put("description", description.getBytes("UTF-8"));
mapRange.put("start", startIp);
mapRange.put("end", endIp);
filters.add(mapRange);
}
} while (true);
fos = null;
File filtersFile = FileUtil.getUserFile("filters.config");
fos = new FileOutputStream(filtersFile);
fos.write(BEncoder.encode(map));
if (fos != null)
fos.close();
break MISSING_BLOCK_LABEL_237;
Exception exception;
exception;
if (fos != null)
fos.close();
throw exception;
class_mon.exit();
break MISSING_BLOCK_LABEL_257;
Exception exception1;
exception1;
class_mon.exit();
throw exception1;
}
private void loadFilters(boolean allowAsyncDownloading, boolean loadOldWhileAsyncDownloading)
throws Exception
{
long startTime;
startTime = System.currentTimeMillis();
ipFilterAutoLoader.loadOtherFilters(allowAsyncDownloading, loadOldWhileAsyncDownloading);
if (getNbRanges() > 0)
Logger.log(new LogEvent(LOGID, (new StringBuilder()).append(System.currentTimeMillis() - startTime).append("ms for ").append(getNbRanges()).append(". now loading norm").toString()));
List new_ipRanges;
FileInputStream fin;
BufferedInputStream bin;
class_mon.enter();
new_ipRanges = new ArrayList(1024);
fin = null;
bin = null;
File filtersFile = FileUtil.getUserFile("filters.config");
if (filtersFile.exists())
{
fin = new FileInputStream(filtersFile);
bin = new BufferedInputStream(fin, 16384);
Map map = BDecoder.decode(bin);
List list = (List)map.get("ranges");
IpRangeImpl ipRange;
for (Iterator iter = list.listIterator(); iter.hasNext(); new_ipRanges.add(ipRange))
{
Map range = (Map)iter.next();
String description = new String((byte[])(byte[])range.get("description"), "UTF-8");
String startIp = new String((byte[])(byte[])range.get("start"));
String endIp = new String((byte[])(byte[])range.get("end"));
ipRange = new IpRangeImpl(description, startIp, endIp, false);
ipRange.setAddedToRangeList(true);
}
}
if (bin != null)
try
{
bin.close();
}
catch (Throwable e) { }
if (fin != null)
try
{
fin.close();
}
catch (Throwable e) { }
for (Iterator it = new_ipRanges.iterator(); it.hasNext(); ((IpRange)it.next()).checkValid());
markAsUpToDate();
break MISSING_BLOCK_LABEL_443;
Exception exception;
exception;
if (bin != null)
try
{
bin.close();
}
catch (Throwable e) { }
if (fin != null)
try
{
fin.close();
}
catch (Throwable e) { }
for (Iterator it = new_ipRanges.iterator(); it.hasNext(); ((IpRange)it.next()).checkValid());
markAsUpToDate();
throw exception;
class_mon.exit();
break MISSING_BLOCK_LABEL_463;
Exception exception1;
exception1;
class_mon.exit();
throw exception1;
Logger.log(new LogEvent(LOGID, (new StringBuilder()).append(System.currentTimeMillis() - startTime).append("ms to load all IP Filters").toString()));
return;
}
protected void loadBannedIPs()
{
if (!COConfigurationManager.getBooleanParameter("Ip Filter Banning Persistent"))
return;
class_mon.enter();
Map map = FileUtil.readResilientConfigFile("banips.config");
List ips = (List)map.get("ips");
if (ips != null)
{
long now = SystemTime.getCurrentTime();
for (int i = 0; i < ips.size(); i++)
{
Map entry = (Map)ips.get(i);
String ip = new String((byte[])(byte[])entry.get("ip"));
String desc = new String((byte[])(byte[])entry.get("desc"), "UTF-8");
Long ltime = (Long)entry.get("time");
long time = ltime.longValue();
boolean drop = false;
if (time > now)
time = now;
else
if (now - time >= 0x240c8400L)
{
drop = true;
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 0, (new StringBuilder()).append("Persistent ban dropped as too old : ").append(ip).append(", ").append(desc).toString()));
}
if (!drop)
{
int int_ip = range_manager.addressToInt(ip);
bannedIps.put(new Integer(int_ip), new BannedIpImpl(ip, desc, time));
}
}
}
class_mon.exit();
break MISSING_BLOCK_LABEL_307;
Throwable e;
e;
Debug.printStackTrace(e);
class_mon.exit();
break MISSING_BLOCK_LABEL_307;
Exception exception;
exception;
class_mon.exit();
throw exception;
}
protected void saveBannedIPs()
{
if (!COConfigurationManager.getBooleanParameter("Ip Filter Banning Persistent"))
return;
class_mon.enter();
Map map = new HashMap();
List ips = new ArrayList();
Map entry;
for (Iterator it = bannedIps.values().iterator(); it.hasNext(); ips.add(entry))
{
BannedIpImpl bip = (BannedIpImpl)it.next();
entry = new HashMap();
entry.put("ip", bip.getIp());
entry.put("desc", bip.getTorrentName().getBytes("UTF-8"));
entry.put("time", new Long(bip.getBanningTime()));
}
map.put("ips", ips);
FileUtil.writeResilientConfigFile("banips.config", map);
class_mon.exit();
break MISSING_BLOCK_LABEL_194;
Throwable e;
e;
Debug.printStackTrace(e);
class_mon.exit();
break MISSING_BLOCK_LABEL_194;
Exception exception;
exception;
class_mon.exit();
throw exception;
}
public boolean isInRange(String ipAddress)
{
return isInRange(ipAddress, "", null);
}
public boolean isInRange(String ipAddress, String torrent_name, byte torrent_hash[])
{
return isInRange(ipAddress, torrent_name, torrent_hash, true);
}
public boolean isInRange(String ipAddress, String torrent_name, byte torrent_hash[], boolean loggable)
{
if (isBanned(ipAddress))
return true;
if (!isEnabled())
return false;
if (ipAddress.equals("127.0.0.1"))
return false;
if (ipAddress.indexOf(":") != -1)
return false;
if (AddressUtils.isLANLocalAddress(ipAddress) != 2)
return false;
boolean allow = ip_filter_allow;
IpRange match = (IpRange)range_manager.isInRange(ipAddress);
if (match == null || allow)
{
IpRange explict_deny = checkExternalHandlers(torrent_hash, ipAddress);
if (explict_deny != null)
{
match = explict_deny;
allow = false;
}
}
if (match != null)
if (!allow)
{
if (AENetworkClassifier.categoriseAddress(ipAddress) != "Public")
return false;
if (addBlockedIP(new BlockedIpImpl(ipAddress, match, torrent_name, loggable), torrent_hash, loggable))
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocked : ").append(ipAddress).append(", in range : ").append(match).toString()));
return true;
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocking Denied : ").append(ipAddress).append(", in range : ").append(match).toString()));
return false;
} else
{
return false;
}
if (allow)
{
if (AENetworkClassifier.categoriseAddress(ipAddress) != "Public")
return false;
if (addBlockedIP(new BlockedIpImpl(ipAddress, null, torrent_name, loggable), torrent_hash, loggable))
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocked : ").append(ipAddress).append(", not in any range").toString()));
return true;
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocking Denied : ").append(ipAddress).append(", not in any range").toString()));
return false;
} else
{
return false;
}
}
public boolean isInRange(InetAddress ipAddress, String torrent_name, byte torrent_hash[], boolean loggable)
{
if (isBanned(ipAddress))
return true;
if (!isEnabled())
return false;
if (ipAddress.isLoopbackAddress() || ipAddress.isLinkLocalAddress() || ipAddress.isSiteLocalAddress())
return false;
if (ipAddress instanceof Inet6Address)
return false;
if (AddressUtils.isLANLocalAddress(ipAddress) != 2)
return false;
boolean allow = ip_filter_allow;
IpRange match = (IpRange)range_manager.isInRange(ipAddress);
if (match == null || allow)
{
IpRange explicit_deny = checkExternalHandlers(torrent_hash, ipAddress);
if (explicit_deny != null)
{
match = explicit_deny;
allow = false;
}
}
if (match != null)
if (!allow)
{
if (addBlockedIP(new BlockedIpImpl(ipAddress.getHostAddress(), match, torrent_name, loggable), torrent_hash, loggable))
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocked : ").append(ipAddress).append(", in range : ").append(match).toString()));
return true;
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocking Denied: ").append(ipAddress).append(", in range : ").append(match).toString()));
return false;
} else
{
return false;
}
if (allow)
{
if (addBlockedIP(new BlockedIpImpl(ipAddress.getHostAddress(), null, torrent_name, loggable), torrent_hash, loggable))
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocked : ").append(ipAddress).append(", not in any range").toString()));
return true;
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, 1, (new StringBuilder()).append("Ip Blocking Denied : ").append(ipAddress).append(", not in any range").toString()));
return false;
} else
{
return false;
}
}
protected IpRange checkExternalHandlers(byte torrent_hash[], String address)
{
label0:
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -