From 507f66b3b6e7b48b30e10a172a589ccec7d77307 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Fri, 18 Jun 2021 15:13:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Ddns=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/utils/music/bd/api-test.js | 5 ++--- src/renderer/utils/music/kg/api-test.js | 5 ++--- src/renderer/utils/music/kw/api-temp.js | 5 ++--- src/renderer/utils/music/kw/api-test.js | 5 ++--- src/renderer/utils/music/mg/api-test.js | 5 ++--- src/renderer/utils/music/tx/api-test.js | 5 ++--- src/renderer/utils/music/utils.js | 15 +++++++++++---- src/renderer/utils/music/wy/api-test.js | 5 ++--- src/renderer/utils/request.js | 3 ++- 9 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/renderer/utils/music/bd/api-test.js b/src/renderer/utils/music/bd/api-test.js index b6818d83..6c19ab2a 100644 --- a/src/renderer/utils/music/bd/api-test.js +++ b/src/renderer/utils/music/bd/api-test.js @@ -1,16 +1,15 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/kg/api-test.js b/src/renderer/utils/music/kg/api-test.js index 85f7043c..aa9f6651 100644 --- a/src/renderer/utils/music/kg/api-test.js +++ b/src/renderer/utils/music/kg/api-test.js @@ -1,16 +1,15 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/kw/api-temp.js b/src/renderer/utils/music/kw/api-temp.js index d13295bb..2e37abc7 100644 --- a/src/renderer/utils/music/kw/api-temp.js +++ b/src/renderer/utils/music/kw/api-temp.js @@ -1,15 +1,14 @@ import { httpFetch } from '../../request' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/kw/api-test.js b/src/renderer/utils/music/kw/api-test.js index ce7732b4..03433ddf 100644 --- a/src/renderer/utils/music/kw/api-test.js +++ b/src/renderer/utils/music/kw/api-test.js @@ -1,7 +1,7 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } from '../utils' const api_test = { // getMusicUrl(songInfo, type) { @@ -16,12 +16,11 @@ 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/mg/api-test.js b/src/renderer/utils/music/mg/api-test.js index 41bf5e69..860b53ec 100644 --- a/src/renderer/utils/music/mg/api-test.js +++ b/src/renderer/utils/music/mg/api-test.js @@ -1,16 +1,15 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/tx/api-test.js b/src/renderer/utils/music/tx/api-test.js index d998489e..a930b1ff 100644 --- a/src/renderer/utils/music/tx/api-test.js +++ b/src/renderer/utils/music/tx/api-test.js @@ -1,16 +1,15 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/music/utils.js b/src/renderer/utils/music/utils.js index a7f63b2d..3092df86 100644 --- a/src/renderer/utils/music/utils.js +++ b/src/renderer/utils/music/utils.js @@ -25,9 +25,9 @@ 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 + const result = ipMap.get(hostname) + if (typeof result === 'object') return result + if (result === true) return ipMap.set(hostname, true) // console.log(hostname) dns.lookup(hostname, { @@ -36,6 +36,13 @@ export const getHostIp = hostname => { }, (err, address, family) => { if (err) return console.log(err) // console.log(address, family) - ipMap.set(hostname, address) + ipMap.set(hostname, { address, family }) }) } + +export const dnsLookup = (hostname, options, callback) => { + const result = getHostIp(hostname) + if (result) return callback(null, result.address, result.family) + + dns.lookup(hostname, options, callback) +} diff --git a/src/renderer/utils/music/wy/api-test.js b/src/renderer/utils/music/wy/api-test.js index b48df637..6ce34b3f 100644 --- a/src/renderer/utils/music/wy/api-test.js +++ b/src/renderer/utils/music/wy/api-test.js @@ -1,16 +1,15 @@ import { httpFetch } from '../../request' import { requestMsg } from '../../message' import { headers, timeout } from '../options' -import { getHostIp } from '../utils' +import { dnsLookup } 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, + lookup: dnsLookup, family: 4, }) requestObj.promise = requestObj.promise.then(({ body }) => { diff --git a/src/renderer/utils/request.js b/src/renderer/utils/request.js index ebe65ae6..81733dae 100644 --- a/src/renderer/utils/request.js +++ b/src/renderer/utils/request.js @@ -263,12 +263,13 @@ const fetchData = async(url, method, { console.log('---start---', url) headers = Object.assign({}, headers) if (headers[bHh]) { + const path = url.replace(/^https?:\/\/[\w.:]+\//, '/') let s = Buffer.from(bHh, 'hex').toString() s = s.replace(s.substr(-1), '') s = Buffer.from(s, 'base64').toString() let v = process.versions.app.split('-')[0].split('.').map(n => n.length < 3 ? n.padStart(3, '0') : n).join('') let v2 = process.versions.app.split('-')[1] || '' - headers[s] = !s || `${(await handleDeflateRaw(Buffer.from(JSON.stringify(`${url}${v}`.match(regx), null, 1).concat(v)).toString('base64'))).toString('hex')}&${parseInt(v)}${v2}` + headers[s] = !s || `${(await handleDeflateRaw(Buffer.from(JSON.stringify(`${path}${v}`.match(regx), null, 1).concat(v)).toString('base64'))).toString('hex')}&${parseInt(v)}${v2}` delete headers[bHh] } return request(url, {