From 181083eec7a426f3510a53606033ee8c6e5b38c9 Mon Sep 17 00:00:00 2001 From: Uriziel Date: Wed, 16 Jan 2013 20:27:32 +0100 Subject: [PATCH] Different answer is given when server is requested with HTTP GET --- syncplay/messages.py | 8 +++++--- syncplay/protocols.py | 14 ++++++++++++-- syncplay/server.py | 10 +++++++++- syncplay/ui/ConfigurationGetter.py | 3 ++- syncplayServer.py | 2 +- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/syncplay/messages.py b/syncplay/messages.py index 6eff284..9f37b7f 100644 --- a/syncplay/messages.py +++ b/syncplay/messages.py @@ -107,12 +107,14 @@ en = { "server-isolate-room-argument" : 'should rooms be isolated?', "server-motd-argument": "path to file from which motd will be fetched", "server-messed-up-motd": "Message of the Day has unescaped placeholders. All $ signs should be doubled ($$).", + "server-http-reply-argument": "path to file from which http reply will be fetched", + "server-default-http-reply": "This server should not be requested with your browser, but Syncplay software available from http://syncplay.pl", #Server errors - "unknown-command-server-error" : "Unknown command\n%s", #message - "not-json-server-error" : "Not a json encoded string\n%s", #message + "unknown-command-server-error" : "Unknown command {}", #message + "not-json-server-error" : "Not a json encoded string {}", #message "not-known-server-error" : "You must be known to server before sending this command", - "client-drop-server-error" : "Client drop: %s -- %s", #host, error + "client-drop-server-error" : "Client drop: {} -- {}", #host, error "password-required-server-error" : "Password required", "wrong-password-server-error" : "Wrong password supplied", "hello-server-error" : "Not enough Hello arguments", diff --git a/syncplay/protocols.py b/syncplay/protocols.py index bd8a51a..8680c1a 100644 --- a/syncplay/protocols.py +++ b/syncplay/protocols.py @@ -39,7 +39,11 @@ class JSONCommandProtocol(LineReceiver): try: messages = json.loads(line) except: - self.dropWithError(getMessage("en", "not-json-server-error").format(line)) + if ("GET / HTTP/1." in line): + self.handleHttpRequest(line) + self.drop() + else: + self.dropWithError(getMessage("en", "not-json-server-error").format(line)) return self.handleMessages(messages) @@ -189,6 +193,9 @@ class SyncClientProtocol(JSONCommandProtocol): self._client.updateGlobalState(position, paused, doSeek, setBy, latency) position, paused, doSeek, stateChange = self._client.getLocalState() self.sendState(position, paused, doSeek, latencyCalculation, stateChange) + + def handleHttpRequest(self, request): + pass def sendState(self, position, paused, doSeek, latencyCalculation, stateChange = False): state = {} @@ -241,7 +248,7 @@ class SyncServerProtocol(JSONCommandProtocol): return wrapper def dropWithError(self, error): - print getMessage("en", "client-drop-server-error").format((self.transport.getPeer().host, error)) + print getMessage("en", "client-drop-server-error").format(self.transport.getPeer().host, error) self.sendError(error) self.drop() @@ -394,6 +401,9 @@ class SyncServerProtocol(JSONCommandProtocol): if(self.serverIgnoringOnTheFly == 0): self._factory.updateWatcherState(self, position, paused, doSeek, latencyCalculation) + def handleHttpRequest(self, request): + self.sendLine(self._factory.gethttpRequestReply()) + def handleError(self, error): self.dropWithError(error["message"]) #TODO: more processing and fallbacking diff --git a/syncplay/server.py b/syncplay/server.py index c1b435d..23cbe50 100644 --- a/syncplay/server.py +++ b/syncplay/server.py @@ -14,12 +14,13 @@ import os from string import Template class SyncFactory(Factory): - def __init__(self, password = '', motdFilePath = None): + def __init__(self, password = '', motdFilePath = None, httpReplyFilePath= None): print getMessage("en", "welcome-server-notification").format(syncplay.version) if(password): password = hashlib.md5(password).hexdigest() self.password = password self._motdFilePath = motdFilePath + self._httpReplyFilePath = httpReplyFilePath self._rooms = {} self._roomStates = {} self._roomUpdate = threading.RLock() @@ -99,6 +100,13 @@ class SyncFactory(Factory): else: return "" + def gethttpRequestReply(self): + if(self._httpReplyFilePath and os.path.isfile(self._httpReplyFilePath)): + tmpl = codecs.open(self._httpReplyFilePath, "r", "utf-8-sig").read() + return tmpl + else: + return getMessage("en", "server-default-http-reply") + def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False): watcher = self.getWatcher(watcherProtocol) if(not watcher): diff --git a/syncplay/ui/ConfigurationGetter.py b/syncplay/ui/ConfigurationGetter.py index bdfd499..b87d30d 100644 --- a/syncplay/ui/ConfigurationGetter.py +++ b/syncplay/ui/ConfigurationGetter.py @@ -206,4 +206,5 @@ class ServerConfigurationGetter(object): self._argparser.add_argument('--port', metavar='port', type=str, nargs='?', help=getMessage("en", "server-port-argument")) self._argparser.add_argument('--password', metavar='password', type=str, nargs='?', help=getMessage("en", "server-password-argument")) self._argparser.add_argument('--isolate-rooms', action='store_true', help=getMessage("en", "server-isolate-room-argument")) - self._argparser.add_argument('--motd-file', metavar='motd', type=str, nargs='?', help=getMessage("en", "server-motd-argument")) + self._argparser.add_argument('--motd-file', metavar='file', type=str, nargs='?', help=getMessage("en", "server-motd-argument")) + self._argparser.add_argument('--http-reply-file', metavar='file', type=str, nargs='?', help=getMessage("en", "server-http-reply-argument")) diff --git a/syncplayServer.py b/syncplayServer.py index 94becb6..d6198dc 100644 --- a/syncplayServer.py +++ b/syncplayServer.py @@ -11,5 +11,5 @@ args = argsGetter.getConfiguration() if(not args.isolate_rooms): reactor.listenTCP(int(args.port), SyncFactory(args.password, args.motd_file)) else: - reactor.listenTCP(int(args.port), SyncIsolatedFactory(args.password, args.motd_file)) + reactor.listenTCP(int(args.port), SyncIsolatedFactory(args.password, args.motd_file, args.http_reply_file)) reactor.run()