-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathampache-handshake.lua
75 lines (66 loc) · 2.51 KB
/
ampache-handshake.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-----------------------------------------------------
-- ----------------------------------------------- --
-- ▄ ▄ ▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄ ▄ --
-- ▐░▌ ▐░▌ ▐░▌▐░█▀▀▀▀▀ ▀▀█░█▀▀ ▐░▌ ▐░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█ █░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░░░░░░░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▀▀▀▀▀█░▌ --
-- ▐░█▄▄▄▄▄ ▐░█▄▄▄█░▌▐░█▄▄▄▄▄ ▄▄█░█▄▄ ▐░▌ --
-- ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀ --
-- ----------------------------------------------- --
---- Luci4 util to get Ampache handshake values -----
-- -------- https://github.com/icefields --------- --
-----------------------------------------------------
local http = require("socket.http")
local ltn12 = require("ltn12")
local cjson = require("cjson")
local sha2 = require("sha2")
-- get the current Unix timestamp
local function getTimestamp()
return os.time()
end
local function calculateSha256(input)
return sha2.sha256(input)
end
-- fetch JSON from the URL
local function fetchJson(url)
local responseBody = {}
local _, code, _, _ = http.request{
url = url,
sink = ltn12.sink.table(responseBody)
}
if code ~= 200 then
error("HTTP request failed with status code " .. code)
end
return table.concat(responseBody)
end
local function str2Json(str)
return cjson.decode(str)
end
-- Main function
local function handshake(serverUrl, username, password)
local timestamp = tostring(getTimestamp())
local passwordSha256 = calculateSha256(password)
local auth = calculateSha256(timestamp .. passwordSha256)
local url = string.format(
"%s/server/json.server.php?action=handshake×tamp=%s&auth=%s&user=%s",
serverUrl,
timestamp,
auth,
username
)
-- print(password)
-- print()
-- print(url)
return str2Json(fetchJson(url))
end
local function getAuthToken(serverUrl, username, password)
local jsonResp = handshake(serverUrl, username, password)
return jsonResp['auth']
end
-- print(getAuthToken())
-- Return a table containing the functions
return {
getAuthToken = getAuthToken,
handshake = handshake
}