2019-06-30 01:08:00 +03:00
|
|
|
// Package app manages main application server.
|
2019-06-27 22:22:47 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-08-08 13:08:29 +03:00
|
|
|
"errors"
|
2019-06-29 19:16:01 +03:00
|
|
|
"fmt"
|
2019-06-27 22:22:47 +03:00
|
|
|
"html/template"
|
|
|
|
"log"
|
2019-06-27 23:09:17 +03:00
|
|
|
"net"
|
2019-06-27 22:22:47 +03:00
|
|
|
"net/http"
|
2019-06-29 06:50:59 +03:00
|
|
|
"net/url"
|
2019-08-08 13:34:53 +03:00
|
|
|
"os"
|
2019-06-29 06:50:59 +03:00
|
|
|
"path"
|
2019-06-29 19:16:01 +03:00
|
|
|
"strconv"
|
2019-06-29 06:50:59 +03:00
|
|
|
"time"
|
2019-06-27 22:22:47 +03:00
|
|
|
|
2019-06-29 01:20:52 +03:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2019-06-29 06:50:59 +03:00
|
|
|
"github.com/gorilla/feeds"
|
2019-06-27 22:22:47 +03:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/wybiral/tube/pkg/media"
|
2019-07-04 00:46:59 +03:00
|
|
|
"github.com/wybiral/tube/pkg/onionkey"
|
2019-06-27 22:22:47 +03:00
|
|
|
)
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// App represents main application.
|
2019-06-27 22:22:47 +03:00
|
|
|
type App struct {
|
2019-06-27 23:09:17 +03:00
|
|
|
Config *Config
|
2019-06-27 22:22:47 +03:00
|
|
|
Library *media.Library
|
2019-06-29 01:20:52 +03:00
|
|
|
Watcher *fsnotify.Watcher
|
2019-06-27 22:22:47 +03:00
|
|
|
Templates *template.Template
|
2019-07-04 00:46:59 +03:00
|
|
|
Tor *tor
|
2019-06-27 23:09:17 +03:00
|
|
|
Listener net.Listener
|
2019-06-27 22:22:47 +03:00
|
|
|
Router *mux.Router
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// NewApp returns a new instance of App from Config.
|
2019-06-27 23:09:17 +03:00
|
|
|
func NewApp(cfg *Config) (*App, error) {
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = DefaultConfig()
|
|
|
|
}
|
|
|
|
a := &App{
|
|
|
|
Config: cfg,
|
|
|
|
}
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Library
|
2019-06-29 01:20:52 +03:00
|
|
|
a.Library = media.NewLibrary()
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Watcher
|
2019-06-29 01:20:52 +03:00
|
|
|
w, err := fsnotify.NewWatcher()
|
2019-06-27 22:22:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-29 01:20:52 +03:00
|
|
|
a.Watcher = w
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Listener
|
2019-06-27 23:09:17 +03:00
|
|
|
ln, err := newListener(cfg.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a.Listener = ln
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Templates
|
2019-06-27 22:22:47 +03:00
|
|
|
a.Templates = template.Must(template.ParseGlob("templates/*"))
|
2019-07-04 00:46:59 +03:00
|
|
|
// Setup Tor
|
|
|
|
if cfg.Tor.Enable {
|
|
|
|
t, err := newTor(cfg.Tor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a.Tor = t
|
|
|
|
}
|
2019-06-30 01:02:05 +03:00
|
|
|
// Setup Router
|
2019-06-27 22:22:47 +03:00
|
|
|
r := mux.NewRouter().StrictSlash(true)
|
|
|
|
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
|
|
|
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/v/{prefix}/{id}.mp4", a.videoHandler).Methods("GET")
|
2019-06-27 22:22:47 +03:00
|
|
|
r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/t/{prefix}/{id}", a.thumbHandler).Methods("GET")
|
2019-06-29 04:44:56 +03:00
|
|
|
r.HandleFunc("/v/{id}", a.pageHandler).Methods("GET")
|
2019-07-03 21:25:42 +03:00
|
|
|
r.HandleFunc("/v/{prefix}/{id}", a.pageHandler).Methods("GET")
|
2019-06-29 06:50:59 +03:00
|
|
|
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
|
2019-06-30 01:02:05 +03:00
|
|
|
// Static file handler
|
2019-06-27 22:22:47 +03:00
|
|
|
fsHandler := http.StripPrefix(
|
|
|
|
"/static/",
|
|
|
|
http.FileServer(http.Dir("./static/")),
|
|
|
|
)
|
|
|
|
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
|
|
|
a.Router = r
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Run imports the library and starts server.
|
2019-06-27 23:09:17 +03:00
|
|
|
func (a *App) Run() error {
|
2019-07-04 00:46:59 +03:00
|
|
|
if a.Tor != nil {
|
|
|
|
var err error
|
|
|
|
cs := a.Config.Server
|
|
|
|
key := a.Tor.OnionKey
|
|
|
|
if key == nil {
|
|
|
|
key, err = onionkey.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-08 13:34:53 +03:00
|
|
|
a.Tor.OnionKey = key
|
2019-07-04 00:46:59 +03:00
|
|
|
}
|
|
|
|
onion, err := key.Onion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
onion.Ports[80] = fmt.Sprintf("%s:%d", cs.Host, cs.Port)
|
|
|
|
err = a.Tor.Controller.AddOnion(onion)
|
|
|
|
if err != nil {
|
2019-08-08 13:08:29 +03:00
|
|
|
return errors.New("unable to start Tor onion service")
|
2019-07-04 00:46:59 +03:00
|
|
|
}
|
|
|
|
log.Printf("Onion service: http://%s.onion", onion.ServiceID)
|
|
|
|
}
|
2019-07-03 21:25:42 +03:00
|
|
|
for _, pc := range a.Config.Library {
|
|
|
|
p := &media.Path{
|
|
|
|
Path: pc.Path,
|
|
|
|
Prefix: pc.Prefix,
|
|
|
|
}
|
|
|
|
err := a.Library.AddPath(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = a.Library.Import(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Watcher.Add(p.Path)
|
2019-06-29 01:20:52 +03:00
|
|
|
}
|
2019-07-03 22:44:31 +03:00
|
|
|
go startWatcher(a)
|
2019-06-27 23:09:17 +03:00
|
|
|
return http.Serve(a.Listener, a.Router)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /
|
2019-06-27 22:22:47 +03:00
|
|
|
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("/")
|
2019-06-29 01:20:52 +03:00
|
|
|
pl := a.Library.Playlist()
|
2019-06-29 01:51:38 +03:00
|
|
|
if len(pl) > 0 {
|
2019-06-29 04:44:56 +03:00
|
|
|
http.Redirect(w, r, "/v/"+pl[0].ID, 302)
|
2019-06-29 01:51:38 +03:00
|
|
|
} else {
|
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
|
|
|
})
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /v/id
|
2019-06-27 22:22:47 +03:00
|
|
|
func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-29 04:44:56 +03:00
|
|
|
log.Printf("/v/%s", id)
|
2019-06-27 22:22:47 +03:00
|
|
|
playing, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
2019-06-29 01:51:38 +03:00
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: &media.Video{ID: ""},
|
|
|
|
Playlist: a.Library.Playlist(),
|
|
|
|
})
|
2019-06-27 22:22:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
|
|
|
Playing *media.Video
|
|
|
|
Playlist media.Playlist
|
|
|
|
}{
|
|
|
|
Playing: playing,
|
2019-06-29 01:20:52 +03:00
|
|
|
Playlist: a.Library.Playlist(),
|
2019-06-27 22:22:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /v/id.mp4
|
2019-06-27 22:22:47 +03:00
|
|
|
func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
log.Printf("/v/%s", id)
|
|
|
|
m, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
title := m.Title
|
|
|
|
disposition := "attachment; filename=\"" + title + ".mp4\""
|
|
|
|
w.Header().Set("Content-Disposition", disposition)
|
|
|
|
w.Header().Set("Content-Type", "video/mp4")
|
2019-07-03 21:25:42 +03:00
|
|
|
http.ServeFile(w, r, m.Path)
|
2019-06-27 22:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /t/id
|
2019-06-27 22:22:47 +03:00
|
|
|
func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
2019-07-03 21:25:42 +03:00
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok {
|
|
|
|
id = path.Join(prefix, id)
|
|
|
|
}
|
2019-06-27 22:22:47 +03:00
|
|
|
log.Printf("/t/%s", id)
|
|
|
|
m, ok := a.Library.Videos[id]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
|
|
|
if m.ThumbType == "" {
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
http.ServeFile(w, r, "static/defaulticon.jpg")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", m.ThumbType)
|
|
|
|
w.Write(m.Thumb)
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 06:50:59 +03:00
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// HTTP handler for /feed.xml
|
2019-06-29 06:50:59 +03:00
|
|
|
func (a *App) rssHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cfg := a.Config.Feed
|
|
|
|
now := time.Now()
|
|
|
|
f := &feeds.Feed{
|
|
|
|
Title: cfg.Title,
|
|
|
|
Link: &feeds.Link{Href: cfg.Link},
|
|
|
|
Description: cfg.Description,
|
|
|
|
Author: &feeds.Author{
|
|
|
|
Name: cfg.Author.Name,
|
|
|
|
Email: cfg.Author.Email,
|
|
|
|
},
|
|
|
|
Created: now,
|
|
|
|
Copyright: cfg.Copyright,
|
|
|
|
}
|
2019-06-29 19:16:01 +03:00
|
|
|
var externalURL string
|
|
|
|
if len(cfg.ExternalURL) > 0 {
|
|
|
|
externalURL = cfg.ExternalURL
|
2019-08-08 13:34:53 +03:00
|
|
|
} else if a.Tor != nil {
|
|
|
|
onion, err := a.Tor.OnionKey.Onion()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
externalURL = fmt.Sprintf("http://%s.onion", onion.ServiceID)
|
2019-06-29 19:16:01 +03:00
|
|
|
} else {
|
2019-08-08 13:34:53 +03:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
host := a.Config.Server.Host
|
|
|
|
port := a.Config.Server.Port
|
|
|
|
externalURL = fmt.Sprintf("http://%s:%d", host, port)
|
|
|
|
} else {
|
|
|
|
externalURL = fmt.Sprintf("http://%s", hostname)
|
|
|
|
}
|
2019-06-29 19:16:01 +03:00
|
|
|
}
|
2019-06-29 06:50:59 +03:00
|
|
|
for _, v := range a.Library.Playlist() {
|
2019-06-29 19:16:01 +03:00
|
|
|
u, err := url.Parse(externalURL)
|
2019-06-29 06:50:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, "v", v.ID)
|
|
|
|
id := u.String()
|
|
|
|
f.Items = append(f.Items, &feeds.Item{
|
|
|
|
Id: id,
|
|
|
|
Title: v.Title,
|
|
|
|
Link: &feeds.Link{Href: id},
|
|
|
|
Description: v.Description,
|
2019-06-29 19:16:01 +03:00
|
|
|
Enclosure: &feeds.Enclosure{
|
|
|
|
Url: id + ".mp4",
|
|
|
|
Length: strconv.FormatInt(v.Size, 10),
|
|
|
|
Type: "video/mp4",
|
|
|
|
},
|
2019-06-29 06:50:59 +03:00
|
|
|
Author: &feeds.Author{
|
|
|
|
Name: cfg.Author.Name,
|
|
|
|
Email: cfg.Author.Email,
|
|
|
|
},
|
|
|
|
Created: v.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
|
|
|
w.Header().Set("Content-Type", "text/xml")
|
|
|
|
f.WriteRss(w)
|
|
|
|
}
|