Proxy issues fixed
This commit is contained in:
parent
602927a5c7
commit
80d6ecba96
@ -1,16 +1,12 @@
|
||||
import tls, { TLSSocket } from 'tls';
|
||||
import { URL } from 'url';
|
||||
import { Timer } from '../YouTube/classes/LiveStream';
|
||||
|
||||
interface ResponseOptions extends tls.ConnectionOptions {
|
||||
body?: string;
|
||||
method: 'GET' | 'POST';
|
||||
cookies?: boolean;
|
||||
interface ProxyOptions extends tls.ConnectionOptions {
|
||||
method: 'GET';
|
||||
headers?: Object;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export class Response {
|
||||
export class Proxy {
|
||||
parsed_url: URL;
|
||||
statusCode: number;
|
||||
rawHeaders: string;
|
||||
@ -18,18 +14,14 @@ export class Response {
|
||||
body: string;
|
||||
socket: TLSSocket;
|
||||
sentHeaders: string;
|
||||
sentBody: string;
|
||||
private options: ResponseOptions;
|
||||
private timer: Timer | null;
|
||||
constructor(req_url: string, options: ResponseOptions) {
|
||||
this.parsed_url = new URL(req_url);
|
||||
private options: ProxyOptions;
|
||||
constructor(parsed_url: URL, options: ProxyOptions){
|
||||
this.parsed_url = parsed_url;
|
||||
this.sentHeaders = '';
|
||||
this.statusCode = 0;
|
||||
this.sentBody = '';
|
||||
this.rawHeaders = '';
|
||||
this.body = '';
|
||||
this.headers = {};
|
||||
this.timer = null;
|
||||
this.options = options;
|
||||
this.socket = tls.connect(
|
||||
{
|
||||
@ -45,7 +37,6 @@ export class Response {
|
||||
this.sentHeaders += `${key}: ${value}\r\n`;
|
||||
}
|
||||
}
|
||||
if (options.body) this.sentBody = options.body;
|
||||
}
|
||||
|
||||
private onConnect() {
|
||||
@ -54,8 +45,7 @@ export class Response {
|
||||
`Host : ${this.parsed_url.hostname}\r\n` +
|
||||
this.sentHeaders +
|
||||
`Connection: close\r\n` +
|
||||
`\r\n` +
|
||||
this.sentBody
|
||||
`\r\n`
|
||||
);
|
||||
}
|
||||
|
||||
@ -75,21 +65,7 @@ export class Response {
|
||||
}
|
||||
}
|
||||
|
||||
stream(): Promise<TLSSocket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.timer = new Timer(() => this.socket.end(), this.options.timeout || 1);
|
||||
this.socket.once('error', (err) => reject(err));
|
||||
this.socket.once('data', (chunk) => {
|
||||
this.rawHeaders = chunk.toString('utf-8');
|
||||
this.parseHeaders();
|
||||
resolve(this.socket);
|
||||
});
|
||||
this.socket.on('data', () => this.timer?.reuse());
|
||||
this.socket.once('end', () => this.timer?.destroy());
|
||||
});
|
||||
}
|
||||
|
||||
fetch(): Promise<Response> {
|
||||
fetch(): Promise<Proxy> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.socket.setEncoding('utf-8');
|
||||
this.socket.once('error', (err) => reject(err));
|
||||
|
||||
@ -1,6 +1,17 @@
|
||||
import { Response } from './classes';
|
||||
import http, { ClientRequest, IncomingMessage } from 'http';
|
||||
import https ,{ RequestOptions } from 'https';
|
||||
import { URL } from 'url';
|
||||
import { getCookies, setCookie, uploadCookie } from '../YouTube/utils/cookie';
|
||||
import { Proxy } from './classes';
|
||||
|
||||
export type Proxy = ProxyOpts | string;
|
||||
export type ProxyOptions = ProxyOpts | string;
|
||||
|
||||
interface RequestOpts extends RequestOptions{
|
||||
body?: string;
|
||||
method?: 'GET' | 'POST';
|
||||
proxies?: ProxyOptions[];
|
||||
cookies?: boolean;
|
||||
}
|
||||
|
||||
interface ProxyOpts {
|
||||
host: string;
|
||||
@ -10,43 +21,192 @@ interface ProxyOpts {
|
||||
password: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface RequestOptions {
|
||||
body?: string;
|
||||
method: 'GET' | 'POST';
|
||||
proxies?: Proxy[];
|
||||
cookies?: boolean;
|
||||
headers?: Object;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
interface StreamGetterOptions {
|
||||
method: 'GET' | 'POST';
|
||||
cookies?: boolean;
|
||||
headers: Object;
|
||||
}
|
||||
|
||||
export function request_stream(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
|
||||
/**
|
||||
* Main module which play-dl uses to make a request to stream url.
|
||||
* @param url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns IncomingMessage from the request
|
||||
*/
|
||||
export function request_stream(req_url: string, options: RequestOpts = { method: 'GET' }): Promise<IncomingMessage> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = new Response(req_url, options);
|
||||
await res.stream();
|
||||
if (res.statusCode >= 300 && res.statusCode < 400) {
|
||||
res = await request_stream((res.headers as any).location, options);
|
||||
await res.stream();
|
||||
let res = await https_getter(req_url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
}
|
||||
|
||||
export function request(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
|
||||
/**
|
||||
* Main module which play-dl uses to make a proxy or normal request
|
||||
* @param url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns body of that request
|
||||
*/
|
||||
export function request(req_url: string, options: RequestOpts = { method: 'GET' }): Promise<string> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = new Response(req_url, options);
|
||||
await res.fetch();
|
||||
if (!options?.proxies || options.proxies.length === 0) {
|
||||
let data = '';
|
||||
let cookies_added = false;
|
||||
if (options.cookies) {
|
||||
let cook = getCookies();
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie: cook });
|
||||
cookies_added = true;
|
||||
}
|
||||
}
|
||||
let res = await https_getter(req_url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (res.headers && res.headers['set-cookie'] && cookies_added) {
|
||||
res.headers['set-cookie'].forEach((x) => {
|
||||
x.split(';').forEach((x) => {
|
||||
const arr = x.split('=');
|
||||
if (arr.length <= 1) return;
|
||||
const key = arr.shift()?.trim() as string;
|
||||
const value = arr.join('=').trim();
|
||||
setCookie(key, value);
|
||||
});
|
||||
});
|
||||
uploadCookie();
|
||||
}
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await request((res.headers as any).location, options);
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
} else if (Number(res.statusCode) > 400) {
|
||||
reject(new Error(`Got ${res.statusCode} from the request`));
|
||||
}
|
||||
resolve(res);
|
||||
res.setEncoding('utf-8');
|
||||
res.on('data', (c) => (data += c));
|
||||
res.on('end', () => resolve(data));
|
||||
}
|
||||
else {
|
||||
let cookies_added = false;
|
||||
if (options.cookies) {
|
||||
let cook = getCookies();
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie: cook });
|
||||
cookies_added = true;
|
||||
}
|
||||
}
|
||||
let res = await proxy_getter(req_url, options.proxies, options.headers).catch((e: Error) => e);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (res.headers && (res.headers as any)['set-cookie'] && cookies_added) {
|
||||
(res.headers as any)['set-cookie'].forEach((x: string) => {
|
||||
x.split(';').forEach((x) => {
|
||||
const arr = x.split('=');
|
||||
if (arr.length <= 1) return;
|
||||
const key = arr.shift()?.trim() as string;
|
||||
const value = arr.join('=').trim();
|
||||
setCookie(key, value);
|
||||
});
|
||||
});
|
||||
uploadCookie();
|
||||
}
|
||||
if (res.statusCode >= 300 && res.statusCode < 400) {
|
||||
res = await proxy_getter((res.headers as any)['location'], options.proxies, options.headers);
|
||||
} else if (res.statusCode > 400) {
|
||||
reject(new Error(`GOT ${res.statusCode} from proxy request`));
|
||||
}
|
||||
resolve(res.body)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Chooses one random number between max and min number.
|
||||
* @param min Minimum number
|
||||
* @param max Maximum number
|
||||
* @returns Random Number
|
||||
*/
|
||||
function randomIntFromInterval(min: number, max: number): number {
|
||||
let x = Math.floor(Math.random() * (max - min + 1) + min);
|
||||
if (x === 0) return 0;
|
||||
else return x - 1;
|
||||
}
|
||||
/**
|
||||
* Main module that play-dl uses for proxy.
|
||||
* @param req_url URL to make https request to
|
||||
* @param req_proxy Proxies array
|
||||
* @returns Object with statusCode, head and body
|
||||
*/
|
||||
function proxy_getter(req_url : string, req_proxy : ProxyOptions[], headers? : Object): Promise<Proxy>{
|
||||
return new Promise((resolve, reject) => {
|
||||
const proxy: string | ProxyOpts = req_proxy[randomIntFromInterval(0, req_proxy.length)];
|
||||
const parsed_url = new URL(req_url)
|
||||
let opts: ProxyOpts;
|
||||
if (typeof proxy === 'string') {
|
||||
const parsed = new URL(proxy);
|
||||
opts = {
|
||||
host: parsed.hostname,
|
||||
port: Number(parsed.port),
|
||||
authentication: {
|
||||
username: parsed.username,
|
||||
password: parsed.password
|
||||
}
|
||||
};
|
||||
} else opts = proxy;
|
||||
let req: ClientRequest;
|
||||
if (opts.authentication?.username.length === 0) {
|
||||
req = http.request({
|
||||
host: opts.host,
|
||||
port: opts.port,
|
||||
method: 'CONNECT',
|
||||
path: `${parsed_url.host}:443`
|
||||
});
|
||||
} else {
|
||||
req = http.request({
|
||||
host: opts.host,
|
||||
port: opts.port,
|
||||
method: 'CONNECT',
|
||||
path: `${parsed_url.host}:443`,
|
||||
headers: {
|
||||
'Proxy-Authorization': `Basic ${Buffer.from(
|
||||
`${opts.authentication?.username}:${opts.authentication?.password}`
|
||||
).toString('base64')}`
|
||||
}
|
||||
});
|
||||
}
|
||||
req.on('connect', async function (res, socket) {
|
||||
const conn_proxy = new Proxy(parsed_url, { method : "GET", socket : socket, headers : headers })
|
||||
await conn_proxy.fetch()
|
||||
socket.end()
|
||||
resolve(conn_proxy)
|
||||
})
|
||||
req.on('error', (e: Error) => reject(e));
|
||||
req.end();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Main module that play-dl uses for making a https request
|
||||
* @param req_url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns Incoming Message from the https request
|
||||
*/
|
||||
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const s = new URL(req_url);
|
||||
options.method ??= 'GET';
|
||||
const req_options: RequestOptions = {
|
||||
host: s.hostname,
|
||||
path: s.pathname + s.search,
|
||||
headers: options.headers ?? {},
|
||||
method: options.method
|
||||
};
|
||||
|
||||
const req = https.request(req_options, resolve);
|
||||
req.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
if (options.method === 'POST') req.write(options.body);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
import { request, request_stream } from '../YouTube/utils/request';
|
||||
import { request, request_stream } from '../Request';
|
||||
import { Readable } from 'stream';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { StreamType } from '../YouTube/stream';
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import { StreamType } from '../YouTube/stream';
|
||||
import { request } from '../YouTube/utils/request';
|
||||
import { request } from '../Request';
|
||||
import { SoundCloudPlaylist, SoundCloudTrack, SoundCloudTrackFormat, Stream } from './classes';
|
||||
|
||||
let soundData: SoundDataOptions;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { request } from '../YouTube/utils/request';
|
||||
import { request } from '../Request';
|
||||
import { SpotifyDataOptions } from '.';
|
||||
|
||||
interface SpotifyTrackAlbum {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { request } from '../YouTube/utils/request';
|
||||
import { request } from '../Request';
|
||||
import { SpotifyAlbum, SpotifyPlaylist, SpotifyTrack } from './classes';
|
||||
import fs from 'fs';
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { Readable } from 'stream';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { parseAudioFormats, StreamOptions, StreamType } from '../stream';
|
||||
import { Proxy, request, request_stream } from '../utils/request';
|
||||
import { ProxyOptions as Proxy, request, request_stream } from '../../Request';
|
||||
import { video_info } from '..';
|
||||
|
||||
export interface FormatInterface {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
|
||||
import { request } from '../utils/request';
|
||||
import { request } from '../../Request';
|
||||
import { YouTubeChannel } from './Channel';
|
||||
import { YouTubeVideo } from './Video';
|
||||
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { request } from './utils/request';
|
||||
import { request } from './../Request';
|
||||
import { ParseSearchInterface, ParseSearchResult } from './utils/parser';
|
||||
import { YouTubeVideo } from './classes/Video';
|
||||
import { YouTubeChannel } from './classes/Channel';
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { video_info } from '.';
|
||||
import { LiveStreaming, Stream } from './classes/LiveStream';
|
||||
import { Proxy } from './utils/request';
|
||||
import { ProxyOptions as Proxy } from './../Request';
|
||||
|
||||
export enum StreamType {
|
||||
Arbitrary = 'arbitrary',
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { URL } from 'url';
|
||||
import { request } from './request';
|
||||
import { request } from './../../Request';
|
||||
import querystring from 'querystring';
|
||||
|
||||
interface formatOptions {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Proxy, request } from './request';
|
||||
import { ProxyOptions as Proxy, request } from './../../Request/index';
|
||||
import { format_decipher } from './cipher';
|
||||
import { YouTubeVideo } from '../classes/Video';
|
||||
import { YouTubePlayList } from '../classes/Playlist';
|
||||
@ -76,7 +76,7 @@ export async function video_basic_info(url: string, options: InfoOptions = {}) {
|
||||
let video_id: string = extractID(url);
|
||||
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;
|
||||
const body = await request(new_url, {
|
||||
proxies: options.proxy ?? undefined,
|
||||
proxies: options.proxy ?? [],
|
||||
headers: { 'accept-language': 'en-US,en-IN;q=0.9,en;q=0.8,hi;q=0.7' },
|
||||
cookies: true
|
||||
});
|
||||
|
||||
@ -1,249 +0,0 @@
|
||||
import https, { RequestOptions } from 'https';
|
||||
import tls from 'tls';
|
||||
import http, { ClientRequest, IncomingMessage } from 'http';
|
||||
import { URL } from 'url';
|
||||
import { getCookies, setCookie, uploadCookie } from './cookie';
|
||||
/**
|
||||
* Types for Proxy
|
||||
*/
|
||||
export type Proxy = ProxyOpts | string;
|
||||
|
||||
interface ProxyOpts {
|
||||
host: string;
|
||||
port: number;
|
||||
authentication?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface ProxyOutput {
|
||||
statusCode: number;
|
||||
head: string[];
|
||||
body: string;
|
||||
}
|
||||
|
||||
interface RequestOpts extends RequestOptions {
|
||||
body?: string;
|
||||
method?: 'GET' | 'POST';
|
||||
proxies?: Proxy[];
|
||||
cookies?: boolean;
|
||||
}
|
||||
/**
|
||||
* Main module that play-dl uses for making a https request
|
||||
* @param req_url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns Incoming Message from the https request
|
||||
*/
|
||||
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const s = new URL(req_url);
|
||||
options.method ??= 'GET';
|
||||
const req_options: RequestOptions = {
|
||||
host: s.hostname,
|
||||
path: s.pathname + s.search,
|
||||
headers: options.headers ?? {},
|
||||
method: options.method
|
||||
};
|
||||
|
||||
const req = https.request(req_options, resolve);
|
||||
req.on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
if (options.method === 'POST') req.write(options.body);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Chooses one random number between max and min number.
|
||||
* @param min Minimum number
|
||||
* @param max Maximum number
|
||||
* @returns Random Number
|
||||
*/
|
||||
function randomIntFromInterval(min: number, max: number): number {
|
||||
let x = Math.floor(Math.random() * (max - min + 1) + min);
|
||||
if (x === 0) return 0;
|
||||
else return x - 1;
|
||||
}
|
||||
/**
|
||||
* Main module that play-dl uses for proxy.
|
||||
* @param req_url URL to make https request to
|
||||
* @param req_proxy Proxies array
|
||||
* @returns Object with statusCode, head and body
|
||||
*/
|
||||
async function proxy_getter(req_url: string, req_proxy: Proxy[]): Promise<ProxyOutput> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const proxy: string | ProxyOpts = req_proxy[randomIntFromInterval(0, req_proxy.length)];
|
||||
const parsed_url = new URL(req_url);
|
||||
let opts: ProxyOpts;
|
||||
if (typeof proxy === 'string') {
|
||||
const parsed = new URL(proxy);
|
||||
opts = {
|
||||
host: parsed.hostname,
|
||||
port: Number(parsed.port),
|
||||
authentication: {
|
||||
username: parsed.username,
|
||||
password: parsed.password
|
||||
}
|
||||
};
|
||||
} else opts = proxy;
|
||||
let req: ClientRequest;
|
||||
if (opts.authentication?.username.length === 0) {
|
||||
req = http.request({
|
||||
host: opts.host,
|
||||
port: opts.port,
|
||||
method: 'CONNECT',
|
||||
path: `${parsed_url.host}:443`
|
||||
});
|
||||
} else {
|
||||
req = http.request({
|
||||
host: opts.host,
|
||||
port: opts.port,
|
||||
method: 'CONNECT',
|
||||
path: `${parsed_url.host}:443`,
|
||||
headers: {
|
||||
'Proxy-Authorization': `Basic ${Buffer.from(
|
||||
`${opts.authentication?.username}:${opts.authentication?.password}`
|
||||
).toString('base64')}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
req.on('connect', function (res, socket, head) {
|
||||
const tlsConnection = tls.connect(
|
||||
{
|
||||
host: parsed_url.hostname,
|
||||
port: 443,
|
||||
socket: socket,
|
||||
rejectUnauthorized: false
|
||||
},
|
||||
function () {
|
||||
tlsConnection.write(
|
||||
`GET ${parsed_url.pathname}${parsed_url.search} HTTP/1.1\r\n` +
|
||||
`Host : ${parsed_url.hostname}\r\n` +
|
||||
'Connection: close\r\n' +
|
||||
'\r\n'
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
tlsConnection.setEncoding('utf-8');
|
||||
let data = '';
|
||||
tlsConnection.once('error', (e) => reject(e));
|
||||
tlsConnection.on('data', (c) => (data += c));
|
||||
tlsConnection.on('end', () => {
|
||||
const y = data.split('\r\n\r\n');
|
||||
const head = y.shift() as string;
|
||||
resolve({
|
||||
statusCode: Number(head.split('\n')[0].split(' ')[1]),
|
||||
head: head.split('\r\n'),
|
||||
body: y.join('\n')
|
||||
});
|
||||
});
|
||||
});
|
||||
req.on('error', (e: Error) => reject(e));
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Main module which play-dl uses to make a proxy or normal request
|
||||
* @param url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns body of that request
|
||||
*/
|
||||
export async function request(url: string, options: RequestOpts = {}): Promise<string> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (!options?.proxies || options.proxies.length === 0) {
|
||||
let data = '';
|
||||
let cookies_added = false;
|
||||
if (options.cookies) {
|
||||
let cook = getCookies();
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie: cook });
|
||||
cookies_added = true;
|
||||
}
|
||||
}
|
||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (res.headers && res.headers['set-cookie'] && cookies_added) {
|
||||
res.headers['set-cookie'].forEach((x) => {
|
||||
x.split(';').forEach((x) => {
|
||||
const arr = x.split('=');
|
||||
if (arr.length <= 1) return;
|
||||
const key = arr.shift()?.trim() as string;
|
||||
const value = arr.join('=').trim();
|
||||
setCookie(key, value);
|
||||
});
|
||||
});
|
||||
uploadCookie();
|
||||
}
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
} else if (Number(res.statusCode) > 400) {
|
||||
reject(new Error(`Got ${res.statusCode} from the request`));
|
||||
}
|
||||
res.setEncoding('utf-8');
|
||||
res.on('data', (c) => (data += c));
|
||||
res.on('end', () => resolve(data));
|
||||
} else {
|
||||
let cookies_added = false;
|
||||
if (options.cookies) {
|
||||
let cook = getCookies();
|
||||
if (typeof cook === 'string' && options.headers) {
|
||||
Object.assign(options.headers, { cookie: cook });
|
||||
cookies_added = true;
|
||||
}
|
||||
}
|
||||
let res = await proxy_getter(url, options.proxies).catch((e: Error) => e);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (res.head && cookies_added) {
|
||||
let cookies = res.head.filter((x) => x.toLocaleLowerCase().startsWith('set-cookie: '));
|
||||
cookies.forEach((x) => {
|
||||
x.toLocaleLowerCase()
|
||||
.split('set-cookie: ')[1]
|
||||
.split(';')
|
||||
.forEach((y) => {
|
||||
const arr = y.split('=');
|
||||
if (arr.length <= 1) return;
|
||||
const key = arr.shift()?.trim() as string;
|
||||
const value = arr.join('=').trim();
|
||||
setCookie(key, value);
|
||||
});
|
||||
});
|
||||
uploadCookie();
|
||||
}
|
||||
if (res.statusCode >= 300 && res.statusCode < 400) {
|
||||
let url = res.head.filter((x) => x.startsWith('Location: '));
|
||||
res = await proxy_getter(url[0].split('\n')[0], options.proxies);
|
||||
} else if (res.statusCode > 400) {
|
||||
reject(new Error(`GOT ${res.statusCode} from proxy request`));
|
||||
}
|
||||
resolve(res.body);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Main module which play-dl uses to make a request to stream url.
|
||||
* @param url URL to make https request to
|
||||
* @param options Request options for https request
|
||||
* @returns IncomingMessage from the request
|
||||
*/
|
||||
export async function request_stream(url: string, options?: RequestOpts): Promise<IncomingMessage> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||
if (res instanceof Error) {
|
||||
reject(res);
|
||||
return;
|
||||
}
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await https_getter(res.headers.location as string, options);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user