-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension-app.js
More file actions
303 lines (256 loc) · 9.48 KB
/
extension-app.js
File metadata and controls
303 lines (256 loc) · 9.48 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Adapted SecureBit Chat App for Browser Extension
// This is a simplified version of the main app adapted for extension use
class SecureBitExtensionApp {
constructor() {
this.isExtension = true;
this.cryptoUtils = null;
this.webrtcManager = null;
this.currentChannel = null;
this.isConnected = false;
this.initializeApp();
}
async initializeApp() {
try {
// Initialize crypto utilities
await this.initializeCrypto();
// Initialize WebRTC manager
await this.initializeWebRTC();
// Set up extension-specific event listeners
this.setupExtensionListeners();
console.log('SecureBit Extension App initialized');
} catch (error) {
console.error('Failed to initialize SecureBit Extension App:', error);
}
}
async initializeCrypto() {
// Load and initialize crypto utilities
try {
// Import the crypto utilities (you'll need to adapt the import for extension)
if (typeof EnhancedSecureCryptoUtils !== 'undefined') {
this.cryptoUtils = EnhancedSecureCryptoUtils;
} else {
// Fallback crypto implementation for extension
this.cryptoUtils = new ExtensionCryptoUtils();
}
} catch (error) {
console.error('Crypto initialization error:', error);
this.cryptoUtils = new ExtensionCryptoUtils();
}
}
async initializeWebRTC() {
// Initialize WebRTC manager for extension
try {
if (typeof EnhancedSecureWebRTCManager !== 'undefined') {
this.webrtcManager = new EnhancedSecureWebRTCManager();
} else {
this.webrtcManager = new ExtensionWebRTCManager();
}
} catch (error) {
console.error('WebRTC initialization error:', error);
this.webrtcManager = new ExtensionWebRTCManager();
}
}
setupExtensionListeners() {
// Listen for messages from popup and content scripts
if (typeof chrome !== 'undefined' && chrome.runtime) {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
this.handleExtensionMessage(request, sender, sendResponse);
return true;
});
}
}
async handleExtensionMessage(request, sender, sendResponse) {
try {
switch (request.action) {
case 'createChannel':
const channelId = await this.createChannel();
sendResponse({ success: true, data: { channelId } });
break;
case 'joinChannel':
const result = await this.joinChannel(request.data.channelId);
sendResponse({ success: true, data: result });
break;
case 'sendMessage':
await this.sendMessage(request.data.message);
sendResponse({ success: true });
break;
case 'getConnectionStatus':
sendResponse({ success: true, data: { connected: this.isConnected } });
break;
case 'disconnect':
await this.disconnect();
sendResponse({ success: true });
break;
default:
sendResponse({ success: false, error: 'Unknown action' });
}
} catch (error) {
console.error('Extension message handling error:', error);
sendResponse({ success: false, error: error.message });
}
}
async createChannel() {
try {
// Generate a new channel ID
const channelId = this.generateChannelId();
// Initialize WebRTC connection
await this.webrtcManager.createOffer();
this.currentChannel = channelId;
this.isConnected = true;
// Notify all connected components
this.notifyComponents('channelCreated', { channelId });
return channelId;
} catch (error) {
console.error('Create channel error:', error);
throw error;
}
}
async joinChannel(channelId) {
try {
// Join existing channel
await this.webrtcManager.joinChannel(channelId);
this.currentChannel = channelId;
this.isConnected = true;
// Notify all connected components
this.notifyComponents('channelJoined', { channelId });
return { success: true, channelId };
} catch (error) {
console.error('Join channel error:', error);
throw error;
}
}
async sendMessage(message) {
try {
if (!this.isConnected) {
throw new Error('Not connected to any channel');
}
// Encrypt the message
const encryptedMessage = await this.cryptoUtils.encryptMessage(message);
// Send via WebRTC
await this.webrtcManager.sendMessage(encryptedMessage);
// Notify components
this.notifyComponents('messageSent', { message, encrypted: true });
} catch (error) {
console.error('Send message error:', error);
throw error;
}
}
async disconnect() {
try {
if (this.webrtcManager) {
await this.webrtcManager.disconnect();
}
this.isConnected = false;
this.currentChannel = null;
// Notify components
this.notifyComponents('disconnected', {});
} catch (error) {
console.error('Disconnect error:', error);
}
}
notifyComponents(event, data) {
// Send notifications to popup and content scripts
if (typeof chrome !== 'undefined' && chrome.runtime) {
chrome.runtime.sendMessage({
action: 'notifyComponents',
event: event,
data: data
});
}
}
generateChannelId() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
}
// Extension-specific crypto utilities
class ExtensionCryptoUtils {
async encryptMessage(message) {
// Simplified encryption for extension
// In a real implementation, you'd use the full crypto suite
const encoder = new TextEncoder();
const data = encoder.encode(message);
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
const iv = crypto.getRandomValues(new Uint8Array(12));
const encrypted = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: iv },
key,
data
);
return {
encrypted: Array.from(new Uint8Array(encrypted)),
iv: Array.from(iv),
keyId: 'temp'
};
}
async decryptMessage(encryptedData) {
// Simplified decryption for extension
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
const decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: new Uint8Array(encryptedData.iv) },
key,
new Uint8Array(encryptedData.encrypted)
);
const decoder = new TextDecoder();
return decoder.decode(decrypted);
}
}
// Extension-specific WebRTC manager
class ExtensionWebRTCManager {
constructor() {
this.peerConnection = null;
this.dataChannel = null;
}
async createOffer() {
// Simplified WebRTC implementation for extension
this.peerConnection = new RTCPeerConnection({
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
});
this.dataChannel = this.peerConnection.createDataChannel('securebit');
this.dataChannel.onopen = () => {
console.log('Data channel opened');
};
this.dataChannel.onmessage = (event) => {
this.handleMessage(event.data);
};
}
async joinChannel(channelId) {
// Simplified channel joining for extension
console.log('Joining channel:', channelId);
}
async sendMessage(message) {
if (this.dataChannel && this.dataChannel.readyState === 'open') {
this.dataChannel.send(JSON.stringify(message));
} else {
throw new Error('Data channel not ready');
}
}
handleMessage(data) {
try {
const message = JSON.parse(data);
// Notify the app about received message
if (typeof window !== 'undefined' && window.secureBitApp) {
window.secureBitApp.handleReceivedMessage(message);
}
} catch (error) {
console.error('Message handling error:', error);
}
}
async disconnect() {
if (this.dataChannel) {
this.dataChannel.close();
}
if (this.peerConnection) {
this.peerConnection.close();
}
}
}
// Initialize the extension app
window.secureBitApp = new SecureBitExtensionApp();