|
|
|
@@ -18,98 +18,13 @@ function getConfig(name: string) : any { |
|
|
|
return config; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
// 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); |
|
|
|
} |
|
|
|
|
|
|
|
const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm); |
|
|
|
|
|
|
|
const serverOptions: webdav.WebDAVServerOptions = { |
|
|
|
port: config.port, |
|
|
|
requireAuthentification: true, |
|
|
|
httpAuthentication, |
|
|
|
privilegeManager, |
|
|
|
}; |
|
|
|
//serverOptions['userManager'] = userManager; |
|
|
|
|
|
|
|
const server = new webdav.WebDAVServer(serverOptions); |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let nMount = 0; |
|
|
|
|
|
|
|
//@ts-ignore |
|
|
|
function mountAndStart() : void { |
|
|
|
if (nMount < mountNames.length) { |
|
|
|
let fsEntry = mountNames[nMount]; |
|
|
|
let realPath = config['mounts'][fsEntry]; |
|
|
|
nMount++; |
|
|
|
console.log(`FileSystem ${fsEntry} -> ${realPath}`) |
|
|
|
server.setFileSystem( |
|
|
|
'/' + fsEntry, new webdav.PhysicalFileSystem(realPath), (_success) => {mountAndStart();}); |
|
|
|
} |
|
|
|
else { |
|
|
|
server.start(httpServer => { |
|
|
|
console.log('Server started with success on the port : ' + httpServer.address()['port']); |
|
|
|
}); |
|
|
|
// httpServer = server.startAsync(config.port); |
|
|
|
} |
|
|
|
} |
|
|
|
mountAndStart(); |
|
|
|
*/ |
|
|
|
|
|
|
|
let server: webdav.WebDAVServer; |
|
|
|
// @ts-ignore |
|
|
|
let httpd: http.Server; |
|
|
|
|
|
|
|
(async ()=>{ |
|
|
|
let PIDFILE = ',,/pid'; |
|
|
|
try { |
|
|
|
const config = getConfig('srvconfig'); |
|
|
|
const users = getConfig('users'); |
|
|
|
@@ -177,10 +92,12 @@ let httpd: http.Server; |
|
|
|
'Allow', |
|
|
|
'PROPPATCH,PROPFIND,OPTIONS,DELETE,UNLOCK,COPY,LOCK,MOVE,HEAD,POST,PUT,GET', |
|
|
|
); |
|
|
|
|
|
|
|
ctx.response.removeHeader('Allow'); |
|
|
|
|
|
|
|
ctx.response.setHeader( |
|
|
|
'Access-Control-Expose-Headers', |
|
|
|
'DAV, Content-Length, Allow', |
|
|
|
'DAV, Content-Length, Allow, Last-Modified', |
|
|
|
); |
|
|
|
ctx.response.setHeader('MS-Author-Via', 'DAV'); |
|
|
|
ctx.setCode(200); |
|
|
|
@@ -216,11 +133,14 @@ let httpd: http.Server; |
|
|
|
let onexit = async () => { |
|
|
|
console.log("Terminated"); |
|
|
|
await server.stopAsync(); |
|
|
|
if (fs.existsSync(PIDFILE)) |
|
|
|
fs.unlinkSync(PIDFILE); |
|
|
|
process.exit(0); |
|
|
|
} |
|
|
|
process.on('SIGTERM', onexit); |
|
|
|
process.on('SIGINT', onexit); |
|
|
|
httpd = await server.startAsync(config.port); |
|
|
|
fs.writeFileSync(PIDFILE, '' + process.pid); |
|
|
|
console.log("WebDAV server is listening on port " + config.port); |
|
|
|
} |
|
|
|
catch (err) { |
|
|
|
|