summaryrefslogtreecommitdiffstats
path: root/main.go
blob: 30bb560e778158202aff9472bb6f8a0fe9b12a0a (plain) (blame)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package main

import (
	"crypto/rand"
	"embed"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"sync"
	"syscall"
	"time"

	"github.com/awnumar/memguard"
)

//go:embed static/index.html
var staticFS embed.FS

// Version info
const (
	Version   = "2.0.0"
	BuildType = "production"
)

// Client represents a connected chat client
type Client struct {
	ID           string
	Nickname     string
	MessageChan  chan []byte
	ConnectedAt  time.Time
	LastActivity time.Time
	mu           sync.Mutex
}

// Message represents an encrypted message
type Message struct {
	Type       string `json:"type"`
	From       string `json:"from,omitempty"`
	FromID     string `json:"fromId,omitempty"`
	Content    string `json:"content,omitempty"`
	PublicKey  string `json:"publicKey,omitempty"`
	TargetID   string `json:"targetId,omitempty"`
	Timestamp  int64  `json:"timestamp,omitempty"`
	SessionID  string `json:"sessionId,omitempty"`
	Users      []User `json:"users,omitempty"`
}

// User represents a user in the user list
type User struct {
	ID        string `json:"id"`
	Nickname  string `json:"nickname"`
	PublicKey string `json:"publicKey,omitempty"`
}

// Server holds the chat server state
type Server struct {
	clients    map[string]*Client
	clientsMu  sync.RWMutex
	publicKeys map[string]string // clientID -> publicKey
	keysMu     sync.RWMutex
}

var server *Server

func main() {
	// Initialize memguard
	memguard.CatchInterrupt()
	defer memguard.Purge()

	// Note: memguard handles core dump protection internally

	// Initialize server
	server = &Server{
		clients:    make(map[string]*Client),
		publicKeys: make(map[string]string),
	}

	// Setup signal handling for graceful shutdown
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

	go func() {
		<-sigChan
		fmt.Println("\nšŸ›‘ Shutting down securely...")
		server.disconnectAll()
		memguard.Purge()
		os.Exit(0)
	}()

	// Setup HTTP routes
	mux := http.NewServeMux()

	// Serve embedded static files
	mux.HandleFunc("/", handleIndex)
	mux.HandleFunc("/events", handleSSE)
	mux.HandleFunc("/send", handleSend)
	mux.HandleFunc("/join", handleJoin)
	mux.HandleFunc("/leave", handleLeave)
	mux.HandleFunc("/key-exchange", handleKeyExchange)
	mux.HandleFunc("/health", handleHealth)

	// Get port from environment or default
	port := os.Getenv("NOSHITALK_PORT")
	if port == "" {
		port = "8080"
	}

	// Print startup banner
	printBanner(port)

	// Start server
	srv := &http.Server{
		Addr:         ":" + port,
		Handler:      securityHeaders(mux),
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 0, // SSE needs no write timeout
		IdleTimeout:  120 * time.Second,
	}

	if err := srv.ListenAndServe(); err != nil {
		log.Fatalf("Server error: %v", err)
	}
}

func printBanner(port string) {
	fmt.Println("╔═══════════════════════════════════════════════════════════╗")
	fmt.Println("ā•‘           šŸ” NoshiTalk Secure Chat v" + Version + "               ā•‘")
	fmt.Println("╠═══════════════════════════════════════════════════════════╣")
	fmt.Println("ā•‘  āœ“ End-to-End Encryption (X25519 + AES-GCM-256)           ā•‘")
	fmt.Println("ā•‘  āœ“ Zero-Knowledge Server (blind relay)                   ā•‘")
	fmt.Println("ā•‘  āœ“ Perfect Forward Secrecy                               ā•‘")
	fmt.Println("ā•‘  āœ“ Memory Protection (memguard)                          ā•‘")
	fmt.Println("ā•‘  āœ“ Zero Logging Policy                                   ā•‘")
	fmt.Println("ā•‘  āœ“ Tor Hidden Service Ready                              ā•‘")
	fmt.Println("╠═══════════════════════════════════════════════════════════╣")
	fmt.Printf("ā•‘  🌐 Listening on: http://localhost:%-22s  ā•‘\n", port)
	fmt.Println("ā•‘  šŸ§… Tor: Configure hidden service to this port           ā•‘")
	fmt.Println("ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•")
	fmt.Println()
}

// Security headers middleware
func securityHeaders(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Security headers
		w.Header().Set("X-Content-Type-Options", "nosniff")
		w.Header().Set("X-Frame-Options", "DENY")
		w.Header().Set("X-XSS-Protection", "1; mode=block")
		w.Header().Set("Referrer-Policy", "no-referrer")
		w.Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()")
		
		// CSP allowing Web Crypto API
		w.Header().Set("Content-Security-Policy", 
			"default-src 'self'; "+
			"script-src 'self' 'unsafe-inline'; "+
			"style-src 'self' 'unsafe-inline'; "+
			"connect-src 'self'; "+
			"img-src 'self' data:; "+
			"font-src 'self' data:;")
		
		next.ServeHTTP(w, r)
	})
}

// Generate secure random ID
func generateID() string {
	bytes := make([]byte, 16)
	if _, err := rand.Read(bytes); err != nil {
		// Fallback to timestamp-based ID
		return fmt.Sprintf("%d", time.Now().UnixNano())
	}
	return hex.EncodeToString(bytes)
}

// Generate secure session token
func generateSessionToken() string {
	bytes := make([]byte, 32)
	if _, err := rand.Read(bytes); err != nil {
		return ""
	}
	tokenStr := hex.EncodeToString(bytes)
	// Clear the original bytes
	for i := range bytes {
		bytes[i] = 0
	}
	return tokenStr
}

// Handle index page
func handleIndex(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
	
	content, err := staticFS.ReadFile("static/index.html")
	if err != nil {
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}
	
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
	w.Write(content)
}

// Handle SSE connections
func handleSSE(w http.ResponseWriter, r *http.Request) {
	// Get session ID from query
	sessionID := r.URL.Query().Get("session")
	if sessionID == "" {
		http.Error(w, "Session required", http.StatusBadRequest)
		return
	}

	// Verify client exists
	server.clientsMu.RLock()
	client, exists := server.clients[sessionID]
	server.clientsMu.RUnlock()

	if !exists {
		http.Error(w, "Invalid session", http.StatusUnauthorized)
		return
	}

	// Set SSE headers
	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("X-Accel-Buffering", "no")

	flusher, ok := w.(http.Flusher)
	if !ok {
		http.Error(w, "Streaming not supported", http.StatusInternalServerError)
		return
	}

	// Send initial connection confirmation
	fmt.Fprintf(w, "event: connected\ndata: {\"status\":\"ok\"}\n\n")
	flusher.Flush()

	// Send current user list
	server.broadcastUserList()

	// Keep connection open and send messages
	for {
		select {
		case msg := <-client.MessageChan:
			fmt.Fprintf(w, "data: %s\n\n", msg)
			flusher.Flush()
			client.mu.Lock()
			client.LastActivity = time.Now()
			client.mu.Unlock()
		case <-r.Context().Done():
			// Client disconnected
			server.removeClient(sessionID)
			return
		}
	}
}

// Handle client join
func handleJoin(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	var req struct {
		Nickname  string `json:"nickname"`
		PublicKey string `json:"publicKey"`
	}

	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid request", http.StatusBadRequest)
		return
	}

	// Sanitize nickname
	nickname := strings.TrimSpace(req.Nickname)
	if nickname == "" {
		nickname = "Anonymous"
	}
	if len(nickname) > 32 {
		nickname = nickname[:32]
	}

	// Generate IDs and tokens
	clientID := generateID()
	tokenStr := generateSessionToken()

	// Create client
	client := &Client{
		ID:           clientID,
		Nickname:     nickname,
		MessageChan:  make(chan []byte, 100),
		ConnectedAt:  time.Now(),
		LastActivity: time.Now(),
	}

	// Store client and public key
	server.clientsMu.Lock()
	server.clients[clientID] = client
	server.clientsMu.Unlock()

	if req.PublicKey != "" {
		server.keysMu.Lock()
		server.publicKeys[clientID] = req.PublicKey
		server.keysMu.Unlock()
	}

	// Broadcast join message to others
	joinMsg := Message{
		Type:      "user_joined",
		From:      nickname,
		FromID:    clientID,
		PublicKey: req.PublicKey,
		Timestamp: time.Now().Unix(),
	}
	server.broadcast(joinMsg, clientID)

	// Respond with session info
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"sessionId": clientID,
		"token":     tokenStr,
		"nickname":  nickname,
	})
}

// Handle client leave
func handleLeave(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	sessionID := r.URL.Query().Get("session")
	if sessionID != "" {
		server.removeClient(sessionID)
	}

	w.WriteHeader(http.StatusOK)
}

// Handle message sending
func handleSend(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	sessionID := r.URL.Query().Get("session")
	if sessionID == "" {
		http.Error(w, "Session required", http.StatusBadRequest)
		return
	}

	// Verify client exists
	server.clientsMu.RLock()
	client, exists := server.clients[sessionID]
	server.clientsMu.RUnlock()

	if !exists {
		http.Error(w, "Invalid session", http.StatusUnauthorized)
		return
	}

	var msg Message
	if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
		http.Error(w, "Invalid message", http.StatusBadRequest)
		return
	}

	// Add metadata
	msg.FromID = sessionID
	msg.From = client.Nickname
	msg.Timestamp = time.Now().Unix()

	// Update activity
	client.mu.Lock()
	client.LastActivity = time.Now()
	client.mu.Unlock()

	// Route message
	if msg.TargetID != "" {
		// Private message - send only to target
		server.sendToClient(msg.TargetID, msg)
		// Also send back to sender for confirmation
		server.sendToClient(sessionID, msg)
	} else {
		// Broadcast to all (including sender)
		server.broadcastAll(msg)
	}

	w.WriteHeader(http.StatusOK)
}

// Handle key exchange requests
func handleKeyExchange(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet {
		// Return all public keys
		server.keysMu.RLock()
		keys := make(map[string]string)
		for id, key := range server.publicKeys {
			keys[id] = key
		}
		server.keysMu.RUnlock()

		// Also include nicknames
		server.clientsMu.RLock()
		users := make([]User, 0, len(server.clients))
		for id, client := range server.clients {
			users = append(users, User{
				ID:        id,
				Nickname:  client.Nickname,
				PublicKey: keys[id],
			})
		}
		server.clientsMu.RUnlock()

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]interface{}{
			"users": users,
		})
		return
	}

	if r.Method == http.MethodPost {
		// Update public key
		sessionID := r.URL.Query().Get("session")
		if sessionID == "" {
			http.Error(w, "Session required", http.StatusBadRequest)
			return
		}

		var req struct {
			PublicKey string `json:"publicKey"`
		}
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			http.Error(w, "Invalid request", http.StatusBadRequest)
			return
		}

		server.keysMu.Lock()
		server.publicKeys[sessionID] = req.PublicKey
		server.keysMu.Unlock()

		// Broadcast key update
		server.clientsMu.RLock()
		client, exists := server.clients[sessionID]
		server.clientsMu.RUnlock()

		if exists {
			keyMsg := Message{
				Type:      "key_update",
				FromID:    sessionID,
				From:      client.Nickname,
				PublicKey: req.PublicKey,
				Timestamp: time.Now().Unix(),
			}
			server.broadcast(keyMsg, sessionID)
		}

		w.WriteHeader(http.StatusOK)
		return
	}

	http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}

// Health check endpoint
func handleHealth(w http.ResponseWriter, r *http.Request) {
	server.clientsMu.RLock()
	clientCount := len(server.clients)
	server.clientsMu.RUnlock()

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"status":  "ok",
		"version": Version,
		"clients": clientCount,
	})
}

// Broadcast to all clients except sender
func (s *Server) broadcast(msg Message, excludeID string) {
	data, _ := json.Marshal(msg)

	s.clientsMu.RLock()
	defer s.clientsMu.RUnlock()

	for id, client := range s.clients {
		if id != excludeID {
			select {
			case client.MessageChan <- data:
			default:
				// Channel full, skip
			}
		}
	}
}

// Broadcast to all clients including sender
func (s *Server) broadcastAll(msg Message) {
	data, _ := json.Marshal(msg)

	s.clientsMu.RLock()
	defer s.clientsMu.RUnlock()

	for _, client := range s.clients {
		select {
		case client.MessageChan <- data:
		default:
			// Channel full, skip
		}
	}
}

// Send to specific client
func (s *Server) sendToClient(clientID string, msg Message) {
	data, _ := json.Marshal(msg)

	s.clientsMu.RLock()
	client, exists := s.clients[clientID]
	s.clientsMu.RUnlock()

	if exists {
		select {
		case client.MessageChan <- data:
		default:
			// Channel full, skip
		}
	}
}

// Broadcast user list to all clients
func (s *Server) broadcastUserList() {
	s.clientsMu.RLock()
	s.keysMu.RLock()

	users := make([]User, 0, len(s.clients))
	for id, client := range s.clients {
		users = append(users, User{
			ID:        id,
			Nickname:  client.Nickname,
			PublicKey: s.publicKeys[id],
		})
	}

	s.keysMu.RUnlock()
	s.clientsMu.RUnlock()

	msg := Message{
		Type:      "user_list",
		Users:     users,
		Timestamp: time.Now().Unix(),
	}

	s.broadcastAll(msg)
}

// Remove client and cleanup
func (s *Server) removeClient(clientID string) {
	s.clientsMu.Lock()
	client, exists := s.clients[clientID]
	if exists {
		close(client.MessageChan)
		delete(s.clients, clientID)
	}
	nickname := ""
	if client != nil {
		nickname = client.Nickname
	}
	s.clientsMu.Unlock()

	// Remove public key
	s.keysMu.Lock()
	delete(s.publicKeys, clientID)
	s.keysMu.Unlock()

	if exists {
		// Broadcast leave message
		leaveMsg := Message{
			Type:      "user_left",
			FromID:    clientID,
			From:      nickname,
			Timestamp: time.Now().Unix(),
		}
		s.broadcast(leaveMsg, clientID)
		s.broadcastUserList()
	}
}

// Disconnect all clients (for shutdown)
func (s *Server) disconnectAll() {
	s.clientsMu.Lock()
	defer s.clientsMu.Unlock()

	for id, client := range s.clients {
		close(client.MessageChan)
		delete(s.clients, id)
	}

	s.keysMu.Lock()
	for id := range s.publicKeys {
		delete(s.publicKeys, id)
	}
	s.keysMu.Unlock()
}