Ubuntu Server 16.04.1 LTS 64-bit
or Ubuntu Server 18.04.1 LTS 64-bit
.SecretId
and SecretKey
) and APPID
are required for deploying and running the key hotlink protection signature distribution service demo.APPID
on the Account Information page in the console as shown below:
ubuntu@VM-69-2-ubuntu:~$ export SECRET_ID=AKxxxxxxxxxxxxxxxxxxxxxxx; export SECRET_KEY=xxxxxxxxxxxxxxxxxxxxx;export APPID=125xxxxxxx;export ANTI_LEECH_KEY=xxxx;git clone https://github.com/tencentyun/vod-server-demo.git ~/vod-server-demo; bash ~/vod-server-demo/installer/ anti_leech_sign_scf_en.sh
[2020-06-04 15:57:10] Start checking npm.[2020-06-04 15:57:18] npm is successfully installed.[2020-06-04 15:57:18] Start installing ServerLess.[2020-06-04 15:57:19] ServerLess is successfully installed.[2020-06-04 15:57:20] Start deploying the VOD key hotlink protection signature distribution service.[2020-06-04 15:57:30] The deployment of the VOD key hotlink protection signature distribution service is completed.[2020-06-04 15:57:32] Service address: https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/anti_leech_sign
https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/anti_leech_sign
in this example).[2020-04-25 17:18:44] Warning: the key hotlink protection signature distribution service failed the test.
curl
command to try directly accessing this URL. The access will be rejected by the server for non-compliance with the key hotlink protection rule, and the HTTP return code will be 403 (during the test, please replace the URL in the command with the actual URL, which also applies below):ubuntu@VM-69-2-ubuntu:~$ curl -I "http://125xxxxxxx.vod2.myqcloud.com/f888c998vodcq125xxxxxxx/c849148f528xxxxxxxxxxxxxxxx/xxxxxxxxxx.mp4"HTTP/1.1 403 ForbiddenServer: NWS_VPConnection: keep-aliveDate: Thu, 04 Jun 2020 08:27:54 GMTContent-Type: text/plainContent-Length: 14
curl
command to request the URL with the hotlink protection signature from the service deployed in step 4 (-d
means to initiate the request in POST method, and the carried parameter is the video URL):ubuntu@VM-69-2-ubuntu:~$ curl -d 'http://125xxxxxxx.vod2.myqcloud.com/f888c998vodcq125xxxxxxx/c849148f528xxxxxxxxxxxxxxxx/xxxxxxxxxx.mp4' https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/anti_leech_sign; echohttp://125xxxxxxx.vod2.myqcloud.com/f888c998vodcq125xxxxxxx/c849148f528xxxxxxxxxxxxxxxx/xxxxxxxxxx.mp4?t=5ed8b8d2&exper=0&rlimit=0&us=455041&sign=fe6394007c2e7aef39fc70a02e897f69
curl
command again to access the URL with the hotlink protection signature obtained in the previous step, and the URL can be accessed normally (the HTTP return code will be 200):ubuntu@VM-69-2-ubuntu:~$ curl -I "http://125xxxxxxx.vod2.myqcloud.com/f888c998vodcq125xxxxxxx/c849148f528xxxxxxxxxxxxxxxx/xxxxxxxxxx.mp4?t=5ed8b8d2&exper=0&rlimit=0&us=455041&sign=fe6394007c2e7aef39fc70a02e897f69"HTTP/1.1 200 OKServer: tencent-cosConnection: keep-aliveDate: Thu, 04 Jun 2020 08:37:17 GMTLast-Modified: Fri, 22 May 2020 15:06:15 GMTContent-Type: video/mp4Content-Length: 232952632Accept-Ranges: bytesETag: "1da6be3a0d1da5edae4ff0b1feff02cf-223"x-cos-hash-crc64ecma: 16209801220610226954x-cos-request-id: NWVkOGIyYmVfZDUyMzYyNjRfYWMwMF85YjkyNzA=X-Daa-Tunnel: hop_count=4X-NWS-LOG-UUID: b404f43e-3c86-4c54-8a78-fb78e4e85cf2 add71e19fb08c6d9dbe1b21a2fb157bfAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Origin,No-Cache,X-Requested-With,If-Modified-Since,Pragma,Last-Modified,Cache-Control,Expires,Content-Type,X_Requested_With,RangeAccess-Control-Allow-Methods: GET,POST,OPTIONSAccess-Control-Allow-Origin: *
Service | Function Name | API Form | Request Content | Response Content |
Key hotlink protection signature distribution | anti_leech_sign | HTTP POST | Original video URL | URL with hotlink protection signature |
main_handler()
is the entry function.parse_conf_file()
and read the configuration information from the config.json
file. The configuration items are as described below (for specific parameters, please see Key Hotlink Protection):Field | Data Type | Description |
key | String | Key hotlink protection key |
t | Integer | Signature validity period in seconds. When a request is being processed, this parameter plus the current time on the SCF server will be `t` in the hotlink protection parameters |
exper | Integer | Preview duration |
rlimit | Integer | Maximum number of client IPs that can access the signature |
Dir
parameter from the request body, generate the t
and us
parameters locally, and read the exper
and rlimit
parameters from the configuration file:original_url = event["body"]parse_result = urlparse(original_url)directory = path.split(parse_result.path)[0] + '/'# Signature parameterstimestamp = int(time.time())rand = random.randint(0, 999999)sign_para = {"t": hex(timestamp + configuration['t'])[2:],"exper": configuration['exper'],"rlimit": configuration['rlimit'],"us": rand}
generate_sign()
to calculate the hotlink protection signature. For the specific algorithm, please see Key Hotlink Protection.QueryString
and add it at the end of the original URL to concatenate a URL with the hotlink protection signature:sign_para["sign"] = signaturequery_string = urlencode(sign_para)new_parse_result = parse_result._replace(query=query_string)signed_url = urlunparse(new_parse_result)
return {"isBase64Encoded": False,"statusCode": 200,"headers": {"Content-Type": "text/plain; charset=utf-8","Access-Control-Allow-Origin": "*","Access-Control-Allow-Methods": "POST,OPTIONS"},"body": signed_url}
Was this page helpful?