2018-01-07 02:58:06 +08:00
|
|
|
local component = require("component")
|
|
|
|
local event = require("event")
|
2018-01-07 22:29:46 +08:00
|
|
|
local serialization = require("serialization")
|
2018-01-07 02:58:06 +08:00
|
|
|
require("util")
|
|
|
|
require("libevent")
|
2018-01-07 22:29:46 +08:00
|
|
|
require("checkarg")
|
|
|
|
|
|
|
|
local function compress(t)
|
|
|
|
checktable(t)
|
|
|
|
return serialization.serialize(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function decompress(x)
|
|
|
|
checkstring(x)
|
|
|
|
return serialization.unserialize(x)
|
|
|
|
end
|
2018-01-07 02:58:06 +08:00
|
|
|
|
|
|
|
local netbox_router_port = 9999
|
|
|
|
local netbox_client_port = 9998
|
|
|
|
|
2018-01-07 22:29:46 +08:00
|
|
|
local internal_port={}
|
|
|
|
|
2018-01-07 02:58:06 +08:00
|
|
|
--- Auto Configure
|
|
|
|
local modem=proxy("modem")
|
|
|
|
local tunnel=proxy("tunnel")
|
|
|
|
local is_router=false
|
|
|
|
|
2018-01-07 22:29:46 +08:00
|
|
|
-- Hardware Check
|
2018-01-07 02:58:06 +08:00
|
|
|
|
2018-01-07 22:29:46 +08:00
|
|
|
local function doHardwareCheck()
|
|
|
|
if(modem==nil) then
|
|
|
|
error("Libnetbox[Warning]: modem not avaliable.")
|
|
|
|
end
|
|
|
|
|
|
|
|
if(tunnel==nil) then
|
|
|
|
is_router=false
|
|
|
|
else
|
|
|
|
is_router=true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- APIs
|
2018-01-07 13:44:27 +08:00
|
|
|
|
2018-01-07 02:58:06 +08:00
|
|
|
function SendData(target_address,port,...)
|
|
|
|
if(not is_router) then
|
2018-01-07 22:29:46 +08:00
|
|
|
local dt={}
|
|
|
|
dt["target"]=target_address
|
|
|
|
dt["method"]="Direct"
|
|
|
|
dt["data"]=compress(table.pack(...))
|
|
|
|
|
|
|
|
modem.broadcast(netbox_router_port,"NetBox",port,compress(dt))
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function BroadcastData(port,...)
|
|
|
|
if(not is_router) then
|
2018-01-07 22:29:46 +08:00
|
|
|
local dt={}
|
|
|
|
dt["method"]="Broadcast"
|
|
|
|
dt["data"]=compress(table.pack(...))
|
|
|
|
|
|
|
|
modem.broadcast(netbox_router_port,"NetBox",port,compress(dt))
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-07 13:44:27 +08:00
|
|
|
function OpenPort(port)
|
|
|
|
checknumber(port)
|
|
|
|
if(internal_port[port]~=nil) then
|
|
|
|
return false,"Port has been opened."
|
|
|
|
else
|
|
|
|
internal_port[port]=true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ClosePort(port)
|
|
|
|
checknumber(port)
|
|
|
|
if(internal_port[port]~=nil) then
|
|
|
|
internal_port[port]=nil
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false,"Port has been closed."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-07 02:58:06 +08:00
|
|
|
function routerMain()
|
2018-01-07 16:20:47 +08:00
|
|
|
if(modem.open(netbox_router_port)==false) then
|
|
|
|
error("Failed to open router port on real netcard.")
|
|
|
|
end
|
|
|
|
|
|
|
|
print("Router Started.")
|
2018-01-07 02:58:06 +08:00
|
|
|
|
|
|
|
local bus=CreateEventBus()
|
2018-01-07 16:20:47 +08:00
|
|
|
bus:listen("modem_message")
|
|
|
|
bus:listen("interrupted")
|
2018-01-07 02:58:06 +08:00
|
|
|
while true do
|
2018-01-07 16:20:47 +08:00
|
|
|
local e=bus:next(-1)
|
|
|
|
|
|
|
|
if(e.event=="interrupted") then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2018-01-07 02:58:06 +08:00
|
|
|
if(e.receiverAddress==tunnel.address) then
|
|
|
|
if(e.data[1]=="NetBoxAir") then
|
2018-01-07 22:29:46 +08:00
|
|
|
local port=e.data[2]
|
|
|
|
local tt=decompress(e.data[3])
|
|
|
|
|
|
|
|
local dt={}
|
|
|
|
dt.senderAddress=tt.senderAddress
|
|
|
|
dt.data=tt.data
|
|
|
|
|
|
|
|
if(tt.method=="Direct") then
|
|
|
|
modem.send(tt.target,netbox_client_port,"NetBox",port,compress(dt))
|
|
|
|
elseif(tt.method=="Broadcast") then
|
|
|
|
modem.broadcast(netbox_client_port,"NetBox",port,compress(dt))
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif(e.receiverAddress==modem.address) then
|
2018-01-07 22:29:46 +08:00
|
|
|
if(e.port==netbox_router_port and e.data[1]=="NetBox") then
|
|
|
|
local port=e.data[2]
|
|
|
|
local dt=decompress(e.data[3])
|
|
|
|
|
|
|
|
local tt={}
|
|
|
|
tt.data=dt.data
|
|
|
|
tt.method=dt.method
|
|
|
|
tt.senderAddress=e.senderAddress
|
|
|
|
|
|
|
|
if(dt.method=="Direct") then
|
|
|
|
tt.target=dt.target
|
|
|
|
tunnel.send("NetBoxAir",port,compress(tt))
|
|
|
|
elseif(dt.method=="Broadcast") then
|
|
|
|
tunnel.send("NetBoxAir",port,compress(tt))
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-07 16:20:47 +08:00
|
|
|
|
|
|
|
bus:reset()
|
|
|
|
|
|
|
|
modem.close(netbox_router_port)
|
|
|
|
|
|
|
|
print("Router Stopped.")
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
|
|
|
|
2018-01-07 22:29:46 +08:00
|
|
|
local _clientService_hand=-1
|
|
|
|
|
|
|
|
function NetBoxInit(redirect)
|
2018-01-07 13:44:27 +08:00
|
|
|
if(modem.open(netbox_client_port)==false) then
|
2018-01-07 22:29:46 +08:00
|
|
|
if(modem.isOpen(netbox_client_port)==false) then
|
|
|
|
error("Failed to start client service. Real modem port can not be opened.")
|
|
|
|
else
|
|
|
|
print("libnetbox: Modem port has been opened. This may cause something wrong...")
|
|
|
|
end
|
2018-01-07 13:44:27 +08:00
|
|
|
end
|
|
|
|
|
2018-01-07 22:29:46 +08:00
|
|
|
if(_clientService_hand<0) then
|
|
|
|
_clientService_hand=AddEventListener("modem_message",function(ev)
|
|
|
|
if(ev.data[1]=="NetBox" and internal_port[ev.data[2]]~=nil) then
|
|
|
|
local dt=decompress(ev.data[3])
|
|
|
|
event.push("net_message",ev.receiverAddress,dt.senderAddress,ev.data[2],table.unpack(decompress(dt.data)))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
print("libnetbox: Service has been started before. This may cause something wrong...")
|
|
|
|
end
|
2018-01-07 02:58:06 +08:00
|
|
|
end
|
2018-01-07 22:29:46 +08:00
|
|
|
|
|
|
|
function NetBoxCleanUp()
|
|
|
|
if(_clientService_hand>=0) then
|
|
|
|
RemoveEventListener(_clientService_hand)
|
|
|
|
_clientService_hand=-1
|
|
|
|
end
|
|
|
|
modem.close(netbox_client_port)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Do hardware check
|
|
|
|
doHardwareCheck()
|