Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- wifi server mk2
- by BigSHinyToys
- ]]--
- local oldError = error
- error = function(...)
- local data = {...}
- local file = fs.open("log","a")
- if file then
- for i = 1,#data do
- file.write(tostring(data[i]).." ")
- end
- file.write("\n")
- file.close()
- end
- oldError(unpack(data))
- end
- local function hold() -- just used for error testing
- print("HOLD")
- local test
- while test ~= 28 do
- local _,bla = os.pullEvent("key")
- test = bla
- end
- end
- local function findDevice(sType)
- for _,sSide in pairs(rs.getSides()) do
- if peripheral.isPresent(sSide) and peripheral.getType(sSide) == sType then
- return sSide,peripheral.wrap(sSide)
- end
- end
- end
- local function printTable(tInput) -- just used for error testing
- if type(tInput) == "table" then
- for i = 1,#tInput do
- write(tostring(tInput[i]).." ")
- end
- print("")
- else
- print(tostring(tInput))
- end
- end
- local modemSide,modem = findDevice("modem")
- local tDraw = {}
- local oldEvent = os.pullEventRaw
- local speed = 0.1
- local function wifiRedirect(sSide,freq)
- local modem = peripheral.wrap(sSide)
- local freq = freq
- local tFunctions = {
- "scroll","setCursorBlink", -- "setCursorPos" "write"
- "clear","clearLine","setTextColour","setTextColor",
- "setBackgroundColour","setBackgroundColor",
- }
- -- "getCursorPos","getSize","isColour","isColor",
- local posX,posY = 1,1
- local tRemote = {
- getCursorPos = function()
- return posX,posY
- end,
- setCursorPos = function(x,y)
- posX = x
- posY = y
- --modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
- if not sendTimer then
- sendTimer = os.startTimer(speed)
- end
- table.insert(tDraw,textutils.serialize({"setCursorPos",x,y}))
- end,
- write = function(line,...)
- if line and type(line) == "string" then
- posX = posX + #line
- end
- --modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
- if not sendTimer then
- sendTimer = os.startTimer(speed)
- end
- table.insert(tDraw,textutils.serialize({"write",line,...}))
- end,
- }
- modem.transmit(freq,freq,"ISC")
- local nTimer = os.startTimer(1)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == nTimer then
- error(" ERROR Remote time out")
- elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput.cmd and tInput.cmd == "REPLY-SCP" then
- if tInput.test and tInput.test == true then
- tRemote.isColour = function()
- return true
- end
- tRemote.isColor = function()
- return true
- end
- else
- tRemote.isColour = function()
- return false
- end
- tRemote.isColor = function()
- return false
- end
- end
- if tInput.h and type(tInput.h) == "number" and tInput.w and type(tInput.w) == "number" then
- local w,h = tInput.w,tInput.h
- tRemote.getSize = function()
- return w,h
- end
- else
- error("Somthing went wrong / wrong ver")
- end
- break
- end
- end
- end
- end
- for i = 1,#tFunctions do
- local funk = tFunctions[i]
- tRemote[funk] = function(...)
- if not sendTimer then
- sendTimer = os.startTimer(speed)
- end
- table.insert(tDraw,textutils.serialize({tFunctions[i],...}))
- --modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
- end
- end
- os.pullEventRaw = function(...)
- while true do
- local event = {coroutine.yield(...)}
- if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput[1] == "INS" then
- table.remove(tInput,1)
- return unpack(tInput)
- end
- end
- elseif event[1] == "timer" and event[2] == sendTimer then
- sendTimer = false
- modem.transmit(freq,freq,textutils.serialize(tDraw))
- tDraw = {}
- else
- return unpack(event)
- end
- end
- end
- term.redirect(tRemote)
- end
- --[[
- "getCursorPos","getSize","isColour","isColor"
- ]]--
- local freq
- local user
- local pass
- local function main()
- os.startTimer(0.5)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput.user and tInput.user == user and tInput.password and tInput.password == pass then
- print("Authenticated user!")
- wifiRedirect(modemSide,freq)
- return
- end
- end
- elseif event[1] == "timer" then
- end
- --printTable(event)
- end
- end
- local tArg = {...}
- if modemSide then
- print(" --- Remote access server --- ")
- while true do
- write("Choose Freq : ")
- local temp = tArg[1] or read()
- if string.lower(temp) == "exit" then
- print(" --- Server stoping --- ")
- return
- end
- temp = tonumber(temp)
- if type(temp) == "number" and temp >= 0 and temp <= 65535 then
- freq = temp
- break
- end
- print("ERROR : Range 0 - 65535 or Exit")
- end
- write("User : ")
- user = tArg[2] or read()
- write("Password : ")
- pass = tArg[3] or read("*")
- print("Opening modem : "..modemSide.." Freq : "..tostring(freq))
- modem.open(freq)
- modem.transmit(65535,freq,"Telnet server")
- main()
- else
- print("ERROR : Failed to find modem ")
- end
Advertisement
Add Comment
Please, Sign In to add comment