DmitrMakeev commited on
Commit
92d5424
·
verified ·
1 Parent(s): fb7fad9

Update chat.html

Browse files
Files changed (1) hide show
  1. chat.html +106 -361
chat.html CHANGED
@@ -1,368 +1,113 @@
1
  <!DOCTYPE html>
2
  <html lang="ru">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Secure Chat 🔒</title>
7
- <style>
8
- * {
9
- margin: 0;
10
- padding: 0;
11
- box-sizing: border-box;
12
- }
13
-
14
- body {
15
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
- height: 100vh;
18
- display: flex;
19
- justify-content: center;
20
- align-items: center;
21
- padding: 20px;
22
- }
23
-
24
- .chat-container {
25
- width: 100%;
26
- max-width: 450px;
27
- height: 650px;
28
- background: white;
29
- border-radius: 15px;
30
- box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
31
- display: flex;
32
- flex-direction: column;
33
- overflow: hidden;
34
- }
35
-
36
- .chat-header {
37
- background: #2d3748;
38
- color: white;
39
- padding: 15px 20px;
40
- text-align: center;
41
- position: relative;
42
- }
43
-
44
- .security-badge {
45
- position: absolute;
46
- top: 10px;
47
- right: 15px;
48
- background: #48bb78;
49
- padding: 4px 8px;
50
- border-radius: 12px;
51
- font-size: 0.7em;
52
- font-weight: bold;
53
- }
54
-
55
- .messages-container {
56
- flex: 1;
57
- padding: 20px;
58
- overflow-y: auto;
59
- background: #f7fafc;
60
- }
61
-
62
- .message {
63
- margin-bottom: 15px;
64
- padding: 10px 15px;
65
- border-radius: 15px;
66
- max-width: 80%;
67
- word-wrap: break-word;
68
- }
69
-
70
- .message.own {
71
- background: #4299e1;
72
- color: white;
73
- margin-left: auto;
74
- border-bottom-right-radius: 5px;
75
- }
76
-
77
- .message.other {
78
- background: #e2e8f0;
79
- color: #2d3748;
80
- border-bottom-left-radius: 5px;
81
- }
82
-
83
- .message-info {
84
- font-size: 0.8em;
85
- opacity: 0.7;
86
- margin-bottom: 3px;
87
- }
88
-
89
- .input-container {
90
- padding: 15px 20px;
91
- background: white;
92
- border-top: 1px solid #e2e8f0;
93
- display: flex;
94
- gap: 10px;
95
- align-items: center;
96
- }
97
-
98
- .message-input {
99
- flex: 1;
100
- padding: 12px 15px;
101
- border: 2px solid #e2e8f0;
102
- border-radius: 25px;
103
- outline: none;
104
- font-size: 14px;
105
- transition: border-color 0.3s;
106
- }
107
-
108
- .message-input:focus {
109
- border-color: #4299e1;
110
- }
111
-
112
- .send-button {
113
- padding: 12px 20px;
114
- background: #4299e1;
115
- color: white;
116
- border: none;
117
- border-radius: 25px;
118
- cursor: pointer;
119
- font-weight: bold;
120
- transition: background 0.3s;
121
- }
122
-
123
- .send-button:hover {
124
- background: #3182ce;
125
- }
126
-
127
- .send-button:disabled {
128
- background: #a0aec0;
129
- cursor: not-allowed;
130
- }
131
-
132
- .clear-button {
133
- padding: 8px 15px;
134
- background: #e53e3e;
135
- color: white;
136
- border: none;
137
- border-radius: 20px;
138
- cursor: pointer;
139
- font-size: 0.9em;
140
- transition: background 0.3s;
141
- margin-left: 10px;
142
- }
143
-
144
- .clear-button:hover {
145
- background: #c53030;
146
- }
147
-
148
- .typing-indicator {
149
- padding: 10px 15px;
150
- font-style: italic;
151
- color: #718096;
152
- font-size: 0.9em;
153
- }
154
-
155
- .encryption-notice {
156
- text-align: center;
157
- padding: 8px;
158
- background: #ebf8ff;
159
- color: #2b6cb0;
160
- font-size: 0.8em;
161
- border-bottom: 1px solid #bee3f8;
162
- }
163
-
164
- .messages-container::-webkit-scrollbar {
165
- width: 6px;
166
- }
167
-
168
- .messages-container::-webkit-scrollbar-track {
169
- background: #f1f1f1;
170
- }
171
-
172
- .messages-container::-webkit-scrollbar-thumb {
173
- background: #cbd5e0;
174
- border-radius: 3px;
175
- }
176
-
177
- .messages-container::-webkit-scrollbar-thumb:hover {
178
- background: #a0aec0;
179
- }
180
- </style>
181
  </head>
182
  <body>
183
- <div class="chat-container">
184
- <div class="chat-header">
185
- <h2>💬 Secure Chat 🔒</h2>
186
- <small>Ваш ID: <span id="user-id">{{ user_id }}</span></small>
187
- <div class="security-badge">ЗАЩИЩЕНО</div>
188
- </div>
189
-
190
- <div class="encryption-notice">
191
- 🔐 Сообщения шифруются на сервере • 🗑️ Данные удаляются при выключении
192
- </div>
193
-
194
- <div class="messages-container" id="messages-container">
195
- <div class="typing-indicator" id="welcome-message">
196
- Добро пожаловать в защищённый чат! Сообщения шифруются и не сохраняются после выключения сервера.
197
- </div>
198
- </div>
199
-
200
- <div class="input-container">
201
- <input type="text"
202
- class="message-input"
203
- id="message-input"
204
- placeholder="Введите сообщение..."
205
- maxlength="500">
206
- <button class="send-button" id="send-button">Отправить</button>
207
- <button class="clear-button" id="clear-button" title="Очистить историю чата">🗑️ Очистить</button>
208
- </div>
209
- </div>
210
 
211
- <script>
212
- class SecureChat {
213
- constructor() {
214
- this.userId = document.getElementById('user-id').textContent;
215
- this.messagesContainer = document.getElementById('messages-container');
216
- this.messageInput = document.getElementById('message-input');
217
- this.sendButton = document.getElementById('send-button');
218
- this.clearButton = document.getElementById('clear-button');
219
- this.lastMessageId = 0;
220
-
221
- this.init();
222
- }
223
-
224
- init() {
225
- this.loadMessages();
226
-
227
- this.sendButton.addEventListener('click', () => this.sendMessage());
228
- this.clearButton.addEventListener('click', () => this.clearChat());
229
- this.messageInput.addEventListener('keypress', (e) => {
230
- if (e.key === 'Enter') {
231
- this.sendMessage();
232
- }
233
- });
234
-
235
- setInterval(() => this.loadNewMessages(), 2000);
236
- this.messageInput.focus();
237
- }
238
-
239
- async sendMessage() {
240
- const message = this.messageInput.value.trim();
241
-
242
- if (!message) return;
243
-
244
- this.sendButton.disabled = true;
245
- this.sendButton.textContent = '📤';
246
-
247
- try {
248
- const response = await fetch('/send_message', {
249
- method: 'POST',
250
- headers: {
251
- 'Content-Type': 'application/json',
252
- },
253
- body: JSON.stringify({
254
- user_id: this.userId,
255
- message: message
256
- })
257
- });
258
-
259
- const result = await response.json();
260
-
261
- if (result.status === 'success') {
262
- this.messageInput.value = '';
263
- this.loadNewMessages();
264
- } else {
265
- alert('Ошибка отправки: ' + result.message);
266
- }
267
- } catch (error) {
268
- console.error('Ошибка:', error);
269
- alert('О��ибка соединения с сервером');
270
- } finally {
271
- this.sendButton.disabled = false;
272
- this.sendButton.textContent = 'Отправить';
273
- }
274
- }
275
-
276
- async clearChat() {
277
- if (!confirm('Вы уверены, что хотите очистить всю историю чата?')) {
278
- return;
279
- }
280
-
281
- try {
282
- const response = await fetch('/clear_chat', {
283
- method: 'POST'
284
- });
285
-
286
- const result = await response.json();
287
-
288
- if (result.status === 'success') {
289
- this.messagesContainer.innerHTML = '<div class="typing-indicator">История чата очищена</div>';
290
- this.lastMessageId = 0;
291
- } else {
292
- alert('Ошибка очистки чата');
293
- }
294
- } catch (error) {
295
- console.error('Ошибка:', error);
296
- alert('Ошибка соединения с сервером');
297
- }
298
- }
299
-
300
- async loadMessages() {
301
- try {
302
- const response = await fetch('/get_messages');
303
- const data = await response.json();
304
-
305
- this.displayMessages(data.messages);
306
- this.scrollToBottom();
307
- } catch (error) {
308
- console.error('Ошибка загрузки сообщений:', error);
309
- }
310
- }
311
-
312
- async loadNewMessages() {
313
- try {
314
- const response = await fetch(`/get_new_messages?last_id=${this.lastMessageId}`);
315
- const data = await response.json();
316
-
317
- if (data.messages.length > 0) {
318
- this.displayMessages(data.messages, false);
319
- this.scrollToBottom();
320
- }
321
- } catch (error) {
322
- console.error('Ошибка загрузки новых сообщений:', error);
323
- }
324
- }
325
-
326
- displayMessages(messages, clear = true) {
327
- if (clear) {
328
- const welcomeMsg = document.getElementById('welcome-message');
329
- if (welcomeMsg) welcomeMsg.remove();
330
- this.messagesContainer.innerHTML = '';
331
- }
332
-
333
- messages.forEach(message => {
334
- if (message.id > this.lastMessageId) {
335
- this.lastMessageId = message.id;
336
- }
337
-
338
- const messageElement = document.createElement('div');
339
- messageElement.className = `message ${message.user_id === this.userId ? 'own' : 'other'}`;
340
-
341
- messageElement.innerHTML = `
342
- <div class="message-info">
343
- ${message.username} • ${message.timestamp}
344
- </div>
345
- <div class="message-text">${this.escapeHtml(message.text)}</div>
346
- `;
347
-
348
- this.messagesContainer.appendChild(messageElement);
349
- });
350
- }
351
-
352
- scrollToBottom() {
353
- this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
354
- }
355
-
356
- escapeHtml(text) {
357
- const div = document.createElement('div');
358
- div.textContent = text;
359
- return div.innerHTML;
360
- }
361
- }
362
-
363
- document.addEventListener('DOMContentLoaded', () => {
364
- new SecureChat();
365
- });
366
- </script>
367
  </body>
368
- </html>
 
1
  <!DOCTYPE html>
2
  <html lang="ru">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Secure Chat (Local Encryption)</title>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
8
+ <style>
9
+ body { font-family: sans-serif; background: #eef2f3; display: flex; justify-content: center; align-items: center; height: 100vh; }
10
+ .chat-container { width: 400px; background: white; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.2); overflow: hidden; display: flex; flex-direction: column; }
11
+ .chat-header { background: #2d3748; color: white; text-align: center; padding: 10px; font-weight: bold; }
12
+ .messages { flex: 1; padding: 10px; overflow-y: auto; background: #f9fafb; }
13
+ .message { margin-bottom: 10px; padding: 8px 12px; border-radius: 12px; max-width: 80%; word-wrap: break-word; }
14
+ .message.own { background: #4299e1; color: white; margin-left: auto; }
15
+ .message.other { background: #e2e8f0; color: #2d3748; }
16
+ .input-container { display: flex; padding: 10px; border-top: 1px solid #ddd; }
17
+ #message-input { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 5px; }
18
+ button { margin-left: 10px; padding: 8px 12px; border: none; border-radius: 5px; background: #3182ce; color: white; cursor: pointer; }
19
+ #clear-btn { background: #e53e3e; }
20
+ #key-input { width: 100%; padding: 8px; margin: 8px 0; border-radius: 5px; border: 1px solid #ccc; }
21
+ </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  </head>
23
  <body>
24
+ <div class="chat-container">
25
+ <div class="chat-header">🔐 Secure Chat (AES)</div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ <div style="padding: 10px;">
28
+ <input type="password" id="key-input" placeholder="Введите общий ключ шифрования">
29
+ </div>
30
+
31
+ <div class="messages" id="messages"></div>
32
+
33
+ <div class="input-container">
34
+ <input type="text" id="message-input" placeholder="Введите сообщение...">
35
+ <button id="send-btn">Отправить</button>
36
+ <button id="clear-btn">🗑</button>
37
+ </div>
38
+ </div>
39
+
40
+ <script>
41
+ const userId = "{{ user_id }}";
42
+ let lastId = 0;
43
+ let encryptionKey = '';
44
+
45
+ const keyInput = document.getElementById('key-input');
46
+ const msgInput = document.getElementById('message-input');
47
+ const messagesDiv = document.getElementById('messages');
48
+
49
+ document.getElementById('send-btn').onclick = sendMessage;
50
+ document.getElementById('clear-btn').onclick = clearChat;
51
+ keyInput.oninput = () => encryptionKey = keyInput.value.trim();
52
+
53
+ async function sendMessage() {
54
+ const text = msgInput.value.trim();
55
+ if (!text || !encryptionKey) return alert('Введите сообщение и ключ');
56
+
57
+ const encrypted = CryptoJS.AES.encrypt(text, encryptionKey).toString();
58
+
59
+ const res = await fetch('/send_message', {
60
+ method: 'POST',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify({ user_id: userId, encrypted_text: encrypted })
63
+ });
64
+
65
+ const data = await res.json();
66
+ if (data.status === 'success') {
67
+ msgInput.value = '';
68
+ loadNewMessages();
69
+ }
70
+ }
71
+
72
+ async function loadMessages() {
73
+ const res = await fetch('/get_messages');
74
+ const data = await res.json();
75
+ renderMessages(data.messages);
76
+ }
77
+
78
+ async function loadNewMessages() {
79
+ const res = await fetch(`/get_new_messages?last_id=${lastId}`);
80
+ const data = await res.json();
81
+ if (data.messages.length) renderMessages(data.messages, false);
82
+ }
83
+
84
+ async function clearChat() {
85
+ if (!confirm('Очистить чат?')) return;
86
+ await fetch('/clear_chat', { method: 'POST' });
87
+ messagesDiv.innerHTML = '';
88
+ lastId = 0;
89
+ }
90
+
91
+ function renderMessages(msgs, clear = true) {
92
+ if (clear) messagesDiv.innerHTML = '';
93
+ msgs.forEach(m => {
94
+ lastId = Math.max(lastId, m.id);
95
+ let decrypted = '[🔒 Сообщение зашифровано]';
96
+ if (encryptionKey) {
97
+ try {
98
+ decrypted = CryptoJS.AES.decrypt(m.encrypted_text, encryptionKey).toString(CryptoJS.enc.Utf8) || decrypted;
99
+ } catch {}
100
+ }
101
+ const div = document.createElement('div');
102
+ div.className = 'message ' + (m.user_id === userId ? 'own' : 'other');
103
+ div.innerHTML = `<div><b>${m.username}</b> • ${m.timestamp}</div><div>${decrypted}</div>`;
104
+ messagesDiv.appendChild(div);
105
+ });
106
+ messagesDiv.scrollTop = messagesDiv.scrollHeight;
107
+ }
108
+
109
+ setInterval(loadNewMessages, 2000);
110
+ loadMessages();
111
+ </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  </body>
113
+ </html>