Any way to verifiy a magnet link (Javascript)

6.3k views Asked by At

Possibly an odd question, but I'm sure someone has thought of it before :) I'm wondering if it's at all possible to verify a given string as being a theoretically valid Magnet link, using JS.

Not particularly bothered about opening the link etc. (That's done elsewhere), I'm more concerned here about weeding out broken/ truncated links.

The best I can come up with from the top of my head is a simple beginning of string match for the magnet:?xt=urn:

I suppose I could preface this with a length condition (20+ characters seems reasonable?), but does anyone have a 'better' solution?

4

There are 4 answers

0
powtac On
<!-- HTML -->
<div id="link">magnet:?xt=urn:3216546465987dfgs9798</div>

The JavaScript:

var magnet_link = document.getElementById('link').innerHTML;

if (magnet_link.match(/magnet:\?xt=urn:[a-z0-9]{20,50}/i) != null) {
    alert('Link is valid');
}

Short:

("magnet:?xt=urn:3216546465987dfgs9798".match(/magnet:\?xt=urn:[a-z0-9]{20,50}/i) != null)

Wikipedia about Magnet URI scheme

3
Aldo Stracquadanio On

The only way I can imagine is to use the Regular Expressions instead of simple string matching and length evaluation. This would allow you to write more strict rules about the form of the magnet link.

If you don't know regular expressions this is the right moment to start using them, they are very powerful and they are a must in the "toolbox" of every programmer (whatever language he is interested in).

Here is a starting point, however you can find plenty of documentation online.

0
Jimbo On

I tried the above regular expression and it didn't work, so I created my own. I looked at the Wikipedia Magnet URI scheme which states that the magnet identifier is Base32, which means:

Base32 is a base-32 transfer encoding using the twenty-six letters A-Z and six digits 2-7. [Although my understanding is that these digits and letters can be interpolated at random].

As a result, we're looking for the following in a regex:

  • The word magnet followed by a semicolon, a questionmark and an "xt=urn:" string
    • /magnet:\?xt=urn:
  • Any number of letters/numbers up to the next colon (the question's regex fails this)
    • [a-z0-9]+:
  • From our research above, 32 characters (base32) of interpolated letters and numbers
    • [a-z0-9]{32}/i

The beginning / and the ending / have to be there, because it's a regex, to denote the start and end, and the i at the end (/i) denotes a case-insensitive regex. If we didn't do the /i, we'd be having to check for [a-zA-Z0-9].

The final regex, which actually works, is as follows:

/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i

You can try this for yourself:

var torrent = "magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C";

if (torrent.match(/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i) !== null)
{
    console.log("It's valid, bloody fantastic!");
}

Obligatory JSFiddle.

2
Marcos On

JFYI, i found a similar problem days ago, and i found magnets are case sensitive and must be capitalized (confirmed with deluge team), so the validation must be A-Z, not a-z.

More info on: deluge bug track

Meanwhile deluge team fix it, i'm planning to write a chrome extension to fix it, but i'm little bussy right now :)