| @@ -1,4 +1,5 @@ | |||||
| import * as fs from 'fs'; | import * as fs from 'fs'; | ||||
| import * as http from 'http'; | |||||
| import { v2 as webdav } from 'webdav-server'; | import { v2 as webdav } from 'webdav-server'; | ||||
| function getConfig(name: string) : any { | function getConfig(name: string) : any { | ||||
| @@ -17,6 +18,7 @@ function getConfig(name: string) : any { | |||||
| return config; | return config; | ||||
| } | } | ||||
| /* | |||||
| const config = getConfig('srvconfig'); | const config = getConfig('srvconfig'); | ||||
| const users = getConfig('users'); | const users = getConfig('users'); | ||||
| @@ -55,7 +57,7 @@ for (let nuser = 0 ; nuser < users.length; nuser++) { | |||||
| const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm); | const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm); | ||||
| const serverOptions: webdav.WebDAVServerOptions = { | const serverOptions: webdav.WebDAVServerOptions = { | ||||
| port: config.port || 8080, | |||||
| port: config.port, | |||||
| requireAuthentification: true, | requireAuthentification: true, | ||||
| httpAuthentication, | httpAuthentication, | ||||
| privilegeManager, | privilegeManager, | ||||
| @@ -67,7 +69,7 @@ const server = new webdav.WebDAVServer(serverOptions); | |||||
| server.afterRequest((_arg, next) => { | server.afterRequest((_arg, next) => { | ||||
| // Display the method, the URI, the returned status code and the returned message | // Display the method, the URI, the returned status code and the returned message | ||||
| /* | |||||
| console.log( | console.log( | ||||
| 'METHOD', _arg.request.method, | 'METHOD', _arg.request.method, | ||||
| 'URI', _arg.requested.uri, | 'URI', _arg.requested.uri, | ||||
| @@ -75,7 +77,7 @@ server.afterRequest((_arg, next) => { | |||||
| 'RESPONSE.StatusCode', _arg.response.statusMessage); | 'RESPONSE.StatusCode', _arg.response.statusMessage); | ||||
| // If available, display the body of the response | // If available, display the body of the response | ||||
| console.log('RESPONSEBODY', _arg.responseBody); | console.log('RESPONSEBODY', _arg.responseBody); | ||||
| */ | |||||
| next(); | next(); | ||||
| }); | }); | ||||
| @@ -96,7 +98,116 @@ function mountAndStart() : void { | |||||
| else { | else { | ||||
| server.start(httpServer => { | server.start(httpServer => { | ||||
| console.log('Server started with success on the port : ' + httpServer.address()['port']); | console.log('Server started with success on the port : ' + httpServer.address()['port']); | ||||
| }); | |||||
| }); | |||||
| // httpServer = server.startAsync(config.port); | |||||
| } | } | ||||
| } | } | ||||
| mountAndStart(); | mountAndStart(); | ||||
| */ | |||||
| let server: webdav.WebDAVServer; | |||||
| // @ts-ignore | |||||
| let httpd: http.Server; | |||||
| (async ()=>{ | |||||
| try { | |||||
| const config = getConfig('srvconfig'); | |||||
| const users = getConfig('users'); | |||||
| if (typeof config.port === undefined) | |||||
| config.port = 8080; | |||||
| if (typeof config.realm === undefined) | |||||
| config.realm = "WebDAV"; | |||||
| let mountNames : string[] = []; | |||||
| for (let mount in config['mounts']) { | |||||
| if (!!config['mounts'][mount] && fs.existsSync(config['mounts'][mount])) | |||||
| mountNames.push(mount); | |||||
| } | |||||
| console.log(config['mounts']); | |||||
| // User manager (tells who are the users) | |||||
| const userManager: webdav.SimpleUserManager = new webdav.SimpleUserManager(); | |||||
| const privilegeManager: webdav.SimplePathPrivilegeManager = new webdav.SimplePathPrivilegeManager(); | |||||
| for (let nuser = 0 ; nuser < users.length; nuser++) { | |||||
| let isAdmin = !!users[nuser]['isAdmin']; | |||||
| const user = userManager.addUser(users[nuser]['username'], users[nuser]['password'], isAdmin); | |||||
| if (isAdmin) { | |||||
| privilegeManager.setRights(user, '/', [ 'all' ]); | |||||
| } | |||||
| else { | |||||
| if (!!users[nuser]['access']) { | |||||
| for (let userMount in users[nuser]['access']) { | |||||
| if (mountNames.includes(userMount) && !!users[nuser]['access'][userMount]) { | |||||
| privilegeManager.setRights(user, '/' + userMount, users[nuser]['access'][userMount]); | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| console.log(user.username); | |||||
| } | |||||
| const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm); | |||||
| const serverOptions: webdav.WebDAVServerOptions = { | |||||
| port: config.port, | |||||
| requireAuthentification: true, | |||||
| httpAuthentication, | |||||
| privilegeManager, | |||||
| }; | |||||
| server = new webdav.WebDAVServer(serverOptions); | |||||
| server.beforeRequest((ctx: webdav.HTTPRequestContext, next) => { | |||||
| if (ctx.request.method === 'OPTIONS') { | |||||
| ctx.response.setHeader('DAV', '1,2'); | |||||
| ctx.response.setHeader('Access-Control-Allow-Origin', '*'); | |||||
| ctx.response.setHeader('Access-Control-Allow-Credentials', 'true'); | |||||
| ctx.response.setHeader( | |||||
| 'Access-Control-Allow-Headers', | |||||
| 'Authorization, Depth, Content-Type', | |||||
| ); | |||||
| ctx.response.setHeader( | |||||
| 'Access-Control-Allow-Methods', | |||||
| 'PROPPATCH,PROPFIND,OPTIONS,DELETE,UNLOCK,COPY,LOCK,MOVE,HEAD,POST,PUT,GET', | |||||
| ); | |||||
| ctx.response.setHeader( | |||||
| 'Access-Control-Expose-Headers', | |||||
| 'DAV, Content-Length, Allow', | |||||
| ); | |||||
| ctx.response.setHeader('MS-Author-Via', 'DAV'); | |||||
| ctx.setCode(200); | |||||
| ctx.exit(); | |||||
| } else { | |||||
| next(); | |||||
| } | |||||
| }); | |||||
| server.afterRequest((_arg, next) => { | |||||
| // Display the method, the URI, the returned status code and the returned message | |||||
| /* | |||||
| console.log( | |||||
| 'METHOD', _arg.request.method, | |||||
| 'URI', _arg.requested.uri, | |||||
| 'RESPONSE.StatusCode', _arg.response.statusCode, | |||||
| 'RESPONSE.StatusCode', _arg.response.statusMessage); | |||||
| // If available, display the body of the response | |||||
| console.log('RESPONSEBODY', _arg.responseBody); | |||||
| */ | |||||
| next(); | |||||
| }); | |||||
| for (let nm = 0; nm < mountNames.length ; nm++) { | |||||
| let fsEntry = mountNames[nm]; | |||||
| let realPath = config['mounts'][fsEntry]; | |||||
| // @ts-ignore | |||||
| let bOk = await server.setFileSystemAsync('/' + fsEntry, new webdav.PhysicalFileSystem(realPath)); | |||||
| console.log(`Mounted (${bOk}) /${fsEntry} -> ${realPath}`); | |||||
| } | |||||
| httpd = await server.startAsync(config.port); | |||||
| console.log("WebDAV server is listening on port " + config.port); | |||||
| } | |||||
| catch (err) { | |||||
| console.error(err); | |||||
| } | |||||
| })(); | |||||