| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2018-12-18 20:34:24 +01:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | const url = require('url'); | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  | const syncOptions = require('./sync_options'); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
 | 
					
						
							|  |  |  | // this allows to support system proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function exec(opts) { | 
					
						
							| 
									
										
										
										
											2020-06-15 18:24:43 +02:00
										 |  |  |     const client = getClient(opts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  |     // hack for cases where electron.net does not work but we don't want to set proxy
 | 
					
						
							|  |  |  |     if (opts.proxy === 'noproxy') { | 
					
						
							|  |  |  |         opts.proxy = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const proxyAgent = getProxyAgent(opts); | 
					
						
							| 
									
										
										
										
											2018-12-17 22:54:54 +01:00
										 |  |  |     const parsedTargetUrl = url.parse(opts.url); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return new Promise(async (resolve, reject) => { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2018-12-17 22:12:26 +01:00
										 |  |  |             const headers = { | 
					
						
							|  |  |  |                 Cookie: (opts.cookieJar && opts.cookieJar.header) || "", | 
					
						
							|  |  |  |                 'Content-Type': 'application/json' | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (opts.auth) { | 
					
						
							| 
									
										
										
										
											2019-05-31 18:46:23 +02:00
										 |  |  |                 const token = Buffer.from(opts.auth.user + ":" + opts.auth.pass).toString('base64'); | 
					
						
							| 
									
										
										
										
											2018-12-17 22:12:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 headers['Authorization'] = `Basic ${token}`; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |             const request = client.request({ | 
					
						
							|  |  |  |                 method: opts.method, | 
					
						
							|  |  |  |                 // url is used by electron net module
 | 
					
						
							|  |  |  |                 url: opts.url, | 
					
						
							|  |  |  |                 // 4 fields below are used by http and https node modules
 | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  |                 protocol: parsedTargetUrl.protocol, | 
					
						
							|  |  |  |                 host: parsedTargetUrl.hostname, | 
					
						
							|  |  |  |                 port: parsedTargetUrl.port, | 
					
						
							|  |  |  |                 path: parsedTargetUrl.path, | 
					
						
							| 
									
										
										
										
											2020-06-13 10:23:36 +02:00
										 |  |  |                 timeout: opts.timeout, // works only for node.js client
 | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  |                 headers, | 
					
						
							|  |  |  |                 agent: proxyAgent | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 21:29:35 +01:00
										 |  |  |             request.on('error', err => reject(generateError(opts, err))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |             request.on('response', response => { | 
					
						
							| 
									
										
										
										
											2018-12-18 20:39:56 +01:00
										 |  |  |                 if (![200, 201, 204].includes(response.statusCode)) { | 
					
						
							|  |  |  |                     reject(generateError(opts, response.statusCode + ' ' + response.statusMessage)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |                 if (opts.cookieJar && response.headers['set-cookie']) { | 
					
						
							|  |  |  |                     opts.cookieJar.header = response.headers['set-cookie']; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 let responseStr = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 response.on('data', chunk => responseStr += chunk); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 response.on('end', () => { | 
					
						
							|  |  |  |                     try { | 
					
						
							|  |  |  |                         const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         resolve(jsonObj); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     catch (e) { | 
					
						
							|  |  |  |                         log.error("Failed to deserialize sync response: " + responseStr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 20:39:56 +01:00
										 |  |  |                         reject(generateError(opts, e.message)); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             request.end(opts.body ? JSON.stringify(opts.body) : undefined); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) { | 
					
						
							| 
									
										
										
										
											2018-12-18 20:39:56 +01:00
										 |  |  |             reject(generateError(opts, e.message)); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getImage(imageUrl) { | 
					
						
							|  |  |  |     const opts = { | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: imageUrl, | 
					
						
							|  |  |  |         proxy: await syncOptions.getSyncProxy() | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const client = getClient(opts); | 
					
						
							|  |  |  |     const proxyAgent = getProxyAgent(opts); | 
					
						
							|  |  |  |     const parsedTargetUrl = url.parse(opts.url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return await new Promise(async (resolve, reject) => { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             const request = client.request({ | 
					
						
							|  |  |  |                 method: opts.method, | 
					
						
							|  |  |  |                 // url is used by electron net module
 | 
					
						
							|  |  |  |                 url: opts.url, | 
					
						
							|  |  |  |                 // 4 fields below are used by http and https node modules
 | 
					
						
							|  |  |  |                 protocol: parsedTargetUrl.protocol, | 
					
						
							|  |  |  |                 host: parsedTargetUrl.hostname, | 
					
						
							|  |  |  |                 port: parsedTargetUrl.port, | 
					
						
							|  |  |  |                 path: parsedTargetUrl.path, | 
					
						
							| 
									
										
										
										
											2020-06-13 10:23:36 +02:00
										 |  |  |                 timeout: opts.timeout, // works only for node client
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |                 headers: {}, | 
					
						
							|  |  |  |                 agent: proxyAgent | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             request.on('error', err => reject(generateError(opts, err))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 18:24:43 +02:00
										 |  |  |             request.on('abort', err => reject(generateError(opts, err))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |             request.on('response', response => { | 
					
						
							|  |  |  |                 if (![200, 201, 204].includes(response.statusCode)) { | 
					
						
							|  |  |  |                     reject(generateError(opts, response.statusCode + ' ' + response.statusMessage)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const chunks = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 response.on('data', chunk => chunks.push(chunk)); | 
					
						
							|  |  |  |                 response.on('end', () => resolve(Buffer.concat(chunks))); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             request.end(undefined); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) { | 
					
						
							|  |  |  |             reject(generateError(opts, e.message)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  | function getProxyAgent(opts) { | 
					
						
							|  |  |  |     if (!opts.proxy) { | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const {protocol} = url.parse(opts.url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (protocol === 'http:' || protocol === 'https:') { | 
					
						
							|  |  |  |         const protoNoColon = protocol.substr(0, protocol.length - 1); | 
					
						
							|  |  |  |         const AgentClass = require(protoNoColon + '-proxy-agent'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new AgentClass(opts.proxy); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | function getClient(opts) { | 
					
						
							| 
									
										
										
										
											2018-12-17 22:54:54 +01:00
										 |  |  |     // it's not clear how to explicitly configure proxy (as opposed to system proxy)
 | 
					
						
							|  |  |  |     // so in that case we always use node's modules
 | 
					
						
							|  |  |  |     if (utils.isElectron() && !opts.proxy) { | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  |         return require('electron').net; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2019-07-24 20:47:41 +02:00
										 |  |  |         const {protocol} = url.parse(opts.url); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (protocol === 'http:' || protocol === 'https:') { | 
					
						
							|  |  |  |             return require(protocol.substr(0, protocol.length - 1)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             throw new Error(`Unrecognized protocol "${protocol}"`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 20:39:56 +01:00
										 |  |  | function generateError(opts, message) { | 
					
						
							|  |  |  |     return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${message}`); | 
					
						
							| 
									
										
										
										
											2018-12-17 21:34:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |     exec, | 
					
						
							|  |  |  |     getImage | 
					
						
							| 
									
										
										
										
											2020-06-13 10:23:36 +02:00
										 |  |  | }; |