-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
95 lines (83 loc) · 2.45 KB
/
Copy pathproxy.php
File metadata and controls
95 lines (83 loc) · 2.45 KB
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
<?php
/**
.htaccess:
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Allow-Headers "*"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
*/
// url is required as a parameter
if (!isset($_GET["url"])) exit;
// utility function
function startsWith ($string, $startString) {
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
// get the URL to contact
$url = rawurldecode($_GET["url"]);
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : "get";
// check URL to make sure we authorize it
$authorizeUrls = [
"https://auth.tado.com/",
"https://my.tado.com/"
];
$canContinue = false;
foreach ($authorizeUrls as $urlOK) {
if (startsWith($url, $urlOK)) {
$canContinue=true;
break;
}
}
if (!$canContinue) exit("Unauthorize URL.");
$data = file_get_contents('php://input');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// transfer the headers
$headers = apache_request_headers();
$curlHeaders = [
"Origin: https://my.tado.com",
"Referer: https://my.tado.com"
];
foreach ($headers as $header => $value) {
if (!in_array($header, [ "Host", "Origin", "Referer" ])) array_push($curlHeaders, $header.': '.$value);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
// send the method and data
if ($method === "POST") {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
// debug
/*$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w+');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, $fp);*/
// read the answer
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
// transfer the headers to the requestor
$headers = explode("\r\n", $headers);
foreach ($headers as $head) {
// remove some headers
switch($head) {
case "X-Content-Type-Options: nosniff":
case "X-XSS-Protection: 1; mode=block":
case "X-Frame-Options: DENY":
case "Transfer-Encoding: chunked":
case "Access-Control-Allow-Origin: *":
case "": break;
default: {
header($head);
}
}
}
// write the body
echo $body;
?>