|
@ -1,10 +1,10 @@ |
|
|
import SubscriptionService from './subscriptionService.js'; |
|
|
//import SubscriptionService from './subscriptionService.js';
|
|
|
import VapidService from './vapidService.js'; |
|
|
import VapidService from './vapidService.js'; |
|
|
import { VapidKeys } from './VapidKeys.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" |
|
|
//import { Subscription } from "./Subscription.js"
|
|
|
|
|
|
|
|
|
export interface Message { |
|
|
export interface Message { |
|
|
title: string; |
|
|
title: string; |
|
@ -12,34 +12,44 @@ export interface Message { |
|
|
[key: string]: any; |
|
|
[key: string]: any; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export class NotificationService { |
|
|
|
|
|
|
|
|
|
|
|
private subscriptionService: SubscriptionService = SubscriptionService.getInstance(); |
|
|
export interface BrowserSubscription { |
|
|
|
|
|
endpoint: string; |
|
|
|
|
|
keys: { |
|
|
|
|
|
p256dh: string; |
|
|
|
|
|
auth: string; |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class NotificationService { |
|
|
|
|
|
private static instance: NotificationService; |
|
|
|
|
|
// private subscriptionService: SubscriptionService = SubscriptionService.getInstance();
|
|
|
private vapidService: VapidService = VapidService.getInstance(); |
|
|
private vapidService: VapidService = VapidService.getInstance(); |
|
|
|
|
|
|
|
|
constructor() { |
|
|
constructor() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private generateSalt(length = 16): Buffer { |
|
|
public static getInstance(): NotificationService { |
|
|
return crypto.randomBytes(length); |
|
|
if (!NotificationService.instance) { |
|
|
|
|
|
NotificationService.instance = new NotificationService(); |
|
|
|
|
|
} |
|
|
|
|
|
return NotificationService.instance; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async broadcast(message: Message): Promise<void> { |
|
|
private generateSalt(length = 16): Buffer { |
|
|
const subscriptions = await this.subscriptionService.fetchSubscriptions(); |
|
|
return crypto.randomBytes(length); |
|
|
|
|
|
|
|
|
for (const subscription of subscriptions) { |
|
|
|
|
|
await this.pushToEndpoint(subscription, message); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async sendNotification(subscription: Subscription, message: Message) { |
|
|
async sendNotification(subscription: BrowserSubscription, message: Message) { |
|
|
await this.pushToEndpoint(subscription, message); |
|
|
await this.pushToEndpoint(subscription, message); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private async pushToEndpoint(subscription: Subscription, message: Message): Promise<void> { |
|
|
private async pushToEndpoint(subscription: BrowserSubscription, message: Message): Promise<void> { |
|
|
const payload = JSON.stringify(message); |
|
|
const payloadString = JSON.stringify(message); |
|
|
|
|
|
const payloadBuffer = Buffer.from(payloadString, 'utf-8'); |
|
|
|
|
|
|
|
|
const encrypted = this.encrypt(subscription.keys_p256dh, subscription.keys_auth, payload); |
|
|
const encrypted = await this.encrypt(subscription.keys.p256dh, subscription.keys.auth, payloadBuffer); |
|
|
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'); |
|
@ -77,18 +87,32 @@ export class NotificationService { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private encrypt(publicKey: string, auth: string, payload: string): Buffer { |
|
|
|
|
|
http_ece.keys = { |
|
|
|
|
|
'p256dh': publicKey, |
|
|
|
|
|
'auth': auth |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const vapidKeys: VapidKeys = this.vapidService.getVapidKeys()[0]; |
|
|
private async encrypt(publicKey: string, auth: string, payload: Buffer): Promise<Buffer> { |
|
|
|
|
|
try { |
|
|
|
|
|
console.log('Public Key:', publicKey); |
|
|
|
|
|
console.log('Auth:', auth); |
|
|
|
|
|
|
|
|
|
|
|
const vapidKeys: VapidKeys[] = await this.vapidService.getVapidKeys(); |
|
|
|
|
|
const vapidkey: VapidKeys = vapidKeys[0]; |
|
|
|
|
|
const vapidPrivateKeyBase64: string = vapidkey['privateKey']; |
|
|
|
|
|
console.log(vapidPrivateKeyBase64); |
|
|
|
|
|
const vapidPrivateKeyBuffer: Buffer = Buffer.from(vapidPrivateKeyBase64, 'base64'); |
|
|
|
|
|
const ecdh = crypto.createECDH('prime256v1'); |
|
|
|
|
|
ecdh.setPrivateKey(vapidPrivateKeyBuffer); |
|
|
|
|
|
const publicKeyBuffer: Buffer = Buffer.from(publicKey, 'base64'); |
|
|
|
|
|
// const authBuffer: Buffer = Buffer.from(auth, 'base64');
|
|
|
|
|
|
|
|
|
return http_ece.encrypt(payload, { |
|
|
return http_ece.encrypt(payload, { |
|
|
'salt': this.generateSalt(), |
|
|
'salt': this.generateSalt(), |
|
|
'dh': vapidKeys.publicKey, |
|
|
'privateKey': ecdh, // Your VAPID private key
|
|
|
'keyid': 'p256dh', |
|
|
'publicKey': publicKeyBuffer, // Client's public key from the subscription object subscription.keys.p256dh
|
|
|
'contentEncoding': 'aes128gcm' |
|
|
'authSecret': auth // Client's auth secret from the subscription object subscription.keys.auth
|
|
|
}); |
|
|
}); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('Error encrypting payload:', error); |
|
|
|
|
|
// Handle the error as appropriate for your application
|
|
|
|
|
|
throw error; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|