@warren-bank/hls-proxy 3.2.4 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -110,7 +110,7 @@ options:
110
110
  --help
111
111
  --version
112
112
  --tls
113
- --host <ip_address>
113
+ --host <host>
114
114
  --port <number>
115
115
  --req-headers <filepath>
116
116
  --origin <header>
@@ -130,6 +130,7 @@ options:
130
130
  --cache-key <number>
131
131
  -v <number>
132
132
  --acl-whitelist <ip_address_list>
133
+ --http-proxy <http[s]://[user:pass@]hostname:port>
133
134
  --tls-cert <filepath>
134
135
  --tls-key <filepath>
135
136
  --tls-pass <filepath>
@@ -342,6 +343,9 @@ options:
342
343
  * show the content of .m3u8 files (both before and after URLs are modified)
343
344
  * _--acl-whitelist_ restricts proxy server access to clients at IP addresses in whitelist
344
345
  * ex: `"192.168.1.100,192.168.1.101,192.168.1.102"`
346
+ * --http-proxy enables all outbound HTTP and HTTPS requests from HLS-Proxy to be tunnelled through an additional external web proxy server
347
+ * SOCKS proxies are not supported
348
+ * ex: `http://myusername:mypassword@myproxy.example.com:1234`
345
349
  * _--tls-cert_ is the filepath to a security certificate to use for HTTPS
346
350
  * _--tls-key_ is the filepath to the private key for the _--tls-cert_ security certificate
347
351
  * _--tls-pass_ is the filepath to a text file containing the security passphrase for the _--tls-key_ private key
@@ -600,7 +604,8 @@ curl --silent --insecure "$URL"
600
604
  * internal `proxy` module exports an Object containing event listeners to process requests that can be either:
601
605
  - added to an instance of [`http.Server`](https://nodejs.org/api/http.html#class-httpserver)
602
606
  - added to an [`Express.js`](https://github.com/expressjs/express) application as middleware to handle a custom route
603
- * important limitation: since `/` is a valid character in a base64 encoded URL, the path for a custom route needs to end with a character that is not allowed in base64 encoding (ex: `'/proxy_/*'`)
607
+ * important requirement: the path for a custom route needs to include exactly one unnamed [parameter](https://expressjs.com/en/guide/routing.html#route-parameters) that matches the base64 encoded URL and (optionally) a file extension (ex: `'/proxy/*'`)
608
+ * the use of nested routers is supported
604
609
  - system requirements:
605
610
  * Node.js v16.0.0 and higher
606
611
  - required features: [`Proxy` constructor](https://node.green/#ES2015-built-ins-Proxy-constructor-requires-new), [`Proxy` 'apply' handler](https://node.green/#ES2015-built-ins-Proxy--apply--handler), [`Reflect.apply`](https://node.green/#ES2015-built-ins-Reflect-Reflect-apply), [`RegExp` 'd' flag](https://node.green/#ES2022-features-RegExp-Match-Indices---hasIndices-----d--flag-)
@@ -42,7 +42,8 @@ const middleware = require('../proxy')({
42
42
  cache_timeout: argv_vals["--cache-timeout"],
43
43
  cache_key: argv_vals["--cache-key"],
44
44
  debug_level: argv_vals["-v"],
45
- acl_whitelist: argv_vals["--acl-whitelist"]
45
+ acl_whitelist: argv_vals["--acl-whitelist"],
46
+ http_proxy: argv_vals["--http-proxy"]
46
47
  })
47
48
 
48
49
  if (middleware.connection)
@@ -6,7 +6,7 @@ options:
6
6
  --help
7
7
  --version
8
8
  --tls
9
- --host <ip_address>
9
+ --host <host>
10
10
  --port <number>
11
11
  --req-headers <filepath>
12
12
  --origin <header>
@@ -26,6 +26,7 @@ options:
26
26
  --cache-key <number>
27
27
  -v <number>
28
28
  --acl-whitelist <ip_address_list>
29
+ --http-proxy <http[s]://[user:pass@]hostname:port>
29
30
  --tls-cert <filepath>
30
31
  --tls-key <filepath>
31
32
  --tls-pass <filepath>
@@ -1,5 +1,7 @@
1
1
  const process_argv = require('@warren-bank/node-process-argv')
2
2
 
3
+ const {HttpProxyAgent, HttpsProxyAgent} = require('hpagent')
4
+
3
5
  const argv_flags = {
4
6
  "--help": {bool: true},
5
7
  "--version": {bool: true},
@@ -30,6 +32,7 @@ const argv_flags = {
30
32
 
31
33
  "-v": {num: "int"},
32
34
  "--acl-whitelist": {},
35
+ "--http-proxy": {},
33
36
 
34
37
  "--tls-cert": {file: "path-exists"},
35
38
  "--tls-key": {file: "path-exists"},
@@ -37,7 +40,8 @@ const argv_flags = {
37
40
  }
38
41
 
39
42
  const argv_flag_aliases = {
40
- "--help": ["-h"]
43
+ "--help": ["-h"],
44
+ "--http-proxy": ["--https-proxy", "--proxy"]
41
45
  }
42
46
 
43
47
  let argv_vals = {}
@@ -160,4 +164,19 @@ if (typeof argv_vals["--cache-key"] !== 'number')
160
164
  if (typeof argv_vals["-v"] !== 'number')
161
165
  argv_vals["-v"] = 0
162
166
 
167
+ if (argv_vals["--http-proxy"]) {
168
+ const proxy_options = {
169
+ keepAlive: true,
170
+ keepAliveMsecs: 1000,
171
+ maxSockets: 256,
172
+ maxFreeSockets: 256,
173
+ proxy: argv_vals["--http-proxy"]
174
+ }
175
+
176
+ argv_vals["--http-proxy"] = {
177
+ "http:": new HttpProxyAgent( proxy_options),
178
+ "https:": new HttpsProxyAgent(proxy_options)
179
+ }
180
+ }
181
+
163
182
  module.exports = argv_vals
@@ -1,32 +1,32 @@
1
- const get_full_req_url = function(req) {
2
- return req.originalUrl || req.url
3
- }
4
-
5
- const has_req_param = function(req, key) {
6
- return (req.params && (typeof req.params === 'object') && req.params[key])
7
- }
8
-
9
- const get_proxy_req_url = function(req) {
10
- const key = "0"
11
- return has_req_param(req, key)
12
- ? `/${req.params[key]}`
13
- : req.url
14
- }
15
-
16
- const get_base_req_url = function(req) {
17
- let base_url = ''
18
- const key = "0"
19
-
20
- if (req.path && has_req_param(req, key)) {
21
- base_url = req.baseUrl || ''
22
- base_url += req.path.substring(0, (req.path.length - req.params[key].length - 1))
23
- }
24
-
25
- return base_url
26
- }
27
-
28
- module.exports = {
29
- get_full_req_url,
30
- get_proxy_req_url,
31
- get_base_req_url
32
- }
1
+ const get_full_req_url = function(req) {
2
+ return req.originalUrl || req.url
3
+ }
4
+
5
+ const has_req_param = function(req, key) {
6
+ return (req.params && (typeof req.params === 'object') && req.params[key])
7
+ }
8
+
9
+ const get_proxy_req_url = function(req) {
10
+ const key = "0"
11
+ return has_req_param(req, key)
12
+ ? `/${req.params[key]}`
13
+ : req.url
14
+ }
15
+
16
+ const get_base_req_url = function(req) {
17
+ let base_url = ''
18
+ const key = "0"
19
+
20
+ if (req.path && has_req_param(req, key)) {
21
+ base_url = req.baseUrl || ''
22
+ base_url += req.path.substring(0, (req.path.length - req.params[key].length - 1))
23
+ }
24
+
25
+ return base_url
26
+ }
27
+
28
+ module.exports = {
29
+ get_full_req_url,
30
+ get_proxy_req_url,
31
+ get_base_req_url
32
+ }
@@ -16,9 +16,6 @@ const url_location_landmarks = {
16
16
  '#EXT-X-RENDITION-REPORT:',
17
17
  '#EXT-X-DATERANGE:',
18
18
  '#EXT-X-CONTENT-STEERING:'
19
- ],
20
- next_line: [
21
- '#EXT-X-STREAM-INF:'
22
19
  ]
23
20
  },
24
21
  ts: {
@@ -26,26 +23,20 @@ const url_location_landmarks = {
26
23
  '#EXT-X-MAP:',
27
24
  '#EXT-X-PART:',
28
25
  '#EXT-X-PRELOAD-HINT:'
29
- ],
30
- next_line: [
31
- '#EXTINF:'
32
26
  ]
33
27
  },
34
28
  json: {
35
29
  same_line: [
36
30
  '#EXT-X-SESSION-DATA:'
37
- ],
38
- next_line: []
31
+ ]
39
32
  },
40
33
  key: {
41
34
  same_line: [
42
35
  '#EXT-X-KEY:'
43
- ],
44
- next_line: []
36
+ ]
45
37
  },
46
38
  other: {
47
- same_line: [],
48
- next_line: []
39
+ same_line: []
49
40
  }
50
41
  }
51
42
 
@@ -148,63 +139,82 @@ const parse_manifest = function(m3u8_content, m3u8_url, referer_url, hooks, cach
148
139
  const extract_embedded_urls = function(m3u8_lines, m3u8_url, referer_url, meta_data) {
149
140
  const embedded_urls = []
150
141
 
151
- let m3u8_line, has_next_m3u8_line, next_m3u8_line, matches, matching_landmark, matching_url
142
+ // one of: ['master','media']
143
+ // determined by the detection of either: ['#EXT-X-STREAM-INF','#EXTINF'], respectively
144
+ // until the type is determined, URI lines are ignored (as per the HLS spec)
145
+ let manifest_type = null
146
+
147
+ let m3u8_line, matches, matching_landmark, matching_url
152
148
 
153
149
  for (let i=0; i < m3u8_lines.length; i++) {
154
- m3u8_line = m3u8_lines[i]
155
- has_next_m3u8_line = ((i+1) < m3u8_lines.length)
156
-
157
- matches = regexs.m3u8_line_landmark.exec(m3u8_line)
158
- if (!matches) continue
159
- matching_landmark = matches[1]
160
-
161
- matches = regexs.m3u8_line_url.exec(m3u8_line)
162
- matching_url = matches
163
- ? matches[1]
164
- : null
165
-
166
- if (meta_data !== null)
167
- extract_meta_data(meta_data, m3u8_line, matching_landmark)
168
-
169
- for (let url_type in url_location_landmarks) {
170
- if (matching_url && (url_location_landmarks[url_type]['same_line'].indexOf(matching_landmark) >= 0)) {
171
- embedded_urls.push({
172
- line_index: i,
173
- url_indices: matches.indices[1],
174
- url_type: url_type,
175
- original_match_url: matching_url,
176
- resolved_match_url: (new URL(matching_url, m3u8_url)).href,
177
- redirected_url: null,
178
- referer_url: referer_url,
179
- encoded_url: null
180
- })
181
- break
150
+ m3u8_line = m3u8_lines[i]
151
+
152
+ if (is_m3u8_line_a_blank(m3u8_line) || is_m3u8_line_a_comment(m3u8_line))
153
+ continue
154
+
155
+ if (is_m3u8_line_a_tag(m3u8_line)) {
156
+ matches = regexs.m3u8_line_landmark.exec(m3u8_line)
157
+ if (!matches) continue
158
+ matching_landmark = matches[1]
159
+
160
+ if (manifest_type === null) {
161
+ if (matching_landmark === '#EXT-X-STREAM-INF:')
162
+ manifest_type = 'master'
163
+ else if (matching_landmark === '#EXTINF:')
164
+ manifest_type = 'media'
182
165
  }
183
- if (has_next_m3u8_line && (url_location_landmarks[url_type]['next_line'].indexOf(matching_landmark) >= 0)) {
184
- next_m3u8_line = m3u8_lines[i+1].trim()
185
166
 
186
- if (next_m3u8_line && (next_m3u8_line[0] !== '#')) {
187
- i++
167
+ if (meta_data !== null)
168
+ extract_meta_data(meta_data, m3u8_line, matching_landmark)
188
169
 
170
+ matches = regexs.m3u8_line_url.exec(m3u8_line)
171
+ if (!matches) continue
172
+ matching_url = matches[1]
173
+
174
+ for (let url_type in url_location_landmarks) {
175
+ if (url_location_landmarks[url_type]['same_line'].indexOf(matching_landmark) >= 0) {
189
176
  embedded_urls.push({
190
177
  line_index: i,
191
- url_indices: null,
178
+ url_indices: matches.indices[1],
192
179
  url_type: url_type,
193
- original_match_url: next_m3u8_line,
194
- resolved_match_url: (new URL(next_m3u8_line, m3u8_url)).href,
180
+ original_match_url: matching_url,
181
+ resolved_match_url: (new URL(matching_url, m3u8_url)).href,
195
182
  redirected_url: null,
196
183
  referer_url: referer_url,
197
184
  encoded_url: null
198
185
  })
186
+ break
199
187
  }
200
- break
201
188
  }
202
189
  }
190
+ else {
191
+ // line is a URI
192
+ if (manifest_type === null) continue
193
+
194
+ const url_type = (manifest_type === 'master') ? 'm3u8' : 'ts'
195
+
196
+ embedded_urls.push({
197
+ line_index: i,
198
+ url_indices: null,
199
+ url_type: url_type,
200
+ original_match_url: m3u8_line,
201
+ resolved_match_url: (new URL(m3u8_line, m3u8_url)).href,
202
+ redirected_url: null,
203
+ referer_url: referer_url,
204
+ encoded_url: null
205
+ })
206
+ }
203
207
  }
204
208
 
205
209
  return embedded_urls
206
210
  }
207
211
 
212
+ const is_m3u8_line_a_blank = (line) => (!line || !line.trim())
213
+
214
+ const is_m3u8_line_a_comment = (line) => ((line.indexOf('#') === 0) && !is_m3u8_line_a_tag(line))
215
+
216
+ const is_m3u8_line_a_tag = (line) => (line.indexOf('#EXT') === 0)
217
+
208
218
  const extract_meta_data = function(meta_data, m3u8_line, matching_landmark) {
209
219
  for (let meta_data_key in meta_data_location_landmarks) {
210
220
  if (meta_data_location_landmarks[meta_data_key]['same_line'].indexOf(matching_landmark) >= 0) {
@@ -97,7 +97,7 @@ const debug = function() {
97
97
  }
98
98
 
99
99
  const get_request_options = function(params, url, is_m3u8, referer_url) {
100
- const {req_headers, req_options, hooks} = params
100
+ const {req_headers, req_options, hooks, http_proxy} = params
101
101
 
102
102
  const additional_req_options = (hooks && (hooks instanceof Object) && hooks.add_request_options && (typeof hooks.add_request_options === 'function'))
103
103
  ? hooks.add_request_options(url, is_m3u8)
@@ -107,7 +107,7 @@ const get_request_options = function(params, url, is_m3u8, referer_url) {
107
107
  ? hooks.add_request_headers(url, is_m3u8)
108
108
  : null
109
109
 
110
- if (!req_options && !additional_req_options && !req_headers && !additional_req_headers && !referer_url) return url
110
+ if (!req_options && !http_proxy && !additional_req_options && !req_headers && !additional_req_headers && !referer_url) return url
111
111
 
112
112
  const request_options = Object.assign(
113
113
  {},
@@ -125,6 +125,13 @@ const get_request_options = function(params, url, is_m3u8, referer_url) {
125
125
  (referer_url ? {"referer": referer_url, "origin": referer_url.replace(regexs.origin, '$1')} : {})
126
126
  )
127
127
 
128
+ // normalize
129
+ if (request_options.protocol)
130
+ request_options.protocol = request_options.protocol.toLowerCase()
131
+
132
+ if (!request_options.agent && http_proxy && request_options.protocol && http_proxy[request_options.protocol])
133
+ request_options.agent = http_proxy[request_options.protocol]
134
+
128
135
  return request_options
129
136
  }
130
137
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@warren-bank/hls-proxy",
3
3
  "description": "Node.js server to proxy HLS video streams",
4
- "version": "3.2.4",
4
+ "version": "3.3.0",
5
5
  "scripts": {
6
6
  "start": "node hls-proxy/bin/hlsd.js",
7
7
  "sudo": "sudo node hls-proxy/bin/hlsd.js"
@@ -11,8 +11,9 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@warren-bank/node-process-argv": "^1.2.1",
14
- "@warren-bank/node-request": "^2.0.10",
14
+ "@warren-bank/node-request": "^2.0.12",
15
15
  "@warren-bank/url": "^3.1.0",
16
+ "hpagent": "^1.2.0",
16
17
  "tough-cookie": "^3.0.1"
17
18
  },
18
19
  "license": "GPL-2.0",