aboutsummaryrefslogtreecommitdiff
path: root/fetch.lua
blob: 72b579a4fa027374f566f1c1498872f97b02120b (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
--
-- ~~~ fetch a pawsd service
--

-- require a name & service index to fetch
if not arg[1] or not arg[2] then
    error("server name & index required")
end

-- imports
local PawSD = require("src/pawsd/variables")
local Servers = require("src/pawsd/servers")
require("src/utils")

local pawsd = {}
pawsd.response = require("src/pawsd/metadata")
pawsd.signature = require("src/pawsd/signature")
pawsd.service = require("src/pawsd/service")

local socket = require("socket")
local client = assert(socket.tcp())

-- get arguments & check validity
local server = Servers[arg[1]]
local index = arg[2]

if not server then
    error("server not found")
end

-- connect to client
client:connect(server.host, server.port)

-- send request
local request = string.fromhex(PawSD.Magic.REQUEST .. PawSD.Version .. PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. index)
client:send(request)

-- print response
local response = {}

while true do
    local packet, status = client:receive(1)

    -- if packet then print(response) end
    table.insert(response, packet)
    if status == "closed" then break end
end

-- check that the response is correct
local responseMagic = pawsd.response.type(response)
if not responseMagic == PawSD.Magic.RESPONSE then
    error("invalid response")
end

print("R> Response Magic:                                                                          " .. responseMagic)

-- check that the status is okay
local responseStatus = pawsd.response.status(response)
if not responseStatus == PawSD.Responses.OK then
    error("unsuccessful request")
end

print("R> Response Status:                                                                         " .. responseStatus)

-- get signature information
local responseSignature = pawsd.signature.get(response)
local responseSignatureValid = pawsd.signature.validate(responseSignature, server.key)

print("R> Signature:                                                                               " .. responseSignature)

if responseSignatureValid then
    print("R> Signature Validity:                                                                      VALID")
else
    print("R> Signature Validity:                                                                      INVALID")
end

-- get service info
local responseIndex = pawsd.service.index(response)
local responseFlags = pawsd.service.flags(response)

print("\nR> Index:                                                                                   " .. responseIndex)
print("R> Flags:                                                                                   " .. responseFlags)

-- get number of records
local responseRecords = pawsd.service.records.all(response)

-- close connection
client:close()