Sending crossdomains policy to flash via node js o

2019-06-05 02:21发布

问题:

I have a client which is in flash as3. I got 2 server, one main for the flash client and a socket policy server.

I run the socket policy server on port 843 and my client on any port X for example.

Now when i connect with my flash client, using this:

 Security.loadPolicyFile("xmlsocket://domain.com:port");
 SecureSocket.connect(ip, port);

It actually connect first to my policy socket server, send a policy-file-request line and receive the one i am sending using my server which look like this:

 tls.createServer(options, function(sock) {
    sock.on('data', function(data) {
            console.log('Data receveid: '.cyan + data + ' from ' + sock.remoteAddress);
            if (data == "<policy-file-request/>\0") {
                    sock.write('<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*"></cross-domain-policy
                    console.log('Sent policy file to: '.green + sock.remoteAddress);
            }
 ...

After that, the connection is closed automatically. Then it try to connect to my real server and send to server the policy-file-request line again then connection close. I really dont know what to do from now on ive been trying all days and searching around the web. Anyone got an idea of what i am missing?

回答1:

Ok I finally got it to work. First I wanted a SSL connection from my client to my game.

  • Trying it locally in the Flash IDE was fine,
  • However doing it in flash debugguer or from a website wasn't working because it was trying to access a socket policy server (which is a registered port from adobe, the port 843).

So I need a policy socket server to work on that port and found some on adobe website wrote in python and perl. Forget them, they won't work in this case.

Because I am trying to access server in SSL so I need also a SSL policy server.
Since there is none on the net I wrote a simple one:

var tls = require('tls');
var fs = require('fs');

var options = {
  key: fs.readFileSync('/etc/ssl/private/yourserver.key'),
  cert: fs.readFileSync('/etc/ssl/certs/yourserver.crt'),
};

var HOST = 'YOURSERVERIP';
var PORT = 843;

tls.createServer(options, function(sock) {
    sock.on('data', function(data) {
            console.log('Policy Server Request: ' + data);
            if (data.indexOf('policy') !== -1) {
                    console.log("Policy Server: sending regulation file");
                    sock.write('<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"><!-- Policy file for xmlsocket://MYDOMAIN.com --><cross-domain-policy><allow-access-from domain="*" to-ports="*" /><allow-access-from domain="*" to-ports="1-65535" /><allow-access-from domain="MYDOMAIN.com" to-ports="MYGAMEPORT" /><allow-access-from domain="MYIP" to-ports="*" /></cross-domain-policy>\0');
            } else {
                    console.log("Policy Server: unknown request");
            }

    });
    sock.on('clientError', function(exception) {
            console.log('' + data);
    });
    sock.on('close', function(data) {
            console.log('CONNECTION CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });
    sock.on('error', function(err) {
            console.log('\n' + HOST +':'+ err);
    });
}).listen(PORT, HOST);

console.log('\nPolicy Server listening on ' + HOST +':'+ PORT);

I went crazy in the allow access domain form because i had some security sandbox violation with a reduced one, so ive put it all. I will clean this later.

As you can see, I had to add a \0 at end of sending the policy or Flash wouldn't accept it.

Two day wasted on this. And I still don't understand why Adobe would make the connection so hard between flash and the world. Never saw this kind of policy elsewhere while programming sockets.