Repository: pyload/pyload

Issue:
Click And Load not working when "Use HTTPS" for the Webinterface is activated
This is a plugin issue with Click and Load:

When the option "Use HTTPS" for the Webinterface is activated (-> webserver: threaded for SSL support instead of builtin), Click and Load won't work.
Unfortunately, this setting also affects the Click and Load function, although Click and Load listens on a different Port (9666) than the webinterface (8000).

When the option is activated, Click and Load only accepts HTTPS requests. But almost all Click and Load requests connect with http://-ip-:9666/flash/, and not with https://-ip-:9666/flash/

The result is a "400 Bad Request" response from the Click and Load function to the client, Example:
Remote Address:-ip-:9666, Request URL:http://-ip-:9666/flash/addcrypted2
Status Code:400 Bad Request
Response: "The client sent a plain HTTP request, but this server only speaks HTTPS on this port."

-> "Use HTTPS" for the Webinterface should not affect Click and Load.
Click and Load should always listen to plain http requests on port 9666, regardless of whether the webinterface uses https or not.

Turning off "Use HTTPS" for the Webinterface and everything works fine, but that's not really a solution regarding security concerns.

Reproduce:
install pyload on debian (tested on Raspbian), activate SSL /  "Use HTTPS" Webinterface and try to add a link with click and load. (I can add an exact list of steps to reproduce if necessary)



Discussion:
Can you append result this command?

dpkg -l *

wget 127.0.0.1:9666/flash/addcrypted2

wget 127.0.0.1:9666/flashgot

wget localhost:9666/flashgot

I reproduced it on a new debian 8 installation, here is a complete list of all executed commands and the wget / dpkig results:

http://pastebin.com/0jpy6m2v
lines 3-136: commands & configuration to reproduce the pyload installation
lines 138 - 183: wget results
lines 186 - end: dpkg -l *


Resolving patch:
diff --git a/module/plugins/hooks/ClickAndLoad.py b/module/plugins/hooks/ClickAndLoad.py
--- a/module/plugins/hooks/ClickAndLoad.py
+++ b/module/plugins/hooks/ClickAndLoad.py
@@ -3,6 +3,11 @@
 import socket
 import time
 
+try:
+    import ssl
+except ImportError:
+    pass
+
 from threading import Lock
 
 from module.plugins.Hook import Hook, threaded
@@ -24,7 +29,7 @@ def forward(source, destination):
 class ClickAndLoad(Hook):
     __name__    = "ClickAndLoad"
     __type__    = "hook"
-    __version__ = "0.41"
+    __version__ = "0.42"
 
     __config__ = [("activated", "bool", "Activated"                             , True),
                   ("port"     , "int" , "Port"                                  , 9666),
@@ -79,6 +84,21 @@ def _server(self, ip, webport, cnlport):
                 self.logDebug("Connection from %s:%s" % client_addr)
 
                 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+                if self.config['webinterface']['https']:
+                    try:
+                        server_socket = ssl.wrap_socket(server_socket)
+
+                    except NameError:
+                        self.logError(_("pyLoad's webinterface is configured to use HTTPS, Please install python's ssl lib or
+                        client_socket.close()  #: reset the connection.
+                        continue
+
+                    except Exception, e:
+                        self.logError(_("SSL error: %s") % e.message)
+                        client_socket.close()  #: reset the connection.
+                        continue
+
                 server_socket.connect(("127.0.0.1", webport))
 
                 self.manager.startThread(forward, client_socket, server_socket)


Test patch: