View difference between Paste ID: kS3wgFVX and U9ZSdG77
SHOW: | | - or go back to the newest paste.
1-
-- DNS Server by Codian
1+
-- DNS Server by Codian
2-
-- Add DNS'es to the table and their respective ID's
2+
-- Add DNS'es to the table and their respective ID's
3-
-- The server will listen on the specified port with "dnsport"
3+
-- The server will listen on the specified port with "dnsport"
4-
4+
5-
5+
6-
local component = require('component')
6+
local component = require('component')
7-
local event = require('event')
7+
local event = require('event')
8-
local m = component.modem
8+
local m = component.modem
9-
local gpu = component.gpu
9+
local gpu = component.gpu
10-
local term = require('term')
10+
local term = require('term')
11-
11+
12-
-- Listening and replying port
12+
-- Listening and replying port
13-
local dnsport = 42069
13+
local dnsport = 42069
14-
14+
15-
-- DNS Table
15+
-- DNS Table
16-
-- ['dns name'] = 'network card address'
16+
-- ['dns name'] = 'network card address'
17-
dnsdb = {
17+
dnsdb = {
18-
  ['example'] = '00000000-0000-0000-0000-000000000000'
18+
  ['example'] = '00000000-0000-0000-0000-000000000000'
19-
}
19+
}
20-
20+
21-
local localAddress = ''
21+
local localAddress = ''
22-
for address, _ in component.list("modem", false) do
22+
for address, _ in component.list("modem", false) do
23-
  localAddress = address
23+
  localAddress = address
24-
  break
24+
  break
25
end
26-
26+
27-
m.open(dnsport)
27+
m.open(dnsport)
28-
gpu.setBackground(0x000000)
28+
gpu.setBackground(0x000000)
29-
gpu.setForeground(0x00ff00)
29+
gpu.setForeground(0x00ff00)
30-
term.clear()
30+
term.clear()
31-
31+
32-
print('Started DNS Server.')
32+
print('Started DNS Server.')
33-
gpu.setForeground(0xff00ff)
33+
gpu.setForeground(0xff00ff)
34-
print('Listening and replying to requests on port '..dnsport)
34+
print('Listening and replying to requests on port '..dnsport)
35-
term.write('Address: ')
35+
term.write('Address: ')
36-
gpu.setForeground(0xffffff)
36+
gpu.setForeground(0xffffff)
37-
term.write(localAddress)
37+
term.write(localAddress)
38-
38+
39-
while true do
39+
while true do
40-
  local _, _, from, port, _, command, param = event.pull('modem_message')
40+
  local _, _, from, port, _, command, param = event.pull('modem_message')
41-
  local command = string.lower(tostring(command))
41+
  local command = string.lower(tostring(command))
42-
  local param = string.gsub(tostring(param), '\n', '')
42+
  local param = string.gsub(tostring(param), '\n', '')
43-
  gpu.setForeground(0xffff00)
43+
  gpu.setForeground(0xffff00)
44-
  print('Request from '..from)
44+
  print('Request from '..from)
45-
  if command == 'lookup' then
45+
  if command == 'lookup' then
46-
    addr = tostring(dnsdb[param])
46+
    addr = tostring(dnsdb[param])
47-
    gpu.setForeground(0xffffff)
47+
    gpu.setForeground(0xffffff)
48-
    print('DNS Lookup: '.. param .. ' -> ' .. addr)
48+
    print('DNS Lookup: '.. param .. ' -> ' .. addr)
49-
    m.send(from, port, addr)
49+
    m.send(from, port, addr)
50-
  end
50+
  end
51
end