# HG changeset patch # User Nicholas Jillings # Date 1477304582 -3600 # Node ID bf17cc19c1c0a0bc5938611dcc4102822db530b0 # Parent 522f58e7773e6c84fc7a883a38b5c60af251a51e# Parent 22673a6cfcfbee2f366967c69a3eec0dc82a9c29 Merge branch 'master' into Dev_main diff -r 522f58e7773e -r bf17cc19c1c0 js/core.js --- a/js/core.js Tue Oct 18 17:51:53 2016 +0100 +++ b/js/core.js Mon Oct 24 11:23:02 2016 +0100 @@ -3127,7 +3127,7 @@ this.initialise = function (existingStore) { if (existingStore == undefined) { // We need to get the sessionKey - this.SessionKey.generateKey(); + this.SessionKey.requestKey(); this.document = document.implementation.createDocument(null, "waetresult", null); this.root = this.document.childNodes[0]; var projectDocument = specification.projectXML; @@ -3156,9 +3156,8 @@ handleEvent: function () { var parse = new DOMParser(); var xml = parse.parseFromString(this.request.response, "text/xml"); - var shouldGenerateKey = true; if (this.request.response.length == 0) { - console.log("Error: Server did not respond"); + console.error("An unspecified error occured, no server key could be generated"); return; } if (xml.getElementsByTagName("state").length > 0) { @@ -3166,22 +3165,26 @@ this.key = xml.getAllElementsByTagName("key")[0].textContent; this.parent.root.setAttribute("key", this.key); this.parent.root.setAttribute("state", "empty"); - shouldGenerateKey = false; + this.update(); + return; + } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { + this.key = null; + console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); + return; } } - if (shouldGenerateKey === true) { - this.generateKey(); - } + this.key = null; + console.error("An unspecified error occured, no server key could be generated"); }, - generateKey: function () { - var temp_key = randomString(32); + requestKey: function () { + // For new servers, request a new key from the server var returnURL = ""; if (typeof specification.projectReturn == "string") { if (specification.projectReturn.substr(0, 4) == "http") { returnURL = specification.projectReturn; } } - this.request.open("GET", returnURL + "php/keygen.php?key=" + temp_key, true); + this.request.open("GET", returnURL + "php/requestKey.php", true); this.request.addEventListener("load", this); this.request.send(); }, diff -r 522f58e7773e -r bf17cc19c1c0 php/keygen.php --- a/php/keygen.php Tue Oct 18 17:51:53 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -OK".$key_requested.""; -$xml_bad = "NO".$key_requested.""; -$xml_error = "ERROR".$key_requested.""; -if (is_array($saves)) -{ - foreach($saves as $filename) { - $xml_string = file_get_contents($filename, FILE_TEXT); - $xml_object = simplexml_load_string($xml_string); - if ($xml_object != false) { - if (isset($value['key'])) - { - if ($value['key'] == $key_requested) { - echo $xml_bad; - return; - } - } - } - } - echo $xml_good; - // TODO: - // Generate the XML Base file and save it - $doc_struct = new SimpleXMLElement(''); - $doc_struct->addAttribute("key",$key_requested); - // Add start time - // Add IP Address information - // Save the file - $doc_struct->asXML("saves/save-".$key_requested.".xml"); - return; -} else { - echo $xml_error; - return; -} -?> \ No newline at end of file diff -r 522f58e7773e -r bf17cc19c1c0 php/requestKey.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/requestKey.php Mon Oct 24 11:23:02 2016 +0100 @@ -0,0 +1,61 @@ +ERROR".$key."Could not open file for writing"; + return; +} +fclose($fileHandle); +// TODO: +// Generate the XML Base file and save it +$doc_struct = new SimpleXMLElement(''); +$doc_struct->addAttribute("key",$key); +// Add start time +// Add IP Address information +// Save the file +$doc_struct->asXML($filename); +echo "OK".$key.""; +return; +?> diff -r 522f58e7773e -r bf17cc19c1c0 python/pythonServer.py --- a/python/pythonServer.py Tue Oct 18 17:51:53 2016 +0100 +++ b/python/pythonServer.py Mon Oct 24 11:23:02 2016 +0100 @@ -11,6 +11,8 @@ import operator import xml.etree.ElementTree as ET import copy +import string +import random if sys.version_info[0] == 2: # Version 2.x @@ -109,27 +111,26 @@ s.send_header("Content-Length", len(fileBytes)) s.end_headers() s.wfile.write(fileBytes) - -def keygen(s): - reply = "" - options = s.path.rsplit('?') - options = options[1].rsplit('=') - key = options[1] - print("Registered key "+key) - if os.path.isfile("saves/save-"+key+".xml"): - reply = "NO"+key+"" - else: - reply = "OK"+key+"" - s.send_response(200) - s.send_header("Content-type", "application/xml") - s.end_headers() - if sys.version_info[0] == 2: - s.wfile.write(reply) - elif sys.version_info[0] == 3: - s.wfile.write(bytes(reply, "utf-8")) - file = open("../saves/save-"+key+".xml",'w') - file.write("") - file.close(); + +def requestKey(s): + reply = "" + key = '' + while key == '': + tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32)); + if (os.path.isfile("saves/save-"+tempKey+".xml") == False): + key = tempKey + s.send_response(200) + s.send_header("Content-type", "application/xml"); + s.end_headers() + reply = "OK"+key+"" + if sys.version_info[0] == 2: + s.wfile.write(reply) + elif sys.version_info[0] == 3: + s.wfile.write(bytes(reply, "utf-8")) + file = open("../saves/save-"+key+".xml",'w') + file.write("") + file.close() + def saveFile(self): global curFileName @@ -226,8 +227,8 @@ if(request.client_address[0] == "127.0.0.1"): if (request.path == "/favicon.ico"): send404(request) - elif (request.path.split('?',1)[0] == "/php/keygen.php"): - keygen(request); + elif (request.path.split('?',1)[0] == "/php/requestKey.php"): + requestKey(request); elif (request.path.split('?',1)[0] == "/php/pool.php"): poolXML(request); else: