Skip to content
New issue

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

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

Already on GitHub? # to your account

Support for inline (same port) flash socket policy request #85

Merged
1 commit merged into from
Oct 29, 2010
Merged
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
61 changes: 41 additions & 20 deletions lib/socket.io/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,48 @@ Flashsocket.init = function(listener){
} catch(e){}
}
});
if (netserver == null) {
// Could not listen on port 843 so policy requests will be inline
listener.server.addListener('connection', function(stream){
var flashCheck = function (data) {
// Only check the initial data
stream.removeListener("data", flashCheck);
if (data[0] === 60 && data.length == 23) {
if (data == '<policy-file-request/>\0') {
listener.options.log("Answering flash policy request inline");
if (stream && stream.readyState == 'open'){
var xml = Flashsocket.generate_policy([listener]);
stream.write(xml);
stream.end();
}
}
}
};
stream.on("data", flashCheck);
});
}
};

Flashsocket.generate_policy = function(listeners) {
var xml = '<?xml version="1.0"?>\n<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n<cross-domain-policy>\n';

listeners.forEach(function(l){
[].concat(l.options.origins).forEach(function(origin){
var parts = origin.split(':');
xml += '<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>\n';
});
});

xml += '</cross-domain-policy>\n';
return xml;
};

try {
netserver = net.createServer(function(socket){
socket.addListener("error",function(err){
socket.end && socket.end() || socket.destroy && socket.destroy();
});
var xml = '<?xml version="1.0"?>\n<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n<cross-domain-policy>\n';

listeners.forEach(function(l){
[].concat(l.options.origins).forEach(function(origin){
var parts = origin.split(':');
xml += '<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>\n';
});
});

xml += '</cross-domain-policy>\n';
var xml = Flashsocket.generate_policy(listeners);

if(socket && socket.readyState == 'open'){
socket.write(xml);
Expand All @@ -50,20 +75,16 @@ try {
if (e.errno == 13){
console.error("\x1B[1;31m" + [
'================================================',
'| WARNING! DANGER! |',
'| |',
'| The flash websocket transport will not work |',
'| unless you run your node instance with root |',
'| privileges. |',
'| |',
'| A flash XML policy file has to be served on |',
'| port 843 (privileged) for it to work. |',
'| Your node instance does not have root |',
'| privileges. This means that the flash XML |',
'| policy file will be served inline instead of |',
'| on port 843. This is better for security but |',
'| may slow down initial connections slightly. |',
'| |',
'| You can run socket.io without this transport |',
'| to make this message go (refer to README). |',
'| |',
'===============================================|'
].join("\n") + "\x1B[0m");
}
netserver = null;
}
}