NiceLeeのBlog 用爱发电 bilibili~

Java jsonp异域获取QQ音乐链接

2018-10-28
nIceLee

阅读:


接上一篇博文,当时真是脑子没转过弯来, QQ音乐网页版的信息查询请求直接就是异域的。
也就是说,服务器那边是允许不同域名之间的http请求的。
认识到这一点,稍微了解一下jsonp,接下来的工作就好做了.

过程

我们先监视网页的XHR请求,根据内容,我们可以看到一个有效的正儿八经的回复是这样的: (当然,这里的json内容有个getplaysongvkey05544603547563831()给包围起来)

getplaysongvkey05544603547563831({
    "req": {
        "data": {
            "expiration": 86400,
            "freeflowsip": ["http://101.227.216.145/amobile.music.tc.qq.com/", "http://101.227.216.146/amobile.music.tc.qq.com/", "http://101.227.216.148/amobile.music.tc.qq.com/"],
            "keepalivefile": "C400004TsFuW2mZbRR.m4a?guid=231227664&vkey=1AD5455B86EEB852EA91BE49C8846B6F1313027AFDAC7BB3A564BBAD46B6390A95AFD264E1567D6BBC19ADC844FE0CFC3DB658F43E5C4BF5&uin=0&fromtag=3",
            "msg": "ok",
            "retcode": 0,
            "servercheck": "34a85d6cb28ad2d705bb366fb98b17e3",
            "sip": ["http://101.227.216.145/amobile.music.tc.qq.com/", "http://101.227.216.146/amobile.music.tc.qq.com/", "http://isure.stream.qqmusic.qq.com/", "http://101.227.216.148/amobile.music.tc.qq.com/", "http://dl.stream.qqmusic.qq.com/"],
            "testfile2g": "C400003mAan70zUy5O.m4a?guid=231227664&vkey=B13FA54389AF0FCE3C834F847EC1FA5E23AFFB5C30B68B096B3F24BD7A827D792B23033D4606F4F7DA618E42F5F0DEC38824D5FEF252E83A&uin=0&fromtag=3",
            "testfilewifi": "C400003mAan70zUy5O.m4a?guid=231227664&vkey=B13FA54389AF0FCE3C834F847EC1FA5E23AFFB5C30B68B096B3F24BD7A827D792B23033D4606F4F7DA618E42F5F0DEC38824D5FEF252E83A&uin=0&fromtag=3",
            "uin": "",
            "userip": "58.33.181.33",
            "vkey": "6C94D3AFC91AE8EA188974CE8B76C143C24FD7B04EEAD433CFDF4935E25E64AA48EDAA0CAC94F8A61EC961BC95D42678ECEF4A5DA61B0292"
        },
        "code": 0
    },
    "req_0": {
        "data": {
            "expiration": 80400,
            "login_key": "",
            "midurlinfo": [{
                "common_downfromtag": 0,
                "errtype": "",
                "filename": "C400000wDA7M23CRIf.m4a",
                "flowfromtag": "",
                "flowurl": "",
                "hisbuy": 0,
                "hisdown": 0,
                "isbuy": 0,
                "isonly": 0,
                "onecan": 0,
                "opi128kurl": "",
                "opi192koggurl": "",
                "opi192kurl": "",
                "opi48kurl": "",
                "opi96kurl": "",
                "opiflackurl": "",
                "p2pfromtag": 0,
                "pdl": 0,
                "pneed": 0,
                "pneedbuy": 0,
                "premain": 0,
                "purl": "C400000wDA7M23CRIf.m4a?guid=231227664&vkey=4C28457DBFD0983F6E5C274347FF9C261167F95B233A3E7B0444502A957A0DBF50AC09E8316B86B53B7B7DE5EBF0E7FC1FE173452BAC8996&uin=0&fromtag=66",
                "qmdlfromtag": 0,
                "result": 0,
                "songmid": "000wDA7M23CRIf",
                "tips": "",
                "uiAlert": 0,
                "vip_downfromtag": 0,
                "vkey": "4C28457DBFD0983F6E5C274347FF9C261167F95B233A3E7B0444502A957A0DBF50AC09E8316B86B53B7B7DE5EBF0E7FC1FE173452BAC8996",
                "wififromtag": "",
                "wifiurl": ""
            }],
            "msg": "",
            "retcode": 0,
            "servercheck": "34a85d6cb28ad2d705bb366fb98b17e3",
            "sip": ["http://dl.stream.qqmusic.qq.com/", "http://isure.stream.qqmusic.qq.com/"],
            "testfile2g": "C400003mAan70zUy5O.m4a?guid=231227664&vkey=D40EB64E1D4618C492CD49EB75F56BF8C35DE273A325F7B6027700F09F86C18DACA05383D027A8B04879B3B21961D6B38C8D6E53597D02B4&uin=&fromtag=3",
            "testfilewifi": "C400003mAan70zUy5O.m4a?guid=231227664&vkey=D40EB64E1D4618C492CD49EB75F56BF8C35DE273A325F7B6027700F09F86C18DACA05383D027A8B04879B3B21961D6B38C8D6E53597D02B4&uin=&fromtag=3",
            "thirdip": ["", ""],
            "uin": "",
            "verify_type": 0
        },
        "code": 0
    },
    "code": 0,
    "ts": 1540722554473
})

这个请求的链接是//u.y.qq.com/cgi-bin/musicu.fcg,我们继续Ctrl+Shift+F全局搜索一下(关键词可以是这个链接的一部分,也可以是getplaysongvkey,多试一下不会错),最后发现有以下js:

var o = "getplaysongvkey" + (Math.random() + "").replace("0.", "");
  MUSIC.jQueryAjax.jsonp({
      url: "//u.y.qq.com/cgi-bin/musicu.fcg?callback=" + o,
      data: {
          data: JSON.stringify(e)
      },
      jsonpCallback: o,
      charset: "utf-8",
      success: function(e) {
          e && e.req_0 && 0 == e.code && e.req_0.data && e.req_0.data.midurlinfo && e.req_0.data.midurlinfo.length > 0 && ($.each(e.req_0.data.midurlinfo, function(t, n) {
              n.purl && (n.date = (new Date).getTime(),
              n.expiration = e.req_0.data.expiration,
              g_vkey[n.songmid] = n)
          }),
          (0 == m.expCdnArr.length || m.rateSpeed) && e.req && e.req.data && e.req.data.sip && ($.each(e.req.data.sip, function(e, t) {
              m.expCdnArr.push({
                  cdn: t,
                  expType: 0,
                  t: 0
              })
          }),
          e.req.data.testfilewifi && (m.expTestUrl = e.req.data.testfilewifi,
          m.rateSpeed = !1))),
          t && t()
      },
      error: function() {
          t && t()
      }

那么我们可以尝试着自己构造数据发送和解析,最后的代码如下:

<script>
//获取歌曲资源link
var url = "https://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=5381&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&callback=";

url +=  "getplaysongvkey" + (Math.random() + "").replace("0.", "");

var dataOrin = '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"6543312138","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"6543312138","songmid":["004XePmv4CsaEq"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":20,"cv":0}}';
dataOrin.replace("004XePmv4CsaEq","000u9nM71FV0gX");	//000u9nM71FV0gX为 战 排骨教主的歌曲mid
url += "&data=" + escape(dataOrin);
//console.log(url);
$.ajax({
		//url: "https://u.y.qq.com/cgi-bin/musicu.fcg?callback=getplaysongvkey30227171932695884&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%229297346368%22%2C%22songmid%22%3A%5B%22003BD58D4OpO92%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A0%2C%22format%22%3A%22json%22%2C%22ct%22%3A20%2C%22cv%22%3A0%7D%7D",
		url: url,
		//url: "https://sssa",
		//data: t.data,
		type: "GET",
		success: function (data) {
			try{
				var purl = data["req_0"]["data"]["midurlinfo"][0]["purl"];
				var sip = data["req_0"]["data"]["sip"][0];
				//console.log(sip + purl);
				$("#player1").attr("src",sip + purl);						
			}catch (err){
				console.log("获取音乐信息异常!");
			}
			initMusicPlayers();
			
		},
		error: initMusicPlayers,
		charset: "utf-8",						
		type: "get",
		dataType: "jsonp",
		cache: !0,
});
<script>

内容
隐藏