nagadomi / waifu2x

Image Super-Resolution for Anime-Style Art
http://waifu2x.udp.jp/
MIT License
27.56k stars 2.71k forks source link

How to add reCaptcha to Waifu2x web version? #282

Open unit2x opened 5 years ago

unit2x commented 5 years ago

I only know how to use PHP but it seems Waifu2x has used Lua.

nagadomi commented 5 years ago

(It is out-of-support)

reCAPTCHA is just a REST API. There is no programming language dependency. waifu2x.udp.jp's server side code is like this.

local RECAP_KEY = cjson.decode(file.read("conf-e442d0af427517082659c34f0fe7cfb2/key.json")) -- secret key
local RECAP_URL = "https://www.google.com/recaptcha/api/siteverify" -- API

local function check_recap(user_ip, user_response)
   local params = {secret = RECAP_KEY["secret"], response = user_response, remoteip = user_ip}
   local res = coroutine.yield(
      turbo.async.HTTPClient({verify_ca=false},
     nil,
     CURL_MAX_SIZE):fetch(RECAP_URL, tablex.update({method="POST", params=params}, CURL_OPTIONS))
      )
   if res.code == 200 then
      -- return result
      res = cjson.decode(res.body)
      return res["success"]
   else
      -- API error(e.g. 500 error)
      return false
   end
end

-- main
function APIHandler:post()
   -- ...
   local user_ip = self.request.remote_ip
   local recap_response = self:get_argument("recap", "")
   if recap_response:len() == 0 or (not check_recap(user_ip, recap_response)) then
      -- verify failed
      -- ..
   end
   -- ok
   -- ..

See http://waifu2x.udp.jp/ui.js for client side code. (I use recap for the recaptcha response field.)