-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
132 lines (111 loc) · 3.63 KB
/
proxy.php
File metadata and controls
132 lines (111 loc) · 3.63 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
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS' );
header( 'Access-Control-Allow-Headers: Content-Type, Authorization' );
header( 'Content-Type: application/json' );
if ( $_SERVER[ 'REQUEST_METHOD' ] === 'OPTIONS' ) {
exit( 0 );
}
$requestData = json_decode( file_get_contents( 'php://input' ), true );
if ( ! $requestData ) {
http_response_code( 400 );
echo json_encode( [ 'error' => 'Invalid request data' ] );
exit;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $requestData[ 'url' ] );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_ENCODING, '' );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $requestData[ 'method' ] );
// Add cookie handling
curl_setopt( $ch, CURLOPT_COOKIEFILE, "" ); // Enable cookie handling
$headers = [];
if ( isset( $requestData[ 'headers' ] ) && is_array( $requestData[ 'headers' ] ) ) {
foreach ( $requestData[ 'headers' ] as $header ) {
if ( isset( $header[ 'key' ] ) && isset( $header[ 'value' ] ) ) {
$headers[] = $header[ 'key' ] . ': ' . $header[ 'value' ];
}
}
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
if ( in_array( $requestData[ 'method' ], [ 'POST', 'PUT', 'PATCH' ] ) && isset( $requestData[ 'body' ] ) ) {
curl_setopt( $ch, CURLOPT_POSTFIELDS, $requestData[ 'body' ] );
}
$response = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
http_response_code( 500 );
echo json_encode( [ 'error' => curl_error( $ch ) ] );
exit;
}
$status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$header_size = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
$header_text = substr( $response, 0, $header_size );
$body = substr( $response, $header_size );
// Parse headers and cookies
$headers = [];
$cookies = [];
$lines = explode( "\n", str_replace( "\r\n", "\n", $header_text ) );
$firstLine = true;
foreach ( $lines as $line ) {
$line = trim( $line );
if ( empty( $line ) ) {
continue;
}
if ( $firstLine ) {
if ( preg_match( '/^HTTP\/\d+(?:\.\d+)?\s+(\d+)/', $line, $matches ) ) {
$headers[ 'status' ] = $line;
}
$firstLine = false;
continue;
}
if ( preg_match( '/^([^:]+):\s*(.+)$/', $line, $matches ) ) {
$key = trim( $matches[ 1 ] );
$value = trim( $matches[ 2 ] );
// Parse Set-Cookie headers
if ( strtolower( $key ) === 'set-cookie' ) {
$cookie = [];
$parts = explode( ';', $value );
// Parse the main cookie pair
$mainPart = array_shift( $parts );
list( $cookieName, $cookieValue ) = explode( '=', $mainPart, 2 );
$cookie[ 'name' ] = trim( $cookieName );
$cookie[ 'value' ] = trim( $cookieValue );
// Parse additional attributes
foreach ( $parts as $part ) {
$part = trim( $part );
if ( strpos( $part, '=' ) !== false ) {
list( $attrName, $attrValue ) = explode( '=', $part, 2 );
$cookie[ strtolower( trim( $attrName ) ) ] = trim( $attrValue );
} else {
$cookie[ strtolower( $part ) ] = true;
}
}
$cookies[] = $cookie;
} else {
// Handle other headers
if ( in_array( strtolower( $key ), [ 'report-to', 'nel', 'server-timing' ] ) ) {
try {
$decodedValue = json_decode( $value, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
$headers[ $key ] = $decodedValue;
} else {
$headers[ $key ] = $value;
}
} catch ( Exception $e ) {
$headers[ $key ] = $value;
}
} else {
$headers[ $key ] = $value;
}
}
}
}
curl_close( $ch );
echo json_encode( [
'status' => $status_code,
'headers' => $headers,
'cookies' => $cookies,
'body' => json_decode( $body ) ?? $body,
] );