From bd04a93ffb62ed41cb100b41b7de5e3eeac03c16 Mon Sep 17 00:00:00 2001 From: Drew Lemmy Date: Sun, 24 Feb 2019 13:47:09 +0000 Subject: [PATCH 1/3] Handle pastebin spam protection and add a cache buster --- .../computercraft/lua/rom/programs/http/pastebin.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua index fd31bd33a..58abb74e9 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua @@ -20,11 +20,20 @@ end local function get(paste) write( "Connecting to pastebin.com... " ) + -- Add a cache buster so that spam protection is re-checked + local cacheBuster = tostring(math.random()) local response = http.get( - "https://pastebin.com/raw/"..textutils.urlEncode( paste ) + "https://pastebin.com/raw/"..textutils.urlEncode( paste ).."?cb="..cacheBuster ) if response then + -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html + local headers = response.getResponseHeaders() + if not headers["Content-Type"] or not headers["Content-Type"]:find( "^text/plain" ) then + printError( "\nFailed to get paste. Please complete the captcha in a web browser: https://pastebin.com/"..textutils.urlEncode( paste ) ) + return + end + print( "Success." ) local sResponse = response.readAll() From 7fde89ad95dcc8949d07ea2d52858e401b4f408b Mon Sep 17 00:00:00 2001 From: Drew Lemmy Date: Sun, 24 Feb 2019 19:59:10 +0000 Subject: [PATCH 2/3] Use stderr for pastebin errors, and a prettier cache buster --- .../computercraft/lua/rom/programs/http/pastebin.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua index 58abb74e9..dcb77234e 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua @@ -21,8 +21,8 @@ end local function get(paste) write( "Connecting to pastebin.com... " ) -- Add a cache buster so that spam protection is re-checked - local cacheBuster = tostring(math.random()) - local response = http.get( + local cacheBuster = ("%x"):format(math.random(0, 2^30)) + local response, err = http.get( "https://pastebin.com/raw/"..textutils.urlEncode( paste ).."?cb="..cacheBuster ) @@ -30,7 +30,8 @@ local function get(paste) -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html local headers = response.getResponseHeaders() if not headers["Content-Type"] or not headers["Content-Type"]:find( "^text/plain" ) then - printError( "\nFailed to get paste. Please complete the captcha in a web browser: https://pastebin.com/"..textutils.urlEncode( paste ) ) + io.stderr:write( "Failed.\n" ) + print( "Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/"..textutils.urlEncode( paste ) ) return end @@ -40,7 +41,8 @@ local function get(paste) response.close() return sResponse else - print( "Failed." ) + io.stderr:write( "Failed.\n" ) + print(err) end end @@ -76,6 +78,8 @@ if sCommand == "put" then if response then print( "Success." ) + print(response.getResponseHeaders()) + local sResponse = response.readAll() response.close() From d953f031f0e0f34c8da42c0d97586b790a8415f0 Mon Sep 17 00:00:00 2001 From: Drew Lemmy Date: Sun, 24 Feb 2019 20:00:26 +0000 Subject: [PATCH 3/3] Remove debugging line :grimacing: --- .../assets/computercraft/lua/rom/programs/http/pastebin.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua index dcb77234e..b82c52a99 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/pastebin.lua @@ -78,8 +78,6 @@ if sCommand == "put" then if response then print( "Success." ) - print(response.getResponseHeaders()) - local sResponse = response.readAll() response.close()