From cf074be1a8bf1be7047ee7a945a379e3d7409e6e Mon Sep 17 00:00:00 2001 From: Reinachan Date: Thu, 8 Dec 2022 21:44:08 +0100 Subject: [PATCH] Initial commit --- .env.example | 29 +++++ .gitignore | 20 +++ Caddy/Caddyfile | 40 ++++++ Caddy/Dockerfile | 11 ++ Cloudflared/Dockerfile | 14 +++ Cloudflared/config/config.yml | 13 ++ README.md | 158 ++++++++++++++++++++++++ caddy.sh | 3 + compose.yaml | 95 ++++++++++++++ qBittorrent/scripts/hardlink-torrent.sh | 29 +++++ update.sh | 4 + 11 files changed, 416 insertions(+) create mode 100755 .env.example create mode 100755 .gitignore create mode 100755 Caddy/Caddyfile create mode 100755 Caddy/Dockerfile create mode 100755 Cloudflared/Dockerfile create mode 100755 Cloudflared/config/config.yml create mode 100755 README.md create mode 100755 caddy.sh create mode 100755 compose.yaml create mode 100755 qBittorrent/scripts/hardlink-torrent.sh create mode 100755 update.sh diff --git a/.env.example b/.env.example new file mode 100755 index 0000000..d9bcd09 --- /dev/null +++ b/.env.example @@ -0,0 +1,29 @@ +# You can remove all the comments you don't want from this anymore in your .env file + +# General +DOMAIN=domain.tld +TIMEZONE=Europe/London # Look up TZ timezones + +# Subdomains +JELLYFIN_DOMAIN="jellyfin.${DOMAIN}" +SHOKO_ANIME_DOMAIN="shoko.${DOMAIN}" +TORRENT_DOMAIN="torrent.${DOMAIN}" + +# Paths +# Inside the base folder, there should be a folder called downloads and another called import +BASE_DOWNLOADS_FOLDER="/mnt/e/AnimeDownloads" # change this to the folder you want to download anime to +DOWNLOADS_FOLDER="${BASE_DOWNLOADS_FOLDER}/downloads" # don't replace this +IMPORT_FOLDER="${BASE_DOWNLOADS_FOLDER}/import" # don't replace this + +ANIME_FOLDER="/mnt/e/Anime" +MUSIC_FOLDER="/mnt/e/Musikk" +MOVIES_FOLDER="/mnt/e/Movies" + +# Cloudflared +TUNNEL_UUID=x111xx1x-xxx1-1x111x-x11x-x1xxx11x11xx +# ^^ look at the end of the Cloudflare documentation in README.md + +# Caddy Cloudflare plugin +EMAIL=address@domain.tld # Email used when signing up to Cloudflare +CF_API_TOKEN=dsfkjaeslkfssdlfkjaselrkdsfsef +# ^^ Check the Environment Variables section in the README.md diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..40cea06 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# Jellyfin +Jellyfin/ + +# ShokoAnime +ShokoAnime/ + +# qBittorrent +qBittorrent/config/ + +# Caddy +Caddy/* +!Caddy/Caddyfile +!Caddy/Dockerfile + +# Cloudflare +Cloudflared/config/* +!Cloudflared/config/config.yml + +# General +.env \ No newline at end of file diff --git a/Caddy/Caddyfile b/Caddy/Caddyfile new file mode 100755 index 0000000..d7f9ff2 --- /dev/null +++ b/Caddy/Caddyfile @@ -0,0 +1,40 @@ +{ + acme_dns cloudflare {$CF_API_TOKEN} +} + +(redirect) { + @http { + protocol http + } + redir @http https://{host}{uri} permanent +} + + +*.{$DOMAIN} { + + @jellyfin host {$JELLYFIN_DOMAIN} + handle @jellyfin { + reverse_proxy jellyfin:8096 + } + + @shoko host {$SHOKO_ANIME_DOMAIN} + handle @shoko { + reverse_proxy shoko_server:8111 + } + + @torrent host {$TORRENT_DOMAIN} + handle @torrent { + reverse_proxy qbittorrent:9090 + } + + @ping host ping.{$DOMAIN} + handle @ping { + respond "Pong!" + } + + handle { + abort + } + +} + diff --git a/Caddy/Dockerfile b/Caddy/Dockerfile new file mode 100755 index 0000000..dde36a5 --- /dev/null +++ b/Caddy/Dockerfile @@ -0,0 +1,11 @@ +ARG CADDY_VERSION=2.6.2 + +FROM caddy:${CADDY_VERSION}-builder AS builder + +RUN xcaddy build \ + --with github.com/caddy-dns/cloudflare \ + --with github.com/greenpau/caddy-security + +FROM caddy:${CADDY_VERSION}-alpine + +COPY --from=builder /usr/bin/caddy /usr/bin/caddy diff --git a/Cloudflared/Dockerfile b/Cloudflared/Dockerfile new file mode 100755 index 0000000..f2bad9a --- /dev/null +++ b/Cloudflared/Dockerfile @@ -0,0 +1,14 @@ +FROM alpine:latest AS alpine + +ARG DOMAIN +ARG TUNNEL_UUID + +RUN mkdir -p /etc/cloudflared +COPY config/ /etc/cloudflared + +RUN sed -i "s//${DOMAIN}/g" /etc/cloudflared/config.yml +RUN sed -i "s//${TUNNEL_UUID}/g" /etc/cloudflared/config.yml + +FROM cloudflare/cloudflared:latest + +COPY --from=alpine /etc/cloudflared /etc/cloudflared \ No newline at end of file diff --git a/Cloudflared/config/config.yml b/Cloudflared/config/config.yml new file mode 100755 index 0000000..da6ed83 --- /dev/null +++ b/Cloudflared/config/config.yml @@ -0,0 +1,13 @@ +tunnel: +credentials-file: /etc/cloudflared/.json + +ingress: + - hostname: '' + service: https://caddy:443 + originRequest: + originServerName: '' + - hostname: '*.' + service: https://caddy:443 + originRequest: + originServerName: '*.' + - service: http_status:404 diff --git a/README.md b/README.md new file mode 100755 index 0000000..e3b1010 --- /dev/null +++ b/README.md @@ -0,0 +1,158 @@ +# Media Server + +Eases the setup of a media server that requires as little manual input as possible. + +## Modules + +### Jellyfin + +For displaying and streaming your media + +### ShokoAnime + +For sorting your anime after downloading them + +### qBittorrent + +For downloading media + +### Caddy + +For securing your connection over HTTPS/TLS and handling incoming requests + +### [Single PHP Filehost](https://github.com/Rouji/single_php_filehost) + +For uploading and sharing files with others + +### Cloudflare Tunnel (Cloudflared) + +For securing your local router, preventing your IP from being exposed and eliminating the need for opening up a port in your router + +## Scripts + +- `./caddy` - lets you quickly reload your Caddy config if you made changes to the Caddyfile by running `./caddy reload` +- `./qBittorrent/scripts/hardlink-torrent.sh` - used with qBittorrent to create hardlinks to your torrents on successfully completed download. This allows you to keep seeding after Shoko moves the files elsewhere. Files can be deleted from the downloads folder if you no longer wish to seed. All hardlinks to a file has to be deleted for the file to be deleted. +- `./update.sh` will force rebuild (and update) all containers. + +## Setup + +### Dependencies + +Install Docker on your system. Easiest way is to just download [Docker Desktop](https://docs.docker.com/desktop/troubleshoot/overview/) and let it handle it. + +If you're using an Nvidia GPU and want GPU transcoding, you should also follow [these steps](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/nvidia-docker.html) to install the Nvidia container dependencies. For Fedora, try [this guide](https://gist.github.com/JuanM04/fcbed16d0f4405a286adebee5fd31cb2) instead. + +### Cloudflare + +Make sure you setup Cloudflare with your domain. Log into [cloudflare](https://www.cloudflare.com/) and follow the setup in the domains tab. + +You need to setup `cloudflared` once and copy over some files to get the tunnel working. You can follow [this](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/local/#set-up-a-tunnel-locally-cli-setup) guide to set it up locally. I'll summarise it here quickly but will skip how to install the `cloudflared` package as that varies by distro. + +Run this command to generate required files + +```sh +cloudflared tunnel login +``` + +Then this one to create a tunnel. The name can be anything you want that helps you recognise the server. + +```sh +cloudflared tunnel create +``` + +Run this for each of the subdomains, `jellyfin.domain.tld`, `shoko.domain.tld`, `torrent.domain.tld`, `files.domain.tld` and if you want a way to quickly test if Caddy is working, you can also add `ping.domain.tld`. Replace `domain.tld` with your domain. You can use different subdomains by changing them in the `.env` file. + +```sh +cloudflared tunnel route dns +``` + +An example of the command above could be + +```sh +cloudflared tunnel route dns example-pc ping.example.com +``` + +Copy the credentials file to `./Cloudflared/config/` (it should be stored in `~/.cloudflared/`). Example: + +```sh +sudo cp ~/.cloudflared/.json ~/Documents/media-server/Cloudflared/config/ +``` + +The name of the credentials file above is the UUID of the tunnel, put that in the `.env` file as instructed in the next step. + +### Environment Variables + +Create a `.env` file based on the content in `.env.example`. The example will include comments that will help with what value is needed there. + +The CF_API_TOKEN requires you to follow [this guide](https://samjmck.com/en/blog/using-caddy-with-cloudflare/). You can either follow the second step or the third step. If you follow the second step, you can skip the part about downloading some Caddy packages as the Dockerfile handles that already. Just skip to the part about generating an API token + +### Passwords + +**Use strong passwords for all services!!** A strong password is one that is long, not one that uses lots of weird symbols. + +qBittorrent defaults to username 'admin' and password 'adminadmin'. Make sure you go into the settings under WebUI and change that immediately! + +### ShokoAnime + +After going through the setup (or potentially during setup) set the import folders like this + +`Anime Source -> /mnt/import` using drop type "source" and enabling "watch for new files"
+`Anime Destination -> /mnt/anime` using drop type "Destination" + +Go to settings and adjust these settings + +``` +# Import +Rename on Import = true +Move on Import = true +Move after rename = true + +# Relation +Auto Group Series = true +``` + +### Jellyfin + +#### Shokofin + +You'll have to add the Shokofin plugin to Jellyfin. Do that by going to `Settings -> Dashboard -> Plugins`. In there go to the `Repositories` tab at the top. Add new + +Repository Name: `Shokofin Stable`
+Repository URL: `https://raw.githubusercontent.com/ShokoAnime/Shokofin/master/manifest.json` + +Now you have to go to the `Catalog` tab and install Shokofin (might be named Shoko). Restart Jellyfin after this. + +Go back to the Plugins list and click Shoko/Shokofin to configure. + +Host: `http://shoko_server:8111` + +This lets Jellyfin talk to Shoko directly rather than going through Cloudflare for each request. + +For Library settings I recommend using these settings + +Series/Season Grouping: Group series into Seasons based on Shoko's Groups
+Box-Sets/Movie Grouping: Create Box-sets based upon Shoko's Groups and Series entries + +Finally the advanced settings + +Public Shoko host URL: `shoko.domain.tld`
+Add AniDB ID to items: enabled + +#### Transcoding + +You should enable transcoding to offload work from the CPU to the GPU. This process varies from GPU to GPU and detailed setup can be found [here](https://jellyfin.org/docs/general/administration/hardware-acceleration.html). The current config is based on using an Nvidia GPU, in which case you only have to select the Nvidia NVENC option under Hardware Accelleration. + +### qBittorrent + +For this, you need to set the hardlinker script to run after download. + +Set the default downloads folder to /base/downloads. At the top of the downloads settings, set the `Torrent content layout` setting to `Create subfolder`. + +Enable `Run external program on torrent completion` and put this in the input field
+`/scripts/hardlink-torrent.sh "%R" "/base/import/%N"` + +Also set download category as Anime for anime, Music for music, and Movies for movies. You can add more categories in the `hardlink-torrent.sh` script + +## Notes + +Shoko server has a local interface for use with Shoko Desktop in case you need that. The URL for that is `http://localhost:8111`. diff --git a/caddy.sh b/caddy.sh new file mode 100755 index 0000000..cd0f4b4 --- /dev/null +++ b/caddy.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo docker-compose exec -w /etc/caddy caddy caddy $1 \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100755 index 0000000..8ca154d --- /dev/null +++ b/compose.yaml @@ -0,0 +1,95 @@ +version: '3.9' + +services: + jellyfin: + container_name: jellyfin + image: jellyfin/jellyfin + # user: 1000:1000 + volumes: + - ./Jellyfin/config:/config + - ./Jellyfin/cache:/cache + - ./Jellyfin/data:/data + - ${ANIME_FOLDER}:/Anime + - ${MUSIC_FOLDER}:/Music + - ${MOVIES_FOLDER}:/Movies + # ports: 8096:8096 + restart: unless-stopped + environment: + - TZ=${TIMEZONE} + - PUID=1000 + - PGID=1000 + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + + shoko_server: + container_name: shokoserver + image: shokoanime/server:daily + restart: unless-stopped + environment: + - PUID=1000 + - PGID=1000 + - TZ=${TIMEZONE} + - AVDUMP_MONO=true + ports: + - 8111:8111 + volumes: + - ./ShokoAnime:/home/shoko/.shoko/Shoko.CLI + - ${ANIME_FOLDER}:/mnt/anime + - ${BASE_DOWNLOADS_FOLDER}/import:/mnt/import + + qbittorrent: + container_name: qbittorrent + image: lscr.io/linuxserver/qbittorrent:latest + environment: + - PUID=1000 + - PGID=1000 + - TZ=${TIMEZONE} + - WEBUI_PORT=9090 + volumes: + - ./qBittorrent/config:/config + - ./qBittorrent/scripts:/scripts + - ${BASE_DOWNLOADS_FOLDER}:/base + - ${MUSIC_FOLDER}:/Music + - ${MOVIES_FOLDER}:/Movies + # ports: + # - 9090:9090 + # - 6881:6881 + # - 6881:6881/udp + restart: unless-stopped + + caddy: + container_name: caddy + build: ./Caddy + restart: unless-stopped + env_file: + - .env + volumes: + - ./Caddy/Caddyfile:/etc/caddy/Caddyfile + - ./Caddy/site:/srv + - ./Caddy/data:/data + - ./Caddy/config:/config + user: root + depends_on: + - cloudflared + + cloudflared: + container_name: cloudflared + build: + context: ./Cloudflared + args: + DOMAIN: '${DOMAIN}' + TUNNEL_UUID: '${TUNNEL_UUID}' + env_file: + - .env + command: 'tunnel --config /etc/cloudflared/config.yml run' + restart: unless-stopped + # ports: + # - 80:80 + # - 443:443 + # - 443:443/udp + user: root diff --git a/qBittorrent/scripts/hardlink-torrent.sh b/qBittorrent/scripts/hardlink-torrent.sh new file mode 100755 index 0000000..ab8deb6 --- /dev/null +++ b/qBittorrent/scripts/hardlink-torrent.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# 1: root path +# 2: torrent name +# 3: category + +echo "1: $1" >> /base/actions.txt +echo "2: $2" >> /base/actions.txt +echo "3: $3" >> /base/actions.txt + +if [ $3 == 'Anime' ] +then + cp -lR "$1" "/base/import/$2" + echo "cp -lR '$1' '/base/import/$2'" >> /base/actions.txt +fi + +if [ $3 == 'Music' ] +then + mv -R "$1" "/Music/$2" + echo "mv -R '$1' '/Music/$2'" >> /base/actions.txt +fi + +if [ $3 == 'Movies' ] +then + mv -R "$1" "/Movies/$2" + echo "mv -R '$1' '/Movies/$2'" >> /base/actions.txt +fi + +echo "" >> /base/actions.txt \ No newline at end of file diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..ed6609c --- /dev/null +++ b/update.sh @@ -0,0 +1,4 @@ +#!/bin/bash +docker-compose pull +docker-compose up -d --remove-orphans +yes | docker image prune \ No newline at end of file