mayswind / AriaNg

AriaNg, a modern web frontend making aria2 easier to use.
http://ariang.mayswind.net
MIT License
11.85k stars 1.29k forks source link

How to register AriaNg as a magnet protocol handler? #551

Open levicki opened 4 years ago

levicki commented 4 years ago

qBitTorrent web UI has an option to register as a protocol handler for magnet links.

After it is registered, clicking on a magnet link in your browser automatically opens Add URL dialog.

Does AriaNg support that?

If not, is there any chance it could be added?

This is what I have in mind (screenshot is from Firefox):

image

Please check here for implementation details: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/registerProtocolHandler/Web-based_protocol_handlers#Registering

The only obstacle I see is the way adding a new download is implemented at the moment. You should have a GET-able URL which receives link as a parameter. For example:

https://core.levicki.lan:8001/#!/new?link=%s

Where %s is a magnet link placeholder.

mayswind commented 4 years ago

I'm afraid AriaNg cannot do this, but I think there are some browser addons can do this job. This page (http://ariang.mayswind.net/3rd-extensions.html) lists some browser addons. In addition, AriaNg Native provides a native desktop app and it supports opening via torrent file or magnet link.

levicki commented 4 years ago

I'm afraid AriaNg cannot do this...

On the other hand I am almost sure it could, but sadly there seems to be no interest in implementing this feature in any of Aria web UIs.

It is nice that there is a Firefox addon, but I am not looking to fully replace Firefox download manager with AriaNg, just to register AriaNg as a magnet protocol handler.

VaslD commented 4 years ago

Although the web UI doesn't implement this, if the native app registers as a magnet protocol handler (association) system-wide then technically Firefox should pick this up and present you with the dialog just like with QBitTorrent (which is just another "native app" in Firefox's perspective).

levicki commented 4 years ago

@VaslD If I am not mistaken, that would require using native app?

I have AriaNg Web UI served from an Owin-based Windows Service (which I wrote myself in C#, and which also brings up and down aria2c.exe RPC server when started/stopped). This service is running on a remote server, I am not sure how native app would fit with all this,

levicki commented 4 years ago

@mayswind I have managed to register AriaNg as protocol handler:

image

However, the current implementation requires URL to be base64 encoded. When clicking on the magnet link I am getting the following error:

image

This is expected because browser always passes the plain URL and that cannot be changed without extension or say TamperMonkey userscript.

Could you please implement an API version that accepts unencoded URL, and encodes it before passing to Aria RPC server?

For example: https://core.levicki.lan:8001/#!/new/task?rawurl=<unencoded url here>

Then it would be possible to handle magnet links directly from AriaNg. The only other thing you would need is a button which says Register AriaNg as magnet link handler and which executes the following javascript:

<script>
navigator.registerProtocolHandler("magnet", "https://<AriaNg URL>/#!/new/task?rawurl=%s", "AriaNg");
</script>

Which would look like this (Firefox Options) after registration:

image

retardTonic commented 3 years ago

For anyone interested, I have managed to find a way to register a protocol handler in Firefox using brute force, bypassing all sanity checks.

Spoiler 1. Open Browser console (Ctrl+Shift+J) 2. Type in the following (replace `` with URL and `` with preferred handler name): ```javascript var handler = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(Ci.nsIWebHandlerApp); handler.name = ""; handler.uriTemplate = "javascript:(function(){window.location.href='/#!/new/task?url='+btoa('%s').replace('+', '-').replace('/', '_')})()"; var handlerInfo = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService).getProtocolHandlerInfo("magnet"); handlerInfo.possibleApplicationHandlers.appendElement(handler); handlerInfo.alwaysAskBeforeHandling = true; Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService).store(handlerInfo); ``` However, internal API might change at any time, and I am not liable to any damage this snippet may cause. AriaNg still needs to be hosted on a server, as the browser prohibits accessing local files from DOM in any way. ###### References [mozilla/gecko-dev@1f1b24c/browser/components/protocolhandler/WebProtocolHandlerRegistrar.jsm](https://github.com/mozilla/gecko-dev/blob/1f1b24c673b791b885e4152a83d006b70be4e551/browser/components/protocolhandler/WebProtocolHandlerRegistrar.jsm)

Still, implementing unsafe URLs or just using an extension/bookmarklet would be the proper way.

levicki commented 3 years ago

For anyone interested, I have managed to find a way to register a protocol handler in Firefox using brute force, bypassing all sanity checks.

Interesting approach. Is there a way to do similar thing in Chrome perhaps?

Still, implementing unsafe URLs or just using an extension/bookmarklet would be the proper way.

I'd prefer if AriaNg exposed an API for this purpose which is compliant with normal ways browsers handle this.

In the meantime I am using Chrome extension edcakfpjaobkpdfpicldlccdffkhpbfk.