|
|
@ -1,8 +1,10 @@ |
|
|
|
import { SubscriptionService, Subscription } from './subscriptionService.js'; |
|
|
|
import { VapidService, VapidKeys } from './vapidService.js'; |
|
|
|
import { SubscriptionService } from './subscriptionService.js'; |
|
|
|
import { VapidService } from './vapidService.js'; |
|
|
|
import { VapidKeys } from './VapidKeys.js'; |
|
|
|
import * as https from 'https'; |
|
|
|
import * as http_ece from 'http_ece'; |
|
|
|
import crypto from 'crypto'; |
|
|
|
import { Subscription } from "./Subscription.js" |
|
|
|
|
|
|
|
export interface Message { |
|
|
|
title: string; |
|
|
@ -14,7 +16,6 @@ export class NotificationService { |
|
|
|
|
|
|
|
private subscriptionService: SubscriptionService; |
|
|
|
private vapidService: VapidService; |
|
|
|
private vapidKeys: VapidKeys; |
|
|
|
|
|
|
|
constructor() { |
|
|
|
this.subscriptionService = new SubscriptionService(); |
|
|
@ -27,7 +28,6 @@ export class NotificationService { |
|
|
|
|
|
|
|
async broadcast(message: Message): Promise<void> { |
|
|
|
const subscriptions = await this.subscriptionService.fetchSubscriptions(); |
|
|
|
this.vapidKeys = await this.vapidService.getVapidKeys(); |
|
|
|
|
|
|
|
for (const subscription of subscriptions) { |
|
|
|
await this.pushToEndpoint(subscription, message); |
|
|
@ -35,14 +35,13 @@ export class NotificationService { |
|
|
|
} |
|
|
|
|
|
|
|
async sendNotification(subscription: Subscription, message: Message) { |
|
|
|
this.vapidKeys = await this.vapidService.getVapidKeys(); |
|
|
|
await this.pushToEndpoint(subscription, message); |
|
|
|
} |
|
|
|
|
|
|
|
private async pushToEndpoint(subscription: Subscription, message: Message): Promise<void> { |
|
|
|
const payload = JSON.stringify(message); |
|
|
|
|
|
|
|
const encrypted = this.encrypt(subscription.keys.p256dh, subscription.keys.auth, payload); |
|
|
|
const encrypted = this.encrypt(subscription.keys_p256dh, subscription.keys_auth, payload); |
|
|
|
const endpoint = subscription.endpoint; |
|
|
|
|
|
|
|
const vapidHeaders = await this.vapidService.createVapidAuthHeader(endpoint, 12 * 60 * 60, 'mailto:example@example.com'); |
|
|
@ -86,12 +85,12 @@ export class NotificationService { |
|
|
|
'auth': auth |
|
|
|
}; |
|
|
|
|
|
|
|
const vapidKeys: VapidKeys = this.vapidService.getVapidKeys()[0]; |
|
|
|
return http_ece.encrypt(payload, { |
|
|
|
'salt': this.generateSalt(), |
|
|
|
'dh': this.vapidKeys.publicKey, |
|
|
|
'dh': vapidKeys.publicKey, |
|
|
|
'keyid': 'p256dh', |
|
|
|
'contentEncoding': 'aes128gcm' |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|