| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 17:00:28 +00:00
										 |  |  | var PromiseA = require('bluebird').Promise; | 
					
						
							|  |  |  | var https = require('https'); | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  | var fs = PromiseA.promisifyAll(require('fs')); | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports.update = function (opts) { | 
					
						
							|  |  |  |   return new PromiseA(function (resolve, reject) { | 
					
						
							| 
									
										
										
										
											2015-08-14 17:00:28 +00:00
										 |  |  |     var options; | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |     var hostname = opts.hostname || opts.updater; | 
					
						
							|  |  |  |     var port = opts.port; | 
					
						
							|  |  |  |     var pathname = opts.pathname; | 
					
						
							| 
									
										
										
										
											2015-10-05 19:51:58 +00:00
										 |  |  |     var req; | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |     if (!hostname) { | 
					
						
							|  |  |  |       throw new Error('Please specify a DDNS host as opts.hostname'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!pathname) { | 
					
						
							|  |  |  |       throw new Error('Please specify the api route as opts.pathname'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  |     options = { | 
					
						
							|  |  |  |       host: hostname | 
					
						
							|  |  |  |     , port: port | 
					
						
							|  |  |  |     , method: 'POST' | 
					
						
							|  |  |  |     , headers: { | 
					
						
							|  |  |  |         'Content-Type': 'application/json' | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |     , path: pathname | 
					
						
							| 
									
										
										
										
											2015-08-14 21:09:32 +00:00
										 |  |  |     //, auth: opts.auth || 'admin:secret'
 | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-08-14 17:00:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 21:09:32 +00:00
										 |  |  |     if (opts.cacert) { | 
					
						
							|  |  |  |       if (!Array.isArray(opts.cacert)) { | 
					
						
							|  |  |  |         opts.cacert = [opts.cacert]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       options.ca = opts.cacert; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-21 12:37:04 +00:00
										 |  |  |     options.ca = (options.ca||[]).map(function (str) { | 
					
						
							| 
									
										
										
										
											2015-08-14 21:09:32 +00:00
										 |  |  |       if ('string' === typeof str && str.length < 1000) { | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |         str = fs.readFileAsync(str); | 
					
						
							| 
									
										
										
										
											2015-08-14 21:09:32 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       return str; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (opts.token || opts.jwt) { | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |       options.headers.Authorization = 'Bearer ' + (opts.token || opts.jwt); | 
					
						
							| 
									
										
										
										
											2015-08-14 21:09:32 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (false === opts.cacert) { | 
					
						
							|  |  |  |       options.rejectUnauthorized = false; | 
					
						
							| 
									
										
										
										
											2015-08-14 17:00:28 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |     return PromiseA.all(options.ca).then(function (cas) { | 
					
						
							|  |  |  |       options.ca = cas; | 
					
						
							|  |  |  |       options.agent = new https.Agent(options); | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |       req = https.request(options, function(res) { | 
					
						
							|  |  |  |         var textData = ''; | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |         res.on('error', function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-05 19:51:58 +00:00
										 |  |  |           reject(err); | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |         }); | 
					
						
							|  |  |  |         res.on('data', function (chunk) { | 
					
						
							|  |  |  |           textData += chunk.toString(); | 
					
						
							|  |  |  |           // console.log(chunk.toString());
 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         res.on('end', function () { | 
					
						
							|  |  |  |           var err; | 
					
						
							|  |  |  |           try { | 
					
						
							|  |  |  |             resolve(JSON.parse(textData)); | 
					
						
							|  |  |  |           } catch(e) { | 
					
						
							|  |  |  |             err = new Error("Unparsable Server Response"); | 
					
						
							|  |  |  |             err.code = 'E_INVALID_SERVER_RESPONSE'; | 
					
						
							|  |  |  |             err.data = textData; | 
					
						
							|  |  |  |             reject(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-05 19:51:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |       req.on('error', function (err) { | 
					
						
							|  |  |  |         reject(err); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-05 19:51:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 22:24:18 +00:00
										 |  |  |       req.end(JSON.stringify(opts.ddns, null, '  ')); | 
					
						
							|  |  |  |     }, reject); | 
					
						
							| 
									
										
										
										
											2015-02-12 09:40:37 +00:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; |