use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.
the class ExternalSeedHTTPDownloaderRange method download.
public void download(String[] prop_names, String[] prop_values, int length, ExternalSeedHTTPDownloaderListener listener, boolean con_fail_is_perm_fail) throws ExternalSeedException {
boolean connected = false;
InputStream is = null;
String outcome = "";
PluginProxy plugin_proxy = null;
boolean proxy_ok = false;
try {
SESecurityManager.setThreadPasswordHandler(this);
if (NetworkAdmin.getSingleton().hasMissingForcedBind()) {
throw (new ExternalSeedException("Forced bind address is missing"));
}
// System.out.println( "Connecting to " + url + ": " + Thread.currentThread().getId());
HttpURLConnection connection;
int response;
Set<String> redirect_urls = new HashSet<>();
redirect_loop: while (true) {
URL original_url = redirected_url == null ? very_original_url : redirected_url;
URL current_url = original_url;
if (plugin_proxy != null) {
plugin_proxy.setOK(true);
plugin_proxy = null;
}
Proxy current_proxy = null;
if (AENetworkClassifier.categoriseAddress(original_url.getHost()) != AENetworkClassifier.AT_PUBLIC) {
plugin_proxy = AEProxyFactory.getPluginProxy("webseed", original_url);
if (plugin_proxy != null) {
current_url = plugin_proxy.getURL();
current_proxy = plugin_proxy.getProxy();
}
}
for (int ssl_loop = 0; ssl_loop < 2; ssl_loop++) {
try {
if (current_proxy == null) {
connection = (HttpURLConnection) current_url.openConnection();
} else {
connection = (HttpURLConnection) current_url.openConnection(current_proxy);
}
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection ssl_con = (HttpsURLConnection) connection;
// allow for certs that contain IP addresses rather than dns names
ssl_con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String host, SSLSession session) {
return (true);
}
});
TrustManager[] tms_delegate = SESecurityManager.getAllTrustingTrustManager();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, tms_delegate, RandomUtils.SECURE_RANDOM);
SSLSocketFactory factory = sc.getSocketFactory();
ssl_con.setSSLSocketFactory(factory);
}
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", user_agent);
for (int i = 0; i < prop_names.length; i++) {
connection.setRequestProperty(prop_names[i], prop_values[i]);
}
if (plugin_proxy != null) {
connection.setRequestProperty("HOST", plugin_proxy.getURLHostRewrite() + (original_url.getPort() == -1 ? "" : (":" + original_url.getPort())));
}
int time_remaining = listener.getPermittedTime();
if (time_remaining > 0) {
connection.setConnectTimeout(time_remaining);
}
connection.connect();
time_remaining = listener.getPermittedTime();
if (time_remaining < 0) {
throw (new IOException("Timeout during connect"));
}
connection.setReadTimeout(time_remaining);
connected = true;
response = connection.getResponseCode();
if (response == HttpURLConnection.HTTP_ACCEPTED || response == HttpURLConnection.HTTP_OK || response == HttpURLConnection.HTTP_PARTIAL) {
if (redirected_url != null) {
consec_redirect_fails = 0;
}
break redirect_loop;
} else if (response == HttpURLConnection.HTTP_MOVED_TEMP || response == HttpURLConnection.HTTP_MOVED_PERM) {
// auto redirect doesn't work from http to https or vice-versa
String move_to = connection.getHeaderField("location");
if (move_to != null) {
if (redirect_urls.contains(move_to) || redirect_urls.size() > 32) {
throw (new ExternalSeedException("redirect loop"));
}
redirect_urls.add(move_to);
redirected_url = new URL(move_to);
continue redirect_loop;
}
}
if (redirected_url == null) {
break redirect_loop;
}
// try again with original URL
consec_redirect_fails++;
redirected_url = null;
} catch (SSLException e) {
if (ssl_loop == 0) {
if (SESecurityManager.installServerCertificates(current_url) != null) {
// retry with new certificate
continue;
}
}
throw (e);
}
break;
}
}
if (plugin_proxy == null) {
URL final_url = connection.getURL();
if (consec_redirect_fails < 10 && !very_original_url.toExternalForm().equals(final_url.toExternalForm())) {
redirected_url = final_url;
}
}
last_response = response;
last_response_retry_after_secs = -1;
if (response == 503) {
// webseed support for temp unavail - read the retry_after
long retry_after_date = connection.getHeaderFieldDate("Retry-After", -1L);
if (retry_after_date <= -1) {
last_response_retry_after_secs = connection.getHeaderFieldInt("Retry-After", -1);
} else {
last_response_retry_after_secs = (int) ((retry_after_date - System.currentTimeMillis()) / 1000);
if (last_response_retry_after_secs < 0) {
last_response_retry_after_secs = -1;
}
}
}
is = connection.getInputStream();
proxy_ok = true;
if (response == HttpURLConnection.HTTP_ACCEPTED || response == HttpURLConnection.HTTP_OK || response == HttpURLConnection.HTTP_PARTIAL) {
int pos = 0;
byte[] buffer = null;
int buffer_pos = 0;
int buffer_len = 0;
while (pos < length) {
if (buffer == null) {
buffer = listener.getBuffer();
buffer_pos = listener.getBufferPosition();
buffer_len = listener.getBufferLength();
}
listener.setBufferPosition(buffer_pos);
int to_read = buffer_len - buffer_pos;
int permitted = listener.getPermittedBytes();
if (permitted < to_read) {
to_read = permitted;
}
int len = is.read(buffer, buffer_pos, to_read);
if (len < 0) {
break;
}
listener.reportBytesRead(len);
pos += len;
buffer_pos += len;
if (buffer_pos == buffer_len) {
listener.done();
buffer = null;
buffer_pos = 0;
}
}
if (pos != length) {
String log_str;
if (buffer == null) {
log_str = "No buffer assigned";
} else {
log_str = new String(buffer, 0, length);
if (log_str.length() > 64) {
log_str = log_str.substring(0, 64);
}
}
outcome = "Connection failed: data too short - " + length + "/" + pos + " [" + log_str + "]";
throw (new ExternalSeedException(outcome));
}
outcome = "read " + pos + " bytes";
// System.out.println( "download length: " + pos );
} else {
outcome = "Connection failed: " + connection.getResponseMessage();
ExternalSeedException error = new ExternalSeedException(outcome);
error.setPermanentFailure(true);
throw (error);
}
} catch (IOException e) {
if (con_fail_is_perm_fail && !connected) {
outcome = "Connection failed: " + e.getMessage();
ExternalSeedException error = new ExternalSeedException(outcome);
error.setPermanentFailure(true);
throw (error);
} else {
outcome = "Connection failed: " + Debug.getNestedExceptionMessage(e);
if (last_response_retry_after_secs >= 0) {
outcome += ", Retry-After: " + last_response_retry_after_secs + " seconds";
}
ExternalSeedException excep = new ExternalSeedException(outcome, e);
if (e instanceof FileNotFoundException) {
excep.setPermanentFailure(true);
}
throw (excep);
}
} catch (Throwable e) {
if (e instanceof ExternalSeedException) {
throw ((ExternalSeedException) e);
}
outcome = "Connection failed: " + Debug.getNestedExceptionMessage(e);
throw (new ExternalSeedException("Connection failed", e));
} finally {
SESecurityManager.unsetThreadPasswordHandler();
if (is != null) {
try {
is.close();
} catch (Throwable e) {
}
}
if (plugin_proxy != null) {
plugin_proxy.setOK(proxy_ok);
}
}
}
use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.
the class MagnetPlugin method doSecondaryLookup.
protected void doSecondaryLookup(final MagnetPluginProgressListener listener, final Object[] result, byte[] hash, Set<String> networks_enabled, String args) {
if (listener != null) {
listener.reportActivity(getMessageText("report.secondarylookup", null));
}
PluginProxy plugin_proxy = null;
try {
URL original_sl_url = new URL(SECONDARY_LOOKUP + "magnetLookup?hash=" + Base32.encode(hash) + (args.length() == 0 ? "" : ("&args=" + UrlUtils.encode(args))));
URL sl_url = original_sl_url;
Proxy proxy = null;
if (!networks_enabled.contains(AENetworkClassifier.AT_PUBLIC)) {
plugin_proxy = AEProxyFactory.getPluginProxy("secondary magnet lookup", sl_url);
if (plugin_proxy == null) {
throw (new NoRouteToHostException("plugin proxy unavailable"));
} else {
proxy = plugin_proxy.getProxy();
sl_url = plugin_proxy.getURL();
}
}
ResourceDownloaderFactory rdf = plugin_interface.getUtilities().getResourceDownloaderFactory();
ResourceDownloader rd;
if (proxy == null) {
rd = rdf.create(sl_url);
} else {
rd = rdf.create(sl_url, proxy);
rd.setProperty("URL_HOST", original_sl_url.getHost());
}
final PluginProxy f_pp = plugin_proxy;
rd.addListener(new ResourceDownloaderAdapter() {
@Override
public boolean completed(ResourceDownloader downloader, InputStream data) {
try {
if (listener != null) {
listener.reportActivity(getMessageText("report.secondarylookup.ok", null));
}
synchronized (result) {
result[0] = data;
}
return (true);
} finally {
complete();
}
}
@Override
public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
try {
synchronized (result) {
result[0] = e;
}
if (listener != null) {
listener.reportActivity(getMessageText("report.secondarylookup.fail"));
}
} finally {
complete();
}
}
private void complete() {
if (f_pp != null) {
// outcome doesn't really indicate whether the result was wholesome
f_pp.setOK(true);
}
}
});
rd.asyncDownload();
} catch (Throwable e) {
if (plugin_proxy != null) {
// tidy up, no indication of proxy badness here so say its ok
plugin_proxy.setOK(true);
}
if (listener != null) {
listener.reportActivity(getMessageText("report.secondarylookup.fail", Debug.getNestedExceptionMessage(e)));
}
}
}
use of com.biglybt.core.proxy.AEProxyFactory.PluginProxy in project BiglyBT by BiglySoftware.
the class SFPluginDetailsLoaderImpl method loadPluginDetails.
protected void loadPluginDetails(SFPluginDetailsImpl details) throws SFPluginDetailsException {
try {
String page_url_to_use = site_prefix + "update/pluginlist3.php?plugin=" + UrlUtils.encode(details.getId()) + "&" + base_url_params;
page_url_to_use = addEPIDS(page_url_to_use);
try {
PluginInterface defPI = PluginInitializer.getDefaultInterface();
PluginInterface pi = defPI == null ? null : defPI.getPluginManager().getPluginInterfaceByID(details.getId(), false);
if (pi != null) {
String existing_version = pi.getPluginVersion();
if (existing_version != null) {
page_url_to_use += "&ver_" + details.getId() + "=" + UrlUtils.encode(existing_version);
}
}
} catch (Throwable e) {
Debug.out(e);
}
URL original_url = new URL(page_url_to_use);
URL url = original_url;
Proxy proxy = null;
PluginProxy plugin_proxy = null;
boolean tried_proxy = false;
boolean ok = false;
if (COConfigurationManager.getBooleanParameter("update.anonymous")) {
tried_proxy = true;
plugin_proxy = AEProxyFactory.getPluginProxy("loading plugin details", url);
if (plugin_proxy == null) {
throw (new SFPluginDetailsException("Proxy not available"));
} else {
url = plugin_proxy.getURL();
proxy = plugin_proxy.getProxy();
}
}
try {
while (true) {
try {
ResourceDownloader p_dl = rd_factory.create(url, proxy);
if (proxy != null) {
p_dl.setProperty("URL_HOST", original_url.getHost());
}
p_dl = rd_factory.getRetryDownloader(p_dl, 5);
p_dl.addListener(this);
InputStream is = p_dl.download();
try {
if (!processPluginStream(details, is)) {
throw (new SFPluginDetailsException("Plugin details load fails for '" + details.getId() + "': data not found"));
}
ok = true;
break;
} finally {
is.close();
}
} catch (Throwable e) {
if (!tried_proxy) {
tried_proxy = true;
plugin_proxy = AEProxyFactory.getPluginProxy("loading plugin details", url);
if (plugin_proxy == null) {
throw (e);
} else {
url = plugin_proxy.getURL();
proxy = plugin_proxy.getProxy();
}
} else {
throw (e);
}
}
}
} finally {
if (plugin_proxy != null) {
plugin_proxy.setOK(ok);
}
}
} catch (Throwable e) {
Debug.printStackTrace(e);
throw (new SFPluginDetailsException("Plugin details load fails", e));
}
}
Aggregations