From c5abbe4467ccf7b318e08fd8b8c60a035bbc7cef Mon Sep 17 00:00:00 2001 From: tonikelope Date: Thu, 26 Jul 2018 19:08:39 +0200 Subject: [PATCH] 5.5 --- pom.xml | 2 +- .../megabasterd/ChunkDownloader.java | 69 ++++++++++--------- .../megabasterd/ChunkDownloaderMono.java | 15 ++-- .../com/tonikelope/megabasterd/MainPanel.java | 2 +- .../com/tonikelope/megabasterd/MiscTools.java | 58 +++++++++++----- .../megabasterd/StreamChunkDownloader.java | 19 +++-- 6 files changed, 103 insertions(+), 62 deletions(-) diff --git a/pom.xml b/pom.xml index 536a615f1..336b604f8 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.tonikelope MegaBasterd - 5.4 + 5.5 jar diff --git a/src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java b/src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java index 7618af21f..3258d87da 100644 --- a/src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java +++ b/src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java @@ -103,17 +103,11 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable { String worker_url = null, current_proxy = null; - boolean error = false, error509 = false; + boolean error = false, error509 = false, error403 = false; while (!_exit && !_download.isStopped() && (error509 || conta_error < MAX_SLOT_ERROR || MainPanel.isUse_smart_proxy())) { - if (MainPanel.isUse_smart_proxy() && _proxy_manager == null) { - - _proxy_manager = new SmartMegaProxyManager(MainPanel.getUse_smart_proxy_url()); - - } - - if (worker_url == null || error) { + if (worker_url == null || error403) { worker_url = _download.getDownloadUrlForWorker(); } @@ -124,9 +118,17 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable { if (error509 && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()) { - if (error && current_proxy != null) { + if (MainPanel.isUse_smart_proxy() && _proxy_manager == null) { + + _proxy_manager = new SmartMegaProxyManager(null); + + } + + if (error && !error403 && current_proxy != null) { _proxy_manager.blockProxy(current_proxy); + Logger.getLogger(MiscTools.class.getName()).log(Level.WARNING, "{0}: excluding proxy -> {1}", new Object[]{Thread.currentThread().getName(), current_proxy}); + } current_proxy = _proxy_manager.getFastestProxy(); @@ -185,6 +187,8 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable { error = false; + error403 = false; + error509 = false; if (getDownload().isError509()) { @@ -195,25 +199,29 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable { if (!_exit && !_download.isStopped()) { - try (InputStream is = new ThrottledInputStream(con.getInputStream(), _download.getMain_panel().getStream_supervisor())) { + int http_status = con.getResponseCode(); - int http_status = con.getResponseCode(); + if (http_status != 200) { - if (http_status != 200) { - Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Failed : HTTP error code : {1}", new Object[]{Thread.currentThread().getName(), http_status}); + Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Failed : HTTP error code : {1}", new Object[]{Thread.currentThread().getName(), http_status}); - error = true; + error = true; - if (http_status == 509) { + if (http_status == 509) { - error509 = true; + error509 = true; - if (MainPanel.isUse_smart_proxy()) { - getDownload().getView().set509Error(true); - } + if (MainPanel.isUse_smart_proxy()) { + getDownload().getView().set509Error(true); } + } else if (http_status == 403) { - } else { + error403 = true; + } + + } else { + + try (InputStream is = new ThrottledInputStream(con.getInputStream(), _download.getMain_panel().getStream_supervisor())) { byte[] buffer = new byte[DEFAULT_BYTE_BUFFER_SIZE]; @@ -242,20 +250,19 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable { secureWait(); } } + } + } + + if (chunk.getOutputStream().size() < chunk.getSize()) { + + if (chunk.getOutputStream().size() > 0) { + _download.getPartialProgressQueue().add(-1 * chunk.getOutputStream().size()); + + _download.getProgress_meter().secureNotify(); } - if (chunk.getOutputStream().size() < chunk.getSize()) { - - if (chunk.getOutputStream().size() > 0) { - _download.getPartialProgressQueue().add(-1 * chunk.getOutputStream().size()); - - _download.getProgress_meter().secureNotify(); - - } - - error = true; - } + error = true; } if (error && !_download.isStopped()) { diff --git a/src/main/java/com/tonikelope/megabasterd/ChunkDownloaderMono.java b/src/main/java/com/tonikelope/megabasterd/ChunkDownloaderMono.java index f1315f65c..34edc2657 100644 --- a/src/main/java/com/tonikelope/megabasterd/ChunkDownloaderMono.java +++ b/src/main/java/com/tonikelope/megabasterd/ChunkDownloaderMono.java @@ -32,7 +32,7 @@ public class ChunkDownloaderMono extends ChunkDownloader { String worker_url = null; int conta_error = 0, http_status = 200; - boolean error = false, error509 = false; + boolean error = false, error509 = false, error403 = false; getDownload().getView().set509Error(false); @@ -40,7 +40,7 @@ public class ChunkDownloaderMono extends ChunkDownloader { while (!isExit() && !getDownload().isStopped() && (error509 || conta_error < MAX_SLOT_ERROR)) { - if (worker_url == null || error) { + if (worker_url == null || error403) { worker_url = getDownload().getDownloadUrlForWorker(); } @@ -79,13 +79,14 @@ public class ChunkDownloaderMono extends ChunkDownloader { error = false; - if (error509) { - - getDownload().getView().set509Error(false); - } + error403 = false; error509 = false; + if (getDownload().isError509()) { + getDownload().getView().set509Error(false); + } + if (http_status != 200) { Logger.getLogger(getClass().getName()).log(Level.INFO, "{0} Failed : HTTP error code : {1}", new Object[]{Thread.currentThread().getName(), http_status}); @@ -97,6 +98,8 @@ public class ChunkDownloaderMono extends ChunkDownloader { error509 = true; getDownload().getView().set509Error(true); + } else if (http_status == 403) { + error403 = true; } getDownload().rejectChunkId(chunk.getId()); diff --git a/src/main/java/com/tonikelope/megabasterd/MainPanel.java b/src/main/java/com/tonikelope/megabasterd/MainPanel.java index 5874ad1fb..82f3e7570 100644 --- a/src/main/java/com/tonikelope/megabasterd/MainPanel.java +++ b/src/main/java/com/tonikelope/megabasterd/MainPanel.java @@ -47,7 +47,7 @@ import java.io.File; */ public final class MainPanel { - public static final String VERSION = "5.4"; + public static final String VERSION = "5.5"; 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; diff --git a/src/main/java/com/tonikelope/megabasterd/MiscTools.java b/src/main/java/com/tonikelope/megabasterd/MiscTools.java index e5648b5a8..6e917b0e7 100644 --- a/src/main/java/com/tonikelope/megabasterd/MiscTools.java +++ b/src/main/java/com/tonikelope/megabasterd/MiscTools.java @@ -735,10 +735,14 @@ public final class MiscTools { public static boolean checkMegaDownloadUrl(String string_url) { - boolean url_ok = false, error509 = false; + boolean url_ok = false; HttpURLConnection con = null; + boolean error = false, error509 = false, error403 = false; + + SmartMegaProxyManager proxy_manager = null; + String current_proxy = null; do { @@ -747,18 +751,24 @@ public final class MiscTools { URL url = new URL(string_url + "/0-0"); - if (con == null || !url_ok) { + if (con == null || error) { if (error509 && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()) { - if (current_proxy != null) { + if (MainPanel.isUse_smart_proxy() && proxy_manager == null) { - Logger.getLogger(MiscTools.class.getName()).log(Level.WARNING, "{0}: excluding proxy -> {1}", new Object[]{Thread.currentThread().getName(), current_proxy}); + proxy_manager = MainPanel.getProxy_manager(); - MainPanel.getProxy_manager().blockProxy(current_proxy); } - current_proxy = MainPanel.getProxy_manager().getFastestProxy(); + if (error && !error403 && current_proxy != null) { + + proxy_manager.blockProxy(current_proxy); + Logger.getLogger(MiscTools.class.getName()).log(Level.WARNING, "{0}: excluding proxy -> {1}", new Object[]{Thread.currentThread().getName(), current_proxy}); + + } + + current_proxy = proxy_manager.getFastestProxy(); if (current_proxy != null) { @@ -770,11 +780,21 @@ public final class MiscTools { } else { - con = (HttpURLConnection) url.openConnection(); + if (MainPanel.isUse_proxy()) { + + con = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(MainPanel.getProxy_host(), MainPanel.getProxy_port()))); + + if (MainPanel.getProxy_user() != null && !"".equals(MainPanel.getProxy_user())) { + + con.setRequestProperty("Proxy-Authorization", "Basic " + MiscTools.Bin2BASE64((MainPanel.getProxy_user() + ":" + MainPanel.getProxy_pass()).getBytes())); + } + } else { + + con = (HttpURLConnection) url.openConnection(); + } } - } - { + } else { if (MainPanel.isUse_proxy()) { @@ -789,31 +809,33 @@ public final class MiscTools { con = (HttpURLConnection) url.openConnection(); } } + } - error509 = false; + con.setConnectTimeout(Transference.HTTP_TIMEOUT); - try { + con.setReadTimeout(Transference.HTTP_TIMEOUT); - url_ok = (con.getResponseCode() == 200); + con.setRequestProperty("User-Agent", MainPanel.DEFAULT_USER_AGENT); - error509 = (con.getResponseCode() == 509); + int http_status = con.getResponseCode(); + + if (http_status != 403) { + + url_ok = true; - } catch (IOException ex) { - Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, null, ex); } - } catch (Exception ex) { + } catch (IOException ex) { Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, null, ex); } finally { if (con != null) { con.disconnect(); - con = null; } } - } while (error509 && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()); + } while (error509); return url_ok; } diff --git a/src/main/java/com/tonikelope/megabasterd/StreamChunkDownloader.java b/src/main/java/com/tonikelope/megabasterd/StreamChunkDownloader.java index 149fd42f8..5e8f77f8e 100644 --- a/src/main/java/com/tonikelope/megabasterd/StreamChunkDownloader.java +++ b/src/main/java/com/tonikelope/megabasterd/StreamChunkDownloader.java @@ -44,7 +44,7 @@ public class StreamChunkDownloader implements Runnable { String url = _chunkwriter.getUrl(); - boolean error = false, error509 = false; + boolean error = false, error509 = false, error403 = false; String current_proxy = null; @@ -69,7 +69,7 @@ public class StreamChunkDownloader implements Runnable { offset = _chunkwriter.nextOffset(); - } else { + } else if (error403) { url = _chunkwriter.getUrl(); } @@ -141,12 +141,12 @@ public class StreamChunkDownloader implements Runnable { error509 = false; + error403 = false; + try { if (!_exit) { - InputStream is = con.getInputStream(); - http_status = con.getResponseCode(); if (http_status != 200) { @@ -155,10 +155,19 @@ public class StreamChunkDownloader implements Runnable { error = true; - error509 = (http_status == 509); + if (http_status == 509) { + + error509 = true; + + } else if (http_status == 403) { + + error403 = true; + } } else { + InputStream is = con.getInputStream(); + while (!_exit && !_chunkwriter.isExit() && (reads = is.read(buffer)) != -1) { chunk_stream.getOutputStream().write(buffer, 0, reads);