2019-06-28 17:50:02 +00:00
|
|
|
local Crypto = require('opus.crypto.chacha20')
|
|
|
|
local Event = require('opus.event')
|
|
|
|
local Security = require('opus.security')
|
|
|
|
local Socket = require('opus.socket')
|
|
|
|
local Util = require('opus.util')
|
2017-05-05 11:34:20 +00:00
|
|
|
|
2019-04-20 17:48:13 +00:00
|
|
|
local function trustConnection(socket)
|
|
|
|
local data = socket:read(2)
|
|
|
|
if data then
|
|
|
|
local password = Security.getPassword()
|
|
|
|
if not password then
|
|
|
|
socket:write({ msg = 'No password has been set' })
|
|
|
|
else
|
|
|
|
data = Crypto.decrypt(data, password)
|
|
|
|
if data and data.pk and data.dh == socket.dhost then
|
|
|
|
local trustList = Util.readTable('usr/.known_hosts') or { }
|
2019-06-28 01:08:46 +00:00
|
|
|
trustList[data.dh] = Util.byteArrayToHex(data.pk)
|
2019-04-20 17:48:13 +00:00
|
|
|
Util.writeTable('usr/.known_hosts', trustList)
|
|
|
|
|
|
|
|
socket:write({ success = true, msg = 'Trust accepted' })
|
|
|
|
else
|
|
|
|
socket:write({ msg = 'Invalid password' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.addRoutine(function()
|
2018-01-24 22:39:38 +00:00
|
|
|
print('trust: listening on port 19')
|
2019-06-28 01:08:46 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
while true do
|
|
|
|
local socket = Socket.server(19)
|
2017-05-05 11:34:20 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
print('trust: connection from ' .. socket.dhost)
|
2017-05-05 11:34:20 +00:00
|
|
|
|
2019-04-20 17:48:13 +00:00
|
|
|
local s, m = pcall(trustConnection, socket)
|
2018-01-24 22:39:38 +00:00
|
|
|
socket:close()
|
2019-04-20 17:48:13 +00:00
|
|
|
if not s and m then
|
|
|
|
print('Trust error')
|
|
|
|
_G.printError(m)
|
|
|
|
end
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2017-05-05 11:34:20 +00:00
|
|
|
end)
|