From 90118de6ab249e3c323eeb181b0f67660457bb49 Mon Sep 17 00:00:00 2001 From: Izaya Date: Sun, 15 Apr 2018 22:16:15 +1000 Subject: [PATCH] Turned vtunnel into a daemon --- programs.cfg | 2 +- vTunnel/OpenOS/etc/rc.d/vtunnel.lua | 79 +++++++++++++++++++++++++++++ vTunnel/vtunnel.lua | 47 ----------------- 3 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 vTunnel/OpenOS/etc/rc.d/vtunnel.lua delete mode 100644 vTunnel/vtunnel.lua diff --git a/programs.cfg b/programs.cfg index ff9fd7f..508ce89 100644 --- a/programs.cfg +++ b/programs.cfg @@ -49,7 +49,7 @@ ["vtunnel"] = { files = { ["master/vTunnel/interminitel.lua"] = "/lib", - ["master/vTunnel/vtunnel.lua"] = "/bin", + ["master/vTunnel/vtunnel.lua"] = "//etc/rc.d", }, dependencies = { ["vcomponent"] = "" diff --git a/vTunnel/OpenOS/etc/rc.d/vtunnel.lua b/vTunnel/OpenOS/etc/rc.d/vtunnel.lua new file mode 100644 index 0000000..848219e --- /dev/null +++ b/vTunnel/OpenOS/etc/rc.d/vtunnel.lua @@ -0,0 +1,79 @@ +local vcomp = require "vcomponent" +local component = require "component" +local computer = require "computer" +local imt = require "interminitel" +local event = require "event" +local internet = component.internet + +local addr, raddr = vcomp.uuid(),vcomp.uuid() +local poll = 0.5 +local listener, timer + +-- dumb keepalive stuff +local keepalive = 30 +local katimer + +function start(iaddr,port) + if listener then return end + iaddr,port = iaddr or "shadowkat.net", tonumber(port) or 4096 + local socket = internet.connect(iaddr,port) + print("Connecting to "..iaddr..":"..tostring(port).."...") + repeat + os.sleep(0.5) + until socket.finishConnect() + print("Connected!") + + local proxy = {} + local rbuffer = "" + local timer = nil + + function listener(t) + rbuffer=rbuffer..(socket.read(4096) or "") + if imt.decodePacket(rbuffer) then + computer.pushSignal("modem_message",addr,raddr,0,0,imt.decodePacket(rbuffer)) + rbuffer = imt.getRemainder(rbuffer) or "" + end + if t == "internet_ready" and timer then + event.cancel(timer) + timer = nil + end + end + timer = event.timer(poll,listener,math.huge) -- this is only here because OCEmu doesn't do internet_ready + event.listen("internet_ready",listener) + + -- also dumb keepalive stuff, fix this later + katimer = event.timer(keepalive,function() + socket.write("\0\1\0") + end,math.huge) + + function proxy.send(...) + socket.write(imt.encodePacket(...)) + end + + function proxy.maxPacketSize() + return 12288 + end + + proxy.type = "tunnel" + proxy.slot = 4096 + + local docs = { + send = "function(data...) -- Sends the specified data to the card this one is linked to.", + maxPacketSize = "function():number -- Gets the maximum packet size (config setting)." + } + vcomp.register(addr,"tunnel",proxy,docs) +end + +function stop() + if listener then + event.ignore("internet_ready",listener) + listener = nil + end + if timer then + event.cancel(timer) + timer = nil + end + if component.type(addr) then + vcomp.unregister(addr) + end +end diff --git a/vTunnel/vtunnel.lua b/vTunnel/vtunnel.lua deleted file mode 100644 index e9b5a38..0000000 --- a/vTunnel/vtunnel.lua +++ /dev/null @@ -1,47 +0,0 @@ -local vcomp = require "vcomponent" -local component = require "component" -local computer = require "computer" -local imt = require "interminitel" -local event = require "event" -local internet = component.internet -local tArgs = {...} - -local addr, raddr = vcomp.uuid(),vcomp.uuid() -local poll, keepalive = tonumber(tArgs[3]) or 5, tonumber(tArgs[4]) or 30 - -local socket = internet.connect(tArgs[1],tonumber(tArgs[2])) -repeat - os.sleep(1) -until socket.finishConnect() - -local proxy = {} -local rbuffer = "" - -local timer = event.timer(poll,function() - rbuffer=rbuffer..(socket.read(4096) or "") - if imt.decodePacket(rbuffer) then - computer.pushSignal("modem_message",addr,raddr,0,0,imt.decodePacket(rbuffer)) - rbuffer = imt.getRemainder(rbuffer) or "" - end -end,math.huge) - -local katimer = event.timer(keepalive,function() - socket.write("\0\1\0") -end,math.huge) - -function proxy.send(...) - socket.write(imt.encodePacket(...)) -end - -function proxy.maxPacketSize() - return 12288 -end - -proxy.type = "tunnel" -proxy.slot = 4096 - -local docs = { - send = "function(data...) -- Sends the specified data to the card this one is linked to.", - maxPacketSize = "function():number -- Gets the maximum packet size (config setting)." -} -vcomp.register(addr,"tunnel",proxy,docs)