added code

This commit is contained in:
uh wot 2021-01-25 01:39:01 +01:00
parent 168093e034
commit b60bdb0384
Signed by: uhwot
GPG Key ID: CB2454984587B781
5 changed files with 101 additions and 15 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 uh_wot
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
dist/worker.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,27 +1,85 @@
const Router = require('./router') const Router = require('./router')
const aesjs = require('aes-js');
/**
* Example of how router can be used in an application
* */
addEventListener('fetch', event => { addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request)) event.respondWith(handleRequest(event.request))
}) })
function handler(request) { const AESKEY = [106, 111, 54, 97, 101, 121, 54, 104, 97, 105, 100, 50, 84, 101, 105, 104]
const init = {
headers: { 'content-type': 'application/json' }, url_gen = {
"cipher": new aesjs.ModeOfOperation.ecb(AESKEY),
"resolve": async function(md5_origin, format, id, media_version) { // Track URL generation function
result = [md5_origin, format, id, media_version].join('\xa4')
var buf = new ArrayBuffer(result.length);
var result_hash = new Uint8Array(buf);
for (var i=0; i < result.length; i++) {
result_hash[i] = result.charCodeAt(i);
} }
const body = JSON.stringify({ some: 'json' }) result_hash = await crypto.subtle.digest('MD5', result_hash)
return new Response(body, init) result_hash = Array.from(new Uint8Array(result_hash))
result_hash = result_hash.reduce(function(previous, current) {
return previous + "0".concat(current.toString(16)).substr(-2, 2);
}, ''),
result = result_hash + '\xa4' + result + '\xa4'
// padding
result += '\x00'.repeat(result.length % 16 ? 16 - result.length % 16 : 0)
// converting to array for encryption / hex string
result = Array.from(result).map(function(item) {
return item.charCodeAt(0);
})
// encryption / hex string
result = (result = url_gen.cipher.encrypt(result)).reduce(function(previous, current) {
return previous + "0".concat(current.toString(16)).substr(-2, 2);
}, '')
// cdn template with first character of md5 string + hash
return `https://e-cdns-proxy-${md5_origin[0]}.dzcdn.net/api/1/${result}`
}
}
async function track(request) {
const id = request.url.split('/')[4]
if (id === "") {
return new Response("Track ID needs to be specified", {status: 400, headers: {'content-type': "text/plain"}})
}
access_token = await KV.get("access_token")
if (access_token === null) {
const response = await fetch("https://connect.deezer.com/oauth/access_token.php?grant_type=client_credentials&client_id=447462&client_secret=a83bf7f38ad2f137e444727cfc3775cf&output=json")
const json = await response.json()
if (json.error !== undefined) {
return new Response("Couldn't get access token from Deezer", {status: 500, headers: {'content-type': "text/plain"}})
}
access_token = json.access_token
await KV.put("access_token", access_token, {expirationTtl: Number(json.expires)})
}
const response = await fetch(`https://api.deezer.com/track/${id}?access_token=${access_token}`)
const json = await response.json()
if (json.error !== undefined) {
return new Response(JSON.stringify(json.error), {status: 403, headers: {'content-type': "application/json"}})
}
const md5_origin = json.md5_origin
const format = "3"
const media_version = json.media_version
const url = await url_gen.resolve(md5_origin, format, id, media_version)
return new Response(null, {status: 302, headers: {'location': url}})
} }
async function handleRequest(request) { async function handleRequest(request) {
const r = new Router() const r = new Router()
// Replace with the appropriate paths and handlers // Replace with the appropriate paths and handlers
r.get('.*/bar', () => new Response('responding for /bar')) r.get('/track/.*', () => track(request))
r.get('.*/foo', request => handler(request))
r.post('.*/foo.*', request => handler(request))
r.get('/demos/router/foo', request => fetch(request)) // return the response from the origin
r.get('/', () => new Response('Hello worker!')) // return a default message for the root route r.get('/', () => new Response('Hello worker!')) // return a default message for the root route

View File

@ -1,18 +1,19 @@
{ {
"name": "dzserver", "name": "dzserver",
"version": "1.0.0", "version": "1.0.0",
"description": "package for creating workers templates", "description": "worker for getting deezer track urls",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier --write '**/*.{js,css,json,md}'" "format": "prettier --write '**/*.{js,css,json,md}'"
}, },
"author": "uh_wot <uhwot@protonmail.com>", "author": "uh_wot <uhwot@protonmail.com>",
"license": "ISC", "license": "MIT",
"devDependencies": { "devDependencies": {
"prettier": "^1.17.0" "prettier": "^1.17.0"
}, },
"dependencies": { "dependencies": {
"aes-js": "^3.1.2",
"serverless-cloudflare-workers": "^1.2.0" "serverless-cloudflare-workers": "^1.2.0"
} }
} }

View File

@ -1,6 +1,9 @@
name = "dzserver" name = "dz"
type = "webpack" type = "webpack"
account_id = "03479b0523a52b140e0dabac40cb0fc8" account_id = "03479b0523a52b140e0dabac40cb0fc8"
workers_dev = true workers_dev = true
route = "" route = ""
zone_id = "" zone_id = ""
kv_namespaces = [
{ binding = "KV", id = "08543eae8cb7494f8f17addaf70f4143", preview_id = "66868b338ff34776a7d8a5d1877c8181" }
]