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