1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-14 14:28:05 +11:00
OC-Minitel/vTunnel/OpenOS/etc/rc.d/vtunnel.lua

89 lines
2.1 KiB
Lua
Raw Normal View History

2018-04-15 22:16:15 +10:00
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
local socket
2018-04-15 22:16:15 +10:00
-- dumb keepalive stuff
local keepalive = 30
local katimer
2018-04-17 01:09:16 +10:00
function start(faddr)
2018-04-15 22:16:15 +10:00
if listener then return end
2018-04-17 01:16:03 +10:00
if faddr then
local host,nport = faddr:match("(.+):(%d+)")
end
2018-04-17 01:09:16 +10:00
local iaddr,port = host or faddr or "shadowkat.net", tonumber(nport) or 4096
socket = internet.connect(iaddr,port)
2018-04-15 22:16:15 +10:00
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 katimer then
event.cancel(katimer)
katimer = nil
end
2018-04-15 22:16:15 +10:00
if component.type(addr) then
vcomp.unregister(addr)
2018-04-15 22:16:15 +10:00
end
socket.close()
2018-04-15 22:16:15 +10:00
end