diff --git a/README.MD b/README.MD index e988a31..c47cf72 100644 --- a/README.MD +++ b/README.MD @@ -17,6 +17,13 @@ ## 版本新特性 + +### ? ? ? | 2018.01.04 +添加了proxy功能 + +### 2.8.0 | 2018.01.04 +用 'request' 重写了请求函数 + ### 2.6.5 | 2017.7.16 优化 CORS 设置 diff --git a/app.js b/app.js index cd50609..4698d78 100644 --- a/app.js +++ b/app.js @@ -23,6 +23,14 @@ app.use(cache("2 minutes", onlyStatus200)); app.use(express.static(path.resolve(__dirname, "public"))); +app.use(function(req, res, next) { + const proxy = req.query.proxy; + if (proxy) { + req.headers.cookie = req.headers.cookie + `__proxy__${proxy}`; + } + next(); +}); + // 获取专辑内容 app.use("/album", require("./router/album")); diff --git a/docs/README.md b/docs/README.md index 0cba4b7..a5c3c43 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,27 @@ ## 工作原理 跨站请求伪造 (CSRF), 伪造请求头,调用官方 API +## 可以使用代理 +在query参数中加上proxy=your-proxy即可让这一次的请求使用proxy + +```javascript +// 例子 +const url = `http://localhost:3000/music/url?id=33894312&proxy=http://121.196.226.246:84` +fetch(url) +.then(function() +{ + // do what you want +}) + +// 结果 +// {"data":[{"id":33894312,"url":"http://m10.music.126.net/20180104125640/930a968b3fb04908b733506b3833e60b/ymusic/0fd6/4f65/43ed/a8772889f38dfcb91c04da915b301617.mp3","br":320000,"size":10691439,"md5":"a8772889f38dfcb91c04da915b301617","code":200,"expi":1200,"type":"mp3","gain":-2.0E-4,"fee":0,"uf":null,"payed":0,"flag":0,"canExtend":false}],"code": 200} +``` + ## 版本新特性 + +### ? ? ? | 2018.01.04 +添加了proxy功能 + ### 2.8.0 | 2018.01.04 用 'request' 重写了请求函数 @@ -333,9 +353,9 @@ $ set PORT=4000 && node app.js **接口地址:** `/music/url` -**调用例子:** -`/music/url?id=347230` -`/music/url?id=347230,347231` +**调用例子:** +`/music/url?id=33894312` +`/music/url?id=405998841,33894312` 返回数据如下图: ![音乐 url](https://raw.githubusercontent.com/Binaryify/NeteaseCloudMusicApi/master/static/%E9%9F%B3%E4%B9%90%20url.png) @@ -411,8 +431,8 @@ $ set PORT=4000 && node app.js **调用例子:** `/search/multimatch?keywords=海阔天空` -### 收藏单曲到歌单 -说明:调用此接口,传入音乐 id和 limit 参数, 可获得该专辑的所有评论(需要登录) +### 对歌单添加或删除歌曲 +说明:调用此接口,可以添加歌曲到歌单或者从歌单删除某首歌曲(需要登录) **必选参数:** `op`: 从歌单增加单曲为add,删除为 del @@ -435,8 +455,8 @@ $ set PORT=4000 && node app.js **接口地址:** `/lyric` -**调用例子:** -`/lyric?id=347230` +**调用例子:** +`/lyric?id=33894312` 返回数据如下图: ![获取歌词](https://raw.githubusercontent.com/Binaryify/NeteaseCloudMusicApi/master/static/%E6%AD%8C%E8%AF%8D.png) diff --git a/util/util.js b/util/util.js index 1ac5b80..74e862f 100644 --- a/util/util.js +++ b/util/util.js @@ -2,6 +2,8 @@ const Encrypt = require("./crypto.js"); const request = require("request"); const querystring = require("querystring"); +// request.debug = true; + function randomUserAgent() { const userAgentList = [ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", @@ -37,6 +39,9 @@ function createWebAPIRequest( callback, errorcallback ) { + console.log(cookie); + const proxy = cookie.split("__proxy__")[1]; + cookie = cookie.split("__proxy__")[0]; const cryptoreq = Encrypt(data); const options = { url: `http://${host}${path}`, @@ -54,7 +59,8 @@ function createWebAPIRequest( body: querystring.stringify({ params: cryptoreq.params, encSecKey: cryptoreq.encSecKey - }) + }), + proxy: proxy }; request(options, function(error, res, body) {