Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
levicki opened this issue Aug 17, 2020 · 7 comments
Open

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

levicki opened this issue Aug 17, 2020 · 7 comments

Comments

@levicki
Copy link

levicki commented Aug 17, 2020

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
Copy link
Owner

mayswind commented Aug 19, 2020

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
Copy link
Author

levicki commented Aug 19, 2020 via email

@VaslD
Copy link

VaslD commented Sep 9, 2020

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
Copy link
Author

levicki commented Sep 14, 2020

@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
Copy link
Author

levicki commented Sep 14, 2020

@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
Copy link

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 <URL> with URL and <AriaNg> with preferred handler name):
var handler = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(Ci.nsIWebHandlerApp);
handler.name = "<AriaNg>";
handler.uriTemplate = "javascript:(function(){window.location.href='<URL>/#!/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

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

@levicki
Copy link
Author

levicki commented Feb 23, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants