From aeeb911f0f3964324a844b4b22651ec0370cd7b9 Mon Sep 17 00:00:00 2001 From: Preston Date: Fri, 15 Jan 2021 22:08:50 -0500 Subject: [PATCH] Fix network leak when using certain modules with the proxy enabled --- package-lock.json | 119 +++++++++++++++++- package.json | 3 + src/renderer/store/modules/ytdl.js | 187 +++++++++++++++++++++++++++-- 3 files changed, 298 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index f10e3912..608c6978 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2041,6 +2041,11 @@ "defer-to-connect": "^1.0.1" } }, + "@tootallnate/once": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", + "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==" + }, "@types/anymatch": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/@types/anymatch/-/anymatch-1.3.1.tgz", @@ -2848,6 +2853,29 @@ "pkcs7": "^1.0.4" } }, + "agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "requires": { + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -9236,6 +9264,31 @@ "requires-port": "^1.0.0" } }, + "http-proxy-agent": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz", + "integrity": "sha512-k0zdNgqWTGA6aeIRVpvfVob4fL52dTfaehylg0Y4UvSySvOq/Y+BOyPrgpUrA7HylqvU8vIZGsRuXmspskV0Tg==", + "requires": { + "@tootallnate/once": "1", + "agent-base": "6", + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "http-proxy-middleware": { "version": "0.19.1", "resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-0.19.1.tgz", @@ -9379,6 +9432,30 @@ "integrity": "sha1-7AbBDgo0wPL68Zn3/X/Hj//QPHM=", "dev": true }, + "https-proxy-agent": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz", + "integrity": "sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA==", + "requires": { + "agent-base": "6", + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "human-signals": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-1.1.1.tgz", @@ -9572,8 +9649,7 @@ "ip": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", - "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=", - "dev": true + "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=" }, "ip-regex": { "version": "2.1.0", @@ -15267,6 +15343,11 @@ "is-fullwidth-code-point": "^3.0.0" } }, + "smart-buffer": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.1.0.tgz", + "integrity": "sha512-iVICrxOzCynf/SNaBQCw34eM9jROU/s5rzIhpOvzhzuYHfJR/DhZfDkXiZSgKXfgv26HT3Yni3AV/DGw0cGnnw==" + }, "snapdragon": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz", @@ -15425,6 +15506,40 @@ } } }, + "socks": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.5.1.tgz", + "integrity": "sha512-oZCsJJxapULAYJaEYBSzMcz8m3jqgGrHaGhkmU/o/PQfFWYWxkAaA0UMGImb6s6tEXfKi959X6VJjMMQ3P6TTQ==", + "requires": { + "ip": "^1.1.5", + "smart-buffer": "^4.1.0" + } + }, + "socks-proxy-agent": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-5.0.0.tgz", + "integrity": "sha512-lEpa1zsWCChxiynk+lCycKuC502RxDWLKJZoIhnxrWNjLSDGYRFflHA1/228VkRcnv9TIb8w98derGbpKxJRgA==", + "requires": { + "agent-base": "6", + "debug": "4", + "socks": "^2.3.3" + }, + "dependencies": { + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "sort-keys": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/sort-keys/-/sort-keys-1.1.2.tgz", diff --git a/package.json b/package.json index 15ca1700..16303430 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,8 @@ "bulma-pro": "^0.2.0", "dateformat": "^4.4.1", "electron-context-menu": "^2.4.0", + "http-proxy-agent": "^4.0.1", + "https-proxy-agent": "^5.0.0", "jquery": "^3.5.1", "js-yaml": "^4.0.0", "lodash.debounce": "^4.0.8", @@ -27,6 +29,7 @@ "nedb": "^1.8.0", "opml-to-json": "1.0.1", "rss-parser": "^3.10.0", + "socks-proxy-agent": "^5.0.0", "video.js": "7.10.2", "videojs-abloop": "^1.2.0", "videojs-contrib-quality-levels": "^2.0.9", diff --git a/src/renderer/store/modules/ytdl.js b/src/renderer/store/modules/ytdl.js index 2ba6998c..647096c2 100644 --- a/src/renderer/store/modules/ytdl.js +++ b/src/renderer/store/modules/ytdl.js @@ -3,6 +3,9 @@ import ytsr from 'ytsr' import ytpl from 'ytpl' import IsEqual from 'lodash.isequal' +import { SocksProxyAgent } from 'socks-proxy-agent' +import { HttpsProxyAgent } from 'https-proxy-agent' +import { HttpProxyAgent } from 'http-proxy-agent' const state = { main: 0, @@ -38,6 +41,47 @@ const actions = { duration: '' } + let agent = null + const settings = rootState.settings + const useProxy = settings.useProxy + + if (useProxy) { + const proxyProtocol = settings.proxyProtocol + const proxyHostname = settings.proxyHostname + const proxyPort = settings.proxyPort + + switch (proxyProtocol) { + case 'http': + agent = new HttpProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'https': + agent = new HttpsProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'socks4': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 4 + }) + break + case 'socks5': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 5 + }) + break + } + + payload.options.requestOptions = { agent } + } + commit('toggleIsYtSearchRunning') if (!IsEqual(defaultFilters, rootState.utils.searchSettings)) { @@ -79,7 +123,51 @@ const actions = { }, async ytSearchGetFilters ({ rootState }, payload) { - let filter = await ytsr.getFilters(payload.query) + let options = null + let agent = null + const settings = rootState.settings + const useProxy = settings.useProxy + + if (useProxy) { + const proxyProtocol = settings.proxyProtocol + const proxyHostname = settings.proxyHostname + const proxyPort = settings.proxyPort + + switch (proxyProtocol) { + case 'http': + agent = new HttpProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'https': + agent = new HttpsProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'socks4': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 4 + }) + break + case 'socks5': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 5 + }) + break + } + + options = { + requestOptions: { agent } + } + } + + let filter = await ytsr.getFilters(payload.query, options) let filterUrl = null let searchSettings = payload.searchSettings @@ -104,7 +192,7 @@ const actions = { break } filterUrl = filter.get('Sort by').get(filterValue).url - filter = await ytsr.getFilters(filterUrl) + filter = await ytsr.getFilters(filterUrl, options) } console.log(`Current ref: ${filterUrl}`) @@ -118,7 +206,7 @@ const actions = { } filterUrl = filter.get('Duration').get(filterValue).url - filter = await ytsr.getFilters(filterUrl) + filter = await ytsr.getFilters(filterUrl, options) } console.log(`Current ref: ${filterUrl}`) @@ -145,7 +233,7 @@ const actions = { } filterUrl = filter.get('Upload date').get(filterValue).url - filter = await ytsr.getFilters(filterUrl) + filter = await ytsr.getFilters(filterUrl, options) } console.log(`Current ref: ${filterUrl}`) @@ -153,7 +241,7 @@ const actions = { if (searchSettings.type !== 'all') { const filterValue = searchSettings.type.charAt(0).toUpperCase() + searchSettings.type.slice(1) filterUrl = filter.get('Type').get(filterValue).url - filter = await ytsr.getFilters(filterUrl) + filter = await ytsr.getFilters(filterUrl, options) } console.log(`Current ref: ${filterUrl}`) @@ -163,11 +251,52 @@ const actions = { }) }, - ytGetPlaylistInfo (_, playlistId) { + ytGetPlaylistInfo ({ rootState }, playlistId) { return new Promise((resolve, reject) => { console.log(playlistId) console.log('Getting playlist info please wait...') - ytpl(playlistId, { limit: 'Infinity' }).then((result) => { + let agent = null + const settings = rootState.settings + const useProxy = settings.useProxy + + if (useProxy) { + const proxyProtocol = settings.proxyProtocol + const proxyHostname = settings.proxyHostname + const proxyPort = settings.proxyPort + + switch (proxyProtocol) { + case 'http': + agent = new HttpProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'https': + agent = new HttpsProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'socks4': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 4 + }) + break + case 'socks5': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 5 + }) + break + } + } + ytpl(playlistId, { + limit: 'Infinity', + requestOptions: { agent } + }).then((result) => { resolve(result) }).catch((err) => { reject(err) @@ -175,11 +304,51 @@ const actions = { }) }, - ytGetVideoInformation (_, videoId) { + ytGetVideoInformation ({ rootState }, videoId) { return new Promise((resolve, reject) => { console.log('Getting video info please wait...') + let agent = null + const settings = rootState.settings + const useProxy = settings.useProxy + + if (useProxy) { + const proxyProtocol = settings.proxyProtocol + const proxyHostname = settings.proxyHostname + const proxyPort = settings.proxyPort + + switch (proxyProtocol) { + case 'http': + agent = new HttpProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'https': + agent = new HttpsProxyAgent({ + host: proxyHostname, + port: proxyPort + }) + break + case 'socks4': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 4 + }) + break + case 'socks5': + agent = new SocksProxyAgent({ + host: proxyHostname, + port: proxyPort, + type: 5 + }) + break + } + } + ytdl.getInfo(videoId, { - lang: localStorage.getItem('locale') + lang: localStorage.getItem('locale'), + requestOptions: { agent } }).then((result) => { resolve(result) }).catch((err) => {