-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·96 lines (82 loc) · 2.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
import $ from 'shelljs'
import open from 'open'
import { join } from 'path'
import { existsSync } from 'fs'
import { sample } from 'lodash-es'
import ProxyLists from 'proxy-lists'
import { readFile, appendFile } from 'fs/promises'
/**
* Get a list of proxies from proxy-lists
* @returns {Promise<string[]>} proxies
*/
const getProxies = () => new Promise((resolve, reject) => {
const proxyOptions = {
countries: ['us'],
protocols: ['http'],
ipTypes: ['ipv4'],
sourcesBlackList: ['bitproxies', 'kingproxies']
}
const proxList = []
ProxyLists.getProxies(proxyOptions)
.on('data', function (proxies) {
if (proxList.length > 50) return this.emit('end')
proxies.forEach(proxy => {
if (proxy.port !== 3128) {
proxList.push(`${proxy.ipAddress}:${proxy.port}`)
}
})
})
.on('error', function () {
if (proxList.length > 50) this.emit('end')
})
.on('end', function () {
if (proxList.length > 10) return resolve(proxList)
reject(new Error("Couldn't get proxies"))
})
})
const getSpotifyConfigPath = () => {
if (process.platform === 'win32') {
return join(process.env.APPDATA, 'spotify')
} else if (process.platform === 'darwin') {
return join(
process.env.HOME,
'Library',
'Application Support',
'Spotify'
)
} else {
return join(process.env.HOME, '.config', 'spotify')
}
}
async function main () {
console.log('Downloading proxy list...')
const proxies = await getProxies()
console.log('Downloaded proxy list')
const configDir = getSpotifyConfigPath()
const configFile = join(configDir, 'prefs')
if (!existsSync(configDir)) {
$.mkdir('-p', configDir)
$.touch(configFile)
}
const prefs = await readFile(configFile, 'utf8')
if (prefs.search('network.proxy.mode') > -1) {
$.sed('-i', /^network.proxy.mode.*$/, 'network.proxy.mode=2', configFile)
} else {
await appendFile(configFile, 'network.proxy.mode=2')
}
const selectProxy = sample(proxies)
if (prefs.search('network.proxy.addr') >= 0) {
$.sed(
'-i',
/^network.proxy.addr.*$/,
`network.proxy.addr="${selectProxy}@http"`,
configFile
)
} else {
await appendFile(configFile, `network.proxy.addr="${selectProxy}@http"`)
}
console.log('Launching spotify...')
return open.openApp('spotify')
}
main().then(process.exit)