Browse Source

Fix for Torrentflux-b4rt installs in the web root (so with no folder setting).

pull/82/head
Eric Kok 11 years ago
parent
commit
c6e79aa342
  1. 101
      lib/src/org/transdroid/daemon/Tfb4rt/Tfb4rtAdapter.java

101
lib/src/org/transdroid/daemon/Tfb4rt/Tfb4rtAdapter.java

@ -51,13 +51,10 @@ import com.android.internalcopy.http.multipart.FilePart;
import com.android.internalcopy.http.multipart.MultipartEntity; import com.android.internalcopy.http.multipart.MultipartEntity;
import com.android.internalcopy.http.multipart.Part; import com.android.internalcopy.http.multipart.Part;
/** /**
* An adapter that allows for easy access to Torrentflux-b4rt installs. Communication * An adapter that allows for easy access to Torrentflux-b4rt installs. Communication is handled via HTTP GET requests
* is handled via HTTP GET requests and XML responses. * and XML responses.
*
* @author erickok * @author erickok
*
*/ */
public class Tfb4rtAdapter implements IDaemonAdapter { public class Tfb4rtAdapter implements IDaemonAdapter {
@ -84,25 +81,25 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
@Override @Override
public DaemonTaskResult executeTask(DaemonTask task) { public DaemonTaskResult executeTask(DaemonTask task) {
try { try {
switch (task.getMethod()) { switch (task.getMethod()) {
case Retrieve: case Retrieve:
// Request all torrents from server // Request all torrents from server
return new RetrieveTaskSuccessResult((RetrieveTask) task, makeStatsRequest(),null); return new RetrieveTaskSuccessResult((RetrieveTask) task, makeStatsRequest(), null);
case AddByFile: case AddByFile:
// Add a torrent to the server by sending the contents of a local .torrent file // Add a torrent to the server by sending the contents of a local .torrent file
String file = ((AddByFileTask)task).getFile(); String file = ((AddByFileTask) task).getFile();
makeFileUploadRequest("fileUpload", file); makeFileUploadRequest("fileUpload", file);
return null; return null;
case AddByUrl: case AddByUrl:
// Request to add a torrent by URL // Request to add a torrent by URL
String url = ((AddByUrlTask)task).getUrl(); String url = ((AddByUrlTask) task).getUrl();
makeActionRequest("urlUpload", url); makeActionRequest("urlUpload", url);
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
@ -110,15 +107,16 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
// Remove a torrent // Remove a torrent
RemoveTask removeTask = (RemoveTask) task; RemoveTask removeTask = (RemoveTask) task;
makeActionRequest((removeTask.includingData()? "deleteWithData": "delete"), task.getTargetTorrent().getUniqueID()); makeActionRequest((removeTask.includingData() ? "deleteWithData" : "delete"), task.getTargetTorrent()
.getUniqueID());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
case Pause: case Pause:
// Pause a torrent // Pause a torrent
makeActionRequest("stop", task.getTargetTorrent().getUniqueID()); makeActionRequest("stop", task.getTargetTorrent().getUniqueID());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
case PauseAll: case PauseAll:
// Pause all torrents // Pause all torrents
@ -130,21 +128,22 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
// Resume a torrent // Resume a torrent
makeActionRequest("start", task.getTargetTorrent().getUniqueID()); makeActionRequest("start", task.getTargetTorrent().getUniqueID());
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
case ResumeAll: case ResumeAll:
// Resume all torrents // Resume all torrents
makeActionRequest("bulkStart", null); makeActionRequest("bulkStart", null);
return new DaemonTaskSuccessResult(task); return new DaemonTaskSuccessResult(task);
case SetTransferRates: case SetTransferRates:
// Request to set the maximum transfer rates // Request to set the maximum transfer rates
// TODO: Implement this? // TODO: Implement this?
return null; return null;
default: 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) { } catch (DaemonException e) {
return new DaemonTaskFailureResult(task, e); return new DaemonTaskFailureResult(task, e);
@ -154,22 +153,22 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
private List<Torrent> makeStatsRequest() throws DaemonException { private List<Torrent> makeStatsRequest() throws DaemonException {
try { try {
// Initialise the HTTP client // Initialise the HTTP client
if (httpclient == null) { if (httpclient == null) {
initialise(); initialise();
} }
// Make request // Make request
HttpGet httpget = new HttpGet(buildWebUIUrl(RPC_URL_STATS)); HttpGet httpget = new HttpGet(buildWebUIUrl(RPC_URL_STATS));
HttpResponse response = httpclient.execute(httpget); HttpResponse response = httpclient.execute(httpget);
// Read XML response // Read XML response
InputStream instream = response.getEntity().getContent(); InputStream instream = response.getEntity().getContent();
List<Torrent> torrents = StatsParser.parse(new InputStreamReader(instream)); List<Torrent> torrents = StatsParser.parse(new InputStreamReader(instream));
instream.close(); instream.close();
return torrents; return torrents;
} catch (DaemonException e) { } catch (DaemonException e) {
DLog.d(LOG_NAME, "Parsing error: " + e.toString()); DLog.d(LOG_NAME, "Parsing error: " + e.toString());
throw e; throw e;
@ -177,20 +176,21 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
DLog.d(LOG_NAME, "Error: " + e.toString()); DLog.d(LOG_NAME, "Error: " + e.toString());
throw new DaemonException(ExceptionType.ConnectionError, e.toString()); throw new DaemonException(ExceptionType.ConnectionError, e.toString());
} }
} }
private boolean makeActionRequest(String action, String target) throws DaemonException { private boolean makeActionRequest(String action, String target) throws DaemonException {
try { try {
// Initialise the HTTP client // Initialise the HTTP client
if (httpclient == null) { if (httpclient == null) {
initialise(); initialise();
} }
// Make request // Make request
HttpGet httpget = new HttpGet(buildWebUIUrl(RPC_URL_DISPATCH + action + RPC_URL_DISPATCH2 + RPC_URL_AID + (action.equals("urlUpload")? RPC_URL_URL: RPC_URL_TRANSFER) + target)); HttpGet httpget = new HttpGet(buildWebUIUrl(RPC_URL_DISPATCH + action + RPC_URL_DISPATCH2 + RPC_URL_AID
+ (action.equals("urlUpload") ? RPC_URL_URL : RPC_URL_TRANSFER) + target));
HttpResponse response = httpclient.execute(httpget); HttpResponse response = httpclient.execute(httpget);
// Read response (a successful action always returned '1') // Read response (a successful action always returned '1')
@ -210,21 +210,21 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
DLog.d(LOG_NAME, "Error: " + e.toString()); DLog.d(LOG_NAME, "Error: " + e.toString());
throw new DaemonException(ExceptionType.ConnectionError, e.toString()); throw new DaemonException(ExceptionType.ConnectionError, e.toString());
} }
} }
private boolean makeFileUploadRequest(String action, String target) throws DaemonException { private boolean makeFileUploadRequest(String action, String target) throws DaemonException {
try { try {
// Initialise the HTTP client // Initialise the HTTP client
if (httpclient == null) { if (httpclient == null) {
initialise(); initialise();
} }
// Make request // Make request
HttpPost httppost = new HttpPost(buildWebUIUrl(RPC_URL_DISPATCH + action + RPC_URL_DISPATCH2 + RPC_URL_AID)); HttpPost httppost = new HttpPost(buildWebUIUrl(RPC_URL_DISPATCH + action + RPC_URL_DISPATCH2 + RPC_URL_AID));
File upload = new File(URI.create(target)); File upload = new File(URI.create(target));
Part[] parts = { new FilePart("upload_files[]", upload) }; Part[] parts = { new FilePart("upload_files[]", upload) };
httppost.setEntity(new MultipartEntity(parts, httppost.getParams())); httppost.setEntity(new MultipartEntity(parts, httppost.getParams()));
@ -247,7 +247,7 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
DLog.d(LOG_NAME, "Error: " + e.toString()); DLog.d(LOG_NAME, "Error: " + e.toString());
throw new DaemonException(ExceptionType.ConnectionError, e.toString()); throw new DaemonException(ExceptionType.ConnectionError, e.toString());
} }
} }
/** /**
@ -258,20 +258,27 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
private void initialise() throws DaemonException { private void initialise() throws DaemonException {
httpclient = HttpHelper.createStandardHttpClient(settings, true); httpclient = HttpHelper.createStandardHttpClient(settings, true);
} }
/** /**
* Build the URL of specific Torrentflux site request from the user settings and some requested action. * Build the URL of specific Torrentflux site request from the user settings and some requested action.
* @param act The action to perform, which is an already build query string without usernmae/password, i.e. dispatcher.php?action=stop&transfer=ubuntu.torrent * @param act The action to perform, which is an already build query string without usernmae/password, i.e.
* @return The URL of a specific request, i.e. http://localhost:80/turrentflux/dispatcher.php?action=stop&transfer=ubuntu.torrent&username=admin&md5pass=asd98as7d * dispatcher.php?action=stop&transfer=ubuntu.torrent
* @return The URL of a specific request, i.e.
* http://localhost:80/turrentflux/dispatcher.php?action=stop&transfer=ubuntu
* .torrent&username=admin&md5pass=asd98as7d
*/ */
private String buildWebUIUrl(String act) { private String buildWebUIUrl(String act) {
String folder = settings.getFolder().endsWith("/")? String folder = "";
settings.getFolder().substring(0, settings.getFolder().length() - 1): if (settings.getFolder() != null) {
settings.getFolder(); folder = settings.getFolder();
return (settings.getSsl() ? "https://" : "http://") + settings.getAddress() + ":" + settings.getPort() + if (folder.endsWith("/"))
folder + act + RPC_URL_USER + settings.getUsername() + RPC_URL_PASS + md5Pass((settings.getPassword() == null? "": settings.getPassword())); folder = folder.substring(0, folder.length() - 1);
}
return (settings.getSsl() ? "https://" : "http://") + settings.getAddress() + ":" + settings.getPort() + folder
+ act + RPC_URL_USER + settings.getUsername() + RPC_URL_PASS
+ md5Pass((settings.getPassword() == null ? "" : settings.getPassword()));
} }
/** /**
* Calculate the MD5 hash of a password to use with the Torrentflux-b4rt dispatcher requests. * Calculate the MD5 hash of a password to use with the Torrentflux-b4rt dispatcher requests.
* @param pass The plain text password * @param pass The plain text password
@ -280,9 +287,9 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
public static String md5Pass(String pass) { public static String md5Pass(String pass) {
try { try {
MessageDigest m = MessageDigest.getInstance("MD5"); MessageDigest m = MessageDigest.getInstance("MD5");
byte[] data = pass.getBytes(); byte[] data = pass.getBytes();
m.update(data,0,data.length); m.update(data, 0, data.length);
BigInteger i = new BigInteger(1,m.digest()); BigInteger i = new BigInteger(1, m.digest());
return String.format("%1$032X", i); return String.format("%1$032X", i);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
@ -299,5 +306,5 @@ public class Tfb4rtAdapter implements IDaemonAdapter {
public DaemonSettings getSettings() { public DaemonSettings getSettings() {
return this.settings; return this.settings;
} }
} }

Loading…
Cancel
Save