@@ -8,18 +8,19 @@ import jwt from "../security/jwt.js";
88import stream from "../stream/stream.js" ;
99import match from "../processing/match.js" ;
1010
11- import { env , isCluster , setTunnelPort } from "../config.js" ;
11+ import { env } from "../config.js" ;
1212import { extract } from "../processing/url.js" ;
13- import { Green , Bright , Cyan } from "../misc/console-text.js" ;
13+ import { Bright , Cyan } from "../misc/console-text.js" ;
1414import { hashHmac } from "../security/secrets.js" ;
1515import { createStore } from "../store/redis-ratelimit.js" ;
1616import { randomizeCiphers } from "../misc/randomize-ciphers.js" ;
1717import { verifyTurnstileToken } from "../security/turnstile.js" ;
1818import { friendlyServiceName } from "../processing/service-alias.js" ;
19- import { verifyStream , getInternalStream } from "../stream/manage.js" ;
19+ import { verifyStream } from "../stream/manage.js" ;
2020import { createResponse , normalizeRequest , getIP } from "../processing/request.js" ;
2121import * as APIKeys from "../security/api-keys.js" ;
2222import * as Cookies from "../processing/cookie/manager.js" ;
23+ import { setupTunnelHandler } from "./itunnel.js" ;
2324
2425const git = {
2526 branch : await getBranch ( ) ,
@@ -263,6 +264,15 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
263264 }
264265 } )
265266
267+ app . use ( '/tunnel' , cors ( {
268+ methods : [ 'GET' ] ,
269+ exposedHeaders : [
270+ 'Estimated-Content-Length' ,
271+ 'Content-Disposition'
272+ ] ,
273+ ...corsConfig ,
274+ } ) ) ;
275+
266276 app . get ( '/tunnel' , apiTunnelLimiter , async ( req , res ) => {
267277 const id = String ( req . query . id ) ;
268278 const exp = String ( req . query . exp ) ;
@@ -292,31 +302,7 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
292302 }
293303
294304 return stream ( res , streamInfo ) ;
295- } )
296-
297- const itunnelHandler = ( req , res ) => {
298- if ( ! req . ip . endsWith ( '127.0.0.1' ) ) {
299- return res . sendStatus ( 403 ) ;
300- }
301-
302- if ( String ( req . query . id ) . length !== 21 ) {
303- return res . sendStatus ( 400 ) ;
304- }
305-
306- const streamInfo = getInternalStream ( req . query . id ) ;
307- if ( ! streamInfo ) {
308- return res . sendStatus ( 404 ) ;
309- }
310-
311- streamInfo . headers = new Map ( [
312- ...( streamInfo . headers || [ ] ) ,
313- ...Object . entries ( req . headers )
314- ] ) ;
315-
316- return stream ( res , { type : 'internal' , ...streamInfo } ) ;
317- } ;
318-
319- app . get ( '/itunnel' , itunnelHandler ) ;
305+ } ) ;
320306
321307 app . get ( '/' , ( _ , res ) => {
322308 res . type ( 'json' ) ;
@@ -378,17 +364,5 @@ export const runAPI = async (express, app, __dirname, isPrimary = true) => {
378364 }
379365 } ) ;
380366
381- if ( isCluster ) {
382- const istreamer = express ( ) ;
383- istreamer . get ( '/itunnel' , itunnelHandler ) ;
384- const server = istreamer . listen ( {
385- port : 0 ,
386- host : '127.0.0.1' ,
387- exclusive : true
388- } , ( ) => {
389- const { port } = server . address ( ) ;
390- console . log ( `${ Green ( '[✓]' ) } cobalt sub-instance running on 127.0.0.1:${ port } ` ) ;
391- setTunnelPort ( port ) ;
392- } ) ;
393- }
367+ setupTunnelHandler ( ) ;
394368}
0 commit comments