@samitouri / QOSami-HFS / commits / 796e4f74

fix: basic-web: faulty logout when done from same folder of login

Massimo Melina committed Jun 18, 2024 at 20:26 UTC 796e4f749d7a4777e7428402c96a97b5f5705d4b
1 file changed +24 -24
src/basicWeb.ts
+24 -24
@@ -14,7 +14,7 @@ export function basicWeb(ctx: Koa.Context, node: VfsNode) {
14 const { get } = ctx.query
15 if (get === 'login') {
16 if (getCurrentUsername(ctx))
17 - ctx.redirect('?')
17 + ctx.redirect(ctx.get('referer'))
18 else {
19 ctx.set('WWW-Authenticate', 'Basic')
20 ctx.status = HTTP_UNAUTHORIZED
@@ -22,33 +22,33 @@ export function basicWeb(ctx: Koa.Context, node: VfsNode) {
22 return true
23 }
24 if (get === 'logout') {
25 - ctx.body = `<script>setTimeout(() => location = '?', 1000)</script>`
25 + ctx.body = `<script>location = ${JSON.stringify(ctx.get('referer'))}</script>`
26 setLoggedIn(ctx, false)
27 ctx.status = HTTP_UNAUTHORIZED // not effective on firefox30
28 return true
29 }
30 const forced = get === 'basic'
31 - if (forced || detectBasicAgent(ctx) && get !== 'nobasic') {
32 - ctx.type = 'html'
33 - const force = forced ? '?get=basic' : ''
34 - const walker = walkNode(node, { ctx, depth: 0 })
35 - const stream = asyncGeneratorToReadable(filterMapGenerator(walker, async el => {
36 - const isFolder = await nodeIsDirectory(el)
37 - const name = getNodeName(el) + (isFolder ? '/' : '')
38 - return `<li>${a(pathEncode(name) + (isFolder ? force : ''), name)}\n`
39 - }))
40 - ctx.body = stream
41 - stream.push(`<title>${title.get()}</title><body>`)
42 - stream.push(getSection('basicHeader'))
43 - const u = getCurrentUsername(ctx)
44 - const links: Dict<string> = u ? { '?get=logout': `Logout (${u})` } : { '?get=login': "Login" }
45 - stream.push(_.map(links, (v,k) => a(k, v)).join(' ') + '\n<ul>\n')
46 - if (ctx.state.originalPath.length > 1)
47 - stream.push('<li>' + a('..' + force, '..') + '\n')
48 - stream.on('ending', () =>
49 - stream.push('</ul>\n' + getSection('basicFooter')) )
50 - return true
51 - }
31 + const goBasic = forced || detectBasicAgent(ctx) && get !== 'nobasic'
32 + if (!goBasic) return
33 + ctx.type = 'html'
34 + const force = forced ? '?get=basic' : ''
35 + const walker = walkNode(node, { ctx, depth: 0 })
36 + const stream = asyncGeneratorToReadable(filterMapGenerator(walker, async el => {
37 + const isFolder = await nodeIsDirectory(el)
38 + const name = getNodeName(el) + (isFolder ? '/' : '')
39 + return `<li>${a(pathEncode(name) + (isFolder ? force : ''), name)}\n`
40 + }))
41 + ctx.body = stream
42 + stream.push(`<title>${title.get()}</title><body>`)
43 + stream.push(getSection('basicHeader'))
44 + const u = getCurrentUsername(ctx)
45 + const links: Dict<string> = u ? { [`//LOGOUT%00:@${ctx.get('host')}/?get=logout`]: `Logout (${u})` } : { '/?get=login': "Login" }
46 + stream.push(_.map(links, (v,k) => a(k, v)).join(' ') + '\n<ul>\n')
47 + if (ctx.state.originalPath.length > 1)
48 + stream.push('<li>' + a('..' + force, '..') + '\n')
49 + stream.on('ending', () =>
50 + stream.push('</ul>\n' + getSection('basicFooter')) )
51 + return true
52
53 function a(href: string, label: string) {
54 return `<a href='${href}'>${label}</a>`
@@ -59,7 +59,7 @@ export function basicWeb(ctx: Koa.Context, node: VfsNode) {
59 export function detectBasicAgent(ctx: Koa.Context) {
60 const ua = ctx.get('user-agent')
61 const v = autoBasic.get()
62 - return v && (/Mozilla\/4|WebKit\/([234]\d\d|5[012]\d|53[0123456])[. ]|Trident|Lynx|curl|Firefox\/(\d|[123]\d)\./.test(ua)
62 + return v && (/Mozilla\/4|WebKit\/([234]\d\d|5[012]\d|53[0123456])[. ]|Trident|Lynx|curl|Firefox\/(\d|[1234]\d)\./.test(ua)
63 || _.isString(v) && ua.includes(v))
64 }
65