Posted 25 March 2012 - 09:01 AM
INTERNET CLIENT CODE:
http://pastebin.com/sC2mWpsy
How to Use the Browser:
1. Make a file called "clientConnect."
2. In each line, add the ID of a server you want to connect to (have to be able to connect to all of them!).
3. Go onto websites others created.
4. Type upload:<fileName> or download:<fileName> to upload or download a file from or to a server.
Note – client will lock up if you go onto an invalid or nonexistent website or attempt downloading a file that does not exist.
INTERNET SERVER CODE:
http://pastebin.com/1s16bqvx
How to Create the Server and Websites:
1. Make a file called "serverWebsites."
2. In each line of this file, add a name of a website you will host.
3. Go into the main terminal and type edit <name of your website>
4. The first line is the code a client will run when people get on to your website.
^ You got that right, you can make them run your own code.
5. The next 14 lines will be the text of your website that people will see when they go on.
5a. These 14 lines cannot be left blank, if you must leave them blank, leave a space in each line.
http://pastebin.com/sC2mWpsy
Spoiler
serverBroadcastIDs = {}
function getNumTable(tbl)
sReturn = 0
for i = 1,math.huge do
if tbl[i] == nil then
return sReturn
else
sReturn = sReturn+1
end
end
end
PAGE1 = {}
rednet.open("back")
rednet.open("front")
rednet.open("left")
rednet.open("right")
rednet.open("top")
rednet.open("bottom")
function Start()
while true do
print("+-----------------------------------------------+")
print("| |")
print("+-----------------------------------------------+")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("| |")
print("+-----------------------------------------------+")
term.setCursorPos(3,2)
websiteInput = read()
if string.sub(websiteInput,1,9) ~= "download:" then
if string.sub(websiteInput,1,7) ~= "upload:" then
for i = 1, numberOfServerBroadcastIDs do
sentCheck = rednet.send(serverBroadcastIDs[i], "getHTTP"..websiteInput)
inputStuff()
break
end
else
if fs.exists(string.sub(websiteInput,8)) == true then
for i = 1, numberOfServerBroadcastIDs do
local sFile = fs.open(string.sub(websiteInput,8), "r")
rednet.send(serverBroadcastIDs[i], "UPL"..checkLines(string.sub(websiteInput,8)))
rednet.send(serverBroadcastIDs[i], string.sub(websiteInput,8))
broadcastID = serverBroadcastIDs[i]
for i = 1, checkLines(string.sub(websiteInput,8)) do
rednet.send(broadcastID, tostring(sFile:readLine()))
end
end
sFile:close()
else
term.clear()
term.setCursorPos(1,1)
error("File does not exist.")
end
end
else
for i = 1, numberOfServerBroadcastIDs do
rednet.send(serverBroadcastIDs[i], "DWN"..string.sub(websiteInput,10))
id, numOfLines = rednet.receive()
numOfLines = tonumber(numOfLines)
local sFilez = fs.open(string.sub(websiteInput,10), "w")
for i = 1,numOfLines do
id, received = rednet.receive()
lol = sFilez.write(received.."n")
end
sFilez:close()
end
end
end
end
function inputStuff()
for i = 1, 15 do
if i == 1 then
CODE = io.open("WEBSITECODE","w")
id, msg = rednet.receive()
CODE:write(msg)
CODE:close()
else
id, PAGE1[i] = rednet.receive()
end
end
for i = 1, 14 do
if PAGE1[i] ~= nil then
term.setCursorPos(2, 2+i)
io.write(PAGE1[i])
end
end
while true do
shell.run("WEBSITECODE")
event, key = os.pullEvent()
if event == "key" then
if key ~= 1 then
term.clear()
term.setCursorPos(1,1)
Start()
end
end
end
end
function checkLines(file)
if fs.exists(file) == true then
sFile = fs.open(file, "r")
sCount = 0
for i = 1, math.huge do
line = sFile:readLine()
if line ~= nil then
sCount = sCount+1
else
return sCount
end
end
else
return false
end
end
if fs.exists("clientConnect") == true then
local filez = fs.open("clientConnect","r")
for i = 1,checkLines("clientConnect") do
table.insert(serverBroadcastIDs, tonumber(filez.readLine()))
end
filez.close()
else
error("Make clientConnect file!")
end
numberOfServerBroadcastIDs = getNumTable(serverBroadcastIDs)
Start()
How to Use the Browser:
1. Make a file called "clientConnect."
2. In each line, add the ID of a server you want to connect to (have to be able to connect to all of them!).
3. Go onto websites others created.
4. Type upload:<fileName> or download:<fileName> to upload or download a file from or to a server.
Note – client will lock up if you go onto an invalid or nonexistent website or attempt downloading a file that does not exist.
INTERNET SERVER CODE:
http://pastebin.com/1s16bqvx
Spoiler
websiteURLs = {}
function getNumTable(tbl)
sReturn = 0
for i = 1,math.huge do
if tbl[i] == nil then
return sReturn
else
sReturn = sReturn+1
end
end
end
function checkLines(file)
if fs.exists(file) == true then
sFile = fs.open(file, "r")
sCount = 0
for i = 1, math.huge do
line = sFile:readLine()
if line ~= nil then
sCount = sCount+1
else
return sCount
end
end
else
return false
end
end
if fs.exists("UPL")~=true then
fs.makeDir("UPL")
end
if fs.exists("serverWebsites") == true then
local filez = fs.open("serverWebsites","r")
for i = 1,checkLines("serverWebsites") do
table.insert(websiteURLs, filez.readLine())
end
filez.close()
else
error("You need to make serverWebsites file!")
end
numberOfWebsites = getNumTable(websiteURLs)
rednet.open("back")
rednet.open("front")
rednet.open("left")
rednet.open("right")
rednet.open("top")
rednet.open("bottom")
while true do
local id, msg = rednet.receive()
print(id)
for i = 1, numberOfWebsites do
if msg == "getHTTP"..websiteURLs[i] then
website = fs.open(websiteURLs[i], "r")
for i = 1, 15 do
rednet.send(id, website:readLine())
end
website:close()
end
if string.sub(msg,1,3) == "UPL" then
if fs.exists("UPL/"..msg) ~= true then
numOfLines = string.sub(msg,4)
local id, msg = rednet.receive()
sFile = fs.open("UPL/"..msg, "w")
for i = 1, numOfLines do
id, msg = rednet.receive()
sFile.write(msg.."n")
end
sFile:close()
end
end
if string.sub(msg,1,3) == "DWN" then
if fs.exists("UPL/"..string.sub(msg,4),"r") == true then
local sFilez = fs.open("UPL/"..string.sub(msg,4),"r")
rednet.send(id, tostring(checkLines("UPL/"..string.sub(msg,4))))
for i = 1, checkLines("UPL/"..string.sub(msg,4)) do
rednet.send(id,sFilez:readLine())
end
sFilez:close()
end
end
end
end
How to Create the Server and Websites:
1. Make a file called "serverWebsites."
2. In each line of this file, add a name of a website you will host.
3. Go into the main terminal and type edit <name of your website>
4. The first line is the code a client will run when people get on to your website.
^ You got that right, you can make them run your own code.
5. The next 14 lines will be the text of your website that people will see when they go on.
5a. These 14 lines cannot be left blank, if you must leave them blank, leave a space in each line.