优化网络请求,尝试去解决无法连接服务器的问题

pull/590/head
lyswhut 2021-06-16 10:23:48 +08:00
parent 66631ac105
commit 198238d4b5
13 changed files with 48 additions and 22 deletions

17
package-lock.json generated
View File

@ -3739,7 +3739,8 @@
"asap": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
"integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY="
"integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=",
"dev": true
},
"asar": {
"version": "3.0.3",
@ -6663,15 +6664,6 @@
"buffer-indexof": "^1.0.0"
}
},
"dnscache": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/dnscache/-/dnscache-1.0.2.tgz",
"integrity": "sha512-2FFKzmLGOnD+Y378bRKH+gTjRMuSpH7OKgPy31KjjfCoKZx7tU8Dmqfd/3fhG2d/4bppuN8/KtWMUZBAcUCRnQ==",
"requires": {
"asap": "^2.0.6",
"lodash.clone": "^4.5.0"
}
},
"doctrine": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz",
@ -10361,11 +10353,6 @@
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
},
"lodash.clone": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clone/-/lodash.clone-4.5.0.tgz",
"integrity": "sha1-GVhwRQ9aExkkeN9Lw9I9LeoZB7Y="
},
"lodash.clonedeep": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",

View File

@ -228,7 +228,6 @@
},
"dependencies": {
"crypto-js": "^4.0.0",
"dnscache": "^1.0.2",
"electron-log": "^4.3.5",
"electron-store": "^8.0.0",
"electron-updater": "^4.3.9",

View File

@ -3,6 +3,10 @@
- 添加 win arm64 架构的安装包构建
- 新增“添加歌曲到列表时的位置”设置,可选项为列表的“顶部”与“底部”
### 优化
- 优化网络请求,尝试去解决无法连接服务器的问题
### 变更
- 添加歌曲到列表时从原来的底部改为顶部,若想要恢复原来的行为则可以去更改“添加歌曲到列表时的位置”设置项

View File

@ -20,7 +20,6 @@
</template>
<script>
import dnscache from 'dnscache'
import { mapMutations, mapGetters, mapActions } from 'vuex'
import { rendererOn, rendererSend, rendererInvoke, NAMES } from '../common/ipc'
import { isLinux } from '../common/utils'
@ -30,11 +29,6 @@ import { base as eventBaseName } from './event/names'
import apiSourceInfo from './utils/music/api-source-info'
window.ELECTRON_DISABLE_SECURITY_WARNINGS = process.env.ELECTRON_DISABLE_SECURITY_WARNINGS
dnscache({
enable: true,
ttl: 21600,
cachesize: 1000,
})
const listThrottle = (fn, delay = 100) => {
let timer = null

View File

@ -1,13 +1,16 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_test = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/bd/${songInfo.songmid}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,13 +1,16 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_test = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/kg/${songInfo._types[type].hash}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,12 +1,15 @@
import { httpFetch } from '../../request'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_temp = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('tm.tempmusic.tk')
const requestObj = httpFetch(`http://tm.tempmusic.tk/url/kw/${songInfo.songmid}/${type}`, {
method: 'get',
headers,
timeout,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,6 +1,7 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_test = {
// getMusicUrl(songInfo, type) {
@ -15,10 +16,12 @@ const api_test = {
// return requestObj
// },
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/kw/${songInfo.songmid}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,13 +1,16 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_test = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/mg/${songInfo.copyrightId}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,13 +1,16 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_messoer = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/tx/${songInfo.songmid}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -1,4 +1,6 @@
import crypto from 'crypto'
import dns from 'dns'
/**
* 获取音乐音质
@ -19,3 +21,21 @@ export const getMusicType = (info, type) => {
}
export const toMD5 = str => crypto.createHash('md5').update(str).digest('hex')
const ipMap = new Map()
export const getHostIp = hostname => {
const ip = ipMap.get(hostname)
if (typeof ip === 'string') return ip
if (ip === true) return
ipMap.set(hostname, true)
// console.log(hostname)
dns.lookup(hostname, {
// family: 4,
all: false,
}, (err, address, family) => {
if (err) return console.log(err)
// console.log(address, family)
ipMap.set(hostname, address)
})
}

View File

@ -1,13 +1,16 @@
import { httpFetch } from '../../request'
import { requestMsg } from '../../message'
import { headers, timeout } from '../options'
import { getHostIp } from '../utils'
const api_test = {
getMusicUrl(songInfo, type) {
const ip = getHostIp('ts.tempmusic.tk')
const requestObj = httpFetch(`http://ts.tempmusic.tk/url/wy/${songInfo.songmid}/${type}`, {
method: 'get',
timeout,
headers,
host: ip,
family: 4,
})
requestObj.promise = requestObj.promise.then(({ body }) => {

View File

@ -21,6 +21,7 @@ const request = (url, options, callback) => {
options.json = false
}
options.response_timeout = options.timeout
console.log(options)
return needle.request(options.method || 'get', url, data, options, (err, resp, body) => {
if (!err) {
body = resp.body = resp.raw.toString()