Socialify

Folder ..

Viewing m3u8-proxy.ts
90 lines (76 loc) • 2.9 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import axios, { AxiosRequestConfig } from 'axios';
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';

class M3U8Proxy {
  private getM3U8 = async (url: string, options: AxiosRequestConfig): Promise<any> => {
    const data = await axios.get(url, options);

    return data.data;
  };

  private toQueryString = (obj: any) => {
    const parts = [];
    for (const i in obj) {
      if (obj[i]) {
        parts.push(encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]));
      }
    }
    return parts.join('&');
  };

  public getM3U8Proxy = async (fastify: FastifyInstance, options: RegisterOptions) => {
    fastify.get('/m3u8-proxy/*', async (request: FastifyRequest, reply: FastifyReply) => {
      // split params
      const params = (request.params as any)['*'].split('/');
      const queries = request.query as any;
      console.log('params', params);

      // last element is the url
      const url = params.pop();
      // decode safe base64
      const decodedUrl = Buffer.from(url, 'base64').toString('ascii');

      const domain = Buffer.from(params.join(''), 'base64').toString('ascii');

      // queries to object
      const data = await this.getM3U8(
        decodedUrl.startsWith('https')
          ? decodedUrl
          : domain + url + '?' + this.toQueryString(queries),
        {
          headers: {
            'User-Agent':
              'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
            watchsb: 'streamsb',
          },
        },
      );

      //const decodedData = Buffer.from(data, 'binary').toString('utf8');
      reply.header(
        'Content-Type',
        decodedUrl.startsWith('https') ? 'application/vnd.apple.mpegurl' : 'video/mp2t',
      );
      reply.header('Access-Control-Allow-Origin', '*');
      reply.header('Access-Control-Allow-Headers', '*');
      reply.header('Access-Control-Allow-Methods', '*');

      reply.send(data);
    });

    fastify.get('/m3u8/*', async (request: FastifyRequest, reply: FastifyReply) => {
      const params = (request.params as any)['*'];

      var url = Buffer.from(params, 'base64').toString('utf8');
      try {
        var req = await axios.get(url, {
          headers: {
            'User-Agent':
              'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
          },
        });

        const pattern = new RegExp('https://', 'g');
        const final = req.data
          .toString()
          .replace(pattern, `https://cors.proxy.consumet.org/https://`);

        reply
          .header('Content-Type', 'application/vnd.apple.mpegurl')
          .header('Content-Disposition', 'attachment; filename=stream.m3u8')
          .status(200)
          .send(Buffer.from(final));
      } catch (error) {
        reply.status(400).send(error);
      }
    });
  };
}

export default M3U8Proxy;