-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
381 lines (339 loc) · 10.4 KB
/
main.go
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"container/ring"
"encoding/json"
"fmt"
"html"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
const (
MAX_MESSAGES = 100
)
var chat_token = "chat"
type Message struct {
Name string `json:"name"`
Text string `json:"text"`
Image string `json:"image"` // New field for image URL
Time string `json:"time"`
}
type ChatServer struct {
messages *ring.Ring
messagesMux sync.RWMutex
announcedIPs map[string]bool
serverRunning bool
}
func NewChatServer() *ChatServer {
return &ChatServer{
messages: ring.New(MAX_MESSAGES),
announcedIPs: make(map[string]bool),
serverRunning: true,
}
}
func (s *ChatServer) addMessage(msg Message) {
s.messagesMux.Lock()
defer s.messagesMux.Unlock()
s.messages.Value = msg
s.messages = s.messages.Next()
}
func (s *ChatServer) getMessages() []Message {
s.messagesMux.RLock()
defer s.messagesMux.RUnlock()
var messages []Message
s.messages.Do(func(x interface{}) {
if x != nil {
messages = append(messages, x.(Message))
}
})
return messages
}
func (s *ChatServer) clearMessages() {
s.messagesMux.Lock()
s.messages = ring.New(MAX_MESSAGES)
s.messagesMux.Unlock()
s.addMessage(Message{
Name: "System",
Text: "Message history has been cleared",
Time: time.Now().Format("15:04:05"),
})
}
func (s *ChatServer) handleRequest(w http.ResponseWriter, r *http.Request) {
// Check token
if len(r.URL.Path) < len(chat_token) || r.URL.Path[1:len(chat_token)+1] != chat_token {
http.Error(w, "Invalid token", http.StatusForbidden)
return
}
// Handle new IP announcement
clientIP := r.RemoteAddr
if strings.Contains(r.RemoteAddr, ":") {
clientIPparts := strings.Split(r.RemoteAddr, ":")
clientIP = clientIPparts[0]
}
if !s.announcedIPs[clientIP] {
s.announcedIPs[clientIP] = true
s.addMessage(Message{
Name: "System",
Text: fmt.Sprintf("New user joined from IP: %s", clientIP),
Time: time.Now().Format("15:04:05"),
})
}
path := r.URL.Path[len(chat_token)+1:]
switch {
case r.Method == "GET" && path == "/shutdown":
s.serverRunning = false
fmt.Fprintf(w, "Server shutting down...")
go func() {
time.Sleep(time.Second)
os.Exit(0)
}()
case r.Method == "GET" && path == "/clear":
s.clearMessages()
fmt.Fprintf(w, "Messages cleared")
case r.Method == "GET" && path == "/messages":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(s.getMessages())
case r.Method == "POST" && path == "":
if err := r.ParseForm(); err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
name := r.FormValue("name")
message := r.FormValue("message")
image := r.FormValue("image") // New field for image URL
if name != "" && (message != "" || image != "") {
s.addMessage(Message{
Name: html.EscapeString(name),
Text: html.EscapeString(message),
Image: html.EscapeString(image),
Time: time.Now().Format("15:04:05"),
})
}
http.Redirect(w, r, "/"+chat_token, http.StatusSeeOther)
default:
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, getHTML())
}
}
func getHTML() string {
return `<!DOCTYPE html>
<html>
<head>
<title>KISS WebChat</title>
<style>
body {
max-width: 800px;
margin: 0 auto;
padding: 20px;
font-family: Arial, sans-serif;
}
.message {
padding: 10px;
margin: 5px 0;
background: #f0f0f0;
border-radius: 5px;
}
.message.system {
background: #e8f5e9;
border-left: 3px solid #4caf50;
font-size: 0.5em;
padding: 5px;
}
.name {
font-weight: bold;
}
.name.system {
color: #2e7d32;
}
.time {
color: #666;
font-size: 0.8em;
}
form {
margin: 20px 0;
}
input[type="text"] {
padding: 5px;
margin-right: 10px;
}
input[type="submit"], button {
padding: 5px 15px;
}
#messages {
height: 400px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
}
#controls {
display: flex;
gap: 10px;
margin-top: 10px;
}
.admin-btn {
border: none;
border-radius: 3px;
cursor: pointer;
color: white;
font-weight: bold;
}
.kill-btn {
background: #ff5252;
}
.kill-btn:hover {
background: #ff1744;
}
.clear-btn {
background: #ffa726;
}
.clear-btn:hover {
background: #fb8c00;
}
.image {
max-width: 100%;
height: auto;
border-radius: 5px;
margin-top: 5px;
}
</style>
<script>
// Load saved username from cookie
document.addEventListener('DOMContentLoaded', () => {
const savedName = getCookie('username');
if (savedName) {
document.querySelector('input[name="name"]').value = savedName;
}
});
function getCookie(name) {
const value = ` + "`" + `; ${document.cookie}` + "`" + `;
const parts = value.split(` + "`" + `; ${name}=` + "`" + `);
if (parts.length === 2) return parts.pop().split(';').shift();
}
function setCookie(name, value) {
document.cookie = ` + "`" + `${name}=${value};path=/;max-age=31536000` + "`" + `;
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function scrollToBottom() {
const messages = document.getElementById('messages');
messages.scrollTop = messages.scrollHeight;
}
function updateMessages() {
fetch('` + chat_token + `/messages')
.then(response => response.json())
.then(messages => {
const messagesDiv = document.getElementById('messages');
if(JSON.stringify(messages) === JSON.stringify(window.messages)) return;
window.messages = messages;
messagesDiv.innerHTML = messages.map(msg => {
const isSystem = msg.name === 'System';
const messageContent = msg.text && ` + "`" + `<span class="text">${escapeHtml(msg.text)}</span>` + "`" + `;
const imageContent = msg.image && ` + "`" + `<img src="${escapeHtml(msg.image)}" alt="Image" class="image">` + "`" + `;
return ` + "`" + `
<div class="message ${isSystem ? 'system' : ''}">
<span class="name ${isSystem ? 'system' : ''}">${escapeHtml(msg.name)}:</span>
${messageContent || ''}
${imageContent || ''}
<span class="time">[${escapeHtml(msg.time)}]</span>
</div>
` + "`" + `;
}).join('');
scrollToBottom();
});
}
async function killServer() {
if (confirm('Are you sure you want to shut down the server?')) {
try {
await fetch('/` + chat_token + `/shutdown');
alert('Server is shutting down...');
} catch (e) {
// Error is expected as server shuts down
}
}
}
async function clearMessages() {
if (confirm('Are you sure you want to clear all messages?')) {
try {
await fetch('/` + chat_token + `/clear');
updateMessages();
} catch (e) {
alert('Failed to clear messages');
}
}
}
// Update messages every 2 seconds
setInterval(updateMessages, 2000);
// Submit form without page reload
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('form');
form.onsubmit = async (e) => {
e.preventDefault();
const formData = new FormData(form);
// Save username to cookie
const username = formData.get('name');
setCookie('username', username);
await fetch('/` + chat_token + `', {
method: 'POST',
body: new URLSearchParams(formData)
});
// Only reset message field, keep the name
document.querySelector('input[name="message"]').value = '';
document.querySelector('input[name="image"]').value = '';
updateMessages();
};
});
</script>
</head>
<body onload="updateMessages()">
<h1>KISS WebChat</h1>
<div id="messages"></div>
<form>
<input type="text" name="name" placeholder="Your name" required>
<input type="text" name="message" placeholder="Your message" required>
<input type="text" name="image" placeholder="Image URL (optional)">
<input type="submit" value="Send">
</form>
<div id="controls">
<button onclick="clearMessages()" class="admin-btn clear-btn">Clear Messages</button>
<button onclick="killServer()" class="admin-btn kill-btn">Shutdown Server</button>
</div>
</body>
</html>`
}
func main() {
port := 8000
if len(os.Args) > 1 {
if p, err := strconv.Atoi(os.Args[1]); err == nil {
port = p
}
}
if len(os.Args) > 2 {
chat_token = os.Args[2]
}
server := NewChatServer()
fmt.Printf("\nChat URL: https://localhost:%d/%s\n", port, chat_token)
fmt.Printf("Admin token for shutdown/clear: %s\n\n", chat_token)
http.HandleFunc("/", server.handleRequest)
if port != 8000 {
// Configure TLS
err := http.ListenAndServeTLS(fmt.Sprintf(":%d", port), "server.crt", "server.key", nil)
if err != nil {
log.Fatal("ListenAndServeTLS: ", err)
}
} else {
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
}