-
Notifications
You must be signed in to change notification settings - Fork 113
/
BPplayurl.php
137 lines (137 loc) · 5.13 KB
/
BPplayurl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/* Config */
$upstream_pc_url = 'https://api.bilibili.com/pgc/player/web/playurl';
$upstream_pc_url_v2 = 'https://api.bilibili.com//pgc/player/web/v2/playurl';
$upstream_app_url = 'https://api.bilibili.com/pgc/player/api/playurl';
$upstream_pc_search_url = 'https://api.bilibili.com/x/web-interface/search/type';
$upstream_pc_qr_login = 'https://passport.bilibili.com/x/passport-login/web/qrcode/poll';
$timeout = 5;
// seconds
/* Read incoming request */
$request_method = $_SERVER['REQUEST_METHOD'];
$request_query = $_SERVER['QUERY_STRING'];
$request_uri = $_SERVER['REQUEST_URI'];
$req_referer = $_SERVER['HTTP_REFERER'];
$request_headers = getallheaders();
$request_body = file_get_contents('php://input');
//通过request_query字符串生成queries键值对
foreach(explode('&', $request_query)as $value) {
$request_query_tmp = explode('=', $value);
$queries[$request_query_tmp[0]] = $request_query_tmp[1];
}
/*tool*/
//某个字符串在另一个字符串第N此出现的下标
function str_n_pos($str, $find, $n) {
$pos_val = 0;
for ($i = 1; $i <= $n; $i++) {
$pos = strpos($str, $find);
$str = substr($str, $pos + 1);
$pos_val = $pos + $pos_val + 1;
}
$count = $pos_val - 1;
return $count;
}
function array_remove_by_key($arr, $key) {
if (!array_key_exists($key, $arr)) {
return $arr;
}
$keys = array_keys($arr);
$index = array_search($key, $keys);
if ($index !== FALSE) {
array_splice($arr, $index, 1);
}
return $arr;
}
/* Forward request */
$ch = curl_init();
//处理请求相关header
$request_headers = array_remove_by_key($request_headers, 'Host');
$request_headers = array_remove_by_key($request_headers, 'X-Forwarded-For');
//配置body压缩方式
$request_headers = array_remove_by_key($request_headers, 'Accept-Encoding');
curl_setopt($ch, CURLOPT_ENCODING, "identity");
//好像b站只有br压缩
$headers = array();
foreach($request_headers as $key => $value) {
$headers[] = $key.': '.$value;
}
//判断请求接口
if (substr_count($request_uri, '/auth/set_cookie_qrcode') != 0) {
$url = $upstream_pc_qr_login.'?'.$request_query;
curl_setopt($ch, CURLOPT_REFERER, $req_referer);
} elseif (substr_count($request_uri, '/search/type') != 0) {
//搜索接口强制需要buvid3这个cookie,应该与账号无关,访问https://api.bilibili.com/x/frontend/finger/spi可直接获取
if (substr_count($request_query, 'buvid3') != 0)
array_push($headers, "cookie: buvid3=".$queries['buvid3']);
$url = $upstream_pc_search_url.'?'.$request_query;
curl_setopt($ch, CURLOPT_REFERER, $req_referer);
} elseif(substr_count($request_uri, 'playurl') != 0) {
//判断使用的那个pc还是app接口
if (substr_count($request_query, 'platform=android') != 0) {
$url = $upstream_app_url.'?'.$request_query;
curl_setopt($ch, CURLOPT_USERAGENT, 'Bilibili Freedoooooom/MarkII');
//新版本的视频接口
} elseif(substr_count($request_uri, '/v2/playurl') != 0 ) {
$url = $upstream_pc_url_v2.'?'.$request_query;
curl_setopt($ch, CURLOPT_REFERER, $req_referer);
} else {
$url = $upstream_pc_url.'?'.$request_query;
curl_setopt($ch, CURLOPT_REFERER, $req_referer);
}
} else {
header('HTTP/1.1 502 Bad Gateway');
header('Content-Type: text/plain');
echo 'Failed to match interface./r/n';
return 1;
}
//url配置
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
header('HTTP/1.1 502 Bad Gateway');
header('Content-Type: text/plain');
echo 'Upstream host did not respond.';
} else {
$header_length = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response_headers = explode("\n", substr($response, 0, $header_length));
$response_body = substr($response, $header_length);
//处理返回相关header
foreach($response_headers as $n => $response_header) {
//配置返回的body压缩方式
if (strpos($response_header, "Content-Encoding") !== false) {
$response_headers[$n] = "Content-Encoding: identity\n";
}
//删除B站返回的Content-Length,防止浏览器只读取Content-Length长度的数据,造成json不完整
if (strpos($response_header, "Content-Length") !== false) {
unset($response_headers[$n]);
//header('Content-Length: '.strlen($response_body));
}
if (strpos($response_header, "Set-Cookie") !== false) {
$response_headers[$n] = str_replace("Domain=bilibili.com","Secure;samesite=none;Domain=".$_SERVER[HTTP_HOST],$response_headers[$n]);
}
}
unset($response_header);
/*跨域问题*/
header('access-control-allow-credentials: true');
header('access-control-allow-origin: '.substr($req_referer, 0, str_n_pos($req_referer, '/', 3)));
header('access-control-allow-headers: *');
foreach($response_headers as $header) {
$header = trim($header);
if (substr_count($header,'Set-Cookie') != 0) {
//允许set-cookie出现多个
header(trim($header),false);
} elseif ($header) {
header(trim($header));
}
}
echo $response_body;
}
curl_close($ch);
?>