1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-17 06:04:50 +00:00
opus/sys/network/trust.lua

34 lines
948 B
Lua
Raw Normal View History

2017-05-05 11:34:20 +00:00
local Socket = require('socket')
local process = require('process')
2017-05-06 01:43:17 +00:00
local Crypto = require('crypto')
2017-05-05 11:34:20 +00:00
process:newThread('trust_server', function()
print('trust: listening on port 19')
while true do
local socket = Socket.server(19)
print('trust: connection from ' .. socket.dhost)
local data = socket:read(2)
if data then
2017-05-06 01:43:17 +00:00
local password = os.getPassword()
if not password then
2017-05-19 23:00:23 +00:00
socket:write({ msg = 'No password has been set' })
2017-05-05 11:34:20 +00:00
else
2017-05-06 01:43:17 +00:00
data = Crypto.decrypt(data, password)
2017-05-19 23:00:23 +00:00
if data and data.pk and data.dh == socket.dhost then
2017-05-20 22:27:26 +00:00
local trustList = Util.readTable('usr/.known_hosts') or { }
2017-05-09 05:57:00 +00:00
trustList[data.dh] = data.pk
2017-05-20 22:27:26 +00:00
Util.writeTable('usr/.known_hosts', trustList)
2017-05-06 01:43:17 +00:00
2017-05-19 23:00:23 +00:00
socket:write({ success = true, msg = 'Trust accepted' })
2017-05-06 01:43:17 +00:00
else
2017-05-19 23:00:23 +00:00
socket:write({ msg = 'Invalid password' })
2017-05-06 01:43:17 +00:00
end
2017-05-05 11:34:20 +00:00
end
end
socket:close()
end
end)