-
Notifications
You must be signed in to change notification settings - Fork 1
/
code
480 lines (453 loc) · 31.5 KB
/
code
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
set $server_files_location "/etc/code-server-hub";
set $server_service_name "cshub";
set $server_service_name_full "Visual Studio Code Server";
set $py3 "/usr/bin/python3";
listen 8443 ssl;
listen [::]:8443 ssl;
ssl_certificate '/etc/code-server-hub/cert/ssl.pem';
ssl_certificate_key '/etc/code-server-hub/cert/ssl.key';
server_name default_server;
# Managing requests to verify letsencrypt host
location /.well-known/Meteor.jpg {
content_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require 'url_tool'
local prefix = ngx.var.server_files_location .. '/util/anime_pic'
local python_get_img = io.popen(ngx.var.py3 .. ' "' .. ngx.var.server_files_location .. '/util/get_pic_anime.py" "' .. prefix .. '" 2>&1', "r")
local img_path = python_get_img:read("*all")
local img_name = url_tool.getname(img_path, prefix)
if img_name ~= nil then
ngx.header.location = "/.well-known/Meteor" .. img_name
ngx.status = 302
else
ngx.status = 500
ngx.say(img_path)
end
}
}
location /.well-known/Meteor {
alias ${server_files_location}/util/anime_pic;
}
location /.well-known {
alias /var/www/html/.well-known;
}
client_max_body_size 0;
# Set this and add to line 140 if you set cockpit under some subpath . ex: 14: set $cockpit_addr "/cockpit";140:location ~ "/(cockpit).*" {
set $cockpit_addr "";
set $jupyter_addr "";
set $cockpit_addr_internal "https://127.0.0.1:9090";
set $jupyter_addr_internal "https://127.0.0.1:18517";
set $server_salt "1!2@3=我是鹽";
set $server_sal2 "1!2@3=我是糖";
# Managing literal requests to the front end
location / {
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host ;
proxy_set_header Origin "https://${http_host}" ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $server_auth_base64 "bm9sb2dpbjpOb3RTZXRZZXQ=";
set $server_sock_url "Not_Set_Yet";
rewrite_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
if ngx.var.http_authorization and ngx.var.remote_user then
local md5 = require('md5')
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
server_auth = remote_user .. ":" .. md5.sumhexa(ngx.var.server_salt .. remote_password)
server_sock_name = remote_user .. "_" .. md5.sumhexa(ngx.var.server_sal2 .. remote_password)
server_sock_path = ngx.var.server_files_location .. "/sock/" .. ngx.var.server_service_name .. "-" .. server_sock_name .. ".sock"
server_sock_url = "http://unix:" .. server_sock_path
ngx.var.server_sock_url = server_sock_url
ngx.var.server_auth_base64 = ngx.encode_base64( server_auth )
fp = io.popen(ngx.var.py3 .. " " .. ngx.var.server_files_location .. "/util/create_login.py","w")
fp:write(server_sock_path)
fp:close()
end
}
proxy_set_header Authorization "Basic ${server_auth_base64}";
proxy_pass $server_sock_url;
proxy_redirect ./login ./302-code-server.html;
# websocket headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# if proxy_pass to workspace failed, spawn workspace
error_page 502 =404 /502-server_conn_err.html;
}
location = /502-server_conn_err.html {
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
return 404 '<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> *{font-family: Arial, "文泉驛正黑", "WenQuanYi Zen Hei", "儷黑 Pro", "LiHei Pro", "微軟正黑體", "Microsoft JhengHei", "標楷體", DFKai-SB, sans-serif;}.MainTitle{text-shadow: -1px -1px 0 #000,1px -1px 0 #000,-1px 1px 0 #000,1px 1px 0 #000,2px 2px 2px black; color:white; } body { background: url("/.well-known/Meteor.jpg") no-repeat fixed; background-position: center top; background-size: cover; } .buttonRed { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #f22424; border: 10px; border-radius: 15px; box-shadow: 0 9px #999; } .buttonRed:hover { background-color: #d12626 } .buttonRed:active { background-color: #d12626; box-shadow: 0 5px #666; transform: translateY(4px); } .button { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #4CAF50; border: 10px; border-radius: 15px; box-shadow: 0 9px #999; } .button:hover { background-color: #3e8e41 } .button:active { background-color: #3e8e41; box-shadow: 0 5px #666; transform: translateY(4px); } .buttonGray { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #525252; border: 10px; border-radius: 15px; box-shadow: 0 9px #999; } .buttonGray:hover { background-color: #525252 } </style></head><body><a href="https://raw.githubusercontent.com/HuJK/Code-Server-Hub/refs/heads/master/util/sites/readme.png"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://raw.githubusercontent.com/HuJK/Code-Server-Hub/refs/heads/master/util/sites/readme.png" alt="Usage Doc"></a> <center> <h1 class=MainTitle>${server_service_name_full} Control Panel</h1></center> <center> <input type="button" class="button" value="Start Server" onclick="window.location.href=\'/200-server_spawn.html\'" style="width:160px;height:60px;font-size:20px;"> <input type="button" class="buttonRed" value="Log out" onclick="window.location.href=\'/200-logout.html\'" style="width:120px;height:60px;font-size:20px;"> </center> <hr style="visibility: hidden;" /> <center> <input type="button" class="buttonGray" value="VS code" style="width:120px;height:60px;font-size:20px;" disabled> <input type="button" class="button" value="Jupyter" onclick="window.open(\'/302-jupyterhub.html\')" style="width:120px;height:60px;font-size:20px;"> <input type="button" class="button" value="Cockpit" onclick="window.open(\'/302-cockpit.html\')" style="width:120px;height:60px;font-size:20px;"> <input type="button" class="button" value="Account" onclick="window.open(\'/302-account.html\')" style="width:120px;height:60px;font-size:20px;"> </center> <hr style="visibility: hidden;" /></body></html>';
}
location = /200-server_spawn.html {
default_type 'text/html';
add_header "Content-Type" "text/html" always;
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
content_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local md5 = require 'md5'
if ngx.var.http_authorization then
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
server_auth = remote_user .. ":" .. md5.sumhexa(ngx.var.server_salt .. remote_password)
server_sock_name = remote_user .. "_" .. md5.sumhexa(ngx.var.server_sal2 .. remote_password)
server_sock_path = ngx.var.server_files_location .. "/sock/" .. ngx.var.server_service_name .. "-" .. server_sock_name .. ".sock"
response = ngx.location.capture('/')
ngx.log(ngx.STDERR, "response.status=" .. tostring(response.status))
if response.status >= 400 then
ndcmd = "$HOME/." .. ngx.var.server_service_name .. "/bin/code-server --socket " .. server_sock_path .. " --auth password "
spcmd = "unset TMUX ;cp -prn " .. ngx.var.server_files_location .. "/." .. ngx.var.server_service_name .. " $HOME ;rm -f " .. server_sock_path .." 2>/dev/null ;tmux kill-session -t " .. ngx.var.server_service_name .. " ;tmux new -d -s " .. ngx.var.server_service_name .. "_setenv ;tmux set-environment -g PASSWORD " .. server_auth .. " ;tmux new -d -s " .. ngx.var.server_service_name .. " " .. ndcmd .. " ;tmux kill-session -t " .. ngx.var.server_service_name .. "_setenv ; tmux new -d -s chmod766 bash " .. ngx.var.server_files_location .. "/util/chmod766.sh " .. server_sock_path .. " "
spcmd_by_su = "su -l " .. remote_user .. " -s /bin/bash -c '" .. spcmd .. "'"
fp = io.popen(ngx.var.py3 .. " " .. ngx.var.server_files_location .. "/util/pip2su.py " .. spcmd_by_su ,"w")
fp:write(remote_password)
fp:close()
ngx.say('<head><style>code {display: block; /* fixes a strange ie margin bug */font-family: Courier New;font-size: 11pt;overflow:auto;background: #f0f0f0 url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAASwCAYAAAAt7rCDAAAABHNCSVQICAgIfAhkiAAAAQJJREFUeJzt0kEKhDAMBdA4zFmbM+W0upqFOhXrDILwsimFR5pfMrXW5jhZr7PwRlxVX8//jNHrGhExjXzdu9c5IiIz+7iqVmB7Hwp4OMa2nhhwN/PRGEMBh3Zjt6KfpzPztxW9MSAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzAMwzB8HS+J9kUTvzEDMwAAAABJRU5ErkJggg==) left top repeat-y;border: 10px solid white;padding: 10px 10px 10px 21px;max-height:1000px;line-height: 1.2em;}</style></head>')
ngx.say('<body>')
ngx.say("<center><h1>Starting workspace</h1></center>")
ngx.say("<p>If server not start, try to run this command in ssh console:<br>")
ngx.say("<code>")
ngx.say(spcmd:gsub(";", "\n"):gsub("&", "&"):gsub("\"", """):gsub("<", "<"):gsub(">", ">"):gsub("\n", "<br>") .. "<br>")
ngx.say("</code></p>")
ngx.say("<p>If server still not start, Please run this command in ssh console, check if there has any error:<br>")
ngx.say("<code>")
ngx.say(("export PASSWORD=" .. server_auth .. ";" .. ndcmd):gsub(";", "\n"):gsub("\"", """):gsub("<", "<"):gsub(">", ">"):gsub("&", "&"):gsub("\n", "<br>") .. "<br>")
ngx.say("</code></p>")
ngx.say("<p>You can check " .. ngx.var.server_service_name_full .. " workspace status by this command: <br><code>tmux a -t " .. ngx.var.server_service_name .. "</code><br></p>")
ngx.say("<hr />")
ngx.say("<br>Please refresh this page after a few seconds.<br>")
ngx.say('<script>window.onload = function() { setInterval(function(){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { console.log(xhttp); if ((xhttp.readyState === 4 && xhttp.status === 0) || (xhttp.status < 400 && xhttp.status >= 200)) { window.location.replace("/200-panel.html"); } }; xhttp.open("GET", "/", true); xhttp.send();},1000);};</script>')
ngx.say('</body>')
ngx.say("<!--\n" .. spcmd_by_su .. "\n-->")
else
ngx.redirect("/")
end
else
ngx.say("Need auth info!")
end
}
}
set $panelHTML '<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> *{font-family: Arial, "文泉驛正黑", "WenQuanYi Zen Hei", "儷黑 Pro", "LiHei Pro", "微軟正黑體", "Microsoft JhengHei", "標楷體", DFKai-SB, sans-serif;}.MainTitle{text-shadow: -1px -1px 0 #000,1px -1px 0 #000,-1px 1px 0 #000,1px 1px 0 #000,2px 2px 2px black; color:white; } body {background: url("/.well-known/Meteor.jpg") no-repeat fixed;background-position: center top;background-size:cover;} .buttonRed { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #f22424; border: 10px; border-radius: 15px; box-shadow: 0 9px #999; } .buttonRed:hover { background-color: #d12626 } .buttonRed:active { background-color: #d12626; box-shadow: 0 5px #666; transform: translateY(4px); } .button { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #4CAF50; border: 10px; border-radius: 15px; box-shadow: 0 9px #999; } .button:hover { background-color: #3e8e41 } .button:active { background-color: #3e8e41; box-shadow: 0 5px #666; transform: translateY(4px); } </style></head><body><a href="https://github.com/HuJK/Code-Server-Hub/blob/master/util/sites/README.md"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://raw.githubusercontent.com/HuJK/Code-Server-Hub/refs/heads/master/util/sites/readme.png" alt="Usage Doc"></a> <center> <h1 class=MainTitle>${server_service_name_full} Control Panel</h1></center> <center> <input type="button" class="buttonRed" value="Stop Server" onclick="window.location.href=\'/200-shutdown.html\'" style="width:160px;height:60px;font-size:20px;"> <input type="button" class="buttonRed" value="Log out" onclick="window.location.href=\'/200-logout.html\'" style="width:120px;height:60px;font-size:20px;"> </center> <hr style="visibility: hidden;" /> <center> <input type="button" class="button" value="VS code" onclick="window.open(\'/\')" style="width:120px;height:60px;font-size:20px;"> <input type="button" class="button" value="Jupyter" onclick="window.open(\'/302-jupyterhub.html\')" style="width:120px;height:60px;font-size:20px;"> <input type="button" class="button" value="Cockpit" onclick="window.open(\'/302-cockpit.html\')" style="width:120px;height:60px;font-size:20px;"> <input type="button" class="button" value="Account" onclick="window.open(\'/302-account.html\')" style="width:120px;height:60px;font-size:20px;"> </center> <hr style="visibility: hidden;" /></body></html>';
location = /200-panel.html {
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
content_by_lua_block {
response = ngx.location.capture('/')
if response.status >= 400 then
ngx.redirect("/502-server_conn_err.html")
else
ngx.say(ngx.var.panelHTML)
end
}
}
location = /200-shutdown.html {
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
access_by_lua_block {
if ngx.var.http_authorization and ngx.var.remote_user then
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
spcmd = "unset TMUX ;tmux send-keys -t " .. ngx.var.server_service_name .. " C-c ;sleep 1 ;tmux kill-session -t " .. ngx.var.server_service_name .. ""
spcmd_by_su = "su " .. remote_user .. " -c '" .. spcmd .. "'"
fp = io.popen(ngx.var.py3 .. " " .. ngx.var.server_files_location .. "/util/pip2su.py " .. spcmd_by_su ,"w")
fp:write(remote_password)
fp:close()
ngx.redirect("/")
end
}
}
location = /200-logout.html {
add_header Set-Cookie "cockpit=disabled; Path=/; Secure; HttpOnly" ;
return 200 '<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Logout. Back to homepage after 0.1 seconds.</p><form name="homepage" action="/401-logout.html" method="get"></form></body></html>';
}
location ~ "^/(cockpit|system|storage|network|docker|users|apps|updates|dashboard).*" {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass $cockpit_addr_internal;
proxy_set_header Host $http_host ;
gzip off;
}
location ~ "^/(user|hub)/.*" {
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass $jupyter_addr_internal;
proxy_set_header Host $http_host ;
gzip off;
}
location = /302-code-server.html {
add_header "Content-Type" "text/html" always;
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
set $setcookie00 "nil";
set $SetCookie11 "nil";
access_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local md5 = require 'md5'
local url_tool = require 'url_tool'
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
server_auth = remote_user .. ":" .. md5.sumhexa(ngx.var.server_salt .. remote_password)
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded")
response = ngx.location.capture('/login', { method = ngx.HTTP_POST, body = "password=" .. url_tool.urlEncode(server_auth)})
if response.status < 400 then
if url_tool.serialize(response.header["set-cookie"]) ~= nil then
ngx.var.setcookie00 = url_tool.serialize(response.header["set-cookie"])
end
if url_tool.serialize(response.header["Set-Cookie"]) ~= nil then
ngx.var.SetCookie11 = url_tool.serialize(response.header["Set-Cookie"])
end
end
}
header_filter_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
url_tool = require 'url_tool'
if ngx.var.setcookie00 ~= "nil" then
ngx.header["set-cookie"] = url_tool.unserialize(ngx.var.setcookie00)
elseif ngx.var.setcookie11 ~= "nil" then
ngx.header["Set-Cookie"] = url_tool.unserialize(ngx.var.SetCookie11)
end
}
content_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
if ngx.var.setcookie00 ~= "nil" or ngx.var.setcookie11 ~= "nil" then
ngx.say('<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Retrieve cookie success.<br/>Redirect to ' .. "/" .. ' after 0.1 seconds.</p><form name="homepage" action="' .. "/" .. '" method="get"></form></body></html>')
else
ngx.status = 404
ngx.say('<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Retrieve cookie failed.<br/>Redirect to ' .. "/200-server_spawn.html" .. ' to try again after 0.1 seconds.</p><form name="homepage" action="' .. "/200-server_spawn.html" .. '" method="get"></form></body></html>')
end
}
}
location = /302-jupyterhub.html {
add_header "Content-Type" "text/html" always;
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
set $setcookie00 "nil";
set $SetCookie11 "nil";
access_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require 'url_tool'
local cookie_tool = require 'cookie_tool'
local xsrf_value = ngx.var.cookie__xsrf
if xsrf_value == nil then
local response_get_xsrf = ngx.location.capture(ngx.var.jupyter_addr .. '/hub/login')
local xsrf_cookie = cookie_tool.parsecookie(response_get_xsrf.header["set-cookie"])
xsrf_value = xsrf_cookie["_xsrf"]
ngx.req.set_header("Cookie",response_get_xsrf.header["set-cookie"])
--ngx.header["set-cookie"] = response_get_xsrf.header["set-cookie"]
--ngx.say("_xsrf: " .. xsrf_value)
else
--ngx.say("use old _xsrf: " .. xsrf_value)
end
if xsrf_value == nil then
xsrf_value = ""
end
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded")
response = ngx.location.capture(ngx.var.jupyter_addr .. '/hub/login?next=', { method = ngx.HTTP_POST, body = "username=" .. url_tool.urlEncode(remote_user) .. "&password=" .. url_tool.urlEncode(remote_password) .. "&_xsrf=" .. url_tool.urlEncode(xsrf_value)})
if response.status < 400 then
ngx.var.setcookie00 = url_tool.serialize(response.header["set-cookie"])
ngx.var.SetCookie11 = url_tool.serialize(response.header["Set-Cookie"])
else
ngx.say("Unable to retrive cookie from " .. ngx.var.jupyter_addr_internal .. " Please check systemctl status jupyterhub. Error" .. response.body)
end
}
header_filter_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require 'url_tool'
if ngx.var.setcookie00 ~= "nil" then
ngx.header["set-cookie"] = url_tool.unserialize(ngx.var.setcookie00)
else
ngx.header["Set-Cookie"] = url_tool.unserialize(ngx.var.SetCookie11)
end
}
content_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
if ngx.var.setcookie00 ~= "nil" or ngx.var.setcookie11 ~= "nil" then
ngx.say('<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Retrieve cookie success.<br/>Redirect to ' .. ngx.var.jupyter_addr .. "/hub/" .. ' after 0.1 seconds.</p><form name="homepage" action="' .. ngx.var.jupyter_addr .. "/hub/" .. '" method="get"></form></body></html>')
else
ngx.status = 404
ngx.say("Unable to retrive cookie from " .. ngx.var.jupyter_addr_internal .. " Please check systemctl status jupyterhub. Error" .. response.body)
end
}
}
location = /302-cockpit.html {
add_header "Content-Type" "text/html" always;
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
set $setcookie00 "nil";
set $SetCookie11 "nil";
access_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require('url_tool')
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded")
response = ngx.location.capture(ngx.var.cockpit_addr .. '/cockpit/login')
if response.status < 400 then
ngx.var.setcookie00 = url_tool.serialize(response.header["set-cookie"])
ngx.var.SetCookie11 = url_tool.serialize(response.header["Set-Cookie"])
end
}
header_filter_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require('url_tool')
if ngx.var.setcookie00 ~= "nil" then
ngx.header["set-cookie"] = url_tool.unserialize(ngx.var.setcookie00)
else
ngx.header["Set-Cookie"] = url_tool.unserialize(ngx.var.SetCookie11)
end
}
content_by_lua_block {
if ngx.var.setcookie00 ~= "nil" or ngx.var.setcookie11 ~= "nil" then
ngx.say('<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Retrieve cookie success.<br/>Redirect to ' .. ngx.var.cockpit_addr .. "/system" .. ' after 0.1 seconds.</p><form name="homepage" action="' .. ngx.var.cockpit_addr .. "/system" .. '" method="get"></form></body></html>')
else
ngx.status = 404
ngx.say("Unable to connect to " .. ngx.var.cockpit_addr_internal)
end
}
}
location = /302-account.html {
add_header "Content-Type" "text/html" always;
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
set $setcookie00 "nil";
set $SetCookie11 "nil";
access_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require('url_tool')
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded")
response = ngx.location.capture(ngx.var.cockpit_addr .. '/cockpit/login')
if response.status < 400 then
ngx.var.setcookie00 = url_tool.serialize(response.header["set-cookie"])
ngx.var.SetCookie11 = url_tool.serialize(response.header["Set-Cookie"])
end
}
header_filter_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
local url_tool = require('url_tool')
if ngx.var.setcookie00 ~= "nil" then
ngx.header["set-cookie"] = url_tool.unserialize(ngx.var.setcookie00)
else
ngx.header["Set-Cookie"] = url_tool.unserialize(ngx.var.SetCookie11)
end
}
content_by_lua_block {
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
if ngx.var.setcookie00 ~= "nil" or ngx.var.setcookie11 ~= "nil" then
ngx.say('<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Retrieve cookie success.<br/>Redirect to ' .. ngx.var.cockpit_addr .. "/users#/" .. remote_user .. ' after 0.1 seconds.</p><form name="homepage" action="' .. ngx.var.cockpit_addr .. "/users#/" .. remote_user .. '" method="get"></form></body></html>')
else
ngx.status = 404
ngx.say("Unable to connect to " .. ngx.var.cockpit_addr_internal)
end
}
}
location = /401-logout.html {
auth_pam "uU86bq7aCRrTz0bT5fYbhKAX";
auth_pam_service_name "login";
access_by_lua_block {
if string.find(package.path, ngx.var.server_files_location .. "/util/?.lua",1,true) == nil then
package.path = package.path .. ";" .. ngx.var.server_files_location .. "/util/?.lua"
end
if ngx.var.http_authorization and ngx.var.remote_user then
local md5 = require('md5')
local tmp = ngx.var.http_authorization
tmp = tmp:sub(tmp:find(' ')+1)
tmp = ngx.decode_base64(tmp)
remote_user = tmp:sub(0,tmp:find(":")-1)
remote_password = tmp:sub(tmp:find(':')+1)
server_sock_name = remote_user .. "_" .. md5.sumhexa(ngx.var.server_sal2 .. remote_password)
user_lgin_tokn_path = ngx.var.server_files_location .. "/sock/" .. ngx.var.server_service_name .. "-" .. server_sock_name .. ".login"
lginfile = io.open(user_lgin_tokn_path, "r")
if lginfile ~= nil then
login_status = "1"
lginfile:close()
os.remove(user_lgin_tokn_path)
else
login_status = "0"
end
if login_status == "0" then
ngx.redirect("/")
end
end
}
try_files DUMMY @return401;
error_page 401 =200 @return200;
}
location @test {
return 200 "testttt";
}
location @return401 {
set $www_auth_str 'Basic Realm=uU86bq7aCRrTz0bT5fYbhKAX';
add_header 'WWW-Authenticate' $www_auth_str always;
return 401 '<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Logout. Back to homepage after 0.1 seconds.</p><form name="homepage" action="/" method="get"></form></body></html>';
}
location @return200 {
return 200 '<!DOCTYPE html><html><body><script>setTimeout(function(){document.homepage.submit();},100);</script><p>Logout. Back to homepage after 0.1 seconds.</p><form name="homepage" action="/" method="get"></form></body></html>';
}
}