Skip to content

Instantly share code, notes, and snippets.

@fangpsh
Last active July 31, 2018 09:29
Show Gist options
  • Save fangpsh/c7de2d718f6c50befe389eb646e60426 to your computer and use it in GitHub Desktop.
Save fangpsh/c7de2d718f6c50befe389eb646e60426 to your computer and use it in GitHub Desktop.
whoami.fangpeishi.com (whoami.akamai.com) based on openresty
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log debug;
#pid logs/nginx.pid;
events {
accept_mutex off;
worker_connections 1024;
}
stream {
lua_package_path '$prefix/lualib/?.lua;;';
init_by_lua_block {
jit.opt.start("minstitch=2")
}
server {
listen 53 udp;
content_by_lua_block {
require "whoami".go()
}
}
}
local bit = require "bit"
local byte = string.byte
local lshift = bit.lshift
local rshift = bit.rshift
local char = string.char
local sub = string.sub
local gmatch = string.gmatch
local _M = {}
local resp_tb = {}
function _M.go()
local sock, err = ngx.req.udp_socket()
if not sock then
ngx.log(ngx.ERR, "failed to get the request socket:", err)
return ngx.exit(ngx.ERROR)
end
local req, err = sock:receive()
if not req then
ngx.log(ngx.ERR, "failed to receive:", err)
return ngx.exit(ngx.ERROR)
end
local id
local n = #req
if n < 12 then
ngx.log(ngx.ERR, "request truncated")
return
end
-- Transaction ID
resp_tb[1] = char(byte(req, 1))
resp_tb[2] = char(byte(req, 2))
-- Flags
resp_tb[3] = "\x84\xa0"
-- Questions, Answer RRS, Authority RRs,Additional RRs
resp_tb[4] = "\x00\x01\x00\x01\x00\x00\x00\x00"
-- Queries
-- for "whoami.fangpeishi.com"
resp_tb[5] = char(byte(req, 13,39))
-- Answers
-- Name
resp_tb[6] = "\xc0\x0c"
-- TTL 180s
resp_tb[7] = "\x00\x01\x00\x01\x00\x00\x00\x02"
-- RDLength
resp_tb[8] = "\x00\x04"
-- RDdata
local i = 9
for code in gmatch(ngx.var.remote_addr, '([0-9]+)') do
resp_tb[i] = char(code)
i = i+1
end
local ok, err = sock:send{resp_tb}
if not ok then
ngx.log(ngx.ERR, "failed to send:", err)
return
end
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment