| 45 | } |
| 46 | |
| 47 | export async function getStreamInfo( |
| 48 | url: string, |
| 49 | options: { |
| 50 | httpUserAgent?: string | null |
| 51 | httpReferrer?: string | null |
| 52 | timeout?: number |
| 53 | proxy?: string |
| 54 | } |
| 55 | ): Promise<StreamInfo | undefined> { |
| 56 | let data: string | undefined |
| 57 | if (TESTING) { |
| 58 | if (url.includes('.m3u8')) { |
| 59 | data = fs.readFileSync( |
| 60 | path.resolve(__dirname, '../tests/__data__/input/playlist_update/playlist.m3u8'), |
| 61 | 'utf8' |
| 62 | ) |
| 63 | } else if (url.includes('.mpd')) { |
| 64 | data = fs.readFileSync( |
| 65 | path.resolve(__dirname, '../tests/__data__/input/playlist_update/manifest.mpd'), |
| 66 | 'utf8' |
| 67 | ) |
| 68 | } |
| 69 | } else { |
| 70 | try { |
| 71 | const timeout = options.timeout || 1000 |
| 72 | let request: AxiosRequestConfig = { |
| 73 | signal: AbortSignal.timeout(timeout), |
| 74 | responseType: 'text', |
| 75 | headers: { |
| 76 | 'User-Agent': options.httpUserAgent || 'Mozilla/5.0', |
| 77 | Referer: options.httpReferrer |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (options.proxy !== undefined) { |
| 82 | const proxyParser = new ProxyParser() |
| 83 | const proxy = proxyParser.parse(options.proxy) as AxiosProxyConfig |
| 84 | if ( |
| 85 | proxy.protocol && |
| 86 | ['socks', 'socks5', 'socks5h', 'socks4', 'socks4a'].includes(String(proxy.protocol)) |
| 87 | ) { |
| 88 | const socksProxyAgent = new SocksProxyAgent(options.proxy) |
| 89 | |
| 90 | request = { ...request, ...{ httpAgent: socksProxyAgent, httpsAgent: socksProxyAgent } } |
| 91 | } else { |
| 92 | request = { ...request, ...{ proxy } } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | const response = await axios(url, request) |
| 97 | |
| 98 | data = response.data |
| 99 | } catch { |
| 100 | // do nothing |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!data) return undefined |