Skip to content

Commit

Permalink
added direct play with playlist file
Browse files Browse the repository at this point in the history
  • Loading branch information
glblduh committed Aug 19, 2024
1 parent 155fee6 commit 75b8d99
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 16 deletions.
29 changes: 13 additions & 16 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,22 @@ func checkAuthEnabled(isEnabled bool) {
// Check for API key on the HTTP query
func checkAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Check if authencation is enabled
if !authEnabled {
next(w, r)
}

// Get API key from HTTP query
key := r.URL.Query().Get("key")

// Unescape the API key
unescapedKey, unescapeErr := url.QueryUnescape(key)
if unescapeErr != nil {
errorRes(w, "Error unescaping the API key", http.StatusInternalServerError)
return
}

// Check if API key is valid
if unescapedKey != apiKey {
errorRes(w, "Key is not valid", http.StatusForbidden)
return
if authEnabled {
// Unescape the API key
unescapedKey, unescapeErr := url.QueryUnescape(key)
if unescapeErr != nil {
errorRes(w, "Error unescaping the API key", http.StatusInternalServerError)
return
}

// Check if API key is valid
if unescapedKey != apiKey {
errorRes(w, "Key is not valid", http.StatusForbidden)
return
}
}

next(w, r)
Expand Down
79 changes: 79 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,82 @@ func apiAddTorrentFile(w http.ResponseWriter, r *http.Request) {
res := createAddTorrentRes(t)
encodeRes(w, &res)
}

// Make playlist for direct streaming of magnet link or torrent files
func apiDirectPlay(w http.ResponseWriter, r *http.Request) {
/* Get magnet or infohash from query and validates */
magnet := r.URL.Query().Get("magnet")
infoHash := r.URL.Query().Get("infohash")
files, filesOk := r.URL.Query()["file"]

unescapedMagnet, unescapeErr := url.QueryUnescape(magnet)
if unescapeErr != nil {
errorRes(w, "Magnet query unescape error", http.StatusBadRequest)
return
}

unescapedInfoHash, unescapeErr := url.QueryUnescape(magnet)
if unescapeErr != nil {
errorRes(w, "Infohash query unescape error", http.StatusBadRequest)
return
}

if magnet != "" && infoHash != "" {
errorRes(w, "Only a single torrent is allowed", http.StatusBadRequest)
return
}

var t *torrent.Torrent
if magnet != "" && infoHash == "" {
spec, err := torrent.TorrentSpecFromMagnetUri(unescapedMagnet)
if err != nil {
errorRes(w, "Magnet to spec error", http.StatusBadRequest)
return
}

var addTorrentErr error
t, addTorrentErr = btEngine.addTorrent(spec, false)
if addTorrentErr != nil {
errorRes(w, "Adding torrent error", http.StatusBadRequest)
return
}
}

if infoHash != "" && magnet == "" {
var getTorrHandleErr error
t, getTorrHandleErr = btEngine.getTorrHandle(unescapedInfoHash)
if getTorrHandleErr != nil {
errorRes(w, "Getting torrent handle error", http.StatusBadRequest)
return
}
}

/* Create the playlist file with the selected files */
w.Header().Set("Content-Disposition", "attachment; filename=\""+t.InfoHash().String()+".m3u\"")
playList := "#EXTM3U\n"

httpScheme := "http"
if r.Header.Get("X-Forwarded-Proto") != "" {
httpScheme = r.Header.Get("X-Forwarded-Proto")
}

if !filesOk {
for _, file := range t.Files() {
file.SetPriority(torrent.PiecePriorityNormal)
saveSpecFile(t.InfoHash().String(), file.DisplayPath(), file.Priority())
playList += appendFilePlaylist(httpScheme, r.Host, t.InfoHash().String(), file.DisplayPath())
}
}

for _, file := range files {
torrentFile, getTorretnFileErr := getTorrentFile(t, file)
if getTorretnFileErr != nil {
continue
}
torrentFile.SetPriority(torrent.PiecePriorityNormal)
saveSpecFile(t.InfoHash().String(), torrentFile.DisplayPath(), torrentFile.Priority())
playList += appendFilePlaylist(httpScheme, r.Host, t.InfoHash().String(), torrentFile.DisplayPath())
}

w.Write([]byte(playList))
}
6 changes: 6 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,9 @@ func torrentPriorityToString(p torrent.PiecePriority) string {
}
return returnString
}

func appendFilePlaylist(scheme string, host string, infohash string, name string) string {
playList := "#EXTINF:-1," + safenDisplayPath(name) + "\n"
playList += scheme + "://" + host + createFileLink(infohash, name, false) + "\n"
return playList
}
1 change: 1 addition & 0 deletions torrenttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
r.HandleFunc("/api/file/{infohash}/{file:.*}", checkAuth(apiDownloadFile)).Methods("GET")
r.HandleFunc("/api/torrents", checkAuth(apiTorrentStats)).Methods("GET")
r.HandleFunc("/api/torrents/{infohash}", checkAuth(apiTorrentStats)).Methods("GET")
r.HandleFunc("/api/play", checkAuth(apiDirectPlay)).Methods("GET")

/* CORS middleware */
c := cors.New(cors.Options{
Expand Down

0 comments on commit 75b8d99

Please sign in to comment.