-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay_server.go
More file actions
330 lines (288 loc) · 10.1 KB
/
relay_server.go
File metadata and controls
330 lines (288 loc) · 10.1 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
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
package main
import (
"io"
"net/http"
"net/url"
"path"
"strings"
)
// ---------- Server ----------
// relayServer implements the TLTV protocol HTTP endpoints for relayed channels.
type relayServer struct {
registry *relayRegistry
client *Client // for proxying streams from upstream
cache *hlsCache // optional HLS cache (nil = disabled)
peerReg *peerRegistry // optional external peers from --peers (nil = disabled)
bufMgr *relayBufferManager // optional buffer manager (nil = no buffering)
mux *http.ServeMux
}
// newRelayServer creates a relay HTTP server with all protocol endpoints.
// Pass cache=nil to disable caching, peerReg=nil to disable external peers,
// bufMgr=nil to disable buffering.
func newRelayServer(registry *relayRegistry, client *Client, cache *hlsCache, peerReg *peerRegistry, bufMgr *relayBufferManager) *relayServer {
s := &relayServer{
registry: registry,
client: client,
cache: cache,
peerReg: peerReg,
bufMgr: bufMgr,
mux: http.NewServeMux(),
}
s.mux.HandleFunc("GET /.well-known/tltv", s.handleNodeInfo)
s.mux.HandleFunc("GET /tltv/v1/channels/{id}", s.handleChannelMeta)
s.mux.HandleFunc("GET /tltv/v1/channels/{id}/{path...}", s.handleChannelPath)
s.mux.HandleFunc("GET /tltv/v1/peers", s.handlePeers)
s.mux.HandleFunc("GET /health", s.handleHealth)
s.mux.HandleFunc("/.well-known/tltv", s.handleMethodNotAllowed)
s.mux.HandleFunc("/tltv/", s.handleMethodNotAllowed)
return s
}
// ServeHTTP adds CORS headers and handles OPTIONS preflight.
func (s *relayServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
return
}
s.mux.ServeHTTP(w, r)
}
// ---------- Handlers ----------
// handleNodeInfo serves GET /.well-known/tltv
// Relay channels appear in "relaying", not "channels".
func (s *relayServer) handleNodeInfo(w http.ResponseWriter, r *http.Request) {
channels := s.registry.ListChannels()
var relaying []interface{}
for _, ch := range channels {
// Skip migrated entries (they have no stream, just the migration doc)
if ch.Name == "(migrated)" {
continue
}
entry := map[string]interface{}{
"id": ch.ChannelID,
"name": ch.Name,
}
// Include delay field when buffer+delay is active
if s.bufMgr != nil && s.bufMgr.delay > 0 {
entry["delay"] = int(s.bufMgr.delay.Seconds())
}
relaying = append(relaying, entry)
}
if relaying == nil {
relaying = []interface{}{}
}
w.Header().Set("Cache-Control", "max-age=60")
writeJSON(w, map[string]interface{}{
"protocol": "tltv",
"versions": []int{1},
"channels": []interface{}{},
"relaying": relaying,
}, http.StatusOK)
}
// handlePeers serves GET /tltv/v1/peers with full gossip exchange.
// Own relayed channels are visible in /.well-known/tltv; peers endpoint
// shows the network around this node — gossip (if --gossip) + external peers (--peers).
func (s *relayServer) handlePeers(w http.ResponseWriter, r *http.Request) {
// External: gossip-discovered peers + verified --peers
// Own relayed channels are visible in /.well-known/tltv, not here.
var external []peerEntry
external = append(external, s.registry.ListGossipPeers()...)
if s.peerReg != nil {
external = append(external, s.peerReg.ListPeers()...)
}
peers := buildPeersResponse(nil, external)
w.Header().Set("Cache-Control", "max-age=300")
writeJSON(w, map[string]interface{}{
"peers": peers,
}, http.StatusOK)
}
// handleChannelMeta serves GET /tltv/v1/channels/{id}
// Serves the raw verified metadata bytes verbatim.
func (s *relayServer) handleChannelMeta(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ch := s.registry.GetChannel(id)
if ch == nil || ch.Metadata == nil {
jsonError(w, "channel_not_found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "max-age=60")
w.Write(ch.Metadata)
}
// handleChannelPath serves GET /tltv/v1/channels/{id}/{path...}
func (s *relayServer) handleChannelPath(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
subPath := r.PathValue("path")
ch := s.registry.GetChannel(id)
if ch == nil {
jsonError(w, "channel_not_found", http.StatusNotFound)
return
}
switch subPath {
case "guide.json":
s.serveGuideJSON(w, ch)
case "guide.xml":
s.serveGuideXML(w, ch)
default:
s.serveStream(w, r, ch, subPath)
}
}
// handleHealth serves GET /health
func (s *relayServer) handleHealth(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]interface{}{
"status": "ok",
"version": version,
"relaying": s.registry.ChannelCount(),
}, http.StatusOK)
}
// handleMethodNotAllowed returns 400 for non-GET methods.
func (s *relayServer) handleMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
jsonError(w, "invalid_request", http.StatusBadRequest)
}
// ---------- Guide Serving ----------
// serveGuideJSON serves the raw verified guide bytes verbatim.
func (s *relayServer) serveGuideJSON(w http.ResponseWriter, ch *relayRegisteredChannel) {
if ch.Guide == nil {
jsonError(w, "channel_not_found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "max-age=300")
w.Write(ch.Guide)
}
// serveGuideXML generates XMLTV from parsed guide entries.
func (s *relayServer) serveGuideXML(w http.ResponseWriter, ch *relayRegisteredChannel) {
entries := ch.GuideEntries
if len(entries) == 0 && ch.Guide == nil {
jsonError(w, "channel_not_found", http.StatusNotFound)
return
}
if len(entries) == 0 {
entries = defaultGuideEntries(ch.Name)
}
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.Header().Set("Cache-Control", "max-age=300")
w.Write([]byte(guideToXMLTV(ch.ChannelID, ch.Name, entries)))
}
// ---------- Stream Proxying ----------
// serveStream proxies HLS content from the upstream origin.
// Rewrites absolute URLs to relative so viewers fetch through the relay.
// When cache is enabled, uses singleflight deduplication and protocol-compliant
// TTLs (1s for manifests, 3600s for segments) per spec section 9.10.
func (s *relayServer) serveStream(w http.ResponseWriter, r *http.Request, ch *relayRegisteredChannel, subPath string) {
if !validateSubPath(subPath) {
jsonError(w, "invalid_request", http.StatusBadRequest)
return
}
// Buffer path: serve from proactive buffers if available.
// - stream.m3u8 serves the cached root master playlist when upstream is multivariant,
// or a generated buffered media playlist when upstream is single-variant.
// - child media playlists (stream_*.m3u8, audio_*.m3u8, subs_*.m3u8) serve generated
// buffered manifests.
// - segment and subtitle files serve directly from buffered bytes.
// Falls through to upstream proxy if a buffer is empty or unavailable.
if s.bufMgr != nil {
if subPath == "stream.m3u8" {
if manifest := s.bufMgr.GetRootManifest(ch.ChannelID); manifest != "" {
setStreamHeaders(w, subPath, false)
w.Write([]byte(manifest))
return
}
}
if strings.HasSuffix(subPath, ".m3u8") {
if manifest := s.bufMgr.GetManifest(ch.ChannelID, subPath); manifest != "" {
setStreamHeaders(w, subPath, false)
w.Write([]byte(manifest))
return
}
} else if data := s.bufMgr.GetSegmentByPath(ch.ChannelID, subPath); data != nil {
setStreamHeaders(w, subPath, false)
w.Write(data)
return
}
}
if ch.StreamHint == "" {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
// Construct upstream URL
base := s.client.baseURL(ch.StreamHint)
manifestURL := base + "/tltv/v1/channels/" + ch.ChannelID + "/stream.m3u8"
var fetchURL string
if subPath == "stream.m3u8" {
fetchURL = manifestURL
} else {
parsed, err := url.Parse(manifestURL)
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
ref, err := url.Parse(subPath)
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
parsed.Path = path.Dir(parsed.Path) + "/"
fetchURL = parsed.ResolveReference(ref).String()
}
// Cache path: use full request path as cache key
if s.cache != nil {
cacheKey := r.URL.Path
data, contentType, hit, err := s.cache.getOrFetch(cacheKey, func() (*hlsCacheFetchResult, error) {
fr, err := hlsCacheFetchUpstream(s.client.http, fetchURL, r)
if err != nil {
return nil, err
}
// Rewrite manifests before caching
if strings.HasSuffix(subPath, ".m3u8") {
rewritten := rewriteManifest(manifestURL, fr.data, "")
fr.data = rewritten
}
return fr, nil
})
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
setStreamHeadersWithContentType(w, subPath, false, contentType)
if hit {
w.Header().Set("Cache-Status", "HIT")
} else {
w.Header().Set("Cache-Status", "MISS")
}
w.Write(data)
return
}
// Non-cache path: direct proxy
req, err := http.NewRequestWithContext(r.Context(), "GET", fetchURL, nil)
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
req.Header.Set("User-Agent", "tltv-cli/"+version)
resp, err := s.client.http.Do(req)
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
// Set headers (no private channel handling -- relay never has private channels)
setStreamHeadersWithContentType(w, subPath, false, resp.Header.Get("Content-Type"))
if strings.HasSuffix(subPath, ".m3u8") {
body, err := io.ReadAll(io.LimitReader(resp.Body, maxManifestSize))
if err != nil {
jsonError(w, "stream_unavailable", http.StatusServiceUnavailable)
return
}
// Rewrite absolute-to-relative, no token (relay has no private channels)
rewritten := rewriteManifest(manifestURL, body, "")
w.Write(rewritten)
return
}
io.Copy(w, resp.Body)
}