diff --git a/nas-webdav.service b/nas-webdav.service index 2010818..08f5a3a 100644 --- a/nas-webdav.service +++ b/nas-webdav.service @@ -4,6 +4,7 @@ Description=NAS WebDav-Server [Service] # EnvironmentFile=-/etc/default/my-application ExecStart=/home/michael/nas-webdav/start.sh +ExecStop=/home/michael/nas-webdav/stop.sh WorkingDirectory=/home/michael/nas-webdav LimitNOFILE=4096 IgnoreSIGPIPE=false diff --git a/package-lock.json b/package-lock.json index 6d83e6d..e5207c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -832,8 +832,7 @@ "lodash": { "version": "4.17.20", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", - "dev": true + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" }, "lru-cache": { "version": "6.0.0", @@ -882,6 +881,11 @@ "brace-expansion": "^1.1.7" } }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -1036,6 +1040,15 @@ "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", "dev": true }, + "simple-node-logger": { + "version": "18.12.24", + "resolved": "https://registry.npmjs.org/simple-node-logger/-/simple-node-logger-18.12.24.tgz", + "integrity": "sha512-4dTqpYecHsvPjWo+i+J3pLty8WJDNbxOVesNj5ch8pYH95LIGAFH4dxMSqyf+Os0RTchXifEtI/mfm3AVJftmg==", + "requires": { + "lodash": "^4.17.12", + "moment": "^2.20.1" + } + }, "slash": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", diff --git a/package.json b/package.json index af1104e..1b39d97 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "author": "Michael Manelis (manelis.de)", "license": "MIT", "dependencies": { + "simple-node-logger": "^18.12.24", "webdav-server": "^2.6.2" }, "devDependencies": { @@ -28,4 +29,4 @@ "prettier": "^2.1.2", "rimraf": "^3.0.2" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 64cfe69..26c6de9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { diff --git a/start.sh b/start.sh index 8075c58..076a610 100755 --- a/start.sh +++ b/start.sh @@ -1,3 +1,3 @@ #!/bin/bash source ../.nvm/nvm.sh -node dist/index.js \ No newline at end of file +node dist/index.js & \ No newline at end of file diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..b87020b --- /dev/null +++ b/stop.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +PIDFILE="" +PIDVAL="" + +pidfile() { + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + PIDFILE="$DIR/pid" + if [ ! -f $PIDFILE ]; then + return PIDFILE + else + return "" + fi +} + +pidval() { + pidfile + if [ ! -z $PIDFILE ]; then + PIDVAL =`pgrep -F "$PIDFILE"` + else + PIDVAL = "" + fi +} + +pidval +if [ ! -z "$PIDVAL" ]; then + kill -SIGTERM "$PIDVAL" + COUNTER=500 + while [ $COUNTER -gt 0 ]; do + pidval + if [ -z "$PIDVAL" ]; then + COUNTER=0 + else + sleep 0.1 + let ((--COUNTER)) + fi + done + if [ ! -z $PIDVAL ]; then + kill -SIGKILL "$PIDVAL" + sleep 1 + fi +fi +pidfile +if [ ! -z "$PIDFILE" ]; then + rm "$PIDFILE" +fi +