-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
241 lines (230 loc) · 7.71 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* cfworker反向代理工具
* reference: https://developer.mozilla.org/zh-CN/docs/Web/API/URL
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
//获取服务器hostname,Protocol,Port
let srvurl=new URL(request.url);
let srvHostname = srvurl.hostname;
let srvHost = srvurl.host;
let reqHostname = null;
let reqProtocol = null;
let reqPort = null;
let reqReferer = null;
let reqRefererd = null;
let refHost = null;
let havereqPort = true;
let enableReferRewrite = true;
let enableUrlRewrite = false;
let enableCookieRewrite = true;
//取域名第一个斜杠后的所有信息为代理链接
let url = request.url.substr(8);
url = url.substr(url.indexOf('/') + 1);
//返回对象
var response;
//需要忽略的代理
if (request.method == "OPTIONS" || url.length == 0 || url == "favicon.ico" || url == "robots.txt") {
//输出提示
var htm = [];
var htmHeaders = new Headers;
htmHeaders.set("content-type", "text/html");
htm.push("<html><body>");
htm.push("<h1>404 Page Not Found</h1>");
htm.push("Request file not found on this server.");
htm.push("<hr>");
htm.push("HTTP Server at tc.0110.inaddr Port 8080");
htm.push("</body></html>");
response = new Response(htm.join('\n\n'), {
status: 404,
headers: htmHeaders,
});
} else {
//补上前缀 https://
if (url.toLowerCase().indexOf("http") == -1) {
url = "https://" + url;
}
const requrl = new URL(url);
//获取目标host
reqHostname = requrl.hostname;
//获取protocol
reqProtocol = requrl.protocol;
//获取port,没有就设置默认port
reqPort = requrl.port;
if (reqPort === "") {
havereqPort = false;
if (reqProtocol === "http:") {
reqPort = "80";
}
if (reqProtocol === "https:") {
reqPort = "443";
}
requrl.port = reqPort;
}
//处理Upgrade请求(websocket)
var reqHeaders = new Headers(request.headers);
let connectionType = reqHeaders.get('Connection');
if (connectionType === 'Upgrade')
{
let uprequest=new Request(requrl,request);
return fetch(uprequest);
}
//获取referer中的Host信息。reference: https://developer.mozilla.org/zh-CN/docs/Web/API/URL/host
reqReferer = reqHeaders.get('Referer');
if (reqReferer != null) {
const reqRefererUrla = new URL(reqReferer);
refHost = reqRefererUrla.host;
} else {
refHost = srvHostname+':'+reqPort;
}
//改写referer,复原请求原本的referer
if (enableReferRewrite == true) {
if (reqReferer !== null) {
reqReferer = reqReferer.substr(8);
reqReferer = reqReferer.substr(reqReferer.indexOf('/') + 1);
if (reqReferer.toLowerCase().indexOf("http") == -1) {
reqReferer = "https://" + reqReferer;
}
reqHeaders.set('Referer', reqReferer);
//console.log(reqReferer);
} else {
reqReferer="";
}
}
//重写cookie路径和域
const send_cookie = reqHeaders.get('cookie');
if (send_cookie !== null &&
enableCookieRewrite == true) {
let text = send_cookie.replace(new RegExp("Path=/"+reqHostname+'/', 'g'), "Path=/");
text = text.replace(new RegExp("Domain="+srvHostname, 'g'), "Domain="+reqHostname);
reqHeaders.set('cookie', text);
}
//跟踪跳转 ref: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
const reqInit = {
'method': request.method,
'headers': reqHeaders,
'redirect': 'follow',
}
//当连接为POST或PUT时,需要传递body
if (request.method === 'POST' ||
request.method === 'PUT'
) {
reqInit.body = request.body
}
//发起 fetch
response = await fetch(requrl, reqInit);
//跳转相对路径到正确的地址,原理为从referer中获取域名,拼接之后301跳转
if (reqReferer != "") {
if (response.status==530 || response.status==502 || (response.status==403 && response.statusText=="Forbidden")) {
const reqRefererUrl = new URL(reqReferer);
reqHostname = reqRefererUrl.hostname;
console.log(requrl.hostname)
let re = new RegExp('https://', 'g');
let j = 'https://'+refHost+'/'+reqHostname+'/';
realurl = requrl.href.replace(re, j);
return Response.redirect(realurl, 301);
}
}
}
//添加跨域头
var myHeaders = new Headers(response.headers);
myHeaders.set("Access-Control-Allow-Origin", "*");
myHeaders.set("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
myHeaders.set("Access-Control-Allow-Headers", "*");
myHeaders.set('access-control-allow-credentials', true);
myHeaders.delete('content-security-policy');
myHeaders.delete('content-security-policy-report-only');
myHeaders.delete('clear-site-data');
//重写返回链接
let response_clone = response.clone();
let original_text = null;
var i, j;
if (havereqPort === true) {
reqHostname = reqHostname+":"+reqPort;
}
//替换html链接
const replace_html = {
'href="/': 'href="/'+reqHostname+'/',
'href="https://': 'href="https://'+srvHost+'/',
'href="http://': 'href="https://'+srvHost+'/http://',
'src="//': 'src="'+reqProtocol+'//',
'src="/': 'src="'+reqProtocol+'//'+reqHostname+'/',
'srcset="/': 'srcset="/'+reqHostname+'/',
'src="https://': 'src="https://'+srvHost+'/',
'src="http://': 'src="https://'+srvHost+'/http://',
'content="https://': 'content="https://'+srvHost+'/',
'content="//': 'content="'+reqProtocol+'//',
'content="/': 'content="/'+reqHostname+'/',
'url[(]https://': 'url(https://'+srvHost+'/',
'url[(]"/': 'url("/'+reqHostname+'/',
'url[(]/': 'url(/'+reqHostname+'/',
"src='//": "src='//"+srvHost+'/',
"src='/": "src='/"+reqHostname+'/',
"u='/xjs/": "u='/"+reqHostname+"/xjs/",
"var s='/": "var s='/"+reqHostname+"/",
}
const replace_css = {
'url[(]"/': 'url("/'+reqHostname+'/',
//'url[(]https://': 'url(https://'+reqHostname+'/',
'https://': 'https://'+srvHostname+'/',
'http://': 'https://'+srvHostname+'/http://',
}
const replace_js = {
"src='//": "src='//"+srvHostname+'/',
"src='/": "src='/"+reqHostname+'/',
'https://': 'https://'+srvHostname+'/',
'http://': 'https://'+srvHostname+'/http://',
}
//判断能否替换
const content_type = myHeaders.get('content-type');
if (content_type !== null &&
enableUrlRewrite == true) {
let text = await response_clone.text();
if (content_type.includes('text/html'))
{
for (i in replace_html) {
j = replace_html[i]
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
}
if (content_type.includes('-text/javascript'))
{
for (i in replace_js) {
j = replace_js[i]
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
}
if (content_type.includes('-text/css'))
{
for (i in replace_css) {
j = replace_css[i]
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
}
original_text = text;
} else {
original_text = response.body;
}
//替换cookie,支持登陆
const set_cookie = myHeaders.get('set-cookie');
if (set_cookie !== null &&
enableCookieRewrite == true) {
let text = set_cookie.replace(RegExp("Path=/", 'g'), "Path=/"+reqHostname+'/');
text = text.replace(RegExp("Domain="+reqHostname, 'g'), "Domain="+srvHostname);
myHeaders.set('set-cookie', text);
}
return new Response(original_text, {
status: response.status,
headers: myHeaders
})
return myresponse;
}