DmitrMakeev commited on
Commit
1587276
·
verified ·
1 Parent(s): a7689f9

Update chat.html

Browse files
Files changed (1) hide show
  1. chat.html +205 -220
chat.html CHANGED
@@ -3,198 +3,186 @@
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
- * {
10
- margin: 0;
11
- padding: 0;
12
- box-sizing: border-box;
13
- }
14
-
15
- body {
16
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
17
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
18
- height: 100vh;
19
- display: flex;
20
- justify-content: center;
21
- align-items: center;
22
- padding: 20px;
23
- }
24
-
25
- .chat-container {
26
- width: 100%;
27
- max-width: 450px;
28
- height: 650px;
29
- background: white;
30
- border-radius: 15px;
31
- box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
32
- display: flex;
33
- flex-direction: column;
34
- overflow: hidden;
35
- }
36
-
37
- .chat-header {
38
- background: #2d3748;
39
- color: white;
40
- padding: 15px 20px;
41
- text-align: center;
42
- position: relative;
43
- }
44
-
45
- .security-badge {
46
- position: absolute;
47
- top: 10px;
48
- right: 15px;
49
- background: #48bb78;
50
- padding: 4px 8px;
51
- border-radius: 12px;
52
- font-size: 0.7em;
53
- font-weight: bold;
54
- }
55
-
56
- .messages-container {
57
- flex: 1;
58
- padding: 20px;
59
- overflow-y: auto;
60
- background: #f7fafc;
61
- }
62
-
63
- .message {
64
- margin-bottom: 15px;
65
- padding: 10px 15px;
66
- border-radius: 15px;
67
- max-width: 80%;
68
- word-wrap: break-word;
69
- }
70
-
71
- .message.own {
72
- background: #4299e1;
73
- color: white;
74
- margin-left: auto;
75
- border-bottom-right-radius: 5px;
76
- }
77
-
78
- .message.other {
79
- background: #e2e8f0;
80
- color: #2d3748;
81
- border-bottom-left-radius: 5px;
82
- }
83
-
84
- .message-info {
85
- font-size: 0.8em;
86
- opacity: 0.7;
87
- margin-bottom: 3px;
88
- }
89
-
90
- .input-container {
91
- padding: 15px 20px;
92
- background: white;
93
- border-top: 1px solid #e2e8f0;
94
- display: flex;
95
- gap: 10px;
96
- align-items: center;
97
- }
98
-
99
- .message-input {
100
- flex: 1;
101
- padding: 12px 15px;
102
- border: 2px solid #e2e8f0;
103
- border-radius: 25px;
104
- outline: none;
105
- font-size: 14px;
106
- transition: border-color 0.3s;
107
- }
108
-
109
- .message-input:focus {
110
- border-color: #4299e1;
111
- }
112
-
113
- .send-button {
114
- padding: 12px 20px;
115
- background: #4299e1;
116
- color: white;
117
- border: none;
118
- border-radius: 25px;
119
- cursor: pointer;
120
- font-weight: bold;
121
- transition: background 0.3s;
122
- }
123
-
124
- .send-button:hover {
125
- background: #3182ce;
126
- }
127
-
128
- .send-button:disabled {
129
- background: #a0aec0;
130
- cursor: not-allowed;
131
- }
132
-
133
- .clear-button {
134
- padding: 8px 15px;
135
- background: #e53e3e;
136
- color: white;
137
- border: none;
138
- border-radius: 20px;
139
- cursor: pointer;
140
- font-size: 0.9em;
141
- transition: background 0.3s;
142
- margin-left: 10px;
143
- }
144
-
145
- .clear-button:hover {
146
- background: #c53030;
147
- }
148
-
149
- .typing-indicator {
150
- padding: 10px 15px;
151
- font-style: italic;
152
- color: #718096;
153
- font-size: 0.9em;
154
- }
155
-
156
- .encryption-notice {
157
- text-align: center;
158
- padding: 8px;
159
- background: #ebf8ff;
160
- color: #2b6cb0;
161
- font-size: 0.8em;
162
- border-bottom: 1px solid #bee3f8;
163
- }
164
-
165
- .messages-container::-webkit-scrollbar {
166
- width: 6px;
167
- }
168
-
169
- .messages-container::-webkit-scrollbar-track {
170
- background: #f1f1f1;
171
- }
172
-
173
- .messages-container::-webkit-scrollbar-thumb {
174
- background: #cbd5e0;
175
- border-radius: 3px;
176
- }
177
-
178
- .messages-container::-webkit-scrollbar-thumb:hover {
179
- background: #a0aec0;
180
- }
181
- </style>
182
  </head>
 
183
  <body>
184
  <div class="chat-container">
185
- <div class="chat-header">🔐 Secure Chat (AES)</div>
 
 
 
 
 
186
 
187
- <div style="padding: 10px;">
188
- <input type="password" id="key-input" placeholder="Введите общий ключ шифрования">
189
- </div>
190
 
191
- <div class="messages" id="messages"></div>
192
 
193
- <div class="input-container">
194
- <input type="text" id="message-input" placeholder="Введите сообщение...">
195
- <button id="send-btn">Отправить</button>
196
- <button id="clear-btn">🗑</button>
197
- </div>
 
 
198
  </div>
199
 
200
  <script>
@@ -211,59 +199,56 @@ document.getElementById('clear-btn').onclick = clearChat;
211
  keyInput.oninput = () => encryptionKey = keyInput.value.trim();
212
 
213
  async function sendMessage() {
214
- const text = msgInput.value.trim();
215
- if (!text || !encryptionKey) return alert('Введите сообщение и ключ');
216
 
217
- const encrypted = CryptoJS.AES.encrypt(text, encryptionKey).toString();
218
 
219
- const res = await fetch('/send_message', {
220
- method: 'POST',
221
- headers: { 'Content-Type': 'application/json' },
222
- body: JSON.stringify({ user_id: userId, encrypted_text: encrypted })
223
- });
224
 
225
- const data = await res.json();
226
- if (data.status === 'success') {
227
  msgInput.value = '';
228
  loadNewMessages();
229
- }
230
  }
231
 
232
  async function loadMessages() {
233
- const res = await fetch('/get_messages');
234
- const data = await res.json();
235
- renderMessages(data.messages);
236
  }
237
 
238
  async function loadNewMessages() {
239
- const res = await fetch(`/get_new_messages?last_id=${lastId}`);
240
- const data = await res.json();
241
- if (data.messages.length) renderMessages(data.messages, false);
242
  }
243
 
244
  async function clearChat() {
245
- if (!confirm('Очистить чат?')) return;
246
- await fetch('/clear_chat', { method: 'POST' });
247
- messagesDiv.innerHTML = '';
248
- lastId = 0;
249
  }
250
 
251
  function renderMessages(msgs, clear = true) {
252
- if (clear) messagesDiv.innerHTML = '';
253
- msgs.forEach(m => {
254
- lastId = Math.max(lastId, m.id);
255
- let decrypted = '[🔒 Сообщение зашифровано]';
256
- if (encryptionKey) {
257
- try {
258
- decrypted = CryptoJS.AES.decrypt(m.encrypted_text, encryptionKey).toString(CryptoJS.enc.Utf8) || decrypted;
259
- } catch {}
260
- }
261
- const div = document.createElement('div');
262
- div.className = 'message ' + (m.user_id === userId ? 'own' : 'other');
263
- div.innerHTML = `<div><b>${m.username}</b> • ${m.timestamp}</div><div>${decrypted}</div>`;
264
- messagesDiv.appendChild(div);
265
- });
266
- messagesDiv.scrollTop = messagesDiv.scrollHeight;
267
  }
268
 
269
  setInterval(loadNewMessages, 2000);
 
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
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
8
+
9
  <style>
10
+ * { margin: 0; padding: 0; box-sizing: border-box; }
11
+
12
+ body {
13
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
14
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
15
+ height: 100vh;
16
+ display: flex;
17
+ justify-content: center;
18
+ align-items: center;
19
+ padding: 20px;
20
+ }
21
+
22
+ .chat-container {
23
+ width: 100%;
24
+ max-width: 480px;
25
+ height: 700px;
26
+ background: white;
27
+ border-radius: 15px;
28
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
29
+ display: flex;
30
+ flex-direction: column;
31
+ overflow: hidden;
32
+ }
33
+
34
+ .chat-header {
35
+ background: #2d3748;
36
+ color: white;
37
+ padding: 15px 20px;
38
+ text-align: center;
39
+ position: relative;
40
+ }
41
+
42
+ .security-badge {
43
+ position: absolute;
44
+ top: 10px;
45
+ right: 15px;
46
+ background: #48bb78;
47
+ padding: 4px 8px;
48
+ border-radius: 12px;
49
+ font-size: 0.7em;
50
+ font-weight: bold;
51
+ }
52
+
53
+ .encryption-notice {
54
+ text-align: center;
55
+ padding: 8px;
56
+ background: #ebf8ff;
57
+ color: #2b6cb0;
58
+ font-size: 0.8em;
59
+ border-bottom: 1px solid #bee3f8;
60
+ }
61
+
62
+ .key-input {
63
+ width: 90%;
64
+ margin: 10px auto;
65
+ display: block;
66
+ padding: 10px 12px;
67
+ border: 2px solid #cbd5e0;
68
+ border-radius: 10px;
69
+ font-size: 14px;
70
+ text-align: center;
71
+ }
72
+
73
+ .messages-container {
74
+ flex: 1;
75
+ padding: 20px;
76
+ overflow-y: auto;
77
+ background: #f7fafc;
78
+ }
79
+
80
+ .message {
81
+ margin-bottom: 15px;
82
+ padding: 10px 15px;
83
+ border-radius: 15px;
84
+ max-width: 80%;
85
+ word-wrap: break-word;
86
+ }
87
+
88
+ .message.own {
89
+ background: #4299e1;
90
+ color: white;
91
+ margin-left: auto;
92
+ border-bottom-right-radius: 5px;
93
+ }
94
+
95
+ .message.other {
96
+ background: #e2e8f0;
97
+ color: #2d3748;
98
+ border-bottom-left-radius: 5px;
99
+ }
100
+
101
+ .message-info {
102
+ font-size: 0.8em;
103
+ opacity: 0.7;
104
+ margin-bottom: 3px;
105
+ }
106
+
107
+ .input-container {
108
+ padding: 15px 20px;
109
+ background: white;
110
+ border-top: 1px solid #e2e8f0;
111
+ display: flex;
112
+ flex-direction: column;
113
+ gap: 10px;
114
+ }
115
+
116
+ .message-input {
117
+ width: 100%;
118
+ height: 70px;
119
+ resize: none;
120
+ padding: 12px 15px;
121
+ border: 2px solid #e2e8f0;
122
+ border-radius: 12px;
123
+ outline: none;
124
+ font-size: 15px;
125
+ line-height: 1.4;
126
+ transition: border-color 0.3s;
127
+ }
128
+
129
+ .message-input:focus { border-color: #4299e1; }
130
+
131
+ .button-row {
132
+ display: flex;
133
+ justify-content: space-between;
134
+ align-items: center;
135
+ }
136
+
137
+ .send-button, .clear-button {
138
+ padding: 10px 20px;
139
+ border: none;
140
+ border-radius: 25px;
141
+ font-weight: bold;
142
+ cursor: pointer;
143
+ transition: background 0.3s;
144
+ }
145
+
146
+ .send-button {
147
+ background: #4299e1;
148
+ color: white;
149
+ }
150
+
151
+ .send-button:hover { background: #3182ce; }
152
+
153
+ .clear-button {
154
+ background: #e53e3e;
155
+ color: white;
156
+ }
157
+
158
+ .clear-button:hover { background: #c53030; }
159
+
160
+ .messages-container::-webkit-scrollbar { width: 6px; }
161
+ .messages-container::-webkit-scrollbar-thumb { background: #cbd5e0; border-radius: 3px; }
162
+ .messages-container::-webkit-scrollbar-thumb:hover { background: #a0aec0; }
163
+ </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  </head>
165
+
166
  <body>
167
  <div class="chat-container">
168
+ <div class="chat-header">
169
+ <h2>💬 Secure Chat 🔒</h2>
170
+ <div class="security-badge">AES ШИФРОВАНИЕ</div>
171
+ </div>
172
+
173
+ <div class="encryption-notice">Введите общий ключ шифрования ниже</div>
174
 
175
+ <input type="password" id="key-input" class="key-input" placeholder="🔑 Ключ шифрования (один и тот же у обоих)" />
 
 
176
 
177
+ <div class="messages-container" id="messages"></div>
178
 
179
+ <div class="input-container">
180
+ <textarea class="message-input" id="message-input" placeholder="Введите сообщение..."></textarea>
181
+ <div class="button-row">
182
+ <button class="send-button" id="send-btn">Отправить</button>
183
+ <button class="clear-button" id="clear-btn">🗑 Очистить</button>
184
+ </div>
185
+ </div>
186
  </div>
187
 
188
  <script>
 
199
  keyInput.oninput = () => encryptionKey = keyInput.value.trim();
200
 
201
  async function sendMessage() {
202
+ const text = msgInput.value.trim();
203
+ if (!text || !encryptionKey) return alert('Введите сообщение и ключ');
204
 
205
+ const encrypted = CryptoJS.AES.encrypt(text, encryptionKey).toString();
206
 
207
+ await fetch('/send_message', {
208
+ method: 'POST',
209
+ headers: { 'Content-Type': 'application/json' },
210
+ body: JSON.stringify({ user_id: userId, encrypted_text: encrypted })
211
+ });
212
 
 
 
213
  msgInput.value = '';
214
  loadNewMessages();
 
215
  }
216
 
217
  async function loadMessages() {
218
+ const res = await fetch('/get_messages');
219
+ const data = await res.json();
220
+ renderMessages(data.messages);
221
  }
222
 
223
  async function loadNewMessages() {
224
+ const res = await fetch(`/get_new_messages?last_id=${lastId}`);
225
+ const data = await res.json();
226
+ if (data.messages.length) renderMessages(data.messages, false);
227
  }
228
 
229
  async function clearChat() {
230
+ if (!confirm('Очистить чат?')) return;
231
+ await fetch('/clear_chat', { method: 'POST' });
232
+ messagesDiv.innerHTML = '';
233
+ lastId = 0;
234
  }
235
 
236
  function renderMessages(msgs, clear = true) {
237
+ if (clear) messagesDiv.innerHTML = '';
238
+ msgs.forEach(m => {
239
+ lastId = Math.max(lastId, m.id);
240
+ let decrypted = '[🔒 Зашифровано]';
241
+ if (encryptionKey) {
242
+ try {
243
+ decrypted = CryptoJS.AES.decrypt(m.encrypted_text, encryptionKey).toString(CryptoJS.enc.Utf8) || decrypted;
244
+ } catch {}
245
+ }
246
+ const div = document.createElement('div');
247
+ div.className = 'message ' + (m.user_id === userId ? 'own' : 'other');
248
+ div.innerHTML = `<div class="message-info">${m.username} • ${m.timestamp}</div><div>${decrypted}</div>`;
249
+ messagesDiv.appendChild(div);
250
+ });
251
+ messagesDiv.scrollTop = messagesDiv.scrollHeight;
252
  }
253
 
254
  setInterval(loadNewMessages, 2000);