春江暮客

春江暮客的个人学习分享网站

Decoding Xunlei Thunder Links: Python and JavaScript Conversion Examples

2019-05-17 Miscellaneous
Decoding Xunlei Thunder Links: Python and JavaScript Conversion Examples

Many people come across Xunlei links that start with thunder://. They do not look like normal http or https URLs, but in many cases they are simply ordinary URLs wrapped in a fixed format.

If you just want to understand that format, or convert between a regular URL and a thunder:// link in code, this article covers the common rule. It focuses on the encoding format itself, not on whether a resource is downloadable.

For example, a random download link is:

https://www.bobobk.com/favicon.ico

The most common thunder:// format can be understood like this:

  1. Add AA before the original URL
  2. Add ZZ after the original URL
  3. Base64 encode the whole string
  4. Add the thunder:// prefix in front

So it is not a completely different protocol in itself. In practice, it is often just a fixed string wrapper.

1.1 Add AA to the beginning and ZZ to the end

The string becomes:

AAhttps://www.bobobk.com/favicon.icoZZ

1.2 Base64 encode it

QUFodHRwczovL3d3dy5ib2JvYmsuY29tL2Zhdmljb24uaWNvWlo=

1.3 Add thunder:// in front

thunder://QUFodHRwczovL3d3dy5ib2JvYmsuY29tL2Zhdmljb24uaWNvWlo=

Conversion Code

import base64


def convert_to_thunder(url: str) -> str:
        wrapped = "AA" + url + "ZZ"
        encoded = base64.b64encode(wrapped.encode("utf-8")).decode("utf-8")
        return "thunder://" + encoded


normal_url = 'https://www.bobobk.com/favicon.ico'
print(convert_to_thunder(normal_url))

《Decoding Real Addresses from Xunlei Thunder Download Links》

2.1 Remove the thunder:// prefix

The address becomes:

QUFodHRwczovL3d3dy5ib2JvYmsuY29tL2Zhdmljb24uaWNvWlo=

2.2 Base64 decode it

AAhttps://www.bobobk.com/favicon.icoZZ

2.3 Remove AA from the front and ZZ from the back

https://www.bobobk.com/favicon.ico

Conversion Code

import base64


def convert_thunder(thunder_url: str) -> str:
        encoded = thunder_url.strip().split("//", 1)[1]
        decoded = base64.b64decode(encoded).decode("utf-8")
        return decoded[2:-2]


thunder = 'thunder://QUFodHRwczovL3d3dy5ib2JvYmsuY29tL2Zhdmljb24uaWNvWlo='
print(convert_thunder(thunder))

JavaScript Code

If you only want a quick browser-side conversion, JavaScript can also do it directly.

function decodeThunder(thunderUrl) {
    const encoded = thunderUrl.replace(/^thunder:\/\//, "");
    const decoded = window.atob(encoded);
    return decoded.substring(2, decoded.length - 2);
}

function encodeThunder(normalUrl) {
    const encoded = window.btoa("AA" + normalUrl + "ZZ");
    return "thunder://" + encoded;
}

document.querySelector('#decoded').innerHTML = decodeThunder(
    document.querySelector('#de_thunder').value
);

document.querySelector('#encoded').innerHTML = encodeThunder(
    document.querySelector('#en_thunder').value
);

Two practical notes

  1. Not every thunder:// link necessarily points to a resource that is still available. The original resource still has to exist and be accessible.
  2. If you are processing links in batch, add error handling in code because malformed Base64 input can raise decoding exceptions.

Conclusion

This article explained the most common rule behind thunder:// links: wrap the original URL with AA and ZZ, Base64 encode it, and prepend the thunder:// prefix.

If you only need a simple converter, both Python and JavaScript are enough to implement it quickly. You can also use the ready-made page at Chunjiang Muker Xunlei Encryption and Parsing.

友情链接

其它