Skip to content

Instantly share code, notes, and snippets.

@mimosa
Last active October 5, 2018 12:23
Show Gist options
  • Save mimosa/de0534f98344c669e79a473b1213275b to your computer and use it in GitHub Desktop.
Save mimosa/de0534f98344c669e79a473b1213275b to your computer and use it in GitHub Desktop.
B站 api key 规则,每3小时轮换一个 :(
function string.fh(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function sort_nums()
return ("6337393836663535306539363566613865616264646435653033363665356466"):fh()
end
bit = require('bit')
lmd5 = require('md5')
function x(a, b)
return bit.bor(a, b) - bit.band(a, b)
end
utility = {}
function utility.d_tv_box(str, key, skey)
local binary_str = ""
for i=1,#str/4 do
local index = (i - 1) * 4 + 1
if(i == 1) then
index = 1
end
local seg = string.sub(str, index, index+3)
local decrypted = x(tonumber(skey..seg, 16), key)
binary_str = binary_str..string.format("%04x",decrypted)
end
return binary_str
end
function utility.check_tv_box(model, s)
local snum = sort_nums()
return utility.d_tv_box(snum, model, s)
end
function utility.get_tv_box_m()
-- f3bb208b3d081dc8 0-3
-- 4fa4601d1caa8b48 4-7
-- 452d3958f048c02a 8-11
-- 86385cdc024c0f6c 12-15
-- 5256c25b71989747 16-19
-- e97210393ad42219 20-23
-- return {1="f3bb208b3d081dc8",
return {
'66336262323038623364303831646338',
'34666134363031643163616138623438',
'34353264333935386630343863303261',
'38363338356364633032346330663663',
'35323536633235623731393839373437',
'65393732313033393361643432323139'
}
end
ak = utility.get_tv_box_m()[2]
ak = ak:fh()
print(ak)
as = utility.check_tv_box(1481126400, '8e3')
md5str = lmd5.sumhexa(as)..'BlUa'
as = lmd5.sumhexa(md5str)
print(as)
@mimosa
Copy link
Author

mimosa commented Dec 10, 2016

话说 Lua 的包管理也不错 💯

以上代码依赖:
luarocks install luabitop
luarocks install md5

@mimosa
Copy link
Author

mimosa commented Dec 10, 2016

  1. 通过 aid 获取 cid:
    http://www.bilibili.com/widget/getPageList?aid=<av后的6~8位数字>
  2. 获得服务器时间(匹配api key用):
    http://interface.bilibili.com/serverdate.js?ts=1
  3. 获得 sid 的 Cookie,对CDN的视频播放速率很关键:
    http://interface.bilibili.com/player?aid=<av后的6~8位数字>&id=cid:<cid>
  4. 从CDN分配视频url,3小时有效:
    http://interface.bilibili.com/playurl?appkey=<通过服务器时间匹配>&cid=<cid>&otype=json&quality=2&type=mp4&sign=<params排序后,转成query str,再 MD5(query str + api secret)>

@mimosa
Copy link
Author

mimosa commented Dec 12, 2016

LuaJit 没有默认 luarocks,得自己编译一个。

curl -L https://github.com/luarocks/luarocks/archive/v2.4.2.tar.gz >> ~/luarocks-2.4.2.tar.gz &&\
  tar zxvf luarocks-2.4.2.tar.gz

./configure --with-lua='/usr/local' --lua-suffix='jit' --with-lua-include='/usr/local/include/luajit-2.0'

make build &&\
make install

@tangblack
Copy link

您好
請問一下
http://interface.bilibili.com/playurl?appkey=<通过服务器时间匹配>&cid=&otype=json&quality=2&type=mp4&sign=<params排序后,转成query str,再 MD5(query str + api secret)>

每一組 appkey 都有對應的 api secret
不過看 gist 似乎看不出來有對應關係
我執行後算出來的 as 為 91b0f8ce5d1513b49e619b3e0dc550f0
但拿去呼叫 http://interface.bilibili.com/playurl? 得到 checksum error

不知道是否哪段邏輯我遺漏了
感謝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment