Proxy support added completely
This commit is contained in:
parent
f21f34ea31
commit
9d946a84a6
@ -47,7 +47,7 @@ export class LiveStreaming {
|
|||||||
if (
|
if (
|
||||||
info.LiveStreamData.isLive === true &&
|
info.LiveStreamData.isLive === true &&
|
||||||
info.LiveStreamData.hlsManifestUrl !== null &&
|
info.LiveStreamData.hlsManifestUrl !== null &&
|
||||||
info.video_details.durationInSec === '0'
|
info.video_details.durationInSec === 0
|
||||||
) {
|
) {
|
||||||
this.url = info.LiveStreamData.dashManifestUrl;
|
this.url = info.LiveStreamData.dashManifestUrl;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
import https, { RequestOptions } from 'https';
|
import https, { RequestOptions } from 'https';
|
||||||
import { IncomingMessage } from 'http';
|
import tls from 'tls';
|
||||||
|
import http , { ClientRequest, IncomingMessage } from 'http';
|
||||||
import { URL } from 'url';
|
import { URL } from 'url';
|
||||||
|
|
||||||
|
export type Proxy = ProxyOpts | string
|
||||||
|
|
||||||
interface ProxyOpts {
|
interface ProxyOpts {
|
||||||
host : string,
|
host : string,
|
||||||
port : number,
|
port : number,
|
||||||
@ -11,10 +14,16 @@ interface ProxyOpts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ProxyOutput {
|
||||||
|
statusCode : number;
|
||||||
|
head : string;
|
||||||
|
body : string;
|
||||||
|
}
|
||||||
|
|
||||||
interface RequestOpts extends RequestOptions {
|
interface RequestOpts extends RequestOptions {
|
||||||
body?: string;
|
body?: string;
|
||||||
method?: 'GET' | 'POST';
|
method?: 'GET' | 'POST';
|
||||||
proxies? : string[] | ProxyOpts[]
|
proxies? : Proxy[]
|
||||||
}
|
}
|
||||||
|
|
||||||
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
|
function https_getter(req_url: string, options: RequestOpts = {}): Promise<IncomingMessage> {
|
||||||
@ -37,8 +46,86 @@ function https_getter(req_url: string, options: RequestOpts = {}): Promise<Incom
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
console.log('Connected')
|
||||||
|
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,
|
||||||
|
body : y.join('\n')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
req.on('error', (e : Error) => reject(e))
|
||||||
|
req.end()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export async function request(url: string, options?: RequestOpts): Promise<string> {
|
export async function request(url: string, options?: RequestOpts): Promise<string> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if(!options?.proxies){
|
||||||
let data = '';
|
let data = '';
|
||||||
let res = await https_getter(url, options).catch((err: Error) => err);
|
let res = await https_getter(url, options).catch((err: Error) => err);
|
||||||
if (res instanceof Error) {
|
if (res instanceof Error) {
|
||||||
@ -53,6 +140,20 @@ export async function request(url: string, options?: RequestOpts): Promise<strin
|
|||||||
res.setEncoding('utf-8');
|
res.setEncoding('utf-8');
|
||||||
res.on('data', (c) => (data += c));
|
res.on('data', (c) => (data += c));
|
||||||
res.on('end', () => resolve(data));
|
res.on('end', () => resolve(data));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let res = await proxy_getter(url, options.proxies).catch((e : Error) => e)
|
||||||
|
if (res instanceof Error) {
|
||||||
|
reject(res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(res.statusCode >= 300 && res.statusCode < 400){
|
||||||
|
res = await proxy_getter(res.head.split('Location: ')[1].split('\n')[0], options.proxies)
|
||||||
|
} else if (res.statusCode > 400){
|
||||||
|
reject(new Error(`GOT ${res.statusCode} from proxy request`))
|
||||||
|
}
|
||||||
|
resolve(res.body)
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user