-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
169 lines (154 loc) · 5.2 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
//v2.0
try{
require 'config.php';
require 'functions.php';
}
catch (Exception $e){
error_log ( "Error with PMS Load Balancer in ".__FILE__." at line 3. Unable to get config and/or functions");
exit;
}
$unm = "";
$utoken = "";
$uhtoken = "";
$logdir = "./";
setupLogging();
$headers = array();
$userheaders = array();
$accepteh=FALSE;
$acceptr=FALSE;
$ouri = $_SERVER['REQUEST_URI'];
$originalurl = "http://$destserver$ouri";
checkBlockUrls($ouri, $originalurl);
$ruri = fixRequestUri($ouri);
$finalurl = "http://$destserver$ruri";
if($clientstreaming === FALSE && stripos($finalurl,"/library/parts/") !== FALSE || stripos($finalurl,"/video/:/transcode/") !== FALSE){
$finalurlre = "http://$destserverre$ruri";
header("Location: $finalurlre");
logger("Redirecting to reduce load");
exit;
}
foreach (getallheaders() as $name => $value) {
$ovalue = $value;
if( $swapidentity === TRUE || $enableanonymousaccess === FALSE){
switch ($name) {
case 'X-Plex-Token':
logger("fixing $name");
$uhtoken = $value;
logger("found user token $uhtoken");
$value = $token;
logger("adding $name $value");
break;
case 'X-Plex-Username':
logger("fixing $name");
$unm = $value;
logger("found username $unm");
$value = $username;
logger("adding $name $value");
break;
case 'Referer':
case 'Host':
case 'Origin':
logger("fixing $name");
$value = str_replace($lbip,$destserver, $value);
logger("adding $name $value");
break;
case 'Range':
if($clientstreaming === TRUE && stripos($finalurl,"/library/parts/") !== FALSE){
logger("checking range $value");
$valsplit = explode("-",$value);
$valsplit[0] = explode("=",$valsplit[0])[1];
if(count($valsplit) > 1 && intval($valsplit[1])-intval($valsplit[0])>$streamsize){
logger('fixing range');
$newend=intval($valsplit[0])+$streamsize;
$value = "bytes=".$valsplit[0]."-$newend";
}
elseif(count($valsplit) == 1 || (count($valsplit) ==2 && $valsplit[1]=="") ){
logger('fixing range');
$newend=intval($valsplit[0])+$streamsize;
$value = "bytes=".$valsplit[0]."-$newend";
}
logger("adding $name $value");
}
break;
/*
//In my tests I found that the client identifier doesnt much matter.
//Only the username and token needs to be changed
case 'Cookie':
$value = replaceCookie($value, $client);
break;
case 'X-Plex-Client-Identifier':
$value = $client;
break;*/
}
}
if(stripos($name,"Accept") !== FALSE && stripos($value,"gzip") !== FALSE){
//Detect gzip encoding
$acceptr=TRUE;
}
$headers[] = "$name: $value";
$userheaders[] = "$name: $ovalue";
}
if($swapidentity === TRUE && $enableanonymousaccess === FALSE
&& $unm == "" && ($utoken != "" || $uhtoken != "" ) ){
if($uhtoken == ""){
$uhtoken = $utoken;
$userheaders[] = "X-Plex-Token: $uhtoken";
}
elseif($utoken == ""){
$utoken = $uhtoken;
}
logger("looking up username with token $utoken");
$unm = getUsername($userheaders);
}
if( $namedusersonly === TRUE && !isset($lbuuidkvp[$unm]) ){
logger("blocking user $unm");
header("Location: $originalurl");
exit;
}
$ch = curl_init($finalurl);
curl_setopt($ch,CURLOPT_ENCODING, '');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_VERBOSE,1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
$output = curl_exec($ch);
//Get the response code before closing curl
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
curl_close($ch);
//Split the response to retreive headers
list($header, $body) = explode("\r\n\r\n", $output, 2);
$header = explode("\r\n",$header);
//Send response code
http_response_code(intval($httpCode));
$firstrun = TRUE;
logger("Server response code $httpCode");
foreach($header as $value){
if($firstrun === TRUE){
//The first header is always HTTP 1/1 CODE
//Sending this messes things up
$firstrun = FALSE;
continue;
}
if(stripos($value,"Content") !== FALSE && stripos($value,"gzip") !== FALSE){
//Detect gzip encoding
$accepteh=TRUE;
}
else if(stripos($value,$destserver) !== FALSE){
logger("Fixing ip from $destserver to $lbip");
$value = str_replace($destserver, $lbip, $value);
}
header("$value");
}
if( $swapidentity === TRUE && $enableanonymousaccess === FALSE ){
$body = swapBackBody($body);
}
if(($accepteh === TRUE && $acceptr === TRUE)){
echo gzencode($body);
}
else{
echo $body;
}
exit;
?>