aboutsummaryrefslogtreecommitdiff
path: root/src/actions.lua
blob: 370f138dd456b38c89cb909b4107e023b3ab1180 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
--
-- ~~~ selenepaw actions
--

require("src/utils")

-- load help messages
local help = require("src/help")

-- load pawsd
local Servers = require("src/pawsd/servers")

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

local send = require("src/pawsd/send")

-- 0000: echo
function echo ()

    -- check length
    if not (#arg == 3) then

        help.echo()
        return

    end

    -- get server name
    local server = Servers[arg[2]]

    if not server then

        print("\nE> Error: server not found.\n")
        return

    end

    -- log temporary warning message
    print("\nW> Warning: this currently does not work I think.\n")

    -- send request
    local request = PawSD.Verbs.ECHO .. string.tohex(arg[3])
    local response = send(server, request)

    -- log response
    print(response)

end

-- 0001: fetch service
function fetchservice ()

    -- check length
    if not (#arg == 3) then

        help.fetchservice()
        return

    end

    -- get server name
    local server = Servers[arg[2]]

    if not server then

        print("\nE> Error: server not found.\n")
        return

    end

    -- send request
    local request = PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. arg[3]
    local response = send(server, request)

    -- check response
    local valid = pawsd.response.validate(response)

    if not valid then

        print("\nE> Error: invalid request.\n")
        return

    end

    -- todo: validate signature

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

    -- log records
    for recordindex = 1, #records, 1 do
        for recordtagindex = 1, #records[recordindex], 1 do
            print(table.dump(records[recordindex][recordtagindex]))
        end
    end

end

-- misc: dns
function dns ()

    -- check length
    if not (#arg == 3) then

        help.dns()
        return

    end

    -- get server name
    local server = Servers[arg[2]]

    if not server then

        print("\nE> Error: server not found.\n")
        return

    end

    -- repeat requests till hostname found or no more services
    local looking = true
    local foundrecord
    local index = 0

    while looking do

        -- convert number to hex
        local hexIndex = string.format("%04x", index)

        -- send request
        local request = PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. hexIndex
        local response = send(server, request)

        -- check response
        local valid = pawsd.response.validate(response)

        if not valid then

            looking = false
            return

        end

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

        -- check each tag & record
        for recordindex = 1, #records, 1 do
            for recordtagindex = 1, #records[recordindex], 1 do

                -- get tag
                local tag = records[recordindex][recordtagindex]

                -- check if "dns-style hostname"
                if tag.types.high == "EDCC1A0A8817F0F6" and tag.types.low == "E8ED44A7A4760A51" then

                    -- strip index unicode char & compare to wanted hostname
                    local check = string.fromhex(string.sub(tag.value, 1, -3)) == arg[3]

                    if check then

                        looking = false
                        foundrecord = records[recordindex]

                    end
                end
            end
        end

        index = index + 1

    end

    -- if hostname has been found
    if foundrecord then
        for tagindex = 1, #foundrecord, 1 do

            local tag = foundrecord[tagindex]

            -- get ipv4 address
            if tag.types.low == "DF4E01093265D87B" then

                -- build address
                local address = {
                    tonumber(string.sub(tag.value, 1, 2), 16),
                    tonumber(string.sub(tag.value, 3, 4), 16),
                    tonumber(string.sub(tag.value, 5, 6), 16),
                    tonumber(string.sub(tag.value, 7, 8), 16)
                }

                print("I> " .. table.concat(address, "."))
            end
        end
    end
end

-- return
return {
    echo = echo,

    fetchservice = fetchservice,

    dns = dns
}