Như đã nói ở chương 1 có 2 cách xác thực bot của công cụ tìm kiếm khác nhau. Tuy vào khi nghiên cứu các yêu cầu đối với trang thương mại điện tử cũng như các công cụ tìm kiếm phổ biến nhất thì các công cụ hay được người dùng dùng nhất là google, bing của microsoft và củ facebook. Do đó phòng phần này triển khai thuật toán xác thực bot tìm kiếm bằng dns và áp dụng với 2 công cụ tìm kiếm là google và bing và xác thực bằng ip với facebook. Module xác thực qua dns dùng thư viện luasocket của lua và được cài đặt thông qua luarocks. Module xác thực qua ip dùng module lua-resty-iputils. Với code rất đơn giản như sau:
Xác thực googlebot
if ngx.re.match(useragent, "googlebot", "i") then -- ngx.say("googlebot")
local socket = require('socket')
google_hostname = socket.dns.tohostname(ngx.var.remote_addr) -- test --
-- google_hostname = socket.dns.tohostname("66.249.66.1") -- ngx.say(google_hostname)
-- test --
if (not google_hostname or not ngx.re.match(google_hostname, "googlebot.com")) then ngx.exit(ngx.HTTP_FORBIDDEN) -- captcha authen end return end Xác thực bingbot --- -- bing's bot authentication --
-- https://udger.com/resources/ua-list/bot-detail?bot=bingbot ---
if ngx.re.match(useragent, "http://www.bing.com/bingbot.htm") then local socket = require("socket")
---
-- https://www.bing.com/webmaster/help/which-crawlers-does-bing-use- 8c184ec0
---
bing_hostname = socket.dns.tohostname(ngx.var.remote_addr)
if (not bing_hostname or not ngx.re.match(bing_hostname, "search.msn.com")) then ngx.exit(ngx.HTTP_FORBIDDEN) end return end Xác thực facebookbot
local iputils = require("resty.iputils") iputils.enable_lrucache() --
local facebook_ips = { … facebook’s ip ranges … }
if ngx.re.match(useragent, "facebookexternalhit") or ngx.re.match(useragent, "Facebot") then
local iputils = require("resty.iputils")
if not iputils.ip_in_cidrs(ngx.var.remote_addr, facebook_whitelist) then return ngx.exit(ngx.HTTP_FORBIDDEN)
end return end
Khi bot không được xác thực sẽ trả lại người dùng cấm truy cập và dừng không cho request vào website nữa.