docs4you commited on
Commit
e942465
·
verified ·
1 Parent(s): 3724dfe

Upload mytv.php

Browse files
Files changed (1) hide show
  1. mytv.php +153 -0
mytv.php ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // 获取请求的 URL 并修正编码
3
+ $request_url = isset($_GET['url']) ? urldecode($_GET['url']) : '';
4
+ if (empty($request_url)) {
5
+ die('缺少 url 参数');
6
+ }
7
+
8
+ // 允许代理的域名列表
9
+ $allowed_domains = [
10
+ 'aktv.top',
11
+ 'php.jdshipin.com',
12
+ 'cdn12.jdshipin.com',
13
+ 'v2h.jdshipin.com',
14
+ 'v2hcdn.jdshipin.com',
15
+ 'cdn.163189.xyz',
16
+ 'cdn2.163189.xyz',
17
+ 'cdn3.163189.xyz',
18
+ 'cdn5.163189.xyz',
19
+ 'cdn6.163189.xyz',
20
+ 'cdn9.163189.xyz'
21
+ ];
22
+
23
+ $parsed_url = parse_url($request_url);
24
+ //说明:如果需要限制仅可代理指定域名的直播源,可以删除注释( /* 和 */ ),并在上面列表中添加你需要代理的网站域名。
25
+ /*
26
+ if (!$parsed_url || !isset($parsed_url['host']) || !in_array($parsed_url['host'], $allowed_domains)) {
27
+ die('非法请求的域名');
28
+ }
29
+ */
30
+
31
+ //自定义 getallheaders() 函数,使得代码可以兼容 FastCGI 模式
32
+ if (!function_exists('getallheaders')) {
33
+ function getallheaders() {
34
+ $headers = [];
35
+ foreach ($_SERVER as $name => $value) {
36
+ if (substr($name, 0, 5) == 'HTTP_') {
37
+ $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
38
+ }
39
+ }
40
+ return $headers;
41
+ }
42
+ }
43
+
44
+ // 处理 HTTP 头信息
45
+ $headers = [];
46
+ foreach (getallheaders() as $name => $value) {
47
+ if (strtolower($name) !== 'host') {
48
+ $headers[] = "$name: $value";
49
+ }
50
+ }
51
+ $headers[] = "Host: {$parsed_url['host']}";
52
+ $headers[] = "User-Agent: AppleCoreMedia/1.0.0.7B367 (iPad; U; CPU OS 4_3_3 like Mac OS X)";
53
+ $headers[] = "Referer: https://{$parsed_url['host']}/";
54
+ $headers[] = "Accept-Encoding: gzip, deflate";
55
+
56
+ // 发送请求
57
+ $ch = curl_init();
58
+ curl_setopt($ch, CURLOPT_URL, $request_url);
59
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60
+ curl_setopt($ch, CURLOPT_HEADER, true); // 获取完整响应头
61
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // 禁用自动跳转
62
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
63
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
64
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
65
+ curl_setopt($ch, CURLOPT_ENCODING, "");
66
+
67
+ // 禁用 HTTP/2,强制使用 HTTP/1.1
68
+ curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
69
+
70
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
71
+ curl_setopt($ch, CURLOPT_POST, true);
72
+ curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
73
+ }
74
+
75
+ $response = curl_exec($ch);
76
+ $curl_error = curl_error($ch);
77
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
78
+ $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
79
+ curl_close($ch);
80
+
81
+ // 分离响应头和响应体
82
+ $headers = substr($response, 0, $header_size);
83
+ $body = substr($response, $header_size);
84
+
85
+ // 解析响应头
86
+ $response_headers = [];
87
+ foreach (explode("\r\n", $headers) as $line) {
88
+ if (strpos($line, 'HTTP/') === 0) {
89
+ $response_headers[] = $line;
90
+ continue;
91
+ }
92
+ $parts = explode(': ', $line, 2);
93
+ if (count($parts) === 2) {
94
+ $name = strtolower($parts[0]);
95
+ $response_headers[$name] = $parts[1];
96
+ }
97
+ }
98
+
99
+ // 处理重定向
100
+ if (in_array($http_code, [301, 302, 303, 307, 308]) && isset($response_headers['location'])) {
101
+ $location = $response_headers['location'];
102
+
103
+ // 处理相对路径
104
+ if (!parse_url($location, PHP_URL_SCHEME)) {
105
+ $base = $parsed_url['scheme'] . '://' . $parsed_url['host'];
106
+ if (isset($parsed_url['port'])) {
107
+ $base .= ':' . $parsed_url['port'];
108
+ }
109
+ $location = $base . '/' . ltrim($location, '/');
110
+ }
111
+
112
+ // 生成代理地址并跳转
113
+ header("Location: mytv.php?url=" . urlencode($location), true, $http_code);
114
+ exit();
115
+ }
116
+
117
+ // 保留原始 Content-Type
118
+ if (isset($response_headers['content-type'])) {
119
+ header('Content-Type: ' . $response_headers['content-type']);
120
+ }
121
+
122
+ // 设置 HTTP 响应状态码
123
+ http_response_code($http_code);
124
+
125
+ if ($response === false) {
126
+ die("CURL ERROR: " . $curl_error);
127
+ }
128
+
129
+ // 处理 m3u8 文件
130
+ if (strpos($request_url, '.m3u8') !== false || (isset($response_headers['content-type']) && strpos($response_headers['content-type'], 'application/vnd.apple.mpegurl') !== false)) {
131
+ $base_url = dirname($request_url) . '/';
132
+ $allowed_domains_regex = implode('|', array_map(function($domain) {
133
+ return preg_quote($domain, '/');
134
+ }, $allowed_domains));
135
+
136
+ $body = preg_replace_callback(
137
+ '/(https?:\/\/(?:' . $allowed_domains_regex . ')\/[^\s"\']+\.ts)|([^\s"\']+\.ts)/',
138
+ function ($matches) use ($base_url) {
139
+ if (!empty($matches[1])) {
140
+ return 'mytv.php?url=' . urlencode($matches[1]);
141
+ } elseif (!empty($matches[2])) {
142
+ $ts_url = $base_url . ltrim($matches[2], '/');
143
+ return 'mytv.php?url=' . urlencode($ts_url);
144
+ }
145
+ return $matches[0];
146
+ },
147
+ $body
148
+ );
149
+ header('Content-Disposition: inline; filename=index.m3u8');
150
+ }
151
+
152
+ echo $body;
153
+ ?>