1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-29 01:50:27 +00:00

network group wip + virtual dirs + better trust

This commit is contained in:
kepler155c@gmail.com 2019-04-08 09:30:47 -04:00
parent 9413785248
commit 70733ab4f2
8 changed files with 92 additions and 31 deletions

View File

@ -1,20 +1,18 @@
local Config = require('config')
local config = { }
local Security = { }
function Security.verifyPassword(password)
Config.load('os', config)
return config.password and password == config.password
local current = Security.getPassword()
return current and password == current
end
function Security.hasPassword()
return not not config.password
return not not Security.getPassword()
end
function Security.getSecretKey()
Config.load('os', config)
local config = Config.load('os')
if not config.secretKey then
config.secretKey = math.random(100000, 999999)
Config.update('os', config)
@ -23,7 +21,6 @@ function Security.getSecretKey()
end
function Security.getPublicKey()
local exchange = {
base = 11,
primeMod = 625210769
@ -47,14 +44,13 @@ function Security.getPublicKey()
end
function Security.updatePassword(password)
Config.load('os', config)
local config = Config.load('os')
config.password = password
Config.update('os', config)
end
function Security.getPassword()
Config.load('os', config)
return config.password
return Config.load('os').password
end
return Security

View File

@ -105,7 +105,7 @@ end
function Socket.connect(host, port)
if not device.wireless_modem then
return false, 'Wireless modem not found'
return false, 'Wireless modem not found', 'NOMODEM'
end
local socket = newSocket(host == os.getComputerID())
@ -138,15 +138,19 @@ function Socket.connect(host, port)
_G.transport.open(socket)
return socket
elseif msg.type == 'NOPASS' then
socket:close()
return false, 'Password not set on target', 'NOPASS'
elseif msg.type == 'REJE' then
socket:close()
return false, 'Password not set on target or not trusted'
return false, 'Trust not established', 'NOTRUST'
end
end
until e == 'timer' and id == timerId
socket:close()
return false, 'Connection timed out'
return false, 'Connection timed out', 'TIMEOUT'
end
local function trusted(msg, port)
@ -190,7 +194,15 @@ function Socket.server(port)
socket.wseq = msg.wseq
socket.rseq = msg.rseq
if trusted(msg, port) then
if not Security.hasPassword() then
socket.transmit(socket.dport, socket.sport, {
type = 'NOPASS',
dhost = socket.dhost,
shost = socket.shost,
})
socket:close()
elseif trusted(msg, port) then
socket.connected = true
socket.transmit(socket.dport, socket.sport, {
type = 'CONN',
@ -201,14 +213,15 @@ function Socket.server(port)
_G.transport.open(socket)
return socket
end
socket.transmit(socket.dport, socket.sport, {
type = 'REJE',
dhost = socket.dhost,
shost = socket.shost,
})
socket:close()
else
socket.transmit(socket.dport, socket.sport, {
type = 'REJE',
dhost = socket.dhost,
shost = socket.shost,
})
socket:close()
end
end
end
end

View File

@ -1,4 +1,5 @@
local Ansi = require('ansi')
local Config = require('config')
local Security = require('security')
local SHA1 = require('sha1')
local UI = require('ui')
@ -53,7 +54,7 @@ local page = UI.Page {
},
password = UI.WizardPage {
index = 3,
labelText = UI.Text {
passwordLabel = UI.Text {
x = 3, y = 2,
value = 'Password'
},
@ -62,14 +63,22 @@ local page = UI.Page {
limit = 32,
mask = true,
shadowText = 'password',
accelerators = {
enter = 'new_password',
},
},
--[[
groupLabel = UI.Text {
x = 3, y = 3,
value = 'Group'
},
group = UI.TextEntry {
x = 12, ex = -3, y = 3,
limit = 32,
shadowText = 'network group',
},
]]
intro = UI.TextArea {
textColor = colors.yellow,
inactive = true,
x = 3, ex = -3, y = 4, ey = -3,
x = 3, ex = -3, y = 5, ey = -3,
value = string.format(passwordIntro, Ansi.white),
},
},
@ -101,6 +110,11 @@ function page.wizard.pages.password:validate()
if #self.newPass.value > 0 then
Security.updatePassword(SHA1.sha1(self.newPass.value))
end
if #self.group.value > 0 then
local config = Config.load('os')
config.group = self.group.value
Config.update('os', config)
end
return true
end

View File

@ -141,6 +141,7 @@ local function sendInfo()
infoTimer = os.clock()
info.label = os.getComputerLabel()
info.uptime = math.floor(os.clock())
info.group = network.getGroup()
if turtle then
info.fuel = turtle.getFuelLevel()
info.status = turtle.getStatus()

View File

@ -24,10 +24,21 @@ if multishell then
multishell.setTitle(multishell.getCurrent(), 'Telnet ' .. remoteId)
end
local socket, msg = Socket.connect(remoteId, 23)
local socket, msg, reason
if not socket then
error(msg)
while true do
socket, msg, reason = Socket.connect(remoteId, 23)
if socket then
break
elseif reason ~= 'NOTRUST' then
error(msg)
end
local s, m = shell.run('trust ' .. remoteId)
if not s then
error(m)
end
end
local ct = Util.shallowCopy(term.current())

View File

@ -6,6 +6,7 @@ local Util = require('util')
local colors = _G.colors
local multishell = _ENV.multishell
local os = _G.os
local shell = _ENV.shell
local term = _G.term
local remoteId
@ -26,7 +27,15 @@ if multishell then
end
local function connect()
local socket, msg = Socket.connect(remoteId, 5900)
local socket, msg, reason = Socket.connect(remoteId, 5900)
if reason == 'NOTRUST' then
local s, m = shell.run('trust ' .. remoteId)
if not s then
return s, m
end
socket, msg = Socket.connect(remoteId, 5900)
end
if not socket then
return false, msg

View File

@ -5,6 +5,8 @@ end
_G.requireInjector(_ENV)
local Util = require('util')
-- TODO: support getDrive for virtual nodes
local fs = _G.fs
fs.native = Util.shallowCopy(fs)
@ -88,6 +90,13 @@ function nativefs.exists(node, dir)
return fs.native.exists(dir)
end
function nativefs.getDrive(node, dir)
if node.mountPoint == dir then
return fs.native.getDrive(dir) or 'virt'
end
return fs.native.getDrive(dir)
end
function nativefs.delete(node, dir)
if node.mountPoint == dir then
fs.unmount(dir)

View File

@ -6,7 +6,15 @@ local device = _G.device
local kernel = _G.kernel
local os = _G.os
_G.network = { }
do
local config = Config.load('os')
_G.network = setmetatable({ }, { __index = {
getGroup = function() return config.group end,
setGroup = function(name)
config.group = name
end
}})
end
local function startNetwork()
kernel.run({