Exemplo n.º 1
0
    def post(self, *args, **kwargs):
        """
        Add XSRF back in after testing...
        Check the header for the 'application/json' Content-Type
        """
        self.set_header("Server", "API Server v" + version.version)

        json_data = "{}"
        update_queue = False

        header = str(self.request.headers["Content-Type"]).lower(
        )  # If Content-Type is not there or malformed then the server catches before this point.

        if args is None or args[0] == "" or header != "application/json":
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tHeader not set correctly" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats[
            #        "requested_prefix"], self.request.protocol, self.request.host, self.request.uri), "WARNING")

            raise tornado.web.HTTPError(
                config.http_response_codes["forbidden"])

        # Force the api ip_restrict to check.
        passed = ip_restrict(self.request.remote_ip, api=True)

        if not passed:
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tIP Restricted: %s" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats["requested_prefix"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip), "WARNING")

            raise tornado.web.HTTPError(
                config.http_response_codes["forbidden"]
            )  # The exception will catch this so force it again

        self.set_header("Content-Type", "application/json")

        command = str(args[0]).lower()

        # Break down the json into a dictionary. Some will be a simple name/value pair and others an array and others a list.
        dict_data = "{}"
        try:
            dict_data = convert(json.loads(self.request.body))
            if command == "servers_interface_ip":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_interface_ip[:] = []  # Clears a list
                    for data in dict_data[command]:
                        config.servers_interface_ip.append(data)
            elif command == "server_defaults":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.server_defaults.clear()
                    config.server_defaults = dict_data[command]
                    config.server_default_port = ":" + str(
                        config.server_defaults["port"]
                    )  # Must be present in the servers.yaml file.
                    if config.server_default_port == ":80" or config.server_default_port == ":":
                        config.server_default_port = ""  # Set it to empty since it's not needed for browsers/players.
            elif command == "server_ports":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.server_ports.clear()
                    config.server_ports = dict_data[command]
            elif command == "server_config":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_server_config.clear()
                    config.servers_server_config = dict_data[command]
            elif command == "purge":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_purge.clear()
                    config.servers_purge = dict_data[command]
            elif command == "servers" or command == "servers/add" or command == "servers/delete":
                if len(dict_data["servers"]) > 0:
                    update_queue = True
                    add_servers = False
                    if command == "servers/add":
                        add_servers = True

                    config.server_count = len(config.servers)
                    config.servers_weighted_count = len(
                        config.servers_weighted)
            elif command == "write":  # Writes out the yaml file
                yaml_write("servers", "servers.yaml")

            if update_queue:
                config.server_queue.put_nowait("update")

            if config.logging:
                log("POST %s\t%s://%s%s\t%s\t%s\t%s" %
                    (config.http_response_codes["found"],
                     self.request.protocol, self.request.host,
                     self.request.uri, self.request.remote_ip,
                     self.request.headers["User-Agent"], dict_data))

            self.write("{\"%s\": %s}" % ("success", True))
        except BaseException, e:
            log(
                "POST %s\t%s://%s%s\t%s\t%s\t%s\t%s" %
                (config.http_response_codes["general_error"],
                 self.request.protocol, self.request.host, self.request.uri,
                 self.request.remote_ip, self.request.headers["User-Agent"], e,
                 dict_data), "CRITICAL")
            self.write("{\"%s\": %s}" % ("success", False))
Exemplo n.º 2
0
    def post(self, *args, **kwargs):
        """
        Add XSRF back in after testing...
        Check the header for the 'application/json' Content-Type
        """
        self.set_header("Server", "API Server v" + version)

        dict_data = "{}"

        header = str(self.request.headers["Content-Type"]).lower(
        )  # If Content-Type is not there or malformed then the server catches before this point.

        if args is None or args[0] == "" or header != "application/json":
            #if config.logging:
            #    log("%s\t%s://%s%s\tForbidden\tHeader not set correctly" % (config.http_response_codes["forbidden"], self.request.protocol, self.request.host, self.request.uri), "WARNING")

            raise tornado.web.HTTPError(
                config.http_response_codes["forbidden"])

        # Force the api ip_restrict to check.
        passed = ip_restrict(self.request.remote_ip, api=True)

        if not passed:
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tIP Restricted: %s" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats["requested_prefix"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip), "WARNING")

            raise tornado.web.HTTPError(
                config.http_response_codes["forbidden"]
            )  # The exception will catch this so force it again

        self.set_header("Content-Type", "application/json")

        command = str(args[0]).lower()

        # Break down the json into a dictionary. Some will be a simple name/value pair and others an array and others a list.
        try:
            dict_data = convert(json.loads(self.request.body))
            if command == "log_rotation":
                if len(dict_data[command]) > 0:
                    config.log_rotation = dict_data[command]
            elif command == "log_backups":
                if len(dict_data[command]) > 0:
                    config.log_backups = dict_data[command]
            elif command == "log_when":
                if len(dict_data[command]) > 0:
                    config.log_when = dict_data[command]
            elif command == "alert_email":
                config.alert_email.clear()
                config.alert_email = dict_data[command]
            elif command == "keys":
                if len(dict_data[command]) > 0:
                    config.keys.clear()
                    config.keys = dict_data[command]
            elif command == "read":
                read()
            elif command == "write":  # Writes out the yaml file
                yaml_write("config", "config.yaml")

            if config.logging:
                log("POST %s\t%s://%s%s\t%s\t%s\t%s" %
                    (config.http_response_codes["found"],
                     self.request.protocol, self.request.host,
                     self.request.uri, self.request.remote_ip,
                     self.request.headers["User-Agent"], dict_data))

            self.write("{\"%s\": %s}" % ("success", True))
        except BaseException, e:
            log(
                "POST %s\t%s://%s%s\t%s\t%s\t%s\t%s" %
                (config.http_response_codes["general_error"],
                 self.request.protocol, self.request.host, self.request.uri,
                 self.request.remote_ip, self.request.headers["User-Agent"], e,
                 dict_data), "CRITICAL")
            self.write("{\"%s\": %s}" % ("success", False))
Exemplo n.º 3
0
    def post(self, *args, **kwargs):
        """
        Add XSRF back in after testing...
        Check the header for the 'application/json' Content-Type
        """
        self.set_header("Server", "API Server v" + version.version)

        json_data = "{}"
        update_queue = False

        header = str(self.request.headers["Content-Type"]).lower()  # If Content-Type is not there or malformed then the server catches before this point.

        if args is None or args[0] == "" or header != "application/json":
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tHeader not set correctly" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats[
            #        "requested_prefix"], self.request.protocol, self.request.host, self.request.uri), "WARNING")

            raise tornado.web.HTTPError(config.http_response_codes["forbidden"])

        # Force the api ip_restrict to check.
        passed = ip_restrict(self.request.remote_ip, api=True)

        if not passed:
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tIP Restricted: %s" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats["requested_prefix"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip), "WARNING")

            raise tornado.web.HTTPError(config.http_response_codes["forbidden"])  # The exception will catch this so force it again

        self.set_header("Content-Type", "application/json")

        command = str(args[0]).lower()

        # Break down the json into a dictionary. Some will be a simple name/value pair and others an array and others a list.
        dict_data = "{}"
        try:
            dict_data = convert(json.loads(self.request.body))
            if command == "servers_interface_ip":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_interface_ip[:] = []  # Clears a list
                    for data in dict_data[command]:
                        config.servers_interface_ip.append(data)
            elif command == "server_defaults":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.server_defaults.clear()
                    config.server_defaults = dict_data[command]
                    config.server_default_port = ":" + str(config.server_defaults["port"])  # Must be present in the servers.yaml file.
                    if config.server_default_port == ":80" or config.server_default_port == ":":
                        config.server_default_port = ""  # Set it to empty since it's not needed for browsers/players.
            elif command == "server_ports":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.server_ports.clear()
                    config.server_ports = dict_data[command]
            elif command == "server_config":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_server_config.clear()
                    config.servers_server_config = dict_data[command]
            elif command == "purge":
                if len(dict_data[command]) > 0:
                    update_queue = True
                    config.servers_purge.clear()
                    config.servers_purge = dict_data[command]
            elif command == "servers" or command == "servers/add" or command == "servers/delete":
                if len(dict_data["servers"]) > 0:
                    update_queue = True
                    add_servers = False
                    if command == "servers/add":
                        add_servers = True

                    config.server_count = len(config.servers)
                    config.servers_weighted_count = len(config.servers_weighted)
            elif command == "write":  # Writes out the yaml file
                yaml_write("servers", "servers.yaml")

            if update_queue:
                config.server_queue.put_nowait("update")

            if config.logging:
                log("POST %s\t%s://%s%s\t%s\t%s\t%s" % (
                    config.http_response_codes["found"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip, self.request.headers["User-Agent"], dict_data))

            self.write("{\"%s\": %s}" % ("success", True))
        except BaseException, e:
            log("POST %s\t%s://%s%s\t%s\t%s\t%s\t%s" % (
                config.http_response_codes["general_error"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip, self.request.headers["User-Agent"], e, dict_data), "CRITICAL")
            self.write("{\"%s\": %s}" % ("success", False))
Exemplo n.º 4
0
    def post(self, *args, **kwargs):
        """
        Add XSRF back in after testing...
        Check the header for the 'application/json' Content-Type
        """
        self.set_header("Server", "API Server v" + version)

        dict_data = "{}"

        header = str(self.request.headers["Content-Type"]).lower()  # If Content-Type is not there or malformed then the server catches before this point.

        if args is None or args[0] == "" or header != "application/json":
            #if config.logging:
            #    log("%s\t%s://%s%s\tForbidden\tHeader not set correctly" % (config.http_response_codes["forbidden"], self.request.protocol, self.request.host, self.request.uri), "WARNING")

            raise tornado.web.HTTPError(config.http_response_codes["forbidden"])

        # Force the api ip_restrict to check.
        passed = ip_restrict(self.request.remote_ip, api=True)

        if not passed:
            #if config.logging:
            #    log("%s\t%s%s%s://%s%s\tForbidden\tIP Restricted: %s" % (config.http_response_codes["forbidden"], config.ip["log_prefix"], config.log_message_formats["requested_prefix"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip), "WARNING")

            raise tornado.web.HTTPError(config.http_response_codes["forbidden"])  # The exception will catch this so force it again

        self.set_header("Content-Type", "application/json")

        command = str(args[0]).lower()

        # Break down the json into a dictionary. Some will be a simple name/value pair and others an array and others a list.
        try:
            dict_data = convert(json.loads(self.request.body))
            if command == "log_rotation":
                if len(dict_data[command]) > 0:
                    config.log_rotation = dict_data[command]
            elif command == "log_backups":
                if len(dict_data[command]) > 0:
                    config.log_backups = dict_data[command]
            elif command == "log_when":
                if len(dict_data[command]) > 0:
                    config.log_when = dict_data[command]
            elif command == "alert_email":
                config.alert_email.clear()
                config.alert_email = dict_data[command]
            elif command == "keys":
                if len(dict_data[command]) > 0:
                    config.keys.clear()
                    config.keys = dict_data[command]
            elif command == "read":
                read()
            elif command == "write":  # Writes out the yaml file
                yaml_write("config", "config.yaml")

            if config.logging:
                log("POST %s\t%s://%s%s\t%s\t%s\t%s" % (
                    config.http_response_codes["found"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip, self.request.headers["User-Agent"], dict_data))

            self.write("{\"%s\": %s}" % ("success", True))
        except BaseException, e:
            log("POST %s\t%s://%s%s\t%s\t%s\t%s\t%s" % (
                config.http_response_codes["general_error"], self.request.protocol, self.request.host, self.request.uri, self.request.remote_ip, self.request.headers["User-Agent"], e, dict_data),
                "CRITICAL")
            self.write("{\"%s\": %s}" % ("success", False))