Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nemec committed Oct 24, 2017
1 parent dbf8197 commit 844fd4e
Show file tree
Hide file tree
Showing 33 changed files with 864 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.idea/workspace.xml
.idea/**/gradle.xml
.idea/**/libraries
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
.idea_modules/
14 changes: 14 additions & 0 deletions .idea/artifacts/JDHttpApi_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/libraries/Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions JDHttpApi.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Core" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-server:9.4.7.v20170914" level="project" />
<orderEntry type="library" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-http:9.4.7.v20170914" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-util:9.4.7.v20170914" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-io:9.4.7.v20170914" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.2" level="project" />
</component>
</module>
125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,127 @@
# JDHttpAPI
JDownloader2 plugin with a local HTTP API to add new files for download

This plugin is loaded by JDownloader2, which then starts up an embedded Jetty
HTTP server to receive

From there, just about anything can send requests to queue up a new link. I
like to use [Tampermonkey userscripts](https://tampermonkey.net/) to scrape
links directly from web pages as I browse.

Note: it binds to your IP address, therefore other PCs on the network can add
links. Configure a password or update the code to bind to something else -
there is not currently a way configure local-only access. This plugin also
accepts all Origins over ajax.

## Install

1. Close JDownloader.
2. Build or download the `JDHttpApi.jar` file.
3. Place that file in `./extensions/` inside the JDownloader2 program folder.
3. Open the file `./tmp/extensioncache/extensionInfos.json` in your JD program
folder and insert the following JSON as a new element in the array:

```
{
"settings" : true,
"configInterface" : "org.jdownloader.extensions.httpAPI.HttpAPIConfig",
"quickToggle" : false,
"headlessRunnable" : true,
"description" : "Http API.",
"lng" : "en_US",
"iconPath" : "folder_add",
"linuxRunnable" : true,
"macRunnable" : true,
"name" : "HTTP API",
"version" : -1,
"windowsRunnable" : true,
"classname" : "org.jdownloader.extensions.httpAPI.HttpAPIExtension",
"jarPath" : "/absolute/path/to/jd2/extensions/JDHttpApi.jar"
}
```

4. Open the file `./update/versioninfo/JD/extensions.installed.json` inside the
JD program folder and insert the string `"jdhttpapi"` as the last element
in the JSON array.
5. Start JDownloader2 and go to the settings page to enable the extension.

Note: there is no official extension install process, so most of this is
tricking JD into thinking the JAR is already installed. It's possible that
an update will trigger a cache invalidation that boots out this extension. Just
reapply the steps above and you should get it back.

## Configure

This plugin has four settings to configure through the JDownloader GUI. In order
to make changes take effect, disable and then re-enable the extension.

1. Use a password: require password authentication.
2. Port: the port that the HTTP server listens on. Defaults to 8297.
3. Password: If you've enabled the password feature, set it here.
4. Allow Get: This disables HTTP GET access to the API. When enabled, you
must POST new links as JSON. This can help you reduce exposure to CSRF
vulnerabilities, especially when combined with a password.

## Use

There is currently only one URL: `/addLink`. You may submit via POST or GET.

### GET Service

There are three parameters, only `url` is required:

* `url`: The URL to add.
* `packageName`: If you want to configure a custom package name, send it here.
* `forcePackageName`: Set this to `true` to set the package name. For whatever
reason, this is a separate field in JD's internals but if you don't force
it, the package name will not be changed. It's possible I am hooking in too
late in the process.

The response has three possible values:

On success: `{"success":true}`
On malformed input: `{"errorMessage":"some error message"}`
On authentication failure: `Login to access API` (this is not JSON)

CURL Examples:

```
curl 'http://localhost:8297/addLink?url=https://i.imgur.com/muChjiN.jpg'
curl 'http://localhost:8297/addLink?url=https://i.imgur.com/muChjiN.jpg&packageName=cutebunny&forcePackageName=true'
```

### POST Service

Send a JSON object via POST with the same values as in the GET request:

```
curl -X POST 'http://localhost:8297/addLink' \
-d '{"url":"https://i.imgur.com/muChjiN.jpg"}'
curl -X POST 'http://localhost:8297/addLink' \
-d '{"url":"https://i.imgur.com/muChjiN.jpg","packageName":"cutebunny","forcePackageName":true}'
```

### Authentication

If you choose to use a password, send it as the password portion of HTTP Basic
Authentication - leave the username blank.

CURL Example:

```
curl -u ':mypassword' 'http://192.168.1.7:8297/addLink?url=https://i.imgur.com/muChjiN.jpg'
curl -u ':mypassword' -X POST 'http://localhost:8297/addLink' \
-d '{"url":"https://i.imgur.com/muChjiN.jpg"}'
```

## Errata

JDownloader2's extension system is surprisingly
modular and easy to understand (aside from the install process). Feel free to
use this project as an example for building other JD extensions as even the
built-in ones are a bit more complex than this. Just make sure to have
[the source code](https://svn.jdownloader.org/projects/jd) open up in
another window for reference - there is virtually no documentation available
and this project only uses the bare minimum of features.


Binary file added lib/Core.jar
Binary file not shown.
Binary file added lib/JDGUI.jar
Binary file not shown.
Binary file added lib/JDownloader.jar
Binary file not shown.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>jdownloaderPlugins</groupId>
<artifactId>JDownloaderPlugins</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>
</dependencies>

</project>
Binary file added settings-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Manifest-Version: 1.0
Class-Path: jetty-io-9.4.7.v20170914.jar jetty-server-9.4.7.v20170914.
jar Core.jar JDownloader.jar JDGUI.jar gson-2.8.2.jar jetty-http-9.4.
7.v20170914.jar javax.servlet-api-3.1.0.jar jetty-util-9.4.7.v2017091
4.jar

29 changes: 29 additions & 0 deletions src/main/java/org/jdownloader/extensions/httpAPI/CFG_HTTPAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jdownloader.extensions.httpAPI;

import org.appwork.storage.config.handler.BooleanKeyHandler;
import org.appwork.storage.config.handler.IntegerKeyHandler;
import org.appwork.storage.config.handler.StorageHandler;
import org.appwork.storage.config.handler.StringKeyHandler;
import org.appwork.utils.Application;
import org.appwork.storage.config.ConfigUtils;
import org.appwork.storage.config.JsonConfig;

public class CFG_HTTPAPI {
public static void main(String[] args) {
ConfigUtils.printStaticMappings(HttpAPIConfig.class, "Application.getResource(\"cfg/\" + " + HttpAPIExtension.class.getSimpleName() + ".class.getName())");
}

public static final HttpAPIConfig CFG = JsonConfig.create(Application.getResource("cfg/" + HttpAPIExtension.class.getName()), HttpAPIConfig.class);

public static final StorageHandler<HttpAPIConfig> SH = (StorageHandler<HttpAPIConfig>) CFG._getStorageHandler();

public static final BooleanKeyHandler ENABLED = SH.getKeyHandler("Enabled", BooleanKeyHandler.class);

public static final IntegerKeyHandler PORT = SH.getKeyHandler("Port", IntegerKeyHandler.class);

public static final BooleanKeyHandler ALLOW_GET = SH.getKeyHandler("AllowGet", BooleanKeyHandler.class);

public static final BooleanKeyHandler USE_PASSWORD = SH.getKeyHandler("UsePassword", BooleanKeyHandler.class);

public static final StringKeyHandler PASSWORD = SH.getKeyHandler("Password", StringKeyHandler.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jdownloader.extensions.httpAPI;

import jd.plugins.ExtensionConfigInterface;
import org.appwork.storage.config.annotations.AboutConfig;
import org.appwork.storage.config.annotations.DefaultBooleanValue;
import org.appwork.storage.config.annotations.DefaultIntValue;

public interface HttpAPIConfig extends ExtensionConfigInterface {

@AboutConfig
@DefaultIntValue(8297)
int getPort();
void setPort(int port);

@AboutConfig
@DefaultBooleanValue(false)
boolean getAllowGet();
void setAllowGet(boolean enabled);

@AboutConfig
@DefaultBooleanValue(false)
boolean getUsePassword();
void setUsePassword(boolean enabled);

@AboutConfig
String getPassword();
void setPassword(String password);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jdownloader.extensions.httpAPI;


import org.jdownloader.extensions.ExtensionConfigPanel;

import jd.gui.swing.jdgui.views.settings.panels.advanced.AdvancedConfigTableModel;
import jd.gui.swing.jdgui.views.settings.panels.advanced.AdvancedTable;

public class HttpAPIConfigPanel extends ExtensionConfigPanel<HttpAPIExtension> {

private AdvancedConfigTableModel model;

public HttpAPIConfigPanel(HttpAPIExtension extension){
super(extension);

add(new AdvancedTable(model= new AdvancedConfigTableModel("HttpAPIConfigPanel"){
@Override
public void refresh(String filterText){ _fireTableStructureChanged(register(), true);}
}));
model.refresh("HttpAPIConfigPanel");
}

@Override
public void save() {
}

@Override
public void updateContents() {

}
}
Loading

0 comments on commit 844fd4e

Please sign in to comment.