C4Grey

Socket policy server for Flash

May 29th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package FlashServer;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6.  
  7. public class PolicyServer extends Thread {
  8.     private int _port;
  9.     private boolean _listening;
  10.     private ServerSocket _socketServer;
  11.  
  12.     public PolicyServer(int port_, String[] allowedHosts_)
  13.     {
  14.         _port = port_;
  15.         _listening = true;
  16.         if (allowedHosts_ == null)
  17.             allowedHosts_ = MainServer.DEFAULT_POLICY;
  18.         buildPolicy(allowedHosts_);
  19.     }
  20.  
  21.     public String buildPolicy(String[] allowedHosts_)
  22.     {
  23.         StringBuffer policyBuffer = new StringBuffer();
  24.  
  25.         policyBuffer.append("<?xml version=\"1.0\"?><cross-domain-policy>");
  26.        
  27.         for (int i = 0; i < allowedHosts_.length; i++)
  28.         {
  29.             String[] hostInfo = allowedHosts_[i].split(":");
  30.             String hostname = hostInfo[0];
  31.             String ports;
  32.             if (hostInfo.length > 1)
  33.                 ports = hostInfo[1];
  34.             else
  35.                 ports = "*";
  36.  
  37.             policyBuffer.append("<allow-access-from domain=\"" + hostname + "\" to-ports=\"" + ports + "\" />");
  38.         }
  39.         policyBuffer.append("</cross-domain-policy>");
  40.  
  41.         return policyBuffer.toString();
  42.     }
  43.  
  44.     public void run()
  45.     {
  46.         try
  47.         {
  48.             _listening = true;
  49.             _socketServer = new ServerSocket(_port, 50);
  50.             if (MainServer.DEBUG)
  51.                 System.out.println("PolicyServer listening on port " + _port);
  52.  
  53.             while (_listening)
  54.             {
  55.                 Socket socket = _socketServer.accept();
  56.  
  57.                 try
  58.                 {
  59.                     if (MainServer.DEBUG)
  60.                         System.out.println("PolicyServer got a connection on port " + _port);
  61.                     (new SocketConnection(socket)).start();
  62.                 }
  63.                 catch (Exception e)
  64.                 {
  65.                     if (MainServer.DEBUG)
  66.                         System.out.println("Exception: " + e.getMessage());
  67.                 }
  68.                 try
  69.                 {
  70.                     sleep(1000);
  71.                 }
  72.                 catch (InterruptedException e)
  73.                 {
  74.                 }
  75.             }
  76.         }
  77.         catch (IOException e)
  78.         {
  79.             if (MainServer.DEBUG)
  80.                 System.out.println("IO Exception: " + e.getMessage());
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment