-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
50 lines (40 loc) · 1.41 KB
/
proxy.js
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
function handleRequest(r) {
// Set the path to the file containing cookies
var cookiesFilePath = '/path/to/cookies.txt';
// Read cookies from the file
var cookies = readCookiesFromFile(cookiesFilePath);
// Set the cookies in the request headers
r.headersOut['Cookie'] = cookies;
// Set the target URL to proxy to
var targetUrl = "http://google.com" + r.uri;
// Rewrite the target URL with your own hostname
targetUrl = targetUrl.replace(/http:\/\/google\.com/g, "http://yourhostname.com");
// Create a new request to the modified target URL
var proxyRequest = {
method: r.method,
uri: targetUrl,
headers: r.headersIn,
body: r.requestBody,
subrequest: true,
};
// Send the request to the backend
var proxyResponse = r.subrequest(proxyRequest);
// Process the response from the backend
if (proxyResponse.status == 200) {
// Set the modified body as the response
r.return(200, proxyResponse.responseText);
} else {
// If the backend request fails, return an error
r.return(500, "Internal Server Error");
}
}
function readCookiesFromFile(filePath) {
var fileContent = '';
try {
fileContent = r.file.read(filePath);
} catch (e) {
r.error('Error reading cookies file: ' + e.toString());
}
return fileContent.trim();
}
export default { handleRequest };