hardlounge/server/plugins/webpush.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

import _ from "lodash";
import log from "../log";
import fs from "fs";
import path from "path";
import WebPushAPI from "web-push";
import Config from "../config";
import Client from "../client";
import * as os from "os";
2017-07-10 19:47:03 +00:00
class WebPush {
vapidKeys?: {
publicKey: string;
privateKey: string;
};
2017-07-10 19:47:03 +00:00
constructor() {
const vapidPath = path.join(Config.getHomePath(), "vapid.json");
2017-07-10 19:47:03 +00:00
let vapidStat: fs.Stats | undefined = undefined;
try {
vapidStat = fs.statSync(vapidPath);
} catch {
// ignored on purpose, node v14.17.0 will give us {throwIfNoEntry: false}
}
if (vapidStat) {
const isWorldReadable = (vapidStat.mode & 0o004) !== 0;
if (isWorldReadable) {
log.warn(
vapidPath,
"is world readable.",
"The file contains secrets. Please fix the permissions."
);
if (os.platform() !== "win32") {
log.warn(`run \`chmod o= "${vapidPath}"\` to correct it.`);
}
}
2017-07-10 19:47:03 +00:00
const data = fs.readFileSync(vapidPath, "utf-8");
const parsedData = JSON.parse(data);
2019-07-17 09:33:59 +00:00
if (
typeof parsedData.publicKey === "string" &&
typeof parsedData.privateKey === "string"
) {
2017-07-10 19:47:03 +00:00
this.vapidKeys = {
publicKey: parsedData.publicKey,
privateKey: parsedData.privateKey,
};
}
}
if (!this.vapidKeys) {
this.vapidKeys = WebPushAPI.generateVAPIDKeys();
fs.writeFileSync(vapidPath, JSON.stringify(this.vapidKeys, null, "\t"), {
mode: 0o600,
});
2017-07-10 19:47:03 +00:00
log.info("New VAPID key pair has been generated for use with push subscription.");
}
WebPushAPI.setVapidDetails(
"https://github.com/thelounge/thelounge",
2017-07-10 19:47:03 +00:00
this.vapidKeys.publicKey,
this.vapidKeys.privateKey
);
}
push(client: Client, payload: any, onlyToOffline: boolean) {
_.forOwn(client.config.sessions, ({pushSubscription}, token) => {
if (pushSubscription) {
if (onlyToOffline && _.find(client.attachedClients, {token}) !== undefined) {
2017-07-10 19:47:03 +00:00
return;
}
this.pushSingle(client, pushSubscription, payload);
2017-07-10 19:47:03 +00:00
}
});
}
pushSingle(client: Client, subscription: WebPushAPI.PushSubscription, payload: any) {
2019-07-17 09:33:59 +00:00
WebPushAPI.sendNotification(subscription, JSON.stringify(payload)).catch((error) => {
if (error.statusCode >= 400 && error.statusCode < 500) {
log.warn(
`WebPush subscription for ${client.name} returned an error (${String(
error.statusCode
)}), removing subscription`
2019-07-17 09:33:59 +00:00
);
2017-07-10 19:47:03 +00:00
2019-07-17 09:33:59 +00:00
_.forOwn(client.config.sessions, ({pushSubscription}, token) => {
if (pushSubscription && pushSubscription.endpoint === subscription.endpoint) {
client.unregisterPushSubscription(token);
}
});
2017-07-10 19:47:03 +00:00
2019-07-17 09:33:59 +00:00
return;
}
2017-07-10 19:47:03 +00:00
log.error(`WebPush Error (${String(error)})`);
2019-07-17 09:33:59 +00:00
});
2017-07-10 19:47:03 +00:00
}
}
export default WebPush;