|
|
|
@@ -1,6 +1,10 @@ |
|
|
|
import * as fs from 'fs'; |
|
|
|
import * as path from 'path'; |
|
|
|
import * as http from 'http'; |
|
|
|
import { v2 as webdav } from 'webdav-server'; |
|
|
|
import * as simpleLogger from 'simple-node-logger'; |
|
|
|
|
|
|
|
// const simpleLogger = require('simple-node-logger'); |
|
|
|
|
|
|
|
function getConfig(name: string) : any { |
|
|
|
let config = {}; |
|
|
|
@@ -23,12 +27,22 @@ let server: webdav.WebDAVServer; |
|
|
|
// @ts-ignore |
|
|
|
let httpd: http.Server; |
|
|
|
|
|
|
|
let log; |
|
|
|
|
|
|
|
(async ()=>{ |
|
|
|
let PIDFILE = ',,/pid'; |
|
|
|
let PIDFILE = path.normalize(path.resolve(__dirname, '../logs/pid')); |
|
|
|
try { |
|
|
|
const config = getConfig('srvconfig'); |
|
|
|
const users = getConfig('users'); |
|
|
|
|
|
|
|
if (config.logOpts === undefined) |
|
|
|
log = console; |
|
|
|
else { |
|
|
|
if (process.stdout.isTTY) |
|
|
|
log = simpleLogger.createSimpleLogger(config.logOpts); |
|
|
|
else |
|
|
|
log = simpleLogger.createSimpleFileLogger(config.logOpts); |
|
|
|
} |
|
|
|
log.debug(PIDFILE); |
|
|
|
if (typeof config.port === undefined) |
|
|
|
config.port = 8080; |
|
|
|
if (typeof config.realm === undefined) |
|
|
|
@@ -40,7 +54,7 @@ let httpd: http.Server; |
|
|
|
mountNames.push(mount); |
|
|
|
} |
|
|
|
|
|
|
|
console.log(config['mounts']); |
|
|
|
log.info(config['mounts']); |
|
|
|
|
|
|
|
// User manager (tells who are the users) |
|
|
|
const userManager: webdav.SimpleUserManager = new webdav.SimpleUserManager(); |
|
|
|
@@ -60,7 +74,7 @@ let httpd: http.Server; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
console.log(user.username); |
|
|
|
log.info(user.username); |
|
|
|
} |
|
|
|
|
|
|
|
const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager, config.realm); |
|
|
|
@@ -74,7 +88,7 @@ let httpd: http.Server; |
|
|
|
|
|
|
|
server = new webdav.WebDAVServer(serverOptions); |
|
|
|
server.beforeRequest((ctx: webdav.HTTPRequestContext, next) => { |
|
|
|
console.log('beforeRequest', ctx.request.method); |
|
|
|
log.debug('beforeRequest: ', ctx.request.method, ' Headers recv:', ctx.request.rawHeaders); |
|
|
|
if (ctx.request.method === 'OPTIONS') { |
|
|
|
ctx.response.setHeader('DAV', '1,2'); |
|
|
|
ctx.response.setHeader('Access-Control-Allow-Origin', '*'); |
|
|
|
@@ -108,7 +122,6 @@ let httpd: http.Server; |
|
|
|
} |
|
|
|
}); |
|
|
|
server.afterRequest((_ctx: webdav.HTTPRequestContext, next) => { |
|
|
|
console.log('After request'); |
|
|
|
// Display the method, the URI, the returned status code and the returned message |
|
|
|
/* |
|
|
|
console.log( |
|
|
|
@@ -119,7 +132,7 @@ let httpd: http.Server; |
|
|
|
// If available, display the body of the response |
|
|
|
console.log('RESPONSEBODY', _arg.responseBody); |
|
|
|
*/ |
|
|
|
console.log('Headers',_ctx.response.getHeaders(), 'Headers sent', _ctx.response.headersSent); |
|
|
|
log.debug(`After request: Status code: ${_ctx.response.statusCode} Headers sent: (${_ctx.response.headersSent})`,_ctx.response.getHeaders()); |
|
|
|
next(); |
|
|
|
}); |
|
|
|
|
|
|
|
@@ -128,10 +141,10 @@ let httpd: http.Server; |
|
|
|
let realPath = config['mounts'][fsEntry]; |
|
|
|
// @ts-ignore |
|
|
|
let bOk = await server.setFileSystemAsync('/' + fsEntry, new webdav.PhysicalFileSystem(realPath)); |
|
|
|
console.log(`Mounted (${bOk}) /${fsEntry} -> ${realPath}`); |
|
|
|
log.info(`Mounted (${bOk}) /${fsEntry} -> ${realPath}`); |
|
|
|
} |
|
|
|
let onexit = async () => { |
|
|
|
console.log("Terminated"); |
|
|
|
log.info("Terminated"); |
|
|
|
await server.stopAsync(); |
|
|
|
if (fs.existsSync(PIDFILE)) |
|
|
|
fs.unlinkSync(PIDFILE); |
|
|
|
@@ -141,10 +154,11 @@ let httpd: http.Server; |
|
|
|
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); |
|
|
|
log.info("WebDAV server is listening on port " + config.port); |
|
|
|
} |
|
|
|
catch (err) { |
|
|
|
console.error(err); |
|
|
|
log.error(err); |
|
|
|
process.exit(1); |
|
|
|
} |
|
|
|
})(); |
|
|
|
|