forked from chrisirhc/node-mjpeg-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-mjpeg-proxy.js
More file actions
153 lines (133 loc) · 4.17 KB
/
node-mjpeg-proxy.js
File metadata and controls
153 lines (133 loc) · 4.17 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**************************
TODO:
1. Make it work with existing HTTP servers and listen to resource URLs
2. Refactoring
**************************/
var http = require('http'),
sys = require('sys'),
url = require('url');
exports.createProxy = function(map, options) {
return new Proxy(map, options);
};
var Proxy = exports.Proxy = function (map_opt, options) {
if (!map_opt) throw new Error("Please provide a source feed URL or a map");
var map = [];
if (typeof map_opt === 'string') {
var srcURL = url.parse(map);
var auth = srcURL.auth ? srcURL.auth.split(":") : null;
map = [{
"out" : "",
"in" : {
"host" : srcURL.hostname,
"port" : srcURL.port,
"path" : srcURL.pathname + (srcURL.search ? srcURL.search : ""),
"user" : (auth ? auth[0] : null),
"password" : (auth ? auth[1] : null)
}
}];
} else {
for (var p in map_opt) {
if (!map_opt.hasOwnProperty(p)) continue;
var srcURL = url.parse(map_opt[p]);
var auth = srcURL.auth ? srcURL.auth.split(":") : null;
map[map.length] = {
"out": p,
"in" : {
"host" : srcURL.hostname,
"port" : srcURL.port,
"path" : srcURL.pathname + (srcURL.search ? srcURL.search : ""),
"user" : (auth ? auth[0] : null),
"password" : (auth ? auth[1] : null)
}
}
}
}
options = options || {};
this.audienceServer = options.audienceServer || http.createServer();
var audienceServerPort = options.port || 5080;
/**
* Array of the requests to the stream sources.
* @type Array.<http.ClientRequest>
*/
this.clientRequests = [];
map.forEach(function(item, index){
var headers = {'Host': item.host};
var audienceClients = [];
if (!!options.headers) {
for (var h in options.headers) {
if (options.headers.hasOwnProperty(h)) {
headers[h] = options.headers[h];
}
}
}
/** Add HTTP-auth **/
if (item.in.user) {
if (!item.in.password) throw Error('Password for the user not specified');
var auth = 'Basic ' +
new Buffer(item.in.user + ':' + item.in.password).toString('base64');
headers['Authorization'] = auth;
}
// Starting the stream on from the source
var request = http.request({
host: item.in.host,
port: item.in.port || 80,
method: 'GET',
path: item.in.path,
headers: headers
});
this.clientRequests[index] = request;
request.end();
request.on('response', function (srcResponse) {
item.sendResponse = function(req, res) {
/** Replicate the header from the source **/
res.writeHead(200, srcResponse.headers);
/** Push the client into the client list **/
audienceClients.push(res);
/** Clean up connections when they're dead **/
res.socket.on('close', function () {
audienceClients.splice(audienceClients.indexOf(res), 1);
});
};
/** Send data to relevant clients **/
srcResponse.setEncoding('binary');
srcResponse.on('data', function (chunk) {
var i;
for (i = audienceClients.length; i--;) {
// debugger;
audienceClients[i].write(chunk, 'binary');
}
});
});
}, this);
var audienceRoutes = map.map(function(item){
return item.out.replace(/^([^\/])/, "/$1");
});
/** Setup Audience server listener **/
this.audienceServer.on('request', function (req, res) {
for (var i = audienceRoutes.length; i--;) {
if (req.url === audienceRoutes[i]) {
if (typeof map[i].sendResponse === 'function') {
map[i].sendResponse(req, res);
}
return;
}
}
res.writeHead(404);
res.end('Not found');
return;
});
if (typeof this.audienceServer.fd !== 'number') {
this.audienceServer.listen(audienceServerPort);
sys.puts('node-mjpeg-proxy server started on port ' + audienceServerPort);
}
};
/**
* Destroy {@link Proxy} instance.
*/
Proxy.prototype.destroy = function () {
var request = null;
while (request = this.clientRequests.pop()) {
request.abort();
}
this.audienceServer.close();
};