forked from XX-net/XX-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay.php
More file actions
119 lines (95 loc) · 3.3 KB
/
relay.php
File metadata and controls
119 lines (95 loc) · 3.3 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
$__relay__ = 'http://goagent.app.com/index.php';
$__hosts__ = array('goagent.app.com');
$__timeout__ = 16;
function php_getallheaders() {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$name = join('-', array_map('ucfirst', explode('_', strtolower(substr($name, 5)))));
$headers[$name] = $value;
} else if ($name == "CONTENT_TYPE") {
$headers["Content-Type"] = $value;
} else if ($name == "CONTENT_LENGTH") {
$headers["Content-Length"] = $value;
}
}
return $headers;
}
function header_function($ch, $header) {
if (stripos($header, 'Transfer-Encoding:') === false) {
header($header, false);
}
return strlen($header);
}
function write_function($ch, $content) {
echo $content;
return strlen($content);
}
function main() {
$timeout = $GLOBALS['__timeout__'];
$method = $_SERVER['REQUEST_METHOD'] ;
$url = $GLOBALS['__relay__'];
$host = $GLOBALS['__hosts__'][array_rand($GLOBALS['__hosts__'])];
$headers = php_getallheaders();
$body = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$urlparts = parse_url($url);
if ($body && !isset($headers['Content-Length'])) {
$headers['Content-Length'] = strval(strlen($body));
}
if (isset($headers['Connection'])) {
$headers['Connection'] = 'close';
}
$headers['Host'] = $urlparts['host'];
$header_array = array();
foreach ($headers as $key => $value) {
$header_array[] = "$key: $value";
}
$curl_opt = array();
switch (strtoupper($method)) {
case 'HEAD':
$curl_opt[CURLOPT_NOBODY] = true;
break;
case 'GET':
break;
case 'POST':
$curl_opt[CURLOPT_POST] = true;
$curl_opt[CURLOPT_POSTFIELDS] = $body;
break;
default:
$curl_opt[CURLOPT_CUSTOMREQUEST] = $method;
$curl_opt[CURLOPT_POSTFIELDS] = $body;
break;
}
$curl_opt[CURLOPT_HTTPHEADER] = $header_array;
$curl_opt[CURLOPT_RETURNTRANSFER] = true;
$curl_opt[CURLOPT_BINARYTRANSFER] = true;
$curl_opt[CURLOPT_HEADER] = false;
$curl_opt[CURLOPT_HEADERFUNCTION] = 'header_function';
$curl_opt[CURLOPT_WRITEFUNCTION] = 'write_function';
$curl_opt[CURLOPT_FAILONERROR] = false;
$curl_opt[CURLOPT_FOLLOWLOCATION] = false;
$curl_opt[CURLOPT_CONNECTTIMEOUT] = $timeout;
$curl_opt[CURLOPT_TIMEOUT] = $timeout;
$curl_opt[CURLOPT_SSL_VERIFYPEER] = false;
$curl_opt[CURLOPT_SSL_VERIFYHOST] = false;
$new_url = preg_replace('@//[^/]+@', "//$host", $url);
if ($_SERVER['QUERY_STRING']) {
$new_url .= '?' . $_SERVER['QUERY_STRING'];
}
//var_dump(array('new_url' => $new_url, 'headers' => $headers, 'curl_opt' => $curl_opt));
//exit(0);
$ch = curl_init($new_url);
curl_setopt_array($ch, $curl_opt);
$ret = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
if (!headers_sent()) {
header('HTTP/1.1 502 Gateway Error');
header('Content-Type: text/plain');
}
echo "502 Urlfetch Error\r\nPHP Urlfetch Error: curl($errno)\r\n" . curl_error($ch);
}
curl_close($ch);
}
main();