summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/policyserver.py
blob: bda8afe5f1faee0f4a41cce4ecc3b77d8c5ccc75 (plain)
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
#!/usr/bin/env python

"""
Flash Socket Policy Server.

- Starts Flash socket policy file server.
- Defaults to port 843.
- NOTE: Most operating systems require administrative privileges to use
  ports under 1024.

  $ ./policyserver.py [options]
"""

"""
Also consider Adobe's solutions:
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html
"""

from multiprocessing import Process
from optparse import OptionParser
import SocketServer
import logging

# Set address reuse for all TCPServers
SocketServer.TCPServer.allow_reuse_address = True

# Static socket policy file string.
# NOTE: This format is very strict. Edit with care.
socket_policy_file = """\
<?xml version="1.0"?>\
<!DOCTYPE cross-domain-policy\
 SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">\
<cross-domain-policy>\
<allow-access-from domain="*" to-ports="*"/>\
</cross-domain-policy>\0"""


class PolicyHandler(SocketServer.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    Returns a policy file when requested.
    """

    def handle(self):
        """Send policy string if proper request string is received."""
        # get some data
        # TODO: make this more robust (while loop, etc)
        self.data = self.request.recv(1024).rstrip('\0')
        logging.debug("%s wrote:%s" % (self.client_address[0], repr(self.data)))
        # if policy file request, send the file.
        if self.data == "<policy-file-request/>":
            logging.info("Policy server request from %s." % (self.client_address[0]))
            self.request.send(socket_policy_file)
        else:
            logging.info("Policy server received junk from %s: \"%s\"" % \
                    (self.client_address[0], repr(self.data)))


class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    def serve_forever(self):
        """Handle one request at a time until shutdown or keyboard interrupt."""
        try:
           SocketServer.BaseServer.serve_forever(self)
        except KeyboardInterrupt:
           return


def main():
    """Run socket policy file servers."""
    usage = "Usage: %prog [options]"
    parser = OptionParser(usage=usage)
    parser.add_option("", "--host", dest="host", metavar="HOST",
            default="localhost", help="bind to HOST")
    parser.add_option("-p", "--port", dest="port", metavar="PORT",
            default=843, type="int", help="serve on PORT")
    parser.add_option("-d", "--debug", dest="debug", action="store_true",
            default=False, help="debugging output")
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
            default=False, help="verbose output")
    (options, args) = parser.parse_args()

    # setup logging
    if options.debug:
        lvl = logging.DEBUG
    elif options.verbose:
        lvl = logging.INFO
    else:
        lvl = logging.WARNING
    logging.basicConfig(level=lvl, format="%(levelname)-8s %(message)s")

    # log basic info
    logging.info("Flash Socket Policy Server. Use ctrl-c to exit.")
    
    # create policy server
    logging.info("Socket policy serving on %s:%d." % (options.host, options.port))
    policyd = ThreadedTCPServer((options.host, options.port), PolicyHandler)

    # start server
    policy_p = Process(target=policyd.serve_forever)
    policy_p.start()

    while policy_p.is_alive():
        try:
            policy_p.join(1)
        except KeyboardInterrupt:
            logging.info("Stopping test server...")


if __name__ == "__main__":
    main()