Created
July 9, 2013 07:20
-
-
Save ialex32x/5955344 to your computer and use it in GitHub Desktop.
python implementation of unity socket policy server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#description:python implementation of sockpol.cs for Unity corss domain socket security policy. | |
#last-modified:2012/4/9 14:03 | |
#author:huliangjie | |
import socket | |
import select | |
import threading | |
import time | |
PACKET_SIZE = 512 | |
try: | |
policy_file = open("policy.xml") | |
FULL_POLICY = "" | |
for line in policy_file: | |
FULL_POLICY += line | |
policy_file.close() | |
print("read policy from policy.xml") | |
except: | |
print("no policy file") | |
FULL_POLICY = "<?xml version='1.0'?><cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>"; | |
POLICY_DATA = FULL_POLICY.encode('ascii') | |
print(POLICY_DATA) | |
class SocketSecurityServer: | |
def __init__( self, port ): | |
self.port = port | |
self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) | |
self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) | |
self.srvsock.bind( ("", port) ) | |
self.srvsock.listen( 2000 ) | |
self.descriptors = [self.srvsock] | |
#threading.Thread(target=self.periodMessage).start() | |
print("SocketSecurityServer started on port %d" % port) | |
#def periodMessage(self): | |
# while True: | |
# time.sleep(2) | |
# self.broadcast_string('message from server') | |
def run( self ): | |
while 1: | |
# Await an event on a readable socket descriptor | |
(sread, swrite, sexc) = select.select( self.descriptors, [], [] ) | |
# Iterate through the tagged read descriptors | |
for sock in sread: | |
# Received a connect to the server (listening) socket | |
if sock == self.srvsock: | |
self.accept_new_connection() | |
else: | |
# Received something on a client socket | |
try: | |
str = sock.recv(PACKET_SIZE) | |
except socket.error: | |
print("connection halt") | |
str = '' | |
# Check to see if the peer socket closed | |
if str == '': | |
host,port = sock.getpeername() | |
str = 'connection lost %s:%s' % (host, port) | |
#self.broadcast_string( str, sock ) | |
sock.close | |
self.descriptors.remove(sock) | |
else: | |
if str == b"<policy-file-request/>\x00": | |
self.send_policy(sock) | |
else: | |
print("invalid connection %s:%s" % sock.getpeername()) | |
sock.close | |
self.descriptors.remove(sock) | |
#newstr = '[%s:%s] %s' % (host, port, str) | |
#self.broadcast_string( newstr, sock ) | |
def accept_new_connection( self ): | |
newsock, (remhost, remport) = self.srvsock.accept() | |
#newsock.send("You're connected to the Python chatserver") | |
#str = 'Client joined %s:%s' % (remhost, remport) | |
#self.broadcast_string( str ) | |
self.descriptors.append( newsock ) | |
def send_policy(self, sock): | |
try: | |
sock.send(POLICY_DATA) | |
print('policy sent to %s:%s' % sock.getpeername()) | |
except socket.error: | |
print('can not send policy. socket error.') | |
def broadcast_string( self, str, omit_sock=None ): | |
for sock in self.descriptors: | |
if sock != self.srvsock and sock != omit_sock: | |
#~ print sock.getpeername() | |
sock.send(str) | |
if __name__ == "__main__": | |
myServer = SocketSecurityServer(843).run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment