|
| 1 | +package hlsproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/m1k1o/go-transcode/internal/utils" |
| 12 | + |
| 13 | + "github.com/rs/zerolog" |
| 14 | + "github.com/rs/zerolog/log" |
| 15 | +) |
| 16 | + |
| 17 | +// how often should be cache cleanup called |
| 18 | +const cacheCleanupPeriod = 4 * time.Second |
| 19 | + |
| 20 | +const segmentExpiration = 60 * time.Second |
| 21 | + |
| 22 | +const playlistExpiration = 1 * time.Second |
| 23 | + |
| 24 | +type ManagerCtx struct { |
| 25 | + logger zerolog.Logger |
| 26 | + baseUrl string |
| 27 | + prefix string |
| 28 | + |
| 29 | + cache map[string]*utils.Cache |
| 30 | + cacheMu sync.RWMutex |
| 31 | + |
| 32 | + cleanup bool |
| 33 | + cleanupMu sync.RWMutex |
| 34 | + shutdown chan struct{} |
| 35 | +} |
| 36 | + |
| 37 | +func New(baseUrl string, prefix string) *ManagerCtx { |
| 38 | + // ensure it ends with slash |
| 39 | + baseUrl = strings.TrimSuffix(baseUrl, "/") |
| 40 | + baseUrl += "/" |
| 41 | + |
| 42 | + return &ManagerCtx{ |
| 43 | + logger: log.With().Str("module", "hlsproxy").Str("submodule", "manager").Logger(), |
| 44 | + baseUrl: baseUrl, |
| 45 | + prefix: prefix, |
| 46 | + cache: map[string]*utils.Cache{}, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (m *ManagerCtx) Shutdown() { |
| 51 | + m.cleanupStop() |
| 52 | +} |
| 53 | + |
| 54 | +func (m *ManagerCtx) ServePlaylist(w http.ResponseWriter, r *http.Request) { |
| 55 | + url := m.baseUrl + strings.TrimPrefix(r.URL.String(), m.prefix) |
| 56 | + |
| 57 | + cache, ok := m.getFromCache(url) |
| 58 | + if !ok { |
| 59 | + resp, err := http.Get(url) |
| 60 | + if err != nil { |
| 61 | + log.Err(err).Msg("unable to get HTTP") |
| 62 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 63 | + return |
| 64 | + } |
| 65 | + defer resp.Body.Close() |
| 66 | + |
| 67 | + if resp.StatusCode < 200 && resp.StatusCode >= 300 { |
| 68 | + defer resp.Body.Close() |
| 69 | + |
| 70 | + log.Err(err).Int("code", resp.StatusCode).Msg("invalid HTTP response") |
| 71 | + http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + buf, err := io.ReadAll(resp.Body) |
| 76 | + if err != nil { |
| 77 | + log.Err(err).Msg("unadle to read response body") |
| 78 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + var re = regexp.MustCompile(`(?m:^(https?\:\/\/[^\/]+)?\/)`) |
| 83 | + text := re.ReplaceAllString(string(buf), m.prefix) |
| 84 | + |
| 85 | + cache = m.saveToCache(url, strings.NewReader(text), playlistExpiration) |
| 86 | + } |
| 87 | + |
| 88 | + w.Header().Set("Content-Type", "application/vnd.apple.mpegurl") |
| 89 | + w.WriteHeader(200) |
| 90 | + |
| 91 | + cache.ServeHTTP(w) |
| 92 | +} |
| 93 | + |
| 94 | +func (m *ManagerCtx) ServeMedia(w http.ResponseWriter, r *http.Request) { |
| 95 | + url := m.baseUrl + strings.TrimPrefix(r.URL.String(), m.prefix) |
| 96 | + |
| 97 | + cache, ok := m.getFromCache(url) |
| 98 | + if !ok { |
| 99 | + resp, err := http.Get(url) |
| 100 | + if err != nil { |
| 101 | + log.Err(err).Msg("unable to get HTTP") |
| 102 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + if resp.StatusCode < 200 && resp.StatusCode >= 300 { |
| 107 | + defer resp.Body.Close() |
| 108 | + |
| 109 | + log.Err(err).Int("code", resp.StatusCode).Msg("invalid HTTP response") |
| 110 | + http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + cache = m.saveToCache(url, resp.Body, segmentExpiration) |
| 115 | + } |
| 116 | + |
| 117 | + w.Header().Set("Content-Type", "video/MP2T") |
| 118 | + w.WriteHeader(200) |
| 119 | + |
| 120 | + cache.ServeHTTP(w) |
| 121 | +} |
0 commit comments