Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18265871e1 |
+1
-1
@@ -116,7 +116,7 @@ Deploys of this stack never touch Gitea.
|
|||||||
| icecast2 + liquidsoap | `${ICECAST_HOST}` | `${ICECAST_PORT}` | `/radio/` | **harbor port only** |
|
| icecast2 + liquidsoap | `${ICECAST_HOST}` | `${ICECAST_PORT}` | `/radio/` | **harbor port only** |
|
||||||
| vue | `vue` | 5173 (dev only) | `/` (dev only) | no |
|
| vue | `vue` | 5173 (dev only) | `/` (dev only) | no |
|
||||||
| cloudflared | `cloudflared` | — (fixed IP `172.28.255.250`) | all of Caddy, via Cloudflare | no (outbound only) |
|
| cloudflared | `cloudflared` | — (fixed IP `172.28.255.250`) | all of Caddy, via Cloudflare | no (outbound only) |
|
||||||
| dozzle (log viewer) | `dozzle` | 8080 | `/dozzle/` (admin-gated by Caddy `forward_auth`; reads the Docker socket) | no |
|
| dozzle (log viewer) | `dozzle` | 8080 | `/dozzle/` (admin-gated by Caddy `forward_auth`; reads the Docker socket; on `dozzle-net`, shared only with Caddy) | no |
|
||||||
| receipt printer (`receipt serve`, adamf/receipt_maker) | — (runs on **stppi**, not in this stack) | 8765 on stppi's Tailscale IP (`RECEIPT_WEB_UPSTREAM`) | `/receipt/`, prefix stripped (permission-gated on `receipt` by Caddy `forward_auth`; Caddy adds `X-Receipt-Token` and `X-Remote-User`). The only upstream off this host; 503 "offline" when stppi is down | no |
|
| receipt printer (`receipt serve`, adamf/receipt_maker) | — (runs on **stppi**, not in this stack) | 8765 on stppi's Tailscale IP (`RECEIPT_WEB_UPSTREAM`) | `/receipt/`, prefix stripped (permission-gated on `receipt` by Caddy `forward_auth`; Caddy adds `X-Receipt-Token` and `X-Remote-User`). The only upstream off this host; 503 "offline" when stppi is down | no |
|
||||||
| Partner Radar (adamf/cto, its own compose project) | `radar-web` (alias of that project's `web`, on app-network) | 80 | `radar.<DOMAIN>`, the whole host (its own login, no gate; zones `radar` and `radar_login`). Started from the cto checkout with `make stpvm`; 503 "offline" when that stack is down | no |
|
| Partner Radar (adamf/cto, its own compose project) | `radar-web` (alias of that project's `web`, on app-network) | 80 | `radar.<DOMAIN>`, the whole host (its own login, no gate; zones `radar` and `radar_login`). Started from the cto checkout with `make stpvm`; 503 "offline" when that stack is down | no |
|
||||||
| traffic (tails the payload log) | `traffic` | — (no network) | none; its stdout is what Dozzle shows | no |
|
| traffic (tails the payload log) | `traffic` | — (no network) | none; its stdout is what Dozzle shows | no |
|
||||||
|
|||||||
@@ -154,7 +154,6 @@ func main() {
|
|||||||
Resolvers: &graph.Resolver{Store: &store},
|
Resolvers: &graph.Resolver{Store: &store},
|
||||||
Directives: graph.Directives(),
|
Directives: graph.Directives(),
|
||||||
}))
|
}))
|
||||||
gqlSrv.AddTransport(transport.Websocket{KeepAlivePingInterval: 10 * time.Second})
|
|
||||||
gqlSrv.AddTransport(transport.Options{})
|
gqlSrv.AddTransport(transport.Options{})
|
||||||
gqlSrv.AddTransport(transport.POST{})
|
gqlSrv.AddTransport(transport.POST{})
|
||||||
gqlSrv.AddTransport(transport.MultipartForm{})
|
gqlSrv.AddTransport(transport.MultipartForm{})
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ import (
|
|||||||
// RateLimiter is an in-memory sliding window per key, behind Caddy's limits.
|
// RateLimiter is an in-memory sliding window per key, behind Caddy's limits.
|
||||||
type RateLimiter struct {
|
type RateLimiter struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
// Never evicted; grows with distinct client IPs.
|
|
||||||
attempts map[string][]time.Time
|
attempts map[string][]time.Time
|
||||||
max int
|
max int
|
||||||
window time.Duration
|
window time.Duration
|
||||||
|
lastSweep time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRateLimiter(max int, window time.Duration) *RateLimiter {
|
func NewRateLimiter(max int, window time.Duration) *RateLimiter {
|
||||||
@@ -31,6 +31,7 @@ func (rl *RateLimiter) Allow(key string) bool {
|
|||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
cutoff := now.Add(-rl.window)
|
cutoff := now.Add(-rl.window)
|
||||||
|
rl.sweep(now, cutoff)
|
||||||
|
|
||||||
// Filtered in place, reusing the backing array.
|
// Filtered in place, reusing the backing array.
|
||||||
valid := rl.attempts[key][:0]
|
valid := rl.attempts[key][:0]
|
||||||
@@ -48,3 +49,17 @@ func (rl *RateLimiter) Allow(key string) bool {
|
|||||||
rl.attempts[key] = append(valid, now)
|
rl.attempts[key] = append(valid, now)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sweep drops keys with no attempt inside the window, at most once a window,
|
||||||
|
// so keys that are never seen again don't accumulate.
|
||||||
|
func (rl *RateLimiter) sweep(now, cutoff time.Time) {
|
||||||
|
if now.Sub(rl.lastSweep) < rl.window {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rl.lastSweep = now
|
||||||
|
for key, times := range rl.attempts {
|
||||||
|
if len(times) == 0 || !times[len(times)-1].After(cutoff) {
|
||||||
|
delete(rl.attempts, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,6 +58,16 @@ func TestRateLimiterAllow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("keys idle past the window are evicted", func(t *testing.T) {
|
||||||
|
rl := NewRateLimiter(1, 5*time.Millisecond)
|
||||||
|
rl.Allow("gone")
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
rl.Allow("other")
|
||||||
|
if _, ok := rl.attempts["gone"]; ok {
|
||||||
|
t.Fatal("idle key still stored, want evicted")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("max of zero denies everything", func(t *testing.T) {
|
t.Run("max of zero denies everything", func(t *testing.T) {
|
||||||
rl := NewRateLimiter(0, time.Minute)
|
rl := NewRateLimiter(0, time.Minute)
|
||||||
if rl.Allow("ip") {
|
if rl.Allow("ip") {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -54,6 +55,24 @@ const (
|
|||||||
rateLimitMaxMsgs = 10
|
rateLimitMaxMsgs = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The route is public, so frame and field sizes are capped. Writes happen
|
||||||
|
// under mu, so a stalled client may hold it for at most writeWait.
|
||||||
|
// ChatPanel's maxlength of 1000 UTF-16 units is at most 3000 bytes, under
|
||||||
|
// maxContentBytes; over-limit messages are dropped silently.
|
||||||
|
const (
|
||||||
|
maxFrameBytes = 16 << 10
|
||||||
|
maxContentBytes = 4000
|
||||||
|
maxFileURLBytes = 512
|
||||||
|
writeWait = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeJSON(conn *websocket.Conn, v any) error {
|
||||||
|
if err := conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return conn.WriteJSON(v)
|
||||||
|
}
|
||||||
|
|
||||||
type wsIncoming struct {
|
type wsIncoming struct {
|
||||||
Action string `json:"action,omitempty"`
|
Action string `json:"action,omitempty"`
|
||||||
ID uint `json:"id,omitempty"`
|
ID uint `json:"id,omitempty"`
|
||||||
@@ -81,6 +100,7 @@ func InitWebSocket(database *gorm.DB, domain string) {
|
|||||||
// isAdmin allows seeing, sending and deleting private messages.
|
// isAdmin allows seeing, sending and deleting private messages.
|
||||||
func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
conn.SetReadLimit(maxFrameBytes)
|
||||||
|
|
||||||
// Register and snapshot under one lock, so a concurrent broadcast is
|
// Register and snapshot under one lock, so a concurrent broadcast is
|
||||||
// either in the history or delivered after it, never lost or reordered.
|
// either in the history or delivered after it, never lost or reordered.
|
||||||
@@ -94,9 +114,11 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
|||||||
if !isAdmin {
|
if !isAdmin {
|
||||||
historyQuery = historyQuery.Where("private = ?", false)
|
historyQuery = historyQuery.Where("private = ?", false)
|
||||||
}
|
}
|
||||||
historyQuery.Find(&history)
|
if err := historyQuery.Find(&history).Error; err != nil {
|
||||||
|
log.Printf("[ws] loading history: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := conn.WriteJSON(wsHistoryEvent{Action: "history", Messages: history}); err != nil {
|
if err := writeJSON(conn, wsHistoryEvent{Action: "history", Messages: history}); err != nil {
|
||||||
delete(clients, conn)
|
delete(clients, conn)
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
@@ -130,6 +152,10 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(incoming.Text) > maxContentBytes || len(incoming.FileURL) > maxFileURLBytes {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
msg := models.Message{
|
msg := models.Message{
|
||||||
Content: incoming.Text,
|
Content: incoming.Text,
|
||||||
FileURL: incoming.FileURL,
|
FileURL: incoming.FileURL,
|
||||||
@@ -138,17 +164,23 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
wsDB.Create(&msg)
|
if err := wsDB.Create(&msg).Error; err != nil {
|
||||||
|
mu.Unlock()
|
||||||
|
log.Printf("[ws] saving message: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
// Soft delete, so the table still grows; the chat does not.
|
// Soft delete, so the table still grows; the chat does not.
|
||||||
wsDB.Where("id NOT IN (?)",
|
if err := wsDB.Where("id NOT IN (?)",
|
||||||
wsDB.Model(&models.Message{}).Select("id").Order("created_at DESC").Limit(maxMessages),
|
wsDB.Model(&models.Message{}).Select("id").Order("created_at DESC").Limit(maxMessages),
|
||||||
).Delete(&models.Message{})
|
).Delete(&models.Message{}).Error; err != nil {
|
||||||
|
log.Printf("[ws] pruning messages: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
for client, clientAdmin := range clients {
|
for client, clientAdmin := range clients {
|
||||||
if msg.Private && !clientAdmin {
|
if msg.Private && !clientAdmin {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := client.WriteJSON(msg); err != nil {
|
if err := writeJSON(client, msg); err != nil {
|
||||||
client.Close()
|
client.Close()
|
||||||
delete(clients, client)
|
delete(clients, client)
|
||||||
}
|
}
|
||||||
@@ -173,7 +205,7 @@ func deleteMessage(id uint) {
|
|||||||
|
|
||||||
event := wsDeleteEvent{Action: "delete", ID: id}
|
event := wsDeleteEvent{Action: "delete", ID: id}
|
||||||
for client := range clients {
|
for client := range clients {
|
||||||
if err := client.WriteJSON(event); err != nil {
|
if err := writeJSON(client, event); err != nil {
|
||||||
client.Close()
|
client.Close()
|
||||||
delete(clients, client)
|
delete(clients, client)
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-1
@@ -17,6 +17,11 @@ networks:
|
|||||||
usaco-runner-net:
|
usaco-runner-net:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
internal: true
|
internal: true
|
||||||
|
# internal, and shared only with caddy: the admin gate is the only way in,
|
||||||
|
# since dozzle holds the Docker socket.
|
||||||
|
dozzle-net:
|
||||||
|
driver: bridge
|
||||||
|
internal: true
|
||||||
|
|
||||||
# Named volumes survive `down`; only `down -v` deletes them.
|
# Named volumes survive `down`; only `down -v` deletes them.
|
||||||
volumes:
|
volumes:
|
||||||
@@ -88,6 +93,7 @@ services:
|
|||||||
- radar.${DOMAIN}
|
- radar.${DOMAIN}
|
||||||
# Matches cloudflared's extra_hosts.
|
# Matches cloudflared's extra_hosts.
|
||||||
ipv4_address: 172.28.255.251
|
ipv4_address: 172.28.255.251
|
||||||
|
dozzle-net:
|
||||||
ports:
|
ports:
|
||||||
- 80:80
|
- 80:80
|
||||||
- 443:443
|
- 443:443
|
||||||
@@ -360,7 +366,7 @@ services:
|
|||||||
container_name: dozzle
|
container_name: dozzle
|
||||||
restart: always
|
restart: always
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- dozzle-net
|
||||||
environment:
|
environment:
|
||||||
- DOZZLE_BASE=/dozzle
|
- DOZZLE_BASE=/dozzle
|
||||||
- DOZZLE_NO_ANALYTICS=true
|
- DOZZLE_NO_ANALYTICS=true
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ onUnmounted(() => {
|
|||||||
<div>
|
<div>
|
||||||
<input
|
<input
|
||||||
v-model="messageInput"
|
v-model="messageInput"
|
||||||
|
maxlength="1000"
|
||||||
@keyup.enter="sendMessage"
|
@keyup.enter="sendMessage"
|
||||||
:aria-label="isPrivate ? 'Private chat message' : 'Chat message'"
|
:aria-label="isPrivate ? 'Private chat message' : 'Chat message'"
|
||||||
:placeholder="isPrivate ? 'Admins only' : ''"
|
:placeholder="isPrivate ? 'Admins only' : ''"
|
||||||
|
|||||||
Reference in New Issue
Block a user