mirror of https://github.com/FreeTubeApp/FreeTube
Reduce duplicate code by introducing a function to create a proxy agent (#2845)
This commit is contained in:
parent
a9a29f2852
commit
46bbcafbbc
|
@ -15,6 +15,33 @@ const state = {
|
|||
|
||||
const getters = {}
|
||||
|
||||
function createProxyAgent(protocol, hostname, port) {
|
||||
switch (protocol) {
|
||||
case 'http':
|
||||
return new HttpProxyAgent({
|
||||
host: hostname,
|
||||
port: port
|
||||
})
|
||||
case 'https':
|
||||
return new HttpsProxyAgent({
|
||||
host: hostname,
|
||||
port: port
|
||||
})
|
||||
case 'socks4':
|
||||
return new SocksProxyAgent({
|
||||
hostname: hostname,
|
||||
port: port,
|
||||
type: 4
|
||||
})
|
||||
case 'socks5':
|
||||
return new SocksProxyAgent({
|
||||
hostname: hostname,
|
||||
port: port,
|
||||
type: 5
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const actions = {
|
||||
ytSearch ({ commit, dispatch, rootState }, payload) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@ -36,43 +63,10 @@ const actions = {
|
|||
duration: ''
|
||||
}
|
||||
|
||||
let agent = {}
|
||||
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({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 4
|
||||
})
|
||||
break
|
||||
case 'socks5':
|
||||
agent = new SocksProxyAgent({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 5
|
||||
})
|
||||
break
|
||||
}
|
||||
if (settings.useProxy) {
|
||||
const agent = createProxyAgent(settings.proxyProtocol, settings.proxyHostname, settings.proxyPort)
|
||||
|
||||
payload.options.requestOptions = { agent }
|
||||
}
|
||||
|
@ -117,41 +111,9 @@ const actions = {
|
|||
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({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 4
|
||||
})
|
||||
break
|
||||
case 'socks5':
|
||||
agent = new SocksProxyAgent({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 5
|
||||
})
|
||||
break
|
||||
}
|
||||
if (settings.useProxy) {
|
||||
agent = createProxyAgent(settings.proxyProtocol, settings.proxyHostname, settings.proxyPort)
|
||||
}
|
||||
|
||||
options = {
|
||||
|
@ -235,41 +197,9 @@ const actions = {
|
|||
return new Promise((resolve, reject) => {
|
||||
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({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 4
|
||||
})
|
||||
break
|
||||
case 'socks5':
|
||||
agent = new SocksProxyAgent({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 5
|
||||
})
|
||||
break
|
||||
}
|
||||
if (settings.useProxy) {
|
||||
agent = createProxyAgent(settings.proxyProtocol, settings.proxyHostname, settings.proxyPort)
|
||||
}
|
||||
let locale = i18n.locale.replace('_', '-')
|
||||
|
||||
|
@ -293,41 +223,9 @@ const actions = {
|
|||
return new Promise((resolve, reject) => {
|
||||
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({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 4
|
||||
})
|
||||
break
|
||||
case 'socks5':
|
||||
agent = new SocksProxyAgent({
|
||||
hostname: proxyHostname,
|
||||
port: proxyPort,
|
||||
type: 5
|
||||
})
|
||||
break
|
||||
}
|
||||
if (settings.useProxy) {
|
||||
agent = createProxyAgent(settings.proxyProtocol, settings.proxyHostname, settings.proxyPort)
|
||||
}
|
||||
|
||||
ytdl.getInfo(videoId, {
|
||||
|
|
Loading…
Reference in New Issue