-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
174 lines (151 loc) · 4.11 KB
/
server.go
File metadata and controls
174 lines (151 loc) · 4.11 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
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"log"
"net"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 65536,
WriteBufferSize: 65536,
CheckOrigin: func(r *http.Request) bool { return true },
}
type Server struct {
cfg *Config
hub *Hub
srv *http.Server
auth *Auth
limiter *RateLimiter
}
func NewServer(cfg *Config, hub *Hub) *Server {
s := &Server{
cfg: cfg,
hub: hub,
auth: NewAuth(),
limiter: NewRateLimiter(cfg.RateLimitPerIP),
}
mux := http.NewServeMux()
mux.HandleFunc("/", s.handleIndex)
mux.HandleFunc("/health", s.handleHealth)
mux.HandleFunc("/ws", s.handleWS)
s.srv = &http.Server{
Addr: cfg.Addr,
Handler: mux,
ReadTimeout: 120 * time.Second,
WriteTimeout: 120 * time.Second,
IdleTimeout: 120 * time.Second,
}
return s
}
func (s *Server) ListenAndServe() error {
if s.cfg.TLSCert != "" && s.cfg.TLSKey != "" {
s.srv.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS13,
}
log.Printf("TLS enabled (cert=%s)", s.cfg.TLSCert)
return s.srv.ListenAndServeTLS(s.cfg.TLSCert, s.cfg.TLSKey)
}
log.Println("TLS disabled (no cert/key configured)")
return s.srv.ListenAndServe()
}
func (s *Server) Shutdown() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v", err)
}
}
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok"}`))
}
func (s *Server) handleWS(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)
if !s.limiter.Allow(ip) {
http.Error(w, "rate limit exceeded", http.StatusTooManyRequests)
return
}
roomID := r.URL.Query().Get("room")
token := r.URL.Query().Get("token")
pubkey := r.URL.Query().Get("pubkey")
if roomID == "" || token == "" {
http.Error(w, "missing room or token", http.StatusBadRequest)
return
}
// Host provides pubkey to register; guests don't
isHost := pubkey != ""
var claims *Claims
var err error
if isHost {
hostPubKey, decErr := base64.RawURLEncoding.DecodeString(pubkey)
if decErr != nil || len(hostPubKey) != 32 {
http.Error(w, "invalid pubkey", http.StatusBadRequest)
return
}
claims, err = s.auth.ValidateJWT(token, hostPubKey)
if err != nil {
http.Error(w, "invalid token: "+err.Error(), http.StatusUnauthorized)
return
}
if claims.RoomID != roomID {
http.Error(w, "room mismatch", http.StatusForbidden)
return
}
s.hub.RegisterHostKey(roomID, hostPubKey)
} else {
hostKey := s.hub.GetHostKey(roomID)
if hostKey == nil {
http.Error(w, "room not found", http.StatusNotFound)
return
}
claims, err = s.auth.ValidateJWT(token, hostKey)
if err != nil {
http.Error(w, "invalid token: "+err.Error(), http.StatusUnauthorized)
return
}
if claims.RoomID != roomID {
http.Error(w, "room mismatch", http.StatusForbidden)
return
}
}
if isHost {
if s.hub.RoomCount() >= s.cfg.MaxRooms {
http.Error(w, "max rooms reached", http.StatusServiceUnavailable)
return
}
} else {
if count := s.hub.ClientCount(roomID); count >= s.cfg.MaxClientsPerRoom {
http.Error(w, "room full", http.StatusServiceUnavailable)
return
}
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("upgrade error: %v", err)
return
}
// Set generous read limit. Messages within MaxMessageSize are forwarded normally.
// gorilla/websocket closes connection on exceeding ReadLimit, so set it high (50MB)
// to prevent accidental disconnects. Client-side safety net drops messages > 8MB.
conn.SetReadLimit(50 * 1024 * 1024)
client := NewClient(s.hub, conn, roomID, claims.PeerID, claims.Role, ip)
s.hub.Register(client)
}
func clientIP(r *http.Request) string {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
return xff
}
if xri := r.Header.Get("X-Real-IP"); xri != "" {
return xri
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}