warren-bank / HLS-Proxy

Node.js server to proxy HLS video streams
http://webcast-reloaded.surge.sh/proxy.html
GNU General Public License v2.0
244 stars 75 forks source link

embed in express app? #3

Open AlJohri opened 5 years ago

AlJohri commented 5 years ago

I tried modifying your code a bit to be able to embed it within an existing express app. I ran into some issues with the regexes not working and ended up rolling my own quick and dirty solution.

Can you help me understand some of the complexity in the proxy code? My implementation below works well for my fairly particular use case (bypassing CORS restriction) but I wanted to better understand how this might fail in more complicated scenarios that your code presumably is built to handle.

app.get("/proxy/", async function(req, res, next) {
  const protocol = req.protocol;
  const host = req.get("Host"); // host with port
  const source = req.query.url;
  console.log(`proxying ${source}`);
  const agent = stalker.agent;
  const headers = {
    "User-Agent": "Lavf/56.36.100",
    "Icy-Metadata": 1
  };
  const response = await fetch(source, { agent, headers });
  if (source.includes("m3u8?token=")) {
    const text = await response.text();
    const newTextBuilder = [];
    for (line of text.split("\n")) {
      if (line.includes("m3u8?token=") || line.includes("ts?token=")) {
        const parent = source
          .split("/")
          .slice(0, -1)
          .join("/");
        newTextBuilder.push(
          `${protocol}://${host}/proxy/?url=${parent}/${line}`
        );
      } else {
        newTextBuilder.push(line);
      }
    }
    const newText = newTextBuilder.join("\n");
    const responseHeaders = Object.fromEntries(
      Object.entries(response.headers.raw()).map(([k, v]) => [k, v[0]])
    );
    res.set({
      "Content-Type": responseHeaders['content-type']
    });
    try {
      res.send(newText);
    } catch (e) {
      console.error(e);
      console.log(newText);
    }
  } else {
    response.body.pipe(res);
  }
});

P.S. The m3u8 regex was failing on http://11.111.111.111:9999/AajTak/index.m3u8?token=8e55714a60ec4b14a536720df79355ab. Didn't debug much farther than that.

XanXus96 commented 1 year ago

hello @AlJohri, if it did work for you please care to share the solution, i need this

warren-bank commented 1 year ago

examples:

  1. basic usage
  2. advanced usage
warren-bank commented 1 year ago

suggestion:

since there is no documentation that describes the shape of the input parameter to this method:

const get_middleware = function(params) {
  //...
}

an easy way to configure its value is to:

  1. prepend some code to this method.. to dump the value of its input parameter to stdout:
     const get_middleware = function(params) {
       console.log(JSON.stringify(params, null, 2))
       process.exit(0)
     }
  2. use the command-line entry point
    • configure your options as normal
    • pipe stdout to a log file

now, you can copy the value of your configured params (as JSON) from the log file, and use it in your Express.js project (similarly to the examples, above):

  const middleware = require('@warren-bank/hls-proxy/proxy')(params)