Search in sources :

Example 6 with PluginProxy

use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.

the class TorrentUtils method download.

public static TOTorrent download(URL url, long timeout) throws IOException {
    try {
        PluginProxy plugin_proxy = null;
        try {
            if (AENetworkClassifier.categoriseAddress(url.getHost()) != AENetworkClassifier.AT_PUBLIC) {
                plugin_proxy = AEProxyFactory.getPluginProxy("torrent download", url);
            }
            ResourceDownloader rd;
            if (plugin_proxy == null) {
                rd = new ResourceDownloaderFactoryImpl().create(url);
            } else {
                rd = new ResourceDownloaderFactoryImpl().create(plugin_proxy.getURL(), plugin_proxy.getProxy());
                rd.setProperty("URL_HOST", url.getHost());
            }
            if (timeout > 0) {
                rd.setProperty("URL_Connect_Timeout", timeout);
                rd.setProperty("URL_Read_Timeout", timeout);
            }
            byte[] bytes = FileUtil.readInputStreamAsByteArray(rd.download(), BDecoder.MAX_BYTE_ARRAY_SIZE);
            return (TOTorrentFactory.deserialiseFromBEncodedByteArray(bytes));
        } finally {
            if (plugin_proxy != null) {
                plugin_proxy.setOK(true);
            }
        }
    } catch (IOException e) {
        throw ((IOException) e);
    } catch (Throwable e) {
        throw (new IOException(Debug.getNestedExceptionMessage(e)));
    }
}
Also used : PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) ResourceDownloaderFactoryImpl(com.biglybt.pifimpl.local.utils.resourcedownloader.ResourceDownloaderFactoryImpl)

Example 7 with PluginProxy

use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.

the class PlatformMessenger method downloadURL.

private static Object[] downloadURL(URL rpc_url, String postData, boolean forceProxy) throws Throwable {
    Throwable error = null;
    if (!forceProxy) {
        try {
            Object[] result = downloadURLSupport(null, null, rpc_url, postData);
            if (result[1] == null) {
                throw (new Exception("Request failed"));
            } else {
                return (result);
            }
        } catch (Throwable e) {
            error = e;
        }
    }
    try {
        PluginProxy plugin_proxy = AEProxyFactory.getPluginProxy("vuze settings", rpc_url, true);
        if (plugin_proxy == null) {
            if (error != null) {
                throw (error);
            }
            throw (new Exception("Proxy unavailable"));
        } else {
            URL url = plugin_proxy.getURL();
            Proxy proxy = plugin_proxy.getProxy();
            boolean ok = false;
            try {
                String proxy_host = rpc_url.getHost() + (rpc_url.getPort() == -1 ? "" : (":" + rpc_url.getPort()));
                Object[] result = downloadURLSupport(proxy, proxy_host, url, postData);
                ok = true;
                return (result);
            } finally {
                plugin_proxy.setOK(ok);
            }
        }
    } catch (Throwable f) {
        throw (error == null ? f : error);
    }
}
Also used : PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) Proxy(java.net.Proxy) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) URL(java.net.URL)

Example 8 with PluginProxy

use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.

the class TCPTransportImpl method setConnectResult.

void setConnectResult(boolean ok) {
    PluginProxy pp = plugin_proxy;
    if (pp != null) {
        plugin_proxy = null;
        pp.setOK(ok);
    }
}
Also used : PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy)

Example 9 with PluginProxy

use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.

the class WebEngine method getWebPageContent.

protected pageDetails getWebPageContent(SearchParameter[] searchParameters, Map<String, String> searchContext, String headers, boolean only_if_modified, pageDetailsVerifier verifier) throws SearchException {
    String searchURL = searchURLFormat;
    String lc_url = searchURL.toLowerCase(Locale.US);
    boolean explicit_tor = lc_url.startsWith("tor:");
    boolean user_tor = false;
    if (!explicit_tor) {
        String test = Result.adjustLink(searchURL);
        if (test.startsWith("tor:")) {
            user_tor = true;
        }
    }
    if (explicit_tor || user_tor) {
        // strip out any stuff we probably don't want to send
        searchContext = new HashMap<>();
        String target_resource = explicit_tor ? searchURL.substring(4) : searchURL;
        URL location;
        try {
            location = new URL(target_resource);
        } catch (MalformedURLException e) {
            throw (new SearchException(e));
        }
        Map<String, Object> options = new HashMap<>();
        options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
        PluginProxy plugin_proxy = AEProxyFactory.getPluginProxy("Web engine download of '" + target_resource + "'", location, options, true);
        if (plugin_proxy == null) {
            throw (new SearchException("No Tor plugin proxy available for '" + target_resource + "'"));
        }
        URL url = plugin_proxy.getURL();
        Proxy proxy = plugin_proxy.getProxy();
        boolean ok = false;
        try {
            String proxy_host = location.getHost() + (location.getPort() == -1 ? "" : (":" + location.getPort()));
            pageDetails details = getWebPageContentSupport(proxy, proxy_host, url.toExternalForm(), searchParameters, searchContext, headers, only_if_modified);
            if (verifier != null) {
                verifier.verify(details);
            }
            ok = true;
            return (details);
        } finally {
            plugin_proxy.setOK(ok);
        }
    }
    try {
        try {
            URL url = new URL(searchURL);
            if (AENetworkClassifier.categoriseAddress(url.getHost()) != AENetworkClassifier.AT_PUBLIC) {
                // strip out any stuff we probably don't want to send
                searchContext = new HashMap<>();
            }
        } catch (Throwable e) {
        }
        pageDetails details = getWebPageContentSupport(null, null, searchURL, searchParameters, searchContext, headers, only_if_modified);
        if (verifier != null) {
            verifier.verify(details);
        }
        return (details);
    } catch (SearchException e) {
        try {
            URL original_url = new URL(searchURL);
            PluginProxy plugin_proxy = AEProxyFactory.getPluginProxy("getting search results ", original_url);
            if (plugin_proxy == null) {
                throw (e);
            } else {
                URL url = plugin_proxy.getURL();
                Proxy proxy = plugin_proxy.getProxy();
                boolean ok = false;
                try {
                    String proxy_host = original_url.getHost() + (original_url.getPort() == -1 ? "" : (":" + original_url.getPort()));
                    pageDetails details = getWebPageContentSupport(proxy, proxy_host, url.toExternalForm(), searchParameters, searchContext, headers, only_if_modified);
                    if (verifier != null) {
                        verifier.verify(details);
                    }
                    ok = true;
                    return (details);
                } finally {
                    plugin_proxy.setOK(ok);
                }
            }
        } catch (Throwable f) {
            throw (e);
        }
    }
}
Also used : PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) SearchException(com.biglybt.core.metasearch.SearchException) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) JSONObject(org.json.simple.JSONObject)

Example 10 with PluginProxy

use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
    String download_link = original_result.getDownloadLink();
    if (download_link == null) {
        log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
        return;
    }
    final String key = subs.getID() + ":" + original_result.getID();
    final String dl = download_link;
    synchronized (active_result_downloaders) {
        if (active_result_downloaders.contains(key)) {
            return;
        }
        log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
        active_result_downloaders.add(key);
        result_downloader.run(new AERunnable() {

            @Override
            public void runSupport() {
                // need to fix up to the latest history due to the lazy nature of things :(
                SubscriptionResult result = subs.getHistory().getResult(original_result.getID());
                boolean success = false;
                try {
                    if (result == null) {
                        log(subs.getName() + ": result has been deleted - " + original_result.getID());
                        success = true;
                    } else if (result.getRead()) {
                        log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
                        success = true;
                    } else {
                        boolean retry = true;
                        boolean use_ref = subs.getHistory().getDownloadWithReferer();
                        boolean tried_ref_switch = false;
                        while (retry) {
                            retry = false;
                            try {
                                TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
                                URL original_url = new URL(dl);
                                PluginProxy plugin_proxy = null;
                                if (dl.startsWith("tor:")) {
                                    String target_resource = dl.substring(4);
                                    original_url = new URL(target_resource);
                                    Map<String, Object> options = new HashMap<>();
                                    options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
                                    plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
                                    if (plugin_proxy == null) {
                                        throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
                                    }
                                }
                                URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
                                Torrent torrent = null;
                                try {
                                    while (true) {
                                        try {
                                            ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
                                            ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
                                            if (plugin_proxy != null) {
                                                url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
                                            }
                                            String referer = use_ref ? subs.getReferer() : null;
                                            UrlUtils.setBrowserHeaders(url_rd, referer);
                                            Engine engine = subs.getEngine();
                                            if (engine instanceof WebEngine) {
                                                WebEngine we = (WebEngine) engine;
                                                if (we.isNeedsAuth()) {
                                                    String cookies = we.getCookies();
                                                    if (cookies != null && cookies.length() > 0) {
                                                        url_rd.setProperty("URL_Cookie", cookies);
                                                    }
                                                }
                                            }
                                            ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
                                            InputStream is = mr_rd.download();
                                            torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
                                            break;
                                        } catch (Throwable e) {
                                            if (plugin_proxy == null) {
                                                plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
                                                if (plugin_proxy != null) {
                                                    current_url = plugin_proxy.getURL();
                                                    continue;
                                                }
                                            }
                                            throw (e);
                                        }
                                    }
                                } finally {
                                    if (plugin_proxy != null) {
                                        plugin_proxy.setOK(torrent != null);
                                    }
                                }
                                byte[] hash = torrent.getHash();
                                // PlatformTorrentUtils.setContentTitle(torrent, torr );
                                DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                                Download download;
                                // if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
                                // this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
                                boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
                                boolean auto_start = manager.shouldAutoStart(torrent);
                                manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                try {
                                    if (auto_start && !stop_override) {
                                        download = dm.addDownload(torrent);
                                    } else {
                                        download = dm.addDownloadStopped(torrent, null, null);
                                    }
                                } finally {
                                    manager.removePrepareTrigger(hash);
                                }
                                log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
                                // maybe remove this as should be actioned in the trigger?
                                manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                subs.addAssociation(hash);
                                if (auto_start && stop_override) {
                                    download.restart();
                                }
                                result.setRead(true);
                                success = true;
                                if (tried_ref_switch) {
                                    subs.getHistory().setDownloadWithReferer(use_ref);
                                }
                            } catch (Throwable e) {
                                log(subs.getName() + ": Failed to download result " + dl, e);
                                if (e instanceof TOTorrentException && !tried_ref_switch) {
                                    use_ref = !use_ref;
                                    tried_ref_switch = true;
                                    retry = true;
                                    log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
                                }
                            } finally {
                                TorrentUtils.setTLSDescription(null);
                            }
                        }
                    }
                } finally {
                    try {
                        if (!success) {
                            if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
                                // whatever the outcome these have been handled async
                                result.setRead(true);
                            } else {
                                int rad = manager.getAutoDownloadMarkReadAfterDays();
                                if (rad > 0) {
                                    long rad_millis = rad * 24 * 60 * 60 * 1000L;
                                    long time_found = result.getTimeFound();
                                    if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
                                        log(subs.getName() + ": result expired, marking as read - " + result.getID());
                                        result.setRead(true);
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                    synchronized (active_result_downloaders) {
                        active_result_downloaders.remove(key);
                    }
                    calculateSchedule();
                }
            }
        });
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) InputStream(java.io.InputStream) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) DownloadManager(com.biglybt.pif.download.DownloadManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) Download(com.biglybt.pif.download.Download) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Aggregations

PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)18 URL (java.net.URL)7 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)6 InputStream (java.io.InputStream)6 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)5 Proxy (java.net.Proxy)4 LogEvent (com.biglybt.core.logging.LogEvent)3 java.io (java.io)3 java.net (java.net)3 ClientIDGenerator (com.biglybt.pif.clientid.ClientIDGenerator)2 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)2 SFPluginDetailsException (com.biglybt.pifimpl.update.sf.SFPluginDetailsException)2 ExternalSeedException (com.biglybt.plugin.extseed.ExternalSeedException)2 IOException (java.io.IOException)2 X509Certificate (java.security.cert.X509Certificate)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 Engine (com.biglybt.core.metasearch.Engine)1 SearchException (com.biglybt.core.metasearch.SearchException)1 WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)1