Browse Source

Code cleanup and trying to allow Deluge RPC connetions over properly secured SSL sockets as well as unsecure non-SSl socket.

alonalbert-deluge-direct-initial
Eric Kok 7 years ago
parent
commit
96467b807e
  1. 2
      app/build.gradle
  2. 184
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java
  3. 37
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java
  4. 228
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java
  5. 53
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcClient.java

2
app/build.gradle

@ -42,7 +42,7 @@ dependencies { @@ -42,7 +42,7 @@ dependencies {
compile 'com.android.support:support-annotations:23.4.0'
compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.nispok:snackbar:2.11.0'
compile 'com.github.aegnor:rencode-java:-SNAPSHOT'
compile 'com.github.aegnor:rencode-java:cb628e824e'
compile('com.github.afollestad.material-dialogs:core:0.8.5.5@aar') {
transitive = true
}

184
app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java

@ -17,7 +17,67 @@ @@ -17,7 +17,67 @@
*/
package org.transdroid.daemon.Deluge;
import com.android.internalcopy.http.multipart.FilePart;
import com.android.internalcopy.http.multipart.MultipartEntity;
import com.android.internalcopy.http.multipart.Part;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.transdroid.core.gui.log.Log;
import org.transdroid.daemon.Daemon;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Label;
import org.transdroid.daemon.Priority;
import org.transdroid.daemon.Torrent;
import org.transdroid.daemon.TorrentDetails;
import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.TorrentStatus;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.AddByMagnetUrlTask;
import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
import org.transdroid.daemon.task.PauseTask;
import org.transdroid.daemon.task.RemoveTask;
import org.transdroid.daemon.task.ResumeTask;
import org.transdroid.daemon.task.RetrieveTask;
import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
import org.transdroid.daemon.task.SetDownloadLocationTask;
import org.transdroid.daemon.task.SetFilePriorityTask;
import org.transdroid.daemon.task.SetLabelTask;
import org.transdroid.daemon.task.SetTrackersTask;
import org.transdroid.daemon.task.SetTransferRatesTask;
import org.transdroid.daemon.util.HttpHelper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DOWNLOADEDEVER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_ETA;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FIELDS_ARRAY;
@ -61,7 +121,7 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SAVEPATH; @@ -61,7 +121,7 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SAVEPATH;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SESSION_ID;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIMEADDED;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TORRENTS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALPEERS;
@ -69,66 +129,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS; @@ -69,66 +129,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import com.android.internalcopy.http.multipart.FilePart;
import com.android.internalcopy.http.multipart.MultipartEntity;
import com.android.internalcopy.http.multipart.Part;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.transdroid.core.gui.log.Log;
import org.transdroid.daemon.Daemon;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Label;
import org.transdroid.daemon.Priority;
import org.transdroid.daemon.Torrent;
import org.transdroid.daemon.TorrentDetails;
import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.TorrentStatus;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.AddByMagnetUrlTask;
import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
import org.transdroid.daemon.task.PauseTask;
import org.transdroid.daemon.task.RemoveTask;
import org.transdroid.daemon.task.ResumeTask;
import org.transdroid.daemon.task.RetrieveTask;
import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
import org.transdroid.daemon.task.SetDownloadLocationTask;
import org.transdroid.daemon.task.SetFilePriorityTask;
import org.transdroid.daemon.task.SetLabelTask;
import org.transdroid.daemon.task.SetTrackersTask;
import org.transdroid.daemon.task.SetTransferRatesTask;
import org.transdroid.daemon.util.HttpHelper;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
/**
@ -220,8 +222,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -220,8 +222,7 @@ public class DelugeAdapter implements IDaemonAdapter {
// params.put(-1); // cache_id
JSONObject result = makeRequest(buildRequest(RPC_METHOD_GET, params), log);
return new RetrieveTaskSuccessResult((RetrieveTask) task,
parseJsonRetrieveTorrents(result.getJSONObject(RPC_RESULT)),
return new RetrieveTaskSuccessResult((RetrieveTask) task, parseJsonRetrieveTorrents(result.getJSONObject(RPC_RESULT)),
parseJsonRetrieveLabels(result.getJSONObject(RPC_RESULT)));
case GetTorrentDetails:
@ -237,8 +238,8 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -237,8 +238,8 @@ public class DelugeAdapter implements IDaemonAdapter {
params.put(dfields); // keys
JSONObject dinfo = makeRequest(buildRequest(RPC_METHOD_STATUS, params), log);
return new GetTorrentDetailsTaskSuccessResult((GetTorrentDetailsTask) task,
parseJsonTorrentDetails(dinfo.getJSONObject(RPC_RESULT)));
return new GetTorrentDetailsTaskSuccessResult((GetTorrentDetailsTask) task, parseJsonTorrentDetails(dinfo.getJSONObject
(RPC_RESULT)));
case GetFileList:
@ -247,8 +248,8 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -247,8 +248,8 @@ public class DelugeAdapter implements IDaemonAdapter {
params.put(ffields); // keys
JSONObject finfo = makeRequest(buildRequest(RPC_METHOD_STATUS, params), log);
return new GetFileListTaskSuccessResult((GetFileListTask) task,
parseJsonFileListing(finfo.getJSONObject(RPC_RESULT), task.getTargetTorrent()));
return new GetFileListTaskSuccessResult((GetFileListTask) task, parseJsonFileListing(finfo.getJSONObject(RPC_RESULT), task
.getTargetTorrent()));
case AddByFile:
@ -290,8 +291,8 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -290,8 +291,8 @@ public class DelugeAdapter implements IDaemonAdapter {
// Pause a torrent
PauseTask pauseTask = (PauseTask) task;
makeRequest(buildRequest(RPC_METHOD_PAUSE, ((new JSONArray())
.put((new JSONArray()).put(pauseTask.getTargetTorrent().getUniqueID())))), log);
makeRequest(buildRequest(RPC_METHOD_PAUSE, ((new JSONArray()).put((new JSONArray()).put(pauseTask.getTargetTorrent().getUniqueID
())))), log);
return new DaemonTaskSuccessResult(task);
case PauseAll:
@ -304,8 +305,8 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -304,8 +305,8 @@ public class DelugeAdapter implements IDaemonAdapter {
// Resume a torrent
ResumeTask resumeTask = (ResumeTask) task;
makeRequest(buildRequest(RPC_METHOD_RESUME, ((new JSONArray())
.put((new JSONArray()).put(resumeTask.getTargetTorrent().getUniqueID())))), log);
makeRequest(buildRequest(RPC_METHOD_RESUME, ((new JSONArray()).put((new JSONArray()).put(resumeTask.getTargetTorrent()
.getUniqueID())))), log);
return new DaemonTaskSuccessResult(task);
case ResumeAll:
@ -323,8 +324,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -323,8 +324,7 @@ public class DelugeAdapter implements IDaemonAdapter {
params.put(task.getTargetTorrent().getUniqueID()); // torrent_id
params.put(ffields); // keys
JSONObject pinfo = makeRequest(buildRequest(RPC_METHOD_STATUS, params), log);
ArrayList<TorrentFile> pfiles =
parseJsonFileListing(pinfo.getJSONObject(RPC_RESULT), prioTask.getTargetTorrent());
ArrayList<TorrentFile> pfiles = parseJsonFileListing(pinfo.getJSONObject(RPC_RESULT), prioTask.getTargetTorrent());
// Now prepare the new list of priorities
params = new JSONArray();
@ -400,14 +400,13 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -400,14 +400,13 @@ public class DelugeAdapter implements IDaemonAdapter {
case ForceRecheck:
// Pause a torrent
makeRequest(buildRequest(RPC_METHOD_FORCERECHECK,
((new JSONArray()).put((new JSONArray()).put(task.getTargetTorrent().getUniqueID())))),
log);
makeRequest(buildRequest(RPC_METHOD_FORCERECHECK, ((new JSONArray()).put((new JSONArray()).put(task.getTargetTorrent()
.getUniqueID())))), log);
return new DaemonTaskSuccessResult(task);
default:
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported,
task.getMethod() + " is not supported by " + getType()));
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported, task.getMethod() + " is not " +
"supported by " + getType()));
}
} catch (JSONException e) {
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.ParsingFailed, e.toString()));
@ -518,9 +517,8 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -518,9 +517,8 @@ public class DelugeAdapter implements IDaemonAdapter {
// Still no session cookie?
if (sessionCookie == null) {
// Set error message and cancel the action that was requested
throw new DaemonException(ExceptionType.AuthenticationFailure,
"Password error? Server time difference? No (valid) cookie in response and JSON was: " +
HttpHelper.convertStreamToString(instream));
throw new DaemonException(ExceptionType.AuthenticationFailure, "Password error? Server time difference? No (valid) cookie in " +
"response and JSON was: " + HttpHelper.convertStreamToString(instream));
}
}
@ -557,9 +555,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -557,9 +555,7 @@ public class DelugeAdapter implements IDaemonAdapter {
JSONObject json = new JSONObject(result);
instream.close();
log.d(LOG_NAME, "Success: " +
(result.length() > 300 ? result.substring(0, 300) + "... (" + result.length() + " chars)" :
result));
log.d(LOG_NAME, "Success: " + (result.length() > 300 ? result.substring(0, 300) + "... (" + result.length() + " chars)" : result));
// Return JSON object
return json;
@ -585,8 +581,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -585,8 +581,7 @@ public class DelugeAdapter implements IDaemonAdapter {
*/
private void initialise() throws DaemonException {
httpclient = HttpHelper.createStandardHttpClient(settings,
settings.getUsername() != null && !settings.getUsername().equals(""));
httpclient = HttpHelper.createStandardHttpClient(settings, settings.getUsername() != null && !settings.getUsername().equals(""));
httpclient.addRequestInterceptor(HttpHelper.gzipRequestInterceptor);
httpclient.addResponseInterceptor(HttpHelper.gzipResponseInterceptor);
@ -597,18 +592,17 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -597,18 +592,17 @@ public class DelugeAdapter implements IDaemonAdapter {
* @return The URL of the RPC API
*/
private String buildWebUIUrl() {
return (settings.getSsl() ? "https://" : "http://") + settings.getAddress() + ":" + settings.getPort() +
(settings.getFolder() == null ? "" : settings.getFolder());
return (settings.getSsl() ? "https://" : "http://") + settings.getAddress() + ":" + settings.getPort() + (settings.getFolder() == null ? ""
: settings.getFolder());
}
private ArrayList<Torrent> parseJsonRetrieveTorrents(JSONObject response) throws JSONException, DaemonException {
// Parse response
ArrayList<Torrent> torrents = new ArrayList<Torrent>();
ArrayList<Torrent> torrents = new ArrayList<>();
if (response.isNull(RPC_TORRENTS)) {
throw new DaemonException(ExceptionType.NotConnected,
"Web interface probably not connected to a daemon yet, because 'torrents' is null: " +
response.toString());
throw new DaemonException(ExceptionType.NotConnected, "Web interface probably not connected to a daemon yet, because 'torrents' is null:" +
" " + response.toString());
}
JSONObject objects = response.getJSONObject(RPC_TORRENTS);
JSONArray names = objects.names();
@ -668,7 +662,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -668,7 +662,7 @@ public class DelugeAdapter implements IDaemonAdapter {
JSONArray labels = filters.getJSONArray("label");
// Parse response
ArrayList<Label> allLabels = new ArrayList<Label>();
ArrayList<Label> allLabels = new ArrayList<>();
for (int i = 0; i < labels.length(); i++) {
JSONArray labelAndCount = labels.getJSONArray(i);
if (labelAndCount.getString(0).equals("All")) {
@ -683,7 +677,7 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -683,7 +677,7 @@ public class DelugeAdapter implements IDaemonAdapter {
private ArrayList<TorrentFile> parseJsonFileListing(JSONObject response, Torrent torrent) throws JSONException {
// Parse response
ArrayList<TorrentFile> files = new ArrayList<TorrentFile>();
ArrayList<TorrentFile> files = new ArrayList<>();
JSONArray objects = response.getJSONArray(RPC_DETAILS);
JSONArray progress = response.getJSONArray(RPC_FILEPROGRESS);
JSONArray priorities = response.getJSONArray(RPC_FILEPRIORITIES);
@ -713,14 +707,14 @@ public class DelugeAdapter implements IDaemonAdapter { @@ -713,14 +707,14 @@ public class DelugeAdapter implements IDaemonAdapter {
private TorrentDetails parseJsonTorrentDetails(JSONObject response) throws JSONException {
// Parse response
List<String> trackers = new ArrayList<String>();
List<String> trackers = new ArrayList<>();
JSONArray trackerObjects = response.getJSONArray(RPC_TRACKERS);
if (trackerObjects != null && trackerObjects.length() > 0) {
for (int i = 0; i < trackerObjects.length(); i++) {
trackers.add(trackerObjects.getJSONObject(i).getString("url"));
}
}
List<String> errors = new ArrayList<String>();
List<String> errors = new ArrayList<>();
String trackerStatus = response.getString(RPC_TRACKER_STATUS);
errors.add(trackerStatus);

37
app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java

@ -24,7 +24,6 @@ import org.transdroid.daemon.TorrentStatus; @@ -24,7 +24,6 @@ import org.transdroid.daemon.TorrentStatus;
/**
* Common constants and methods used by both adapters.
*
* @author alon.albert
*/
class DelugeCommon {
@ -89,36 +88,11 @@ class DelugeCommon { @@ -89,36 +88,11 @@ class DelugeCommon {
static final String RPC_URL = "url";
static final String RPC_UPLOADEDEVER = "total_uploaded";
static final String[] RPC_DETAILS_FIELDS_ARRAY = {
RPC_TRACKERS,
RPC_TRACKER_STATUS,
};
static final String[] RPC_FIELDS_ARRAY = {
RPC_HASH,
RPC_NAME,
RPC_STATUS,
RPC_SAVEPATH,
RPC_RATEDOWNLOAD,
RPC_RATEUPLOAD,
RPC_NUMPEERS,
RPC_NUMSEEDS,
RPC_TOTALPEERS,
RPC_TOTALSEEDS,
RPC_ETA,
RPC_DOWNLOADEDEVER,
RPC_UPLOADEDEVER,
RPC_TOTALSIZE,
RPC_PARTDONE,
RPC_LABEL,
RPC_MESSAGE,
RPC_TIMEADDED,
RPC_TRACKER_STATUS,
};
static final String[] RPC_FILE_FIELDS_ARRAY = {
RPC_DETAILS,
RPC_FILEPROGRESS,
RPC_FILEPRIORITIES,
};
static final String[] RPC_DETAILS_FIELDS_ARRAY = {RPC_TRACKERS, RPC_TRACKER_STATUS,};
static final String[] RPC_FIELDS_ARRAY = {RPC_HASH, RPC_NAME, RPC_STATUS, RPC_SAVEPATH, RPC_RATEDOWNLOAD, RPC_RATEUPLOAD, RPC_NUMPEERS,
RPC_NUMSEEDS, RPC_TOTALPEERS, RPC_TOTALSEEDS, RPC_ETA, RPC_DOWNLOADEDEVER, RPC_UPLOADEDEVER, RPC_TOTALSIZE, RPC_PARTDONE, RPC_LABEL,
RPC_MESSAGE, RPC_TIMEADDED, RPC_TRACKER_STATUS,};
static final String[] RPC_FILE_FIELDS_ARRAY = {RPC_DETAILS, RPC_FILEPROGRESS, RPC_FILEPRIORITIES,};
static TorrentStatus convertDelugeState(String state) {
// Deluge sends a string with status code
@ -219,4 +193,5 @@ class DelugeCommon { @@ -219,4 +193,5 @@ class DelugeCommon {
}
return version;
}
}

228
app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java

@ -17,7 +17,61 @@ @@ -17,7 +17,61 @@
*/
package org.transdroid.daemon.Deluge;
import android.support.annotation.NonNull;
import org.base64.android.Base64;
import org.transdroid.core.gui.log.Log;
import org.transdroid.daemon.Daemon;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Label;
import org.transdroid.daemon.Priority;
import org.transdroid.daemon.Torrent;
import org.transdroid.daemon.TorrentDetails;
import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.AddByMagnetUrlTask;
import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.ForceRecheckTask;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
import org.transdroid.daemon.task.RemoveTask;
import org.transdroid.daemon.task.RetrieveTask;
import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
import org.transdroid.daemon.task.SetDownloadLocationTask;
import org.transdroid.daemon.task.SetFilePriorityTask;
import org.transdroid.daemon.task.SetLabelTask;
import org.transdroid.daemon.task.SetTrackersTask;
import org.transdroid.daemon.task.SetTransferRatesTask;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DOWNLOADEDEVER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_ETA;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FIELDS_ARRAY;
@ -59,71 +113,18 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_RATEUPLOAD; @@ -59,71 +113,18 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_RATEUPLOAD;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SAVEPATH;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIMEADDED;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALPEERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import android.support.annotation.NonNull;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.base64.android.Base64;
import org.transdroid.core.gui.log.Log;
import org.transdroid.daemon.Daemon;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Label;
import org.transdroid.daemon.Priority;
import org.transdroid.daemon.Torrent;
import org.transdroid.daemon.TorrentDetails;
import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.task.AddByFileTask;
import org.transdroid.daemon.task.AddByMagnetUrlTask;
import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.ForceRecheckTask;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
import org.transdroid.daemon.task.GetTorrentDetailsTask;
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
import org.transdroid.daemon.task.RemoveTask;
import org.transdroid.daemon.task.RetrieveTask;
import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
import org.transdroid.daemon.task.SetDownloadLocationTask;
import org.transdroid.daemon.task.SetFilePriorityTask;
import org.transdroid.daemon.task.SetLabelTask;
import org.transdroid.daemon.task.SetTrackersTask;
import org.transdroid.daemon.task.SetTransferRatesTask;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
/**
* The daemon adapter from the Deluge torrent client using deluged API directly.
*
* @author alon.albert
*/
public class DelugeRpcAdapter implements IDaemonAdapter {
@ -142,7 +143,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -142,7 +143,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
public DaemonTaskResult executeTask(Log log, DaemonTask task) {
final DelugeRpcClient client = new DelugeRpcClient();
try {
client.connect(settings.getAddress(), settings.getPort(), settings.getUsername(), settings.getPassword());
client.connect(settings);
switch (task.getMethod()) {
case Retrieve:
return doRetrieve(client, (RetrieveTask) task);
@ -179,9 +180,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -179,9 +180,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
case ForceRecheck:
return doForceRecheck(client, (ForceRecheckTask) task);
default:
return new DaemonTaskFailureResult(task,
new DaemonException(ExceptionType.MethodUnsupported,
task.getMethod() + " is not supported by " + getType()));
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported, task.getMethod() + " is not " +
"supported by " + getType()));
}
} catch (DaemonException e) {
return new DaemonTaskFailureResult(task, e);
@ -204,8 +204,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -204,8 +204,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) throws DaemonException {
// Get torrents
//noinspection unchecked
final Map<String, Map<String, Object>> torrentsStatus = (Map<String, Map<String, Object>>) client
.sendRequest(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY);
final Map<String, Map<String, Object>> torrentsStatus = (Map<String, Map<String, Object>>) client.sendRequest
(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY);
final List<Torrent> torrents = getTorrents(torrentsStatus.values());
// Check if Label plugin is enabled
@ -213,9 +213,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -213,9 +213,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
// Get label list from server
//noinspection unchecked
final List<String> labelNames = hasLabelPlugin
? (List<String>) client.sendRequest(RPC_METHOD_GET_LABELS)
: new ArrayList<String>();
final List<String> labelNames = hasLabelPlugin ? (List<String>) client.sendRequest(RPC_METHOD_GET_LABELS) : new ArrayList<String>();
// Extract labels & counts from torrents.
final List<Label> labels = getLabels(labelNames, torrents);
@ -223,49 +221,39 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -223,49 +221,39 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
return new RetrieveTaskSuccessResult(task, torrents, labels);
}
private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(DelugeRpcClient client,
GetTorrentDetailsTask task)
throws DaemonException {
private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(DelugeRpcClient client, GetTorrentDetailsTask task) throws DaemonException {
//noinspection unchecked
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(
RPC_METHOD_STATUS,
task.getTargetTorrent().getUniqueID(),
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(RPC_METHOD_STATUS, task.getTargetTorrent().getUniqueID(),
RPC_DETAILS_FIELDS_ARRAY);
//noinspection unchecked
final List<Map<String, Object>> trackerResponses = (List<Map<String, Object>>) response
.get(RPC_TRACKERS);
final List<Map<String, Object>> trackerResponses = (List<Map<String, Object>>) response.get(RPC_TRACKERS);
final List<String> trackers = new ArrayList<>();
for (Map<String, Object> trackerResponse : trackerResponses) {
trackers.add((String) trackerResponse.get(RPC_URL));
}
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails(
trackers,
Collections.singletonList((String) response.get(RPC_TRACKER_STATUS))));
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails(trackers, Collections.singletonList((String) response.get
(RPC_TRACKER_STATUS))));
}
private GetFileListTaskSuccessResult doGetFileList(DelugeRpcClient client,
GetFileListTask task) throws DaemonException {
private GetFileListTaskSuccessResult doGetFileList(DelugeRpcClient client, GetFileListTask task) throws DaemonException {
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent());
return new GetFileListTaskSuccessResult(task, files);
}
private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task,
String method) throws DaemonException {
private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task, String method) throws DaemonException {
client.sendRequest(method, (Object) getTorrentIdsArg(task));
return new DaemonTaskSuccessResult(task);
}
private DaemonTaskResult doRemove(DelugeRpcClient client, RemoveTask task) throws DaemonException {
client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(),
task.includingData());
client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(), task.includingData());
return new DaemonTaskSuccessResult(task);
}
@NonNull
private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task,
String method) throws DaemonException {
private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task, String method) throws DaemonException {
client.sendRequest(method);
return new DaemonTaskSuccessResult(task);
}
@ -285,8 +273,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -285,8 +273,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
@NonNull
private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client,
AddByMagnetUrlTask task) throws DaemonException {
private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client, AddByMagnetUrlTask task) throws DaemonException {
client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>());
return new DaemonTaskSuccessResult(task);
}
@ -303,8 +290,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -303,8 +290,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
@NonNull
private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client,
SetFilePriorityTask task) throws DaemonException {
private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client, SetFilePriorityTask task) throws DaemonException {
// We first need a listing of all the files (because we can only set the priorities all at once)
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent());
@ -331,8 +317,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -331,8 +317,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
@NonNull
private DaemonTaskResult doSetTransferRates(DelugeRpcClient client,
SetTransferRatesTask task) throws DaemonException {
private DaemonTaskResult doSetTransferRates(DelugeRpcClient client, SetTransferRatesTask task) throws DaemonException {
final Map<String, Object> config = new HashMap<>();
config.put(RPC_MAXDOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate());
config.put(RPC_MAXUPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate());
@ -361,14 +346,13 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -361,14 +346,13 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
@NonNull
private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client,
SetDownloadLocationTask task) throws DaemonException {
private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client, SetDownloadLocationTask task) throws DaemonException {
client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation());
return new DaemonTaskSuccessResult(task);
}
@NonNull
private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) throws DaemonException {
private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) {
final List<Torrent> torrents = new ArrayList<>();
int id = 0;
for (Map<String, Object> torrentMap : torrentMaps) {
@ -390,35 +374,21 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -390,35 +374,21 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
error = message;
}
torrents.add(new Torrent(
id++,
(String) torrentMap.get(RPC_HASH),
(String) torrentMap.get(RPC_NAME),
DelugeCommon.convertDelugeState((String) torrentMap.get(RPC_STATUS)),
torrentMap.get(RPC_SAVEPATH) + settings.getOS().getPathSeperator(),
((Number) torrentMap.get(RPC_RATEDOWNLOAD)).intValue(),
((Number) torrentMap.get(RPC_RATEUPLOAD)).intValue(),
((Number) torrentMap.get(RPC_NUMSEEDS)).intValue(),
((Number) torrentMap.get(RPC_TOTALSEEDS)).intValue(),
((Number) torrentMap.get(RPC_NUMPEERS)).intValue(),
((Number) torrentMap.get(RPC_TOTALPEERS)).intValue(),
((Number) torrentMap.get(RPC_ETA)).intValue(),
((Number) torrentMap.get(RPC_DOWNLOADEDEVER)).longValue(),
((Number) torrentMap.get(RPC_UPLOADEDEVER)).longValue(),
((Number) torrentMap.get(RPC_TOTALSIZE)).longValue(),
((Number) torrentMap.get(RPC_PARTDONE)).floatValue() / 100f,
0f, // Not available
(String) torrentMap.get(RPC_LABEL),
timeAddedDate,
null, // Not available
error,
getType()));
torrents.add(new Torrent(id++, (String) torrentMap.get(RPC_HASH), (String) torrentMap.get(RPC_NAME), DelugeCommon.convertDelugeState(
(String) torrentMap.get(RPC_STATUS)), torrentMap.get(RPC_SAVEPATH) + settings.getOS().getPathSeperator(), ((Number) torrentMap
.get(RPC_RATEDOWNLOAD)).intValue(), ((Number) torrentMap.get(RPC_RATEUPLOAD)).intValue(), ((Number) torrentMap.get
(RPC_NUMSEEDS)).intValue(), ((Number) torrentMap.get(RPC_TOTALSEEDS)).intValue(), ((Number) torrentMap.get(RPC_NUMPEERS))
.intValue(), ((Number) torrentMap.get(RPC_TOTALPEERS)).intValue(), ((Number) torrentMap.get(RPC_ETA)).intValue(), ((Number)
torrentMap.get(RPC_DOWNLOADEDEVER)).longValue(), ((Number) torrentMap.get(RPC_UPLOADEDEVER)).longValue(), ((Number) torrentMap
.get(RPC_TOTALSIZE)).longValue(), ((Number) torrentMap.get(RPC_PARTDONE)).floatValue() / 100f, 0f, // Not available
(String) torrentMap.get(RPC_LABEL), timeAddedDate, null, // Not available
error, getType()));
}
return torrents;
}
@NonNull
private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) throws DaemonException {
private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) {
// First get all labels that torrents and count them
final Map<String, MutableInt> labelCounters = new HashMap<>();
for (Torrent torrent : torrents) {
@ -450,14 +420,11 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -450,14 +420,11 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
private ArrayList<TorrentFile> getTorrentFiles(DelugeRpcClient client, Torrent torrent) throws DaemonException {
final ArrayList<TorrentFile> files = new ArrayList<>();
//noinspection unchecked
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(
RPC_METHOD_STATUS,
torrent.getUniqueID(),
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(RPC_METHOD_STATUS, torrent.getUniqueID(),
RPC_FILE_FIELDS_ARRAY);
//noinspection unchecked
final List<Map<String, Object>> fileMaps = (List<Map<String, Object>>) response
.get(RPC_DETAILS);
final List<Map<String, Object>> fileMaps = (List<Map<String, Object>>) response.get(RPC_DETAILS);
//noinspection unchecked
final List<Integer> priorities = (List<Integer>) response.get(RPC_FILEPRIORITIES);
//noinspection unchecked
@ -470,14 +437,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -470,14 +437,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final String path = (String) fileMap.get(RPC_PATH);
final long size = ((Number) fileMap.get(RPC_SIZE)).longValue();
files.add(new TorrentFile(
fileMap.get(RPC_INDEX).toString(),
path,
path,
torrent.getLocationDir() + path,
size,
(long) (size * progress),
convertDelugePriority(client, priority)));
files.add(new TorrentFile(fileMap.get(RPC_INDEX).toString(), path, path, torrent.getLocationDir() + path, size, (long) (size * progress)
, convertDelugePriority(client, priority)));
}
return files;
}
@ -489,8 +450,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -489,8 +450,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
try {
in = new BufferedInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new DaemonException(ExceptionType.FileAccessError,
"File not found: " + file.getAbsolutePath());
throw new DaemonException(ExceptionType.FileAccessError, "File not found: " + file.getAbsolutePath());
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
@ -504,8 +464,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -504,8 +464,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
return out.toByteArray();
} catch (IOException e) {
throw new DaemonException(ExceptionType.FileAccessError,
"Error reading file: " + file.getAbsolutePath());
throw new DaemonException(ExceptionType.FileAccessError, "Error reading file: " + file.getAbsolutePath());
} finally {
try {
in.close();
@ -516,8 +475,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter { @@ -516,8 +475,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
}
@NonNull
private Priority convertDelugePriority(DelugeRpcClient client, int priority)
throws DaemonException {
private Priority convertDelugePriority(DelugeRpcClient client, int priority) throws DaemonException {
ensureVersion(client);
return DelugeCommon.convertDelugePriority(priority, version);
}

53
app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcClient.java

@ -17,9 +17,13 @@ @@ -17,9 +17,13 @@
*/
package org.transdroid.daemon.Deluge;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_DAEMON_LOGIN;
import android.support.annotation.NonNull;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.DaemonSettings;
import org.transdroid.daemon.util.TlsSniSocketFactory;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
@ -32,13 +36,11 @@ import java.util.List; @@ -32,13 +36,11 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.DaemonException.ExceptionType;
import org.transdroid.daemon.util.IgnoreSSLTrustManager;
import se.dimovski.rencode.Rencode;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_DAEMON_LOGIN;
/**
* A Deluge RPC API Client.
*/
@ -51,11 +53,11 @@ class DelugeRpcClient implements Closeable { @@ -51,11 +53,11 @@ class DelugeRpcClient implements Closeable {
private Socket socket;
private static AtomicInteger requestId = new AtomicInteger();
void connect(String address, int port, String username, String paswword) throws DaemonException {
void connect(DaemonSettings settings) throws DaemonException {
try {
socket = openSocket(address, port);
if (username != null) {
sendRequest(RPC_METHOD_DAEMON_LOGIN, username, paswword);
socket = openSocket(settings);
if (settings.shouldUseAuthentication()) {
sendRequest(RPC_METHOD_DAEMON_LOGIN, settings.getUsername(), settings.getPassword());
}
} catch (NoSuchAlgorithmException e) {
throw new DaemonException(ExceptionType.ConnectionError, "Failed to open socket: " + e.getMessage());
@ -70,6 +72,7 @@ class DelugeRpcClient implements Closeable { @@ -70,6 +72,7 @@ class DelugeRpcClient implements Closeable {
public void close() {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// ignore
@ -80,8 +83,7 @@ class DelugeRpcClient implements Closeable { @@ -80,8 +83,7 @@ class DelugeRpcClient implements Closeable {
Object sendRequest(String method, Object... args) throws DaemonException {
final byte[] requestBytes;
try {
requestBytes = compress(
Rencode.encode(new Object[]{new Object[]{requestId.getAndIncrement(), method, args, new HashMap<>()}}));
requestBytes = compress(Rencode.encode(new Object[]{new Object[]{requestId.getAndIncrement(), method, args, new HashMap<>()}}));
} catch (IOException e) {
throw new DaemonException(ExceptionType.ConnectionError, "Failed to encode request: " + e.getMessage());
}
@ -146,12 +148,25 @@ class DelugeRpcClient implements Closeable { @@ -146,12 +148,25 @@ class DelugeRpcClient implements Closeable {
}
@NonNull
private Socket openSocket(String address, int port)
throws NoSuchAlgorithmException, KeyManagementException, IOException {
final TrustManager[] trustAllCerts = new TrustManager[]{new IgnoreSSLTrustManager()};
final SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext.getSocketFactory().createSocket(address, port);
private Socket openSocket(DaemonSettings settings) throws NoSuchAlgorithmException, KeyManagementException, IOException {
if (!settings.getSsl()) {
// Non-ssl connections
return new Socket(settings.getAddress(), settings.getPort());
}
final TlsSniSocketFactory socketFactory;
if (settings.getSslTrustKey() != null && settings.getSslTrustKey().length() != 0) {
socketFactory = new TlsSniSocketFactory(settings.getSslTrustKey());
} else if (settings.getSslTrustAll()) {
socketFactory = new TlsSniSocketFactory(true);
} else {
socketFactory = new TlsSniSocketFactory();
}
return socketFactory.createSocket(null, settings.getAddress(), settings.getPort(), false);
// final TrustManager[] trustAllCerts = new TrustManager[]{new IgnoreSSLTrustManager()};
// final SSLContext sslContext = SSLContext.getInstance("TLSv1");
// sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// return sslContext.getSocketFactory().createSocket(address, port);
}
}

Loading…
Cancel
Save