2022-06-19 00:25:21 +00:00
|
|
|
import {IrcEventHandler} from "../../client";
|
2020-02-19 10:18:47 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import Msg from "../../models/msg";
|
|
|
|
import STSPolicies from "../sts";
|
2020-02-19 10:18:47 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default <IrcEventHandler>function (irc, network) {
|
2020-02-19 10:18:47 +00:00
|
|
|
const client = this;
|
|
|
|
|
|
|
|
irc.on("cap ls", (data) => {
|
2020-02-19 18:20:28 +00:00
|
|
|
handleSTS(data, true);
|
2020-02-19 10:18:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("cap new", (data) => {
|
2020-02-19 18:20:28 +00:00
|
|
|
handleSTS(data, false);
|
2020-02-19 10:18:47 +00:00
|
|
|
});
|
|
|
|
|
2020-02-19 18:20:28 +00:00
|
|
|
function handleSTS(data, shouldReconnect) {
|
2020-02-19 10:18:47 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(data.capabilities, "sts")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isSecure = irc.connection.transport.socket.encrypted;
|
2022-06-19 00:25:21 +00:00
|
|
|
const values = {} as any;
|
2020-02-19 10:18:47 +00:00
|
|
|
|
|
|
|
data.capabilities.sts.split(",").map((value) => {
|
|
|
|
value = value.split("=", 2);
|
|
|
|
values[value[0]] = value[1];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isSecure) {
|
2020-02-19 11:20:22 +00:00
|
|
|
const duration = parseInt(values.duration, 10);
|
|
|
|
|
|
|
|
if (isNaN(duration)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
STSPolicies.update(network.host, network.port, duration);
|
2020-02-19 10:18:47 +00:00
|
|
|
} else {
|
|
|
|
const port = parseInt(values.port, 10);
|
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
2020-02-19 11:20:22 +00:00
|
|
|
text: `Server sent a strict transport security policy, reconnecting to ${network.host}:${port}…`,
|
2020-02-19 10:18:47 +00:00
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2020-02-19 18:20:28 +00:00
|
|
|
// Forcefully end the connection if STS is seen in CAP LS
|
|
|
|
// We will update the port and tls setting if we see CAP NEW,
|
|
|
|
// but will not force a reconnection
|
|
|
|
if (shouldReconnect) {
|
|
|
|
irc.connection.end();
|
|
|
|
}
|
2020-02-19 10:18:47 +00:00
|
|
|
|
|
|
|
// Update the port
|
|
|
|
network.port = port;
|
|
|
|
irc.options.port = port;
|
|
|
|
|
|
|
|
// Enable TLS
|
|
|
|
network.tls = true;
|
|
|
|
network.rejectUnauthorized = true;
|
|
|
|
irc.options.tls = true;
|
|
|
|
irc.options.rejectUnauthorized = true;
|
|
|
|
|
2020-02-19 18:20:28 +00:00
|
|
|
if (shouldReconnect) {
|
|
|
|
// Start a new connection
|
|
|
|
irc.connect();
|
|
|
|
}
|
2020-02-19 10:18:47 +00:00
|
|
|
|
|
|
|
client.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|