Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPS support from fork #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mjpeg-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var url = require('url');
var http = require('http');
var https = require('https');

function extractBoundary(contentType) {
contentType = contentType.replace(/\s+/g, '');
Expand All @@ -36,12 +36,14 @@ function extractBoundary(contentType) {
return contentType.substring(startIndex + 9, endIndex).replace(/"/gi,'').replace(/^\-\-/gi, '');
}

var MjpegProxy = exports.MjpegProxy = function(mjpegUrl) {
var MjpegProxy = exports.MjpegProxy = function(mjpegUrl,allowInsecure=false) {
var self = this;

if (!mjpegUrl) throw new Error('Please provide a source MJPEG URL');

self.mjpegOptions = new URL(mjpegUrl);
self.http = https;
if (self.mjpegOptions.protocol == 'http:') self.http = http;

self.audienceResponses = [];
self.newAudienceResponses = [];
Expand All @@ -59,8 +61,10 @@ var MjpegProxy = exports.MjpegProxy = function(mjpegUrl) {
if (self.mjpegRequest !== null) {
self._newClient(req, res);
} else {
let _old_TLS_setting = process.env["NODE_TLS_REJECT_UNAUTHORIZED"];
if (allowInsecure) process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
// Send source MJPEG request
self.mjpegRequest = http.request(self.mjpegOptions, function(mjpegResponse) {
self.mjpegRequest = self.http.request(self.mjpegOptions, function(mjpegResponse) {
// console.log(`statusCode: ${mjpegResponse.statusCode}`)
self.globalMjpegResponse = mjpegResponse;
self.boundary = extractBoundary(mjpegResponse.headers['content-type']);
Expand Down Expand Up @@ -115,6 +119,7 @@ var MjpegProxy = exports.MjpegProxy = function(mjpegUrl) {
// console.log("...close");
});
});
if (allowInsecure) process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = _old_TLS_setting;

self.mjpegRequest.on('error', function(e) {
console.error('problem with request: ', e);
Expand Down