make regex case insensitive, add api key to docker-compose
Marco Andronaco andronacomarco@gmail.com
Tue, 19 Dec 2023 14:48:13 +0100
4 files changed,
29 insertions(+),
10 deletions(-)
M
README.md
→
README.md
@@ -6,9 +6,15 @@ Replace `www.youtube.com` or `youtu.be` with `y.outube.duckdns.org` to fix embeds for short videos.
https://github.com/BiRabittoh/FixYouTube/assets/26506860/e1ad5397-41c8-4073-9b3e-598c66241255 -## Instructions (Docker) +## Instructions + +First of all, you should set the API key to whatever you want: +``` +cp .env.example .env +nano .env +``` -### With reverse proxy +### 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). ```@@ -18,13 +24,13 @@ ```
Finally: `docker-compose up -d`. -### Without reverse proxy -Simply run: +### Docker without reverse proxy +Just run: ``` -docker run -d -p 3000:3000 --name fixyoutube-go --restart unless-stopped ghcr.io/birabittoh/fixyoutube-go:main +docker-compose -f docker-compose.simple.yaml up -d ``` -## Instructions (local) +## Test and debug locally ``` go test -v ./... go run .
A
docker-compose.simple.yaml
@@ -0,0 +1,10 @@
+services: + app: + build: . + image: ghcr.io/birabittoh/fixyoutube-go:main + container_name: fixyoutube-go + restart: unless-stopped + ports: + - 3000:3000 + environment: + - API_KEY=${API_KEY}
M
docker-compose.yaml
→
docker-compose.yaml
@@ -4,6 +4,8 @@ build: .
image: ghcr.io/birabittoh/fixyoutube-go:main container_name: fixyoutube-go restart: unless-stopped + environment: + - API_KEY=${API_KEY} swag: image: ghcr.io/linuxserver/swag container_name: swag
M
fixyoutube.go
→
fixyoutube.go
@@ -20,7 +20,7 @@ var templatesDirectory = "templates/"
var indexTemplate = template.Must(template.ParseFiles(templatesDirectory + "index.html")) var videoTemplate = template.Must(template.ParseFiles(templatesDirectory + "video.html")) var blacklist = []string{"favicon.ico", "robots.txt"} -var userAgentRegex = regexp.MustCompile(`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`) +var 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`) var apiKey string func indexHandler(w http.ResponseWriter, r *http.Request) {@@ -57,9 +57,10 @@ http.Error(w, "Done.", http.StatusOK)
} func videoHandler(videoId string, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) { - - res := userAgentRegex.MatchString(r.UserAgent()) + userAgent := r.UserAgent() + res := userAgentRegex.MatchString(userAgent) if !res { + log.Println("Regex did not match. Redirecting. UA:", userAgent) url := "https://www.youtube.com/watch?v=" + videoId http.Redirect(w, r, url, http.StatusMovedPermanently) return@@ -117,7 +118,7 @@
func main() { err := godotenv.Load() if err != nil { - log.Println("Error loading .env file") + log.Println("No .env file provided.") } port := os.Getenv("PORT")