mirror of
https://github.com/YuzuZensai/play-dl-test.git
synced 2026-01-31 14:58:05 +00:00
search validation added
This commit is contained in:
@@ -1,110 +1,111 @@
|
||||
import tls , { TLSSocket } from "tls";
|
||||
import { URL } from "url";
|
||||
import { Timer } from "../YouTube/classes/LiveStream";
|
||||
import tls, { TLSSocket } from 'tls';
|
||||
import { URL } from 'url';
|
||||
import { Timer } from '../YouTube/classes/LiveStream';
|
||||
|
||||
interface ResponseOptions extends tls.ConnectionOptions{
|
||||
interface ResponseOptions extends tls.ConnectionOptions {
|
||||
body?: string;
|
||||
method: 'GET' | 'POST';
|
||||
cookies? : boolean
|
||||
headers? : Object;
|
||||
timeout? : number
|
||||
cookies?: boolean;
|
||||
headers?: Object;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export class Response {
|
||||
parsed_url : URL
|
||||
statusCode : number;
|
||||
rawHeaders : string;
|
||||
headers : Object;
|
||||
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)
|
||||
this.sentHeaders = ''
|
||||
this.statusCode = 0
|
||||
this.sentBody = ""
|
||||
this.rawHeaders = ''
|
||||
this.body = ''
|
||||
this.headers = {}
|
||||
this.timer = null
|
||||
this.options = options
|
||||
this.socket = tls.connect({
|
||||
host : this.parsed_url.hostname,
|
||||
port : Number(this.parsed_url.port) || 443,
|
||||
socket : options.socket,
|
||||
rejectUnauthorized : false
|
||||
}, () => this.onConnect())
|
||||
if(options.headers){
|
||||
for(const [ key, value ] of Object.entries(options.headers)){
|
||||
this.sentHeaders += `${key}: ${value}\r\n`
|
||||
parsed_url: URL;
|
||||
statusCode: number;
|
||||
rawHeaders: string;
|
||||
headers: Object;
|
||||
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);
|
||||
this.sentHeaders = '';
|
||||
this.statusCode = 0;
|
||||
this.sentBody = '';
|
||||
this.rawHeaders = '';
|
||||
this.body = '';
|
||||
this.headers = {};
|
||||
this.timer = null;
|
||||
this.options = options;
|
||||
this.socket = tls.connect(
|
||||
{
|
||||
host: this.parsed_url.hostname,
|
||||
port: Number(this.parsed_url.port) || 443,
|
||||
socket: options.socket,
|
||||
rejectUnauthorized: false
|
||||
},
|
||||
() => this.onConnect()
|
||||
);
|
||||
if (options.headers) {
|
||||
for (const [key, value] of Object.entries(options.headers)) {
|
||||
this.sentHeaders += `${key}: ${value}\r\n`;
|
||||
}
|
||||
}
|
||||
if(options.body) this.sentBody = options.body
|
||||
if (options.body) this.sentBody = options.body;
|
||||
}
|
||||
|
||||
private onConnect(){
|
||||
private onConnect() {
|
||||
this.socket.write(
|
||||
`${this.options.method} ${this.parsed_url.pathname}${this.parsed_url.search} HTTP/1.1\r\n` +
|
||||
`Host : ${this.parsed_url.hostname}\r\n` +
|
||||
this.sentHeaders +
|
||||
`Connection: close\r\n` +
|
||||
`\r\n` +
|
||||
this.sentBody
|
||||
)
|
||||
`${this.options.method} ${this.parsed_url.pathname}${this.parsed_url.search} HTTP/1.1\r\n` +
|
||||
`Host : ${this.parsed_url.hostname}\r\n` +
|
||||
this.sentHeaders +
|
||||
`Connection: close\r\n` +
|
||||
`\r\n` +
|
||||
this.sentBody
|
||||
);
|
||||
}
|
||||
|
||||
private parseHeaders(){
|
||||
const head_arr = this.rawHeaders.split('\r\n')
|
||||
this.statusCode = Number(head_arr.shift()?.split(' ')[1]) ?? -1
|
||||
for(const head of head_arr){
|
||||
let [ key, value ] = head.split(': ')
|
||||
if(!value) break;
|
||||
key = key.trim().toLowerCase()
|
||||
value = value.trim()
|
||||
if(Object.keys(this.headers).includes(key)){
|
||||
let val = (this.headers as any)[key]
|
||||
if(typeof val === 'string') val = [val]
|
||||
Object.assign(this.headers, { [key]: [...val, value] })
|
||||
}
|
||||
else Object.assign(this.headers, { [key] : value })
|
||||
private parseHeaders() {
|
||||
const head_arr = this.rawHeaders.split('\r\n');
|
||||
this.statusCode = Number(head_arr.shift()?.split(' ')[1]) ?? -1;
|
||||
for (const head of head_arr) {
|
||||
let [key, value] = head.split(': ');
|
||||
if (!value) break;
|
||||
key = key.trim().toLowerCase();
|
||||
value = value.trim();
|
||||
if (Object.keys(this.headers).includes(key)) {
|
||||
let val = (this.headers as any)[key];
|
||||
if (typeof val === 'string') val = [val];
|
||||
Object.assign(this.headers, { [key]: [...val, value] });
|
||||
} else Object.assign(this.headers, { [key]: value });
|
||||
}
|
||||
}
|
||||
|
||||
stream(): Promise<TLSSocket>{
|
||||
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.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())
|
||||
})
|
||||
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<Response> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.socket.setEncoding('utf-8');
|
||||
this.socket.once('error', (err) => reject(err))
|
||||
this.socket.once('error', (err) => reject(err));
|
||||
this.socket.on('data', (chunk: string) => {
|
||||
if(this.rawHeaders.length === 0){
|
||||
this.rawHeaders = chunk
|
||||
this.parseHeaders()
|
||||
if (this.rawHeaders.length === 0) {
|
||||
this.rawHeaders = chunk;
|
||||
this.parseHeaders();
|
||||
} else {
|
||||
const arr = chunk.split('\r\n');
|
||||
if (arr.length > 1 && arr[0].length < 5) arr.shift();
|
||||
this.body += arr.join('');
|
||||
}
|
||||
else {
|
||||
const arr = chunk.split('\r\n')
|
||||
if(arr.length > 1 && arr[0].length < 5) arr.shift()
|
||||
this.body += arr.join('')
|
||||
}
|
||||
})
|
||||
});
|
||||
this.socket.on('end', () => {
|
||||
resolve(this)
|
||||
})
|
||||
})
|
||||
resolve(this);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Response } from "./classes";
|
||||
|
||||
import { Response } from './classes';
|
||||
|
||||
export type Proxy = ProxyOpts | string;
|
||||
|
||||
@@ -16,38 +15,38 @@ interface RequestOptions {
|
||||
body?: string;
|
||||
method: 'GET' | 'POST';
|
||||
proxies?: Proxy[];
|
||||
cookies? : boolean
|
||||
headers? : Object;
|
||||
timeout? : number
|
||||
cookies?: boolean;
|
||||
headers?: Object;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
interface StreamGetterOptions{
|
||||
interface StreamGetterOptions {
|
||||
method: 'GET' | 'POST';
|
||||
cookies? : boolean
|
||||
headers : Object;
|
||||
cookies?: boolean;
|
||||
headers: Object;
|
||||
}
|
||||
|
||||
export function request_stream(req_url : string, options : RequestOptions = {method : "GET"}): Promise<Response>{
|
||||
return new Promise(async(resolve, reject) => {
|
||||
let res = new Response(req_url, options)
|
||||
await res.stream()
|
||||
export function request_stream(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
|
||||
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()
|
||||
await res.stream();
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
resolve(res);
|
||||
});
|
||||
}
|
||||
|
||||
export function request(req_url : string, options : RequestOptions = {method : "GET"}): Promise<Response>{
|
||||
return new Promise(async(resolve, reject) => {
|
||||
let res = new Response(req_url, options)
|
||||
await res.fetch()
|
||||
export function request(req_url: string, options: RequestOptions = { method: 'GET' }): Promise<Response> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = new Response(req_url, options);
|
||||
await res.fetch();
|
||||
if (Number(res.statusCode) >= 300 && Number(res.statusCode) < 400) {
|
||||
res = await request((res.headers as any).location, options);
|
||||
} else if (Number(res.statusCode) > 400) {
|
||||
reject(new Error(`Got ${res.statusCode} from the request`));
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user