add cache, minor tweaks
Marco Andronaco andronacomarco@gmail.com
Sun, 15 Dec 2024 19:03:10 +0100
14 files changed,
120 insertions(+),
34 deletions(-)
M
.env.example
→
.env.example
@@ -1,6 +1,6 @@
-API_KEY=itsme +LOG_LEVEL=info PORT=3000 -CACHE_DURATION_MINUTES=5 -TIMEOUT_DURATION_MINUTES=10 -MAX_SIZE_MB=20 -CLEANUP_INTERVAL_SECONDS=30 +BURST_TOKENS=3 +RATE_LIMIT=1 +ADMIN_USER=admin +ADMIN_PASS=admin
M
.gitignore
→
.gitignore
@@ -1,4 +1,4 @@
.env __debug_bin* *.sqlite -docker/swag.env +swag/swag.env
M
README.md
→
README.md
@@ -8,27 +8,29 @@ https://github.com/BiRabittoh/FixYouTube/assets/26506860/e1ad5397-41c8-4073-9b3e-598c66241255
## Instructions -First of all, you should set the API key to whatever you want: +First of all, you should duplicate and fill your `.env` file: ``` cp .env.example .env nano .env ``` -### Docker with reverse proxy -Copy the template config file and make your adjustments. My configuration is based on [DuckDNS](http://duckdns.org/) but you can use whatever provider you find [here](https://docs.linuxserver.io/general/swag#docker-compose). - +### Docker without reverse proxy +Just run: ``` -cp docker/swag.env.example docker/swag.env -nano docker/swag.env +docker compose -f compose.simple.yaml up -d ``` -Finally: `docker-compose up -d`. +### Docker with reverse proxy +Copy the template config file and make your adjustments. My configuration is based on [DuckDNS](http://duckdns.org/) but you can use whatever provider you find [here](https://docs.linuxserver.io/general/swag#docker-compose). -### Docker without reverse proxy -Just run: ``` -docker-compose -f docker-compose.simple.yaml up -d +cd swag +cp swag.env.example swag.env +nano swag.env +cd .. ``` + +Finally: `docker compose up -d`. ## Test and debug locally ```
M
docker-compose.yaml
→
compose.yaml
@@ -12,10 +12,9 @@ container_name: swag
cap_add: - NET_ADMIN env_file: - - docker/swag.env + - swag/swag.env volumes: - #- /etc/config/swag:/config - - ./docker/fixyoutube.subdomain.conf:/config/nginx/proxy-confs/fixyoutube.subdomain.conf:ro + - ./swag/fixyoutube.subdomain.conf:/config/nginx/proxy-confs/fixyoutube.subdomain.conf:ro ports: - 443:443 - 80:80
M
docker/fixyoutube.subdomain.conf
→
swag/fixyoutube.subdomain.conf
@@ -2,8 +2,9 @@ # make sure that your app container is named fixyoutube
# make sure that your dns has a cname set for fixyoutube server { - listen 443 ssl http2; - listen [::]:443 ssl http2; + listen 443 ssl; + listen [::]:443 ssl; + http2 on; server_name y.*; # change your subdomain here
M
fixyoutube.go
→
fixyoutube.go
@@ -26,7 +26,7 @@ next.ServeHTTP(w, r)
}) } -func getenvDefault(key string, def string) string { +func getEnvDefault(key string, def string) string { res := os.Getenv(key) if res == "" { return def@@ -34,8 +34,8 @@ }
return res } -func getenvDefaultParse(key string, def string) float64 { - value := getenvDefault(key, def) +func getEnvDefaultParse(key string, def string) float64 { + value := getEnvDefault(key, def) res, err := strconv.ParseFloat(value, 64) if err != nil { logger.Fatal(err)@@ -49,15 +49,28 @@ if err != nil {
logger.Info("No .env file provided.") } - if os.Getenv("DEBUG") != "" { + logLevel, err := logrus.ParseLevel(os.Getenv("LOG_LEVEL")) + if err != nil { + logLevel = logrus.InfoLevel + } + logger.SetLevel(logLevel) + + if logLevel == logrus.DebugLevel { logger.SetLevel(logrus.DebugLevel) logger.Debug("Debug mode enabled (rate limiting is disabled)") debugSwitch = true } - port := getenvDefault("PORT", "3000") - burstTokens := getenvDefaultParse("BURST_TOKENS", "3") - rateLimit := getenvDefaultParse("RATE_LIMIT", "1") + port := getEnvDefault("PORT", "3000") + burstTokens := getEnvDefaultParse("BURST_TOKENS", "3") + rateLimit := getEnvDefaultParse("RATE_LIMIT", "1") + + adminUser = getEnvDefault("ADMIN_USER", "admin") + adminPass = getEnvDefault("ADMIN_PASS", "admin") + + if adminUser == "admin" && adminPass == "admin" { + logger.Println("Admin credentials not set. Please set APP_ADMIN_USER and APP_ADMIN_PASS.") + } r := http.NewServeMux() r.HandleFunc("GET /", indexHandler)@@ -65,6 +78,7 @@ r.HandleFunc("GET /watch", watchHandler)
r.HandleFunc("GET /shorts/{videoID}", shortHandler) r.HandleFunc("GET /proxy/{videoID}", proxyHandler) r.HandleFunc("GET /refresh/{videoID}", refreshHandler) + r.HandleFunc("GET /cache", cacheHandler) r.HandleFunc("GET /{videoID}", shortHandler) r.HandleFunc("POST /download", downloadHandler)
M
go.sum
→
go.sum
@@ -1,7 +1,7 @@
github.com/birabittoh/myks v0.0.2 h1:EBukMUsAflwiqdNo4LE7o2WQdEvawty5ewCZWY+IXSU= github.com/birabittoh/myks v0.0.2/go.mod h1:klNWaeUWm7TmhnBHBMt9vALwCHW11/Xw1BpCNkCx7hs= -github.com/birabittoh/rabbitpipe v0.0.6 h1:n5/FHl1QEtc25IzP/uSNC9jnRwCSb2lm/t3LxywQeDo= -github.com/birabittoh/rabbitpipe v0.0.6/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= +github.com/birabittoh/rabbitpipe v0.0.7 h1:QSp/2DZSytCDeWf/o8dFSMkxjuZGwiQid7v/yjpfA00= +github.com/birabittoh/rabbitpipe v0.0.7/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
M
handlers.go
→
handlers.go
@@ -4,6 +4,7 @@ import (
"embed" "fmt" "io" + "log" "net/http" "net/url" "regexp"@@ -18,12 +19,17 @@
const templatesDirectory = "templates/" var ( - //go:embed templates/index.html templates/video.html + //go:embed templates/index.html templates/video.html templates/cache.html templates embed.FS indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html")) videoTemplate = template.Must(template.New("video.html").Funcs(template.FuncMap{"parseFormat": parseFormat}).ParseFS(templates, templatesDirectory+"video.html")) + cacheTemplate = template.Must(template.New("cache.html").ParseFS(templates, templatesDirectory+"cache.html")) + // userAgentRegex = regexp.MustCompile(`(?i)bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node`) videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`) + + adminUser string + adminPass string ) func parseFormat(f rabbitpipe.Format) (res string) {@@ -199,3 +205,28 @@ }
http.Redirect(w, r, "/"+videoID, http.StatusFound) } + +func cacheHandler(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if !ok || username != adminUser || password != adminPass { + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + var videos []rabbitpipe.Video + for s := range invidious.RP.GetCachedVideos() { + video, err := invidious.RP.GetVideo(s) + if err != nil || video == nil { + continue + } + videos = append(videos, *video) + } + + err := cacheTemplate.Execute(w, videos) + if err != nil { + log.Println("cacheHandler ERROR:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } +}
A
templates/cache.html
@@ -0,0 +1,39 @@
+<!doctype html> +<html lang="en"> + +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Cache - FixYouTube</title> + <link rel="icon" + href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text fill=%22white%22 y=%22.9em%22 font-size=%2290%22>🛠</text></svg>"> + <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css"> +</head> + +<body> + <main class="container" style="max-width: 35rem"> + <hgroup> + <h1>Cached videos</h1> + <a href="/">← Home</a> + </hgroup> + + <section> + <ul> + {{ range . }} + <li> + <a href="/{{ .VideoID }}">{{ .Title }}</a> + <br /> + <small>{{ .Author }}</small> + </li> + {{ end }} + </ul> + <hr> + <small><a href="https://github.com/BiRabittoh/fixyoutube-go" target="_blank">Source code available in + GitHub!</a></small> + <br> + <small>• YouTube is a trademark of Google LLC. This app is not affiliated with Google LLC.</small> + </section> + </main> +</body> + +</html>
M
templates/index.html
→
templates/index.html
@@ -29,7 +29,7 @@ <p>Replace <code>www.youtube.com</code> or <code>youtu.be</code> with <span id="changeme">this domain</span> to fix embeds for short videos.</p>
</header> <video src="https://github.com/BiRabittoh/FixYouTube/assets/26506860/2896d39e-a86e-47ce-939a-785b73d11683" - style="width: 100%; max-height: 100%;" autoplay loop muted controls> + style="width: 100%; max-height: 100%;" autoplay loop muted> Your browser does not support the video tag. </video> <hr>