gknova61

Example of URL Whitelisting (bios.lua)

Jan 7th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Save this as bios.lua and put it in the Computercraft/lua folder
  2. --Find function wrapRequest to see my changes
  3. WhitelistedURLs = {"pastebin","dropbox"} --just put keywords here i.e. pastebin, dropbox, ...
  4.  
  5. function pairs( _t )
  6.     local typeT = type( _t )
  7.     if typeT ~= "table" then
  8.         error( "bad argument #1 to pairs (table expected, got "..typeT..")", 2 )
  9.     end
  10.     return next, _t, nil
  11. end
  12.  
  13. function ipairs( _t )
  14.     local typeT = type( _t )
  15.     if typeT ~= "table" then
  16.         error( "bad argument #1 to ipairs (table expected, got "..typeT..")", 2 )
  17.     end
  18.     return function( t, var )
  19.         var = var + 1
  20.         local value = t[var]
  21.         if value == nil then
  22.             return
  23.         end
  24.         return var, value
  25.     end, _t, 0
  26. end
  27.  
  28. function coroutine.wrap( _fn )
  29.     local typeT = type( _fn )
  30.     if typeT ~= "function" then
  31.         error( "bad argument #1 to coroutine.wrap (function expected, got "..typeT..")", 2 )
  32.     end
  33.     local co = coroutine.create( _fn )
  34.     return function( ... )
  35.         local tResults = { coroutine.resume( co, ... ) }
  36.         if tResults[1] then
  37.             return unpack( tResults, 2 )
  38.         else
  39.             error( tResults[2], 2 )
  40.         end
  41.     end
  42. end
  43.  
  44. function string.gmatch( _s, _pattern )
  45.     local type1 = type( _s )
  46.     if type1 ~= "string" then
  47.         error( "bad argument #1 to string.gmatch (string expected, got "..type1..")", 2 )
  48.     end
  49.     local type2 = type( _pattern )
  50.     if type2 ~= "string" then
  51.         error( "bad argument #2 to string.gmatch (string expected, got "..type2..")", 2 )
  52.     end
  53.    
  54.     local nPos = 1
  55.     return function()
  56.         local nFirst, nLast = string.find( _s, _pattern, nPos )
  57.         if nFirst == nil then
  58.             return
  59.         end    
  60.         nPos = nLast + 1
  61.         return string.match( _s, _pattern, nFirst )
  62.     end
  63. end
  64.  
  65. local nativesetmetatable = setmetatable
  66. function setmetatable( _o, _t )
  67.     if _t and type(_t) == "table" then
  68.         local idx = rawget( _t, "__index" )
  69.         if idx and type( idx ) == "table" then
  70.             rawset( _t, "__index", function( t, k ) return idx[k] end )
  71.         end
  72.         local newidx = rawget( _t, "__newindex" )
  73.         if newidx and type( newidx ) == "table" then
  74.             rawset( _t, "__newindex", function( t, k, v ) newidx[k] = v end )
  75.         end
  76.     end
  77.     return nativesetmetatable( _o, _t )
  78. end
  79.  
  80. -- Install lua parts of the os api
  81. function os.version()
  82.     if turtle then
  83.         return "TurtleOS 1.4"
  84.     end
  85.     return "CraftOS 1.4"
  86. end
  87.  
  88. function os.pullEventRaw( _sFilter )
  89.     return coroutine.yield( _sFilter )
  90. end
  91.  
  92. function os.pullEvent( _sFilter )
  93.     local eventData = {os.pullEventRaw( _sFilter )}
  94.     if eventData[1] == "terminate" then
  95.         printError( "Terminated" )
  96.         error()
  97.     end
  98.     return unpack(eventData)
  99. end
  100.  
  101. -- Install globals
  102. function sleep( _nTime )
  103.     local timer = os.startTimer( _nTime )
  104.     repeat
  105.         local sEvent, param = os.pullEvent( "timer" )
  106.     until param == timer
  107. end
  108.  
  109. function write( sText )
  110.     local w,h = term.getSize()     
  111.     local x,y = term.getCursorPos()
  112.    
  113.     local nLinesPrinted = 0
  114.     local function newLine()
  115.         if y + 1 <= h then
  116.             term.setCursorPos(1, y + 1)
  117.         else
  118.             term.setCursorPos(1, h)
  119.             term.scroll(1)
  120.         end
  121.         x, y = term.getCursorPos()
  122.         nLinesPrinted = nLinesPrinted + 1
  123.     end
  124.    
  125.     -- Print the line with proper word wrapping
  126.     while string.len(sText) > 0 do
  127.         local whitespace = string.match( sText, "^[ \t]+" )
  128.         if whitespace then
  129.             -- Print whitespace
  130.             term.write( whitespace )
  131.             x,y = term.getCursorPos()
  132.             sText = string.sub( sText, string.len(whitespace) + 1 )
  133.         end
  134.        
  135.         local newline = string.match( sText, "^\n" )
  136.         if newline then
  137.             -- Print newlines
  138.             newLine()
  139.             sText = string.sub( sText, 2 )
  140.         end
  141.        
  142.         local text = string.match( sText, "^[^ \t\n]+" )
  143.         if text then
  144.             sText = string.sub( sText, string.len(text) + 1 )
  145.             if string.len(text) > w then
  146.                 -- Print a multiline word              
  147.                 while string.len( text ) > 0 do
  148.                     if x > w then
  149.                         newLine()
  150.                     end
  151.                     term.write( text )
  152.                     text = string.sub( text, (w-x) + 2 )
  153.                     x,y = term.getCursorPos()
  154.                 end
  155.             else
  156.                 -- Print a word normally
  157.                 if x + string.len(text) - 1 > w then
  158.                     newLine()
  159.                 end
  160.                 term.write( text )
  161.                 x,y = term.getCursorPos()
  162.             end
  163.         end
  164.     end
  165.    
  166.     return nLinesPrinted
  167. end
  168.  
  169. function print( ... )
  170.     local nLinesPrinted = 0
  171.     for n,v in ipairs( { ... } ) do
  172.         nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  173.     end
  174.     nLinesPrinted = nLinesPrinted + write( "\n" )
  175.     return nLinesPrinted
  176. end
  177.  
  178. function printError( ... )
  179.     if term.isColour() then
  180.         term.setTextColour( colours.red )
  181.     end
  182.     print( ... )
  183.     term.setTextColour( colours.white )
  184. end
  185.  
  186. function read( _sReplaceChar, _tHistory )
  187.     term.setCursorBlink( true )
  188.  
  189.     local sLine = ""
  190.     local nHistoryPos = nil
  191.     local nPos = 0
  192.     if _sReplaceChar then
  193.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  194.     end
  195.    
  196.     local w, h = term.getSize()
  197.     local sx, sy = term.getCursorPos() 
  198.    
  199.     local function redraw( _sCustomReplaceChar )
  200.         local nScroll = 0
  201.         if sx + nPos >= w then
  202.             nScroll = (sx + nPos) - w
  203.         end
  204.            
  205.         term.setCursorPos( sx, sy )
  206.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  207.         if sReplace then
  208.             term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  209.         else
  210.             term.write( string.sub( sLine, nScroll + 1 ) )
  211.         end
  212.         term.setCursorPos( sx + nPos - nScroll, sy )
  213.     end
  214.    
  215.     while true do
  216.         local sEvent, param = os.pullEvent()
  217.         if sEvent == "char" then
  218.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  219.             nPos = nPos + 1
  220.             redraw()
  221.            
  222.         elseif sEvent == "key" then
  223.             if param == keys.enter then
  224.                 -- Enter
  225.                 break
  226.                
  227.             elseif param == keys.left then
  228.                 -- Left
  229.                 if nPos > 0 then
  230.                     nPos = nPos - 1
  231.                     redraw()
  232.                 end
  233.                
  234.             elseif param == keys.right then
  235.                 -- Right               
  236.                 if nPos < string.len(sLine) then
  237.                     nPos = nPos + 1
  238.                     redraw()
  239.                 end
  240.            
  241.             elseif param == keys.up or param == keys.down then
  242.                 -- Up or down
  243.                 if _tHistory then
  244.                     redraw(" ");
  245.                     if param == keys.up then
  246.                         -- Up
  247.                         if nHistoryPos == nil then
  248.                             if #_tHistory > 0 then
  249.                                 nHistoryPos = #_tHistory
  250.                             end
  251.                         elseif nHistoryPos > 1 then
  252.                             nHistoryPos = nHistoryPos - 1
  253.                         end
  254.                     else
  255.                         -- Down
  256.                         if nHistoryPos == #_tHistory then
  257.                             nHistoryPos = nil
  258.                         elseif nHistoryPos ~= nil then
  259.                             nHistoryPos = nHistoryPos + 1
  260.                         end                    
  261.                     end
  262.                    
  263.                     if nHistoryPos then
  264.                         sLine = _tHistory[nHistoryPos]
  265.                         nPos = string.len( sLine )
  266.                     else
  267.                         sLine = ""
  268.                         nPos = 0
  269.                     end
  270.                     redraw()
  271.                 end
  272.             elseif param == keys.backspace then
  273.                 -- Backspace
  274.                 if nPos > 0 then
  275.                     redraw(" ");
  276.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  277.                     nPos = nPos - 1                
  278.                     redraw()
  279.                 end
  280.             elseif param == keys.home then
  281.                 -- Home
  282.                 nPos = 0
  283.                 redraw()       
  284.             elseif param == keys.delete then
  285.                 if nPos < string.len(sLine) then
  286.                     redraw(" ");
  287.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  288.                     redraw()
  289.                 end
  290.             elseif param == keys["end"] then
  291.                 -- End
  292.                 nPos = string.len(sLine)
  293.                 redraw()
  294.             end
  295.         end
  296.     end
  297.    
  298.     term.setCursorBlink( false )
  299.     term.setCursorPos( w + 1, sy )
  300.     print()
  301.    
  302.     return sLine
  303. end
  304.  
  305. loadfile = function( _sFile )
  306.     local file = fs.open( _sFile, "r" )
  307.     if file then
  308.         local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  309.         file.close()
  310.         return func, err
  311.     end
  312.     return nil, "File not found"
  313. end
  314.  
  315. dofile = function( _sFile )
  316.     local fnFile, e = loadfile( _sFile )
  317.     if fnFile then
  318.         setfenv( fnFile, getfenv(2) )
  319.         return fnFile()
  320.     else
  321.         error( e, 2 )
  322.     end
  323. end
  324.  
  325. -- Install the rest of the OS api
  326. function os.run( _tEnv, _sPath, ... )
  327.     local tArgs = { ... }
  328.     local fnFile, err = loadfile( _sPath )
  329.     if fnFile then
  330.         local tEnv = _tEnv
  331.         --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  332.         setmetatable( tEnv, { __index = _G } )
  333.         setfenv( fnFile, tEnv )
  334.         local ok, err = pcall( function()
  335.             fnFile( unpack( tArgs ) )
  336.         end )
  337.         if not ok then
  338.             if err and err ~= "" then
  339.                 printError( err )
  340.             end
  341.             return false
  342.         end
  343.         return true
  344.     end
  345.     if err and err ~= "" then
  346.         printError( err )
  347.     end
  348.     return false
  349. end
  350.  
  351. local nativegetmetatable = getmetatable
  352. local nativetype = type
  353. local nativeerror = error
  354. function getmetatable( _t )
  355.     if nativetype( _t ) == "string" then
  356.         nativeerror( "Attempt to access string metatable", 2 )
  357.         return nil
  358.     end
  359.     return nativegetmetatable( _t )
  360. end
  361.  
  362. local tAPIsLoading = {}
  363. function os.loadAPI( _sPath )
  364.     local sName = fs.getName( _sPath )
  365.     if tAPIsLoading[sName] == true then
  366.         printError( "API "..sName.." is already being loaded" )
  367.         return false
  368.     end
  369.     tAPIsLoading[sName] = true
  370.        
  371.     local tEnv = {}
  372.     setmetatable( tEnv, { __index = _G } )
  373.     local fnAPI, err = loadfile( _sPath )
  374.     if fnAPI then
  375.         setfenv( fnAPI, tEnv )
  376.         fnAPI()
  377.     else
  378.         printError( err )
  379.         tAPIsLoading[sName] = nil
  380.         return false
  381.     end
  382.    
  383.     local tAPI = {}
  384.     for k,v in pairs( tEnv ) do
  385.         tAPI[k] =  v
  386.     end
  387.    
  388.     _G[sName] = tAPI   
  389.     tAPIsLoading[sName] = nil
  390.     return true
  391. end
  392.  
  393. function os.unloadAPI( _sName )
  394.     if _sName ~= "_G" and type(_G[_sName]) == "table" then
  395.         _G[_sName] = nil
  396.     end
  397. end
  398.  
  399. function os.sleep( _nTime )
  400.     sleep( _nTime )
  401. end
  402.  
  403. local nativeShutdown = os.shutdown
  404. function os.shutdown()
  405.     nativeShutdown()
  406.     while true do
  407.         coroutine.yield()
  408.     end
  409. end
  410.  
  411. -- Install the lua part of the HTTP api (if enabled)
  412. if http then
  413.     local function wrapRequest( _url, _post )
  414.         for i=1,#WhitelistedURLs do --Simple for loop here
  415.             if string.find(_url:lower(),WhitelistedURLs[i]:lower()) == nil then --Only change I did in this was to change the variable names from 'RestrictedURLs' to 'WhitelistedURLs' and reverse the equal sign :P
  416.                 error("URL Restricted!")
  417.             end
  418.         end
  419.         local requestID = http.request( _url, _post )
  420.         while true do
  421.             local event, param1, param2 = os.pullEvent()
  422.             if event == "http_success" and param1 == _url then
  423.                 return param2
  424.             elseif event == "http_failure" and param1 == _url then
  425.                 return nil
  426.             end
  427.         end    
  428.     end
  429.    
  430.     http.get = function( _url )
  431.         return wrapRequest( _url, nil )
  432.     end
  433.  
  434.     http.post = function( _url, _post )
  435.         return wrapRequest( _url, _post or "" )
  436.     end
  437. end
  438.  
  439. -- Install the lua part of the peripheral api
  440. peripheral.wrap = function( _sSide )
  441.     if peripheral.isPresent( _sSide ) then
  442.         local tMethods = peripheral.getMethods( _sSide )
  443.         local tResult = {}
  444.         for n,sMethod in ipairs( tMethods ) do
  445.             tResult[sMethod] = function( ... )
  446.                 return peripheral.call( _sSide, sMethod, ... )
  447.             end
  448.         end
  449.         return tResult
  450.     end
  451.     return nil
  452. end
  453.            
  454. -- Load APIs
  455. local tApis = fs.list( "rom/apis" )
  456. for n,sFile in ipairs( tApis ) do
  457.     if string.sub( sFile, 1, 1 ) ~= "." then
  458.         local sPath = fs.combine( "rom/apis", sFile )
  459.         if not fs.isDir( sPath ) then
  460.             os.loadAPI( sPath )
  461.         end
  462.     end
  463. end
  464.  
  465. if turtle then
  466.     local tApis = fs.list( "rom/apis/turtle" )
  467.     for n,sFile in ipairs( tApis ) do
  468.         if string.sub( sFile, 1, 1 ) ~= "." then
  469.             local sPath = fs.combine( "rom/apis/turtle", sFile )
  470.             if not fs.isDir( sPath ) then
  471.                 os.loadAPI( sPath )
  472.             end
  473.         end
  474.     end
  475. end
  476.  
  477. -- Run the shell
  478. local ok, err = pcall( function()
  479.     parallel.waitForAny(
  480.         function()
  481.             rednet.run()
  482.         end,
  483.         function()
  484.             os.run( {}, "rom/programs/shell" )
  485.         end
  486.     )
  487. end )
  488.  
  489. -- If the shell errored, let the user read it.
  490. if not ok then
  491.     printError( err )
  492. end
  493.  
  494. pcall( function()
  495.     term.setCursorBlink( false )
  496.     print( "Press any key to continue" )
  497.     os.pullEvent( "key" )
  498. end )
  499. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment