forked from AlexChittock/SimpleShortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortener.conf
147 lines (112 loc) · 4.64 KB
/
shortener.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
events {
worker_connections 1024;
}
http {
server {
listen 8080;
set $restricted true;
set $redis_db 1;
location ~ ^/robots.txt$ {
add_header Content-Type text/plain;
echo "User-agent: *";
echo "Disallow: /";
}
location ~ ^/count {
internal;
redis2_query select $redis_db;
redis2_query get count;
redis2_pass 127.0.0.1:6379;
}
location ~ ^/lookup {
internal;
set_unescape_uri $lookup $arg_lookup;
redis2_query select $redis_db;
redis2_query keys $lookup;
redis2_pass 127.0.0.1:6379;
}
location ~ ^/save$ {
internal;
redis2_query select $redis_db;
set_unescape_uri $url $arg_url;
set_unescape_uri $id $arg_id;
redis2_query set $id $url;
redis2_query incr count;
redis2_pass 127.0.0.1:6379;
}
location ~ ^/shorten$ {
add_header Content-Type text/plain;
if ($restricted) {
access_by_lua '
local prefixes = {"http://somedomain.com", "http://someotherdomain.com"};
local ok = false;
for k, v in ipairs(prefixes) do
tmp = string.find(ngx.var.arg_url, v);
if (1 == tmp) then
ok = true;
break;
end
end
if (false == ok) then
return ngx.exit(403);
end
';
}
content_by_lua '
local parser = require "redis.parser"
-- see if we have the url already
local hash = ngx.md5(ngx.var.arg_url);
local res = ngx.location.capture("/lookup", { args = { lookup = "*:"..hash } } );
local results, type = parser.parse_replies(res.body, 2);
if not (table.getn(results[2][1]) == 0) then
local s, e = string.find(results[2][1][1], ":");
ngx.say("http://" .. ngx.var.host .. string.sub(results[2][1][1], 1, s - 1));
return ngx.exit(200);
end
local res = ngx.location.capture("/count");
local results, type = parser.parse_replies(res.body, 2)
if (nil == results[2][1]) then
results[2][1] = 0;
end
basen = function(n)
local digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
local t = {}
repeat
local d = (n % 62) + 1
n = math.floor(n / 62)
table.insert(t, 1, digits:sub(d, d))
until n == 0
return table.concat(t,"");
end
local id = basen(results[2][1] + 1)
ngx.location.capture("/save",
{ args = { id = "/"..tostring(id)..":"..ngx.md5(ngx.var.arg_url), url = ngx.var.arg_url } }
)
ngx.say("http://" .. ngx.var.host .. "/" .. id)
';
}
location /get {
internal;
set_unescape_uri $id $arg_id;
redis2_query select $redis_db;
redis2_query get $id;
redis2_pass 127.0.0.1:6379;
}
location / {
content_by_lua '
local parser = require "redis.parser";
local res = ngx.location.capture("/lookup", { args = { lookup = ngx.var.request_uri..":*" } } );
local results, type = parser.parse_replies(res.body, 2);
if table.getn(results[2][1]) == 0 then
return ngx.exit(ngx.HTTP_NOT_FOUND);
end
local res = ngx.location.capture("/get", { args = { id = results[2][1][1] } } );
local href, type = parser.parse_replies(res.body, 2);
if type == parser.ERROR_REPLY or href == nil or href[2][1] == nil then
return ngx.exit(ngx.HTTP_NOT_FOUND);
else
ngx.redirect(href[2][1]);
end
';
}
}
}