-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (179 loc) · 5.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { rejects } = require("assert");
const { initializeApp } = require("firebase/app");
const { getMessaging, getToken, onMessage } = require("firebase/messaging");
const mqtt = require("mqtt");
const { resolve } = require("url");
const EventEmitter = require('events');
const { Client } = require('@stomp/stompjs');
const CryptoJS = require("crypto-js");
class Arad extends EventEmitter {
constructor() {
super();
// private data and functions
let initing = false;
let fcm = null;
let secretKey = null;
const generateClientId = function() {
let result = 'omid-pwa-';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < 16; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const encryptConfig = function(plainText, secretKey) {
var ciphertext = CryptoJS.AES.encrypt(plainText, secretKey);
return ciphertext.toString();
}
const decryptConfig = function(ciphertext, secretKey) {
var bytes = CryptoJS.AES.decrypt(ciphertext, secretKey);
var originalConfig = bytes.toString(CryptoJS.enc.Utf8);
return JSON.parse(originalConfig);
}
const connect = function() {
const config = this.decryptConfig(localStorage.getItem('config'), this.secretKey);
const client = new Client({
brokerURL: config.url,
connectHeaders: {
login: config.username,
passcode: config.password,
},
onConnect: () => {
client.subscribe(
config.username,
(message) => {
this.emit('MessageReceive', message.body);
client.deactivate();
});
},
onStompError: (frame) => {
console.warn('connection error', frame.headers['message']);
},
onWebSocketClose: (event) => {
// Will be called when WebSocket closes
console.warn('connection closed: ', event);
},
});
client.activate();
}
// public functions
this.init = function(firebaseConfig, vapidkey) {
return new Promise((resolve, reject) => {
if (!initing) {
initing = true;
const app = initializeApp(firebaseConfig);
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
const messaging = getMessaging();
getToken(messaging, { vapidKey: vapidkey }).then((currentToken) => {
if (currentToken) {
fcm = currentToken;
resolve(fcm);
initing = false;
} else {
reject('Can not init firebase.');
initing = false;
}
}).catch((err) => {
reject('Can not init firebase.');
initing = false;
});
// Handle incoming messages
onMessage(messaging, (payload) => {
if (this.checkConfig()) {
this.emit('WakeUp', true);
}
});
} else {
// else
}
});
}
});
}
this.getMessage = function() {
connect();
}
this.checkConfig = function() {
const configObject = localStorage.getItem('config');
let ready = false;
if (!configObject) {
return false;
} else {
const c = decryptConfig(configObject, secretKey);
for (let key in c) {
if (c[key] === null || c[key] === undefined || c[key] === '') {
return false;
}
}
return true;
}
}
this.setConfig = function(user, pass, connectUrl) {
const config = {
username: user,
password: pass,
url: connectUrl,
};
// encrypt and store the config
const encConfig = encryptConfig(JSON.stringify(config), secretKey);
localStorage.setItem('config', encConfig);
}
this.getToken = function() {
return fcm;
}
this.setKey = function(key) {
secretKey = key;
}
}
}
class DeviceUtils {
constructor() {
this.getOs = function() {
const userAgent = window.navigator.userAgent;
const platform = window.navigator.platform;
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
let os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS' + ' (' + platform + ')';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS' + ' (' + platform + ')';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows' + ' (' + platform + ')';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (!os && /Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
this.getBrowser = function() {
const [userAgent] = [navigator.userAgent];
let browserName = '';
if (/MSIE|Trident/.test(userAgent)) {
browserName = 'ie';
} else if (/Edge/.test(userAgent)) {
browserName = 'edge';
} else if (/CriOS/.test(userAgent)) {
browserName = 'chrome';
} else if (/Chrome/.test(userAgent)) {
browserName = 'chrome';
} else if (/Firefox/.test(userAgent)) {
browserName = 'firefox';
} else if (/Safari/.test(userAgent)) {
browserName = 'safari';
} else {
browserName = 'Unknown';
}
return browserName;
}
}
}
// class deviceUtils
module.exports = {
Arad,
DeviceUtils
};