Any way to verifiy a magnet link (Javascript)

本秂侑毒 提交于 2019-12-10 11:27:23

问题


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?


回答1:


<!-- 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




回答2:


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.




回答3:


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 strings / numbers up to the next semi-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.




回答4:


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 :)



来源:https://stackoverflow.com/questions/8227280/any-way-to-verifiy-a-magnet-link-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!