This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Ponder's profile picture

Firewall. Sort of.

Started by Ponder, 01 March 2012 - 06:42 PM
Ponder #1
Posted 01 March 2012 - 07:42 PM
So while i was toying around with my new computers in Minecraft, I was kinda annoyed that I had to tell the computer which sides he may use for networking each time I'd reboot. So, I spent the last two evenings writing this little script which handles that for me.
I don't know whether somebody else already wrote that kind of thing, I just hope not.

I wrote this for RedWorks, but as long as CraftOs also has an autorun folder thingy it should smoothly.
hjhj
REQ: - Str Utils Api

CODE:
Spoiler

local time = 5 -- this is the time the program will wait and display you the results before
local dir = "/etc/" -- this is where config files will be stored
local ports = "ports" -- and this the name of the actual config file
local content = "front, falsenback, falsenleft, falsenright, falsentop, falsenbottom, false" -- I guess the pattern should be clear
function main ()
file_ports = io.open (dir .. ports, 'r')
line = file_ports:read ('*l')

while line do

 port = {}
 for k, str in pairs (StrUtils.seperate (line, ', ')) do
  table.insert (port, str)
 end

 if port [2] == 'true' then
  print ('Open port: ' .. port [1])
  rednet.open (port [1])
 elseif port [2] == 'false' then
  print ('Close port: ' .. port [1])
  rednet.close (port [1])
 else
  print ('Something's wrong with your fire wall setting.')
 end
 line = file_ports:read ('*l')
end

file_ports:close ()
os.sleep (time)
end
function setup (mode) -- mode describes whether /etc exists or not
function make_config(file)
 file_ports = io.open (file, 'w')
 file_ports:write (content)
 file_ports:close ()
end

if mode then
 make_config (dir .. ports)
elseif not mode then
 fs.makeDir (dir)
 make_config (dir .. ports)
end
end
if fs.exists (dir .. ports) then
 main ()
elseif fs.isDir (dir) then
 setup (true)
 main ()
else
 setup (false)
 main ()
end

Place this in your autorun directory (/rom/autorun in RedWorks) and it should work.
On boot it will check whether the config file exists and if not create them from the defaults in the car $content, after that it'll look into the config file and open the appropriate sides for networking.
Any comment on this is appreciated as this is actually my first program ever written in Lua.
Ponder #2
Posted 12 March 2012 - 06:52 PM
Update: should be working correctly now
Again, any comment on this would be appreciated.