-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.php
More file actions
69 lines (57 loc) · 1.85 KB
/
proxy.php
File metadata and controls
69 lines (57 loc) · 1.85 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
<?php
//configuration
$file = "/tmp/traffic";
$host = "example.com";
$url = "https://" . $host;
//setup curl
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$body = NULL;
//prepare request
$path = $_SERVER['REQUEST_URI'];
curl_setopt($c, CURLOPT_URL, $url . $path);
$headers = array();
foreach (getallheaders() as $name => $value) {
if (strcasecmp($name, "Host") == 0) {
array_push($headers, "Host: " . $host);
} else if (strcasecmp($name, "referer") != 0 && strcasecmp($name, "Accept-Encoding") != 0) {
array_push($headers, $name . ": " . $value);
}
}
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
$method = $_SERVER['REQUEST_METHOD'];
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
if ($method === "POST" || $method === "PUT") {
$body = file_get_contents('php://input');
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
}
//send request
$response = curl_exec($c);
//read response
$responseHeaderSize = curl_getinfo($c, CURLINFO_HEADER_SIZE);
$responseHeader = substr($response, 0, $responseHeaderSize);
$responseBody = substr($response, $responseHeaderSize);
foreach (explode(PHP_EOL, $responseHeader) as $key => $val) {
if (strpos($val, "Transfer-Encoding: chunked") === false) {
header($val, true);
}
}
echo $responseBody;
$responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
http_response_code($responseCode);
//log request and response
$data = $method . " " . $path . " " . $_SERVER['SERVER_PROTOCOL'] . "\n";
$data .= implode("\n", $headers);
$data .= "\n\n";
if (isset($body)) {
$data .= $body . "\n\n";
}
$data .= "\n";
$data .= str_replace("\r", "", $response);
$data .= "\n------------\n";
file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
?>