-SmartProxy
This commit is contained in:
tonikelope 2018-05-20 20:32:32 +02:00
parent b160bff5e5
commit cd73aa5fb6
6 changed files with 23 additions and 67 deletions

View File

@ -118,9 +118,7 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
if (error && current_proxy != null) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "{0} Worker [{1}]: excluding proxy -> {2}", new Object[]{Thread.currentThread().getName(), _id, current_proxy});
MainPanel.getProxy_manager().excludeProxy(current_proxy);
MainPanel.getProxy_manager().removeProxy(current_proxy);
}
current_proxy = MainPanel.getProxy_manager().getFastestProxy();
@ -152,9 +150,9 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
HttpGet httpget = new HttpGet(new URI(chunk.getUrl()));
error = false;
error = true;
error509 = false;
error509 = true;
if (getDownload().isError509()) {
getDownload().getView().set509Error(false);

View File

@ -48,7 +48,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
*/
public final class MainPanel {
public static final String VERSION = "4.0";
public static final String VERSION = "4.1";
public static final int THROTTLE_SLICE_SIZE = 16 * 1024;
public static final int DEFAULT_BYTE_BUFFER_SIZE = 16 * 1024;
public static final int STREAMER_PORT = 1337;

View File

@ -312,7 +312,7 @@ public final class MegaAPI {
Logger.getLogger(MiscTools.class.getName()).log(Level.WARNING, "{0}: excluding proxy -> {1}", new Object[]{Thread.currentThread().getName(), current_proxy});
MainPanel.getProxy_manager().excludeProxy(current_proxy);
MainPanel.getProxy_manager().removeProxy(current_proxy);
}
current_proxy = MainPanel.getProxy_manager().getFastestProxy();

View File

@ -820,7 +820,7 @@ public final class MiscTools {
Logger.getLogger(MiscTools.class.getName()).log(Level.WARNING, "{0}: excluding proxy -> {1}", new Object[]{Thread.currentThread().getName(), current_proxy});
MainPanel.getProxy_manager().excludeProxy(current_proxy);
MainPanel.getProxy_manager().removeProxy(current_proxy);
}
current_proxy = MainPanel.getProxy_manager().getFastestProxy();

View File

@ -11,8 +11,6 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import static com.tonikelope.megabasterd.MainPanel.THREAD_POOL;
import static com.tonikelope.megabasterd.MiscTools.getApacheKissHttpClient;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@ -23,12 +21,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
*/
public class SmartMegaProxyManager implements Runnable {
public static final int PROXY_TIMEOUT = 30;
public static final int PROXY_MAX_EXCLUDE_COUNTER = 5;
public static final int PROXY_EXCLUDE_SECS = 10;
public static final int PROXY_TIMEOUT = 15;
private volatile String _proxy_list_url;
private final ConcurrentLinkedQueue<String> _proxy_list;
private final ConcurrentHashMap<String, HashMap> _proxy_info;
private final MainPanel _main_panel;
private volatile boolean _exit;
@ -36,7 +31,6 @@ public class SmartMegaProxyManager implements Runnable {
_main_panel = main_panel;
_proxy_list_url = proxy_list_url;
_proxy_list = new ConcurrentLinkedQueue<>();
_proxy_info = new ConcurrentHashMap<>();
_exit = false;
}
@ -63,47 +57,19 @@ public class SmartMegaProxyManager implements Runnable {
public String getFastestProxy() {
for (String proxy : _proxy_list) {
HashMap<String, Object> proxy_info = (HashMap<String, Object>) _proxy_info.get(proxy);
Long extimestamp = (Long) proxy_info.get("extimestamp");
if (extimestamp == null || extimestamp + PROXY_EXCLUDE_SECS * 1000 < System.currentTimeMillis()) {
return proxy;
} else {
Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Smart Proxy Manager: proxy is temporary excluded -> {1}", new Object[]{Thread.currentThread().getName(), proxy});
}
return _proxy_list.peek();
}
return null;
}
public void removeProxy(String proxy) {
public void excludeProxy(String proxy) {
if (_proxy_info.containsKey(proxy)) {
HashMap<String, Object> proxy_info = (HashMap<String, Object>) _proxy_info.get(proxy);
int excount = (int) proxy_info.get("excount") + 1;
if (excount < PROXY_MAX_EXCLUDE_COUNTER) {
proxy_info.put("excount", excount);
proxy_info.put("extimestamp", System.currentTimeMillis());
} else {
Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Smart Proxy Manager: proxy removed -> {1}", new Object[]{Thread.currentThread().getName(), proxy});
if (_proxy_list.contains(proxy)) {
_proxy_list.remove(proxy);
_proxy_info.remove(proxy);
_main_panel.getView().updateSmartProxyStatus("SmartProxy: " + _proxy_list.size());
Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Smart Proxy Manager: proxy removed -> {1}", new Object[]{Thread.currentThread().getName(), proxy});
if (_proxy_list.isEmpty()) {
THREAD_POOL.execute(new Runnable() {
@ -116,7 +82,6 @@ public class SmartMegaProxyManager implements Runnable {
}
}
}
}
private void _refreshProxyList() {
@ -152,23 +117,18 @@ public class SmartMegaProxyManager implements Runnable {
if (proxy_list.length > 0) {
_proxy_list.clear();
_proxy_info.clear();
for (String proxy : proxy_list) {
if (proxy.trim().matches(".+?:[0-9]{1,5}")) {
_proxy_list.add(proxy);
HashMap<String, Object> proxy_info = new HashMap<>();
proxy_info.put("extimestamp", null);
proxy_info.put("excount", 0);
_proxy_info.put(proxy.trim(), proxy_info);
}
}
}
Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Smart Proxy Manager: proxy list refreshed ({1})", new Object[]{Thread.currentThread().getName(), _proxy_list.size()});
_main_panel.getView().updateSmartProxyStatus("SmartProxy: " + _proxy_list.size());
Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Smart Proxy Manager: proxy list refreshed ({1})", new Object[]{Thread.currentThread().getName(), _proxy_list.size()});
}
} catch (MalformedURLException ex) {

View File

@ -72,9 +72,7 @@ public class StreamChunkDownloader implements Runnable {
if (error && current_proxy != null) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "{0} Worker [{1}]: excluding proxy -> {2}", new Object[]{Thread.currentThread().getName(), _id, current_proxy});
MainPanel.getProxy_manager().excludeProxy(current_proxy);
MainPanel.getProxy_manager().removeProxy(current_proxy);
}
current_proxy = MainPanel.getProxy_manager().getFastestProxy();