Fix reset file server port with config web interface

This commit is contained in:
shortcutme 2020-06-30 17:04:09 +02:00
parent 635c3b27cd
commit ddbd5c7b19
No known key found for this signature in database
GPG Key ID: 5B63BAE6CB9613AE
5 changed files with 33 additions and 12 deletions

View File

@ -32,6 +32,13 @@ class ConfigStorage extends Class
return value.split("\n")
if type == "boolean" and not value
return false
else if type == "number"
if typeof(value) == "number"
return value.toString()
else if not value
return "0"
else
return value
else
return value
@ -68,7 +75,7 @@ class ConfigStorage extends Class
title: "File server port"
type: "text"
valid_pattern: /[0-9]*/
description: "Other peers will use this port to reach your served sites. (default: 15441)"
description: "Other peers will use this port to reach your served sites. (default: randomize)"
section.items.push
key: "ip_external"

View File

@ -57,9 +57,8 @@ class UiConfig extends ZeroFrame
for item, i in changed_values
last = i == changed_values.length - 1
value = @config_storage.deformatValue(item.value, typeof(@config[item.key].default))
value_same_as_default = JSON.stringify(@config[item.key].default) == JSON.stringify(value)
if value_same_as_default
value = null
default_value = @config_storage.deformatValue(@config[item.key].default, typeof(@config[item.key].default))
value_same_as_default = JSON.stringify(default_value) == JSON.stringify(value)
if @config[item.key].item.valid_pattern and not @config[item.key].item.isHidden?()
match = value.match(@config[item.key].item.valid_pattern)
@ -69,6 +68,9 @@ class UiConfig extends ZeroFrame
cb(false)
break
if value_same_as_default
value = null
@saveValue(item.key, value, if last then cb else null)
saveValue: (key, value, cb) =>

View File

@ -1336,6 +1336,14 @@
}
if (type === "boolean" && !value) {
return false;
} else if (type === "number") {
if (typeof value === "number") {
return value.toString();
} else if (!value) {
return "0";
} else {
return value;
}
} else {
return value;
}
@ -1379,7 +1387,7 @@
title: "File server port",
type: "text",
valid_pattern: /[0-9]*/,
description: "Other peers will use this port to reach your served sites. (default: 15441)"
description: "Other peers will use this port to reach your served sites. (default: randomize)"
});
section.items.push({
key: "ip_external",
@ -1616,7 +1624,6 @@
}).call(this);
/* ---- ConfigView.coffee ---- */
@ -1934,17 +1941,16 @@
};
UiConfig.prototype.saveValues = function(cb) {
var base, changed_values, i, item, j, last, len, match, message, results, value, value_same_as_default;
var base, changed_values, default_value, i, item, j, last, len, match, message, results, value, value_same_as_default;
changed_values = this.getValuesChanged();
results = [];
for (i = j = 0, len = changed_values.length; j < len; i = ++j) {
item = changed_values[i];
last = i === changed_values.length - 1;
value = this.config_storage.deformatValue(item.value, typeof this.config[item.key]["default"]);
value_same_as_default = JSON.stringify(this.config[item.key]["default"]) === JSON.stringify(value);
if (value_same_as_default) {
value = null;
}
default_value = this.config_storage.deformatValue(this.config[item.key]["default"], typeof this.config[item.key]["default"]);
this.log("default check:", JSON.stringify(default_value), "==", JSON.stringify(value));
value_same_as_default = JSON.stringify(default_value) === JSON.stringify(value);
if (this.config[item.key].item.valid_pattern && !(typeof (base = this.config[item.key].item).isHidden === "function" ? base.isHidden() : void 0)) {
match = value.match(this.config[item.key].item.valid_pattern);
if (!match || match[0] !== value) {
@ -1954,6 +1960,9 @@
break;
}
}
if (value_same_as_default) {
value = null;
}
results.push(this.saveValue(item.key, value, last ? cb : null));
}
return results;
@ -2054,4 +2063,4 @@
window.Page.createProjector();
}).call(this);
}).call(this);

View File

@ -49,6 +49,7 @@ class FileServer(ConnectionServer):
raise Exception("Can't find bindable port")
if not config.tor == "always":
config.saveValue("fileserver_port", port) # Save random port value for next restart
config.arguments.fileserver_port = port
ConnectionServer.__init__(self, ip, port, self.handleRequest)
self.log.debug("Supported IP types: %s" % self.supported_ip_types)

View File

@ -1194,6 +1194,8 @@ class UiWebsocket(object):
@flag.no_multiuser
def actionConfigSet(self, to, key, value):
import main
self.log.debug("Changing config %s value to %r" % (key, value))
if key not in config.keys_api_change_allowed:
self.response(to, {"error": "Forbidden: You cannot set this config key"})
return