windows integration: support https-only configuration

Massimo Melina committed Nov 25, 2023 at 20:11 UTC 76c86d505563a403c5d74422e54e675fd088e860
1 file changed +16 -20
src/api.vfs.ts
+16 -20
@@ -1,18 +1,13 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 -import {
4 - getNodeName, isSameFilenameAs, nodeIsDirectory, saveVfs, urlToNode, vfs, VfsNode, applyParentToChild,
5 - permsFromParent, nodeIsLink
6 -} from './vfs'
3 +import { getNodeName, isSameFilenameAs, nodeIsDirectory, saveVfs, urlToNode, vfs, VfsNode, applyParentToChild,
4 + permsFromParent, nodeIsLink } from './vfs'
5 import _ from 'lodash'
6 import { mkdir, stat } from 'fs/promises'
7 import { ApiError, ApiHandlers, SendListReadable } from './apiMiddleware'
8 import { dirname, extname, join, resolve } from 'path'
9 import { dirStream, enforceFinal, isDirectory, isWindowsDrive, makeMatcher, PERM_KEYS, VfsNodeAdminSend } from './misc'
12 -import {
13 - IS_WINDOWS,
14 - HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR, HTTP_CONFLICT, HTTP_NOT_ACCEPTABLE,
15 -} from './const'
10 +import { IS_WINDOWS, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR, HTTP_CONFLICT, HTTP_NOT_ACCEPTABLE } from './const'
11 import { getDrives } from './util-os'
12 import { Stats } from 'fs'
13 import { getBaseUrlOrDefault, getServerStatus } from './listen'
@@ -220,7 +215,16 @@ const apis: ApiHandlers = {
215 },
216
217 async windows_integration() {
223 - await windowsIntegration()
218 + const status = await getServerStatus(true)
219 + const h = status.http.listening ? status.http : status.https
220 + const url = h.srv!.name + '://localhost:' + h.port
221 + for (const k of ['*', 'Directory']) {
222 + await reg('add', WINDOWS_REG_KEY.replace('*', k), '/ve', '/f', '/d', 'Add to HFS (new)')
223 + await reg('add', WINDOWS_REG_KEY.replace('*', k) + '\\command', '/ve', '/f', '/d', `powershell -Command "
224 + $j = '{ \\"source\\": "' + ('%1'|convertTo-json) + '" }'; $j = [System.Text.Encoding]::UTF8.GetBytes($j); $wsh = New-Object -ComObject Wscript.Shell;
225 + try { $res = Invoke-WebRequest -Uri '${url}/~/api/add_vfs' -Method POST -Headers @{ 'x-hfs-anti-csrf' = '1' } -ContentType 'application/json' -TimeoutSec 1 -Body $j;
226 + $json = $res.Content | ConvertFrom-Json; $link = $json.link; $link | Set-Clipboard; } catch { $wsh.Popup('Server is down', 0, 'Error', 16); }"`)
227 + }
228 return {}
229 },
230
@@ -259,17 +263,9 @@ function simplifyName(node: VfsNode) {
263
264 const WINDOWS_REG_KEY = 'HKCU\\Software\\Classes\\*\\shell\\AddToHFS3'
265
262 -export async function windowsIntegration() {
263 - const status = await getServerStatus()
264 - const url = 'http://localhost:' + status.http.port
265 - for (const k of ['*', 'Directory']) {
266 - await reg('add', WINDOWS_REG_KEY.replace('*', k), '/ve', '/f', '/d', 'Add to HFS (new)')
267 - await reg('add', WINDOWS_REG_KEY.replace('*', k) + '\\command', '/ve', '/f', '/d', `powershell -Command "
268 - $j = '{ \\"source\\": "' + ('%1'|convertTo-json) + '" }'; $wsh = New-Object -ComObject Wscript.Shell; $j = [System.Text.Encoding]::UTF8.GetBytes($j);
269 - try { $res = Invoke-WebRequest -Uri '${url}/~/api/add_vfs' -Method POST -Headers @{ 'x-hfs-anti-csrf' = '1' } -ContentType 'application/json' -TimeoutSec 1 -Body $j;
270 - $json = $res.Content | ConvertFrom-Json; $link = $json.link; $link | Set-Clipboard; } catch { $wsh.Popup('Server is down', 0, 'Error', 16); }"`)
271 - }
272 -}
266 +if (IS_WINDOWS) // legacy 0.49.0-beta7 2023-10-27. Remove in 0.50
267 + for (const k of ['*', 'Directory'])
268 + reg('delete', `HKCR\\${k}\\shell\\AddToHFS3`, '/f').catch(() => {})
269
270 function reg(...pars: string[]) {
271 return promisify(execFile)('reg', pars)