Network.validate: Deduplicate code + tell users what the invalid hostname is (#4475)

* De-duplicate error message creation in Network.validate()
* Tell users what the invalid hostname is.
* Reword the log error message
This commit is contained in:
Val Lorentz 2022-02-17 01:27:14 +01:00 committed by GitHub
parent cb28204517
commit c12dd6c740
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -114,6 +114,17 @@ Network.prototype.validate = function (client) {
this.proxyPassword = cleanString(this.proxyPassword); this.proxyPassword = cleanString(this.proxyPassword);
this.proxyEnabled = !!this.proxyEnabled; this.proxyEnabled = !!this.proxyEnabled;
const error = function (network, text) {
network.channels[0].pushMessage(
client,
new Msg({
type: Msg.Type.ERROR,
text: text,
}),
true
);
};
if (!this.port) { if (!this.port) {
this.port = this.tls ? 6697 : 6667; this.port = this.tls ? 6697 : 6667;
} }
@ -134,15 +145,7 @@ Network.prototype.validate = function (client) {
this.host.length > 0 && this.host.length > 0 &&
this.host !== Helper.config.defaults.host this.host !== Helper.config.defaults.host
) { ) {
this.channels[0].pushMessage( error(this, `The hostname you specified (${this.host}) is not allowed.`);
client,
new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed.",
}),
true
);
return false; return false;
} }
@ -159,28 +162,16 @@ Network.prototype.validate = function (client) {
} }
if (this.host.length === 0) { if (this.host.length === 0) {
this.channels[0].pushMessage( error(this, "You must specify a hostname to connect.");
client,
new Msg({
type: Msg.Type.ERROR,
text: "You must specify a hostname to connect.",
}),
true
);
return false; return false;
} }
const stsPolicy = STSPolicies.get(this.host); const stsPolicy = STSPolicies.get(this.host);
if (stsPolicy && !this.tls) { if (stsPolicy && !this.tls) {
this.channels[0].pushMessage( error(
client, this,
new Msg({ `${this.host} has an active strict transport security policy, will connect to port ${stsPolicy.port} over a secure connection.`
type: Msg.Type.ERROR,
text: `${this.host} has an active strict transport security policy, will connect to port ${stsPolicy.port} over a secure connection.`,
}),
true
); );
this.port = stsPolicy.port; this.port = stsPolicy.port;