cdrop — 跨 OS 剪贴板与文件传输服务
Commilitia Drop:自托管的跨设备剪贴板同步与点对点文件传输。 - 后端 Go(chi / SQLite WAL / SSE Hub / WebRTC signaling + 状态机 / Relay ring buffer),编译进单个 distroless 镜像(前端 go:embed)。 - 前端 React + TanStack Router + Zustand,自实现 SSE + WebRTC P2P,NAT 受阻时回退服务端中继;聚珍(Juzhen)CJK 综合排版。 - 桌面端 Wails v2(macOS / Windows),瘦客户端复用 web。 - 鉴权 OIDC PKCE(自建 Casdoor 等),refresh_token 信封加密存系统密钥库;iOS Shortcut 用 HS256 scoped token。 架构文档与变更记录见 docs 分支(PROJECT_BRIEF / FRONTEND_DESIGN / CHANGELOG)。 本次为公开发布初始提交:完整开发历史(含部署细节)留存于私有归档,公开仓库自此干净起步。
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"commilitia.net/cdrop/internal/hub"
|
||||
"commilitia.net/cdrop/internal/jwtauth"
|
||||
)
|
||||
|
||||
// maxSignalBytes caps the signaling body. WebRTC SDP offers/answers and bundled
|
||||
// ICE candidates are at most a few KB; the payload is an opaque json.RawMessage
|
||||
// with no other length check, so without this a single request could stream an
|
||||
// unbounded body into memory (R4). 64 KiB leaves generous headroom for fat SDP.
|
||||
const maxSignalBytes = 64 * 1024
|
||||
|
||||
type signalReq struct {
|
||||
To string `json:"to"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
}
|
||||
|
||||
// handleSignal forwards WebRTC offer/answer/ICE candidates between same-user devices.
|
||||
// Body: { to: deviceName, payload: any-json }
|
||||
// 204 if delivered to live SSE; 410 if peer offline.
|
||||
func (s *Server) handleSignal(w http.ResponseWriter, r *http.Request) {
|
||||
claims, _ := jwtauth.ClaimsFromContext(r.Context())
|
||||
from, _ := jwtauth.DeviceNameFromContext(r.Context())
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxSignalBytes)
|
||||
var req signalReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
var mbe *http.MaxBytesError
|
||||
if errors.As(err, &mbe) {
|
||||
writeJSON(w, http.StatusRequestEntityTooLarge,
|
||||
map[string]string{"error": "signal payload too large"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid json"})
|
||||
return
|
||||
}
|
||||
if req.To == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "missing 'to'"})
|
||||
return
|
||||
}
|
||||
if req.To == from {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "cannot signal self"})
|
||||
return
|
||||
}
|
||||
|
||||
delivered := s.hub.SendTo(claims.UserID, req.To, hub.Event{
|
||||
Type: "signal",
|
||||
Data: map[string]any{
|
||||
"from": from,
|
||||
"payload": req.Payload,
|
||||
},
|
||||
})
|
||||
if !delivered {
|
||||
writeJSON(w, http.StatusGone, map[string]string{"error": "peer offline"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user