Skip to content

Instantly share code, notes, and snippets.

@mi2428
Created August 21, 2016 08:04
Show Gist options
  • Save mi2428/15266714b17c7d2b238ebd2e35868d5f to your computer and use it in GitHub Desktop.
Save mi2428/15266714b17c7d2b238ebd2e35868d5f to your computer and use it in GitHub Desktop.
function main()
local uri = ngx.var.request_uri
local chk = string.sub(uri,1,10)
if chk == "/endpoint?" then
local url = ngx.req.get_uri_args().url
local res = ngx.location.capture("/encode?url="..url)
ngx.print(res.body)
else
local nonce = string.sub(uri,2)
local res = ngx.location.capture("/decode?nonce="..nonce)
ngx.print(res.body)
end
end
main()
function generate(wordlist,len)
math.randomseed(os.time())
local nonce = ""
for i = 1, len do
local r = math.random(1,#wordlist)
nonce = nonce..wordlist[r]
end
return nonce
end
function register(url,nonce)
local redis = require "resty.redis"
local client = redis:new()
client:connect("127.0.0.1",6379)
client:hset(nonce,"Nginx::200_shorten-url.conf",url)
return true
end
function main(wordlist,len)
local raw = ngx.req.get_uri_args().url
local nonce = generate(wordlist,len)
while not register(raw,nonce) do
local nonce = generate(wordlist,len)
end
local url = "http://10.0.10.101:90/"..nonce
local html = "<a href=\""..url.."\">"..url.."</a>"
ngx.print(html)
end
WORDLIST = {
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"0","1","2","3","4","5","6","7","8","9"
}
LENGTH=6
main(WORDLIST,LENGTH)
function lookup(nonce)
local redis = require "resty.redis"
local client = redis:new()
client:connect("127.0.0.1",6379)
url,err = client:hget(nonce,"Nginx::200_shorten-url.conf")
return url
end
function main()
local nonce = ngx.req.get_uri_args().nonce
local url = lookup(nonce)
local html = "<a href=\""..url.."\">"..url.."</a>"
ngx.print(html)
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment