1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-27 00:32:07 +01:00

After 2 failed connection attempts file transfer will black list a stream host for 2 hours. SMACK-138

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@4261 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-07-05 18:34:11 +00:00 committed by alex
parent 58254d82aa
commit 386b0abcdd

View file

@ -31,6 +31,7 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smackx.ServiceDiscoveryManager; import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.Bytestream; import org.jivesoftware.smackx.packet.Bytestream;
import org.jivesoftware.smackx.packet.Bytestream.StreamHost; import org.jivesoftware.smackx.packet.Bytestream.StreamHost;
@ -72,6 +73,15 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
protected static final String NAMESPACE = "http://jabber.org/protocol/bytestreams"; protected static final String NAMESPACE = "http://jabber.org/protocol/bytestreams";
/**
* The number of connection failures it takes to a streamhost for that particular streamhost
* to be blacklisted. When a host is blacklisted no more connection attempts will be made to
* it for a period of 2 hours.
*/
private static final int CONNECT_FAILURE_THRESHOLD = 2;
private static final long BLACKLIST_LIFETIME = 60 * 1000 * 120;
public static boolean isAllowLocalProxyHost = true; public static boolean isAllowLocalProxyHost = true;
private final XMPPConnection connection; private final XMPPConnection connection;
@ -88,6 +98,8 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
// locks on the proxy process during its initiatilization process // locks on the proxy process during its initiatilization process
private final Object processLock = new Object(); private final Object processLock = new Object();
private final Cache addressBlacklist = new Cache(100, BLACKLIST_LIFETIME);
public Socks5TransferNegotiator(final XMPPConnection connection) { public Socks5TransferNegotiator(final XMPPConnection connection) {
this.connection = connection; this.connection = connection;
} }
@ -169,9 +181,12 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
} }
/** /**
* @param streamHostsInfo * Selects a host to connect to over which the file will be transmitted.
* @return *
* @throws XMPPException * @param streamHostsInfo the packet recieved from the initiator containing the available hosts
* to transfer the file
* @return the selected host and socket that were created.
* @throws XMPPException when there is no appropriate host.
*/ */
private SelectedHostInfo selectHost(Bytestream streamHostsInfo) private SelectedHostInfo selectHost(Bytestream streamHostsInfo)
throws XMPPException throws XMPPException
@ -181,10 +196,16 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
Socket socket = null; Socket socket = null;
while (it.hasNext()) { while (it.hasNext()) {
selectedHost = (StreamHost) it.next(); selectedHost = (StreamHost) it.next();
String address = selectedHost.getAddress();
// Check to see if this address has been blacklisted
int failures = getConnectionFailures(address);
if(failures >= CONNECT_FAILURE_THRESHOLD) {
continue;
}
// establish socket // establish socket
try { try {
socket = new Socket(selectedHost.getAddress(), selectedHost socket = new Socket(address, selectedHost
.getPort()); .getPort());
establishSOCKS5ConnectionToProxy(socket, createDigest( establishSOCKS5ConnectionToProxy(socket, createDigest(
streamHostsInfo.getSessionID(), streamHostsInfo streamHostsInfo.getSessionID(), streamHostsInfo
@ -193,6 +214,7 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
} }
catch (IOException e) { catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
incrementConnectionFailures(address);
selectedHost = null; selectedHost = null;
socket = null; socket = null;
} }
@ -205,6 +227,22 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
return new SelectedHostInfo(selectedHost, socket); return new SelectedHostInfo(selectedHost, socket);
} }
private void incrementConnectionFailures(String address) {
Integer count = (Integer) addressBlacklist.get(address);
if(count == null) {
count = new Integer(1);
}
else {
count = new Integer(count.intValue() + 1);
}
addressBlacklist.put(address, count);
}
private int getConnectionFailures(String address) {
Integer count = (Integer) addressBlacklist.get(address);
return (count != null ? count.intValue() : 0);
}
/** /**
* Creates the digest needed for a byte stream. It is the SHA1(sessionID + * Creates the digest needed for a byte stream. It is the SHA1(sessionID +
* initiator + target). * initiator + target).