@@ -31,12 +31,12 @@ const PREFLIGHT_INIT = {
3131function makeRes ( body , status = 200 , headers = { } ) {
3232 headers [ '--ver' ] = JS_VER
3333 headers [ 'access-control-allow-origin' ] = '*'
34- return new Response ( body , { status, headers} )
34+ return new Response ( body , { status, headers } )
3535}
3636
3737
3838/**
39- * @param {string } urlStr
39+ * @param {string } urlStr
4040 */
4141function newUrl ( urlStr ) {
4242 try {
@@ -55,7 +55,7 @@ addEventListener('fetch', e => {
5555
5656
5757/**
58- * @param {FetchEvent } e
58+ * @param {FetchEvent } e
5959 */
6060async function fetchHandler ( e ) {
6161 const req = e . request
@@ -102,24 +102,46 @@ async function fetchHandler(e) {
102102 } )
103103 }
104104 }
105-
105+
106+
106107 if ( path . startsWith ( '/http/' ) ) {
107108 return httpHandler ( req , path . substr ( 6 ) )
108109 }
110+ if ( path . startsWith ( '/ws' ) ) {
111+ return wsHandler ( req )
112+ }
109113
110114 switch ( path ) {
111- case '/http' :
112- return makeRes ( '请更新 cfworker 到最新版本!' )
113- case '/ws' :
114- return makeRes ( 'not support' , 400 )
115- case '/works' :
116- return makeRes ( 'it works' )
117- default :
118- // static files
119- return fetch ( ASSET_URL + path )
115+ case '/http' :
116+ return makeRes ( '请更新 cfworker 到最新版本!' )
117+ case '/ws' :
118+ return makeRes ( 'not support' , 400 )
119+ case '/works' :
120+ return makeRes ( 'it works' )
121+ default :
122+ // static files
123+ return fetch ( ASSET_URL + path )
120124 }
121125}
122126
127+ function wsHandler ( req ) {
128+ const urlObj = new URL ( req . url ) ;
129+ const origin = urlObj . searchParams . get ( 'origin' ) ;
130+ const targeturl = urlObj . searchParams . get ( 'url__' ) ;
131+
132+ const headers = new Headers ( req . headers ) ;
133+ origin !== undefined && headers . set ( 'origin' , origin ) ;
134+
135+ return fetch ( targeturl , {
136+ method : req . method ,
137+ headers,
138+ body : req . body ,
139+ redirect : 'follow' ,
140+ } ) . catch ( ( err ) => {
141+ console . log ( err )
142+ throw err ;
143+ } ) ;
144+ }
123145
124146/**
125147 * @param {Request } req
@@ -133,7 +155,7 @@ function httpHandler(req, pathname) {
133155
134156 // preflight
135157 if ( req . method === 'OPTIONS' &&
136- reqHdrRaw . has ( 'access-control-request-headers' )
158+ reqHdrRaw . has ( 'access-control-request-headers' )
137159 ) {
138160 return new Response ( null , PREFLIGHT_INIT )
139161 }
@@ -159,12 +181,12 @@ function httpHandler(req, pathname) {
159181 if ( k . substr ( 0 , 2 ) === '--' ) {
160182 // 系统信息
161183 switch ( k . substr ( 2 ) ) {
162- case 'aceh' :
163- acehOld = true
164- break
165- case 'raw-info' :
166- [ rawSvr , rawLen , rawEtag ] = v . split ( '|' )
167- break
184+ case 'aceh' :
185+ acehOld = true
186+ break
187+ case 'raw-info' :
188+ [ rawSvr , rawLen , rawEtag ] = v . split ( '|' )
189+ break
168190 }
169191 } else {
170192 // 还原 HTTP 请求头
@@ -200,23 +222,23 @@ function httpHandler(req, pathname) {
200222
201223
202224/**
203- *
204- * @param {URL } urlObj
205- * @param {RequestInit } reqInit
206- * @param {number } retryTimes
225+ *
226+ * @param {URL } urlObj
227+ * @param {RequestInit } reqInit
228+ * @param {number } retryTimes
207229 */
208230async function proxy ( urlObj , reqInit , acehOld , rawLen , retryTimes ) {
209231 const res = await fetch ( urlObj . href , reqInit )
210232 const resHdrOld = res . headers
211233 const resHdrNew = new Headers ( resHdrOld )
212234
213235 let expose = '*'
214-
236+
215237 for ( const [ k , v ] of resHdrOld . entries ( ) ) {
216238 if ( k === 'access-control-allow-origin' ||
217- k === 'access-control-expose-headers' ||
218- k === 'location' ||
219- k === 'set-cookie'
239+ k === 'access-control-expose-headers' ||
240+ k === 'location' ||
241+ k === 'set-cookie'
220242 ) {
221243 const x = '--' + k
222244 resHdrNew . set ( x , v )
@@ -277,10 +299,10 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
277299 resHdrNew . delete ( 'clear-site-data' )
278300
279301 if ( status === 301 ||
280- status === 302 ||
281- status === 303 ||
282- status === 307 ||
283- status === 308
302+ status === 302 ||
303+ status === 303 ||
304+ status === 307 ||
305+ status === 308
284306 ) {
285307 status = status + 10
286308 }
@@ -293,7 +315,7 @@ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
293315
294316
295317/**
296- * @param {URL } urlObj
318+ * @param {URL } urlObj
297319 */
298320function isYtUrl ( urlObj ) {
299321 return (
@@ -303,9 +325,9 @@ function isYtUrl(urlObj) {
303325}
304326
305327/**
306- * @param {URL } urlObj
307- * @param {number } newLen
308- * @param {Response } res
328+ * @param {URL } urlObj
329+ * @param {number } newLen
330+ * @param {Response } res
309331 */
310332async function parseYtVideoRedir ( urlObj , newLen , res ) {
311333 if ( newLen > 2000 ) {
@@ -326,8 +348,6 @@ async function parseYtVideoRedir(urlObj, newLen, res) {
326348 return urlObj
327349}
328350
329-
330-
331351/**
332352 * Parse HTTP Basic Authorization value.
333353 * @param {Request } request
0 commit comments