├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # synology-diskstation 2 | 3 | Node module for turning on and off a Synology DiskStation. Tested with a DS214play but is expected to work with all DiskStations. 4 | 5 | ## Installation 6 | 7 | Install using `npm install --save git+ssh://git@github.com/simonbs/synology-diskstation.git` 8 | 9 | ## Usage 10 | 11 | Create an instance of Synology by passing the IP address and port of your DiskStation as arguments. 12 | 13 | ```javascript 14 | var synology = new Synology('192.168.1.1', 5000) 15 | ``` 16 | 17 | ### Turn on 18 | 19 | The DiskStation can be turned on by sending a Wake on Lan request. The MAC address of the DiskStation is required in order to send a WOL request. The MAC address can be found in the [DS finder iOS application](https://itunes.apple.com/us/app/ds-finder/id429865523?mt=8e) and the [DS finder Android application](https://play.google.com/store/apps/details?id=com.synology.DSfinder). 20 | 21 | ```javascript 22 | synology.wol('00:11:22:AA:BB:CC', function(err) { 23 | console.log(err) 24 | }) 25 | ``` 26 | 27 | ### Shutdown 28 | 29 | You must be authorized with the DiskStation in order to shut it down. Pass your account name and password as arguments. On a successfull login, you'll obtain a `sid`, which is used to shutdown the DiskStation. 30 | 31 | ```javascript 32 | synology.login('myaccountname', 'mypassword', function(err, sid) { 33 | if (err) { 34 | console.log(err) 35 | return 36 | } 37 | synology.shutdown(sid, function(err) { 38 | console.log(err) 39 | }) 40 | }) 41 | ``` 42 | 43 | ### Checking if the DiskStation Is On 44 | 45 | Invoke the `isOn` function to check if the DiskStation is turned on. 46 | 47 | ```javascript 48 | synology.isOn(function(err, isOn) { 49 | console.log(isOn) 50 | }) 51 | ``` 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var http = require('http') 2 | var wol = require('wake_on_lan') 3 | 4 | function Synology(ipAddress, port) { 5 | this.ipAddress = ipAddress 6 | this.port = port 7 | } 8 | 9 | Synology.prototype.request = function(options, callback) { 10 | var method = options.method || 'GET' 11 | var headers = options.headers || {} 12 | var body = options.body || null 13 | if (method == 'POST') { 14 | headers['Content-Type'] = 'application/x-www-form-urlencoded' 15 | if (body != null) { 16 | headers['Content-Length'] = Buffer.byteLength(options.body) 17 | } 18 | } 19 | var reqOptions = { 20 | host: this.ipAddress, 21 | port: this.port, 22 | path: options.path, 23 | method: method, 24 | headers: headers 25 | } 26 | var req = http.request(reqOptions, function(res) { 27 | if (res.statusCode != 200) { 28 | return callback({ 29 | type: 'http', 30 | code: res.statusCode 31 | }) 32 | } 33 | res.setEncoding('utf8') 34 | res.on('data', function(data) { 35 | var json = JSON.parse(data) 36 | if (json.error != null) { 37 | return callback({ 38 | type: 'api', 39 | code: json.error.code 40 | }) 41 | } 42 | callback(null, json) 43 | }) 44 | }) 45 | req.on('error', function(err) { 46 | callback({ 47 | type: 'network', 48 | code: -1, 49 | message: err.message 50 | }) 51 | }) 52 | if (body != null) { 53 | req.write(body) 54 | } 55 | req.end() 56 | } 57 | 58 | Synology.prototype.login = function(account, password, callback) { 59 | var options = { 60 | path: '/webapi/auth.cgi?api=SYNO.API.Auth&version=7&method=login&account='+account+'&passwd='+password+'&format=cookie' 61 | } 62 | this.request(options, function(err, body) { 63 | if (err) { 64 | return callback(err) 65 | } 66 | callback(null, body.data.sid) 67 | }) 68 | } 69 | 70 | Synology.prototype.shutdown = function(sid, callback) { 71 | var options = { 72 | path: '/webapi/entry.cgi', 73 | method: 'POST', 74 | headers: { 75 | 'Cookie': 'id='+sid 76 | }, 77 | body: 'api=SYNO.Core.System&method=shutdown&version=1' 78 | } 79 | this.request(options, function(err, body) { 80 | if (err) { 81 | return callback(err) 82 | } 83 | callback(null) 84 | }) 85 | } 86 | 87 | Synology.prototype.wol = function(macAddress, callback) { 88 | wol.wake(macAddress, callback) 89 | } 90 | 91 | Synology.prototype.isOn = function(callback) { 92 | var options = { 93 | path: '/webman/pingpong.cgi' 94 | } 95 | this.request(options, function(err, body) { 96 | if (err) { 97 | return callback(err, false) 98 | } 99 | callback(null, body['boot_done']) 100 | }) 101 | } 102 | 103 | module.exports = Synology 104 | 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "synology-diskstation", 3 | "version": "1.0.0", 4 | "description": "Turn on and off a Synology DiskStation.", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "repository": { 8 | "type": "git", 9 | "url": "git+https://github.com/simonbs/synology-diskstation.git" 10 | }, 11 | "keywords": [ 12 | "synology", 13 | "diskstation", 14 | "home", 15 | "automation", 16 | "disk", 17 | "nas", 18 | "station" 19 | ], 20 | "author": "Simon B. Støvring", 21 | "license": "ISC", 22 | "dependencies": { 23 | "wake_on_lan": "0.0.4" 24 | } 25 | } 26 | --------------------------------------------------------------------------------