mirror of
https://github.com/iv-org/invidious.git
synced 2025-05-31 14:11:54 +05:30
Compare commits
44 Commits
v2.2025050
...
refactor-c
Author | SHA1 | Date | |
---|---|---|---|
6ff616d9a4 | |||
bd2f00f6df | |||
d70b05e5c3 | |||
924ae11721 | |||
43f9af2015 | |||
4628f98eeb | |||
60b45290bd | |||
e5795cab3d | |||
b79177f07e | |||
7fe8f6f24e | |||
936c0ff61b | |||
802790e468 | |||
3dd68872f9 | |||
be9749aefd | |||
7cd974dc24 | |||
bf2eccbcaf | |||
586dfcc1e3 | |||
4e4a084f22 | |||
65e6929037 | |||
2f52ebc7e3 | |||
97b13c042e | |||
c86846e7b7 | |||
95a1f632d8 | |||
220adf53f4 | |||
c2ede1d2a5 | |||
81ca831439 | |||
8feea29607 | |||
c4944ee061 | |||
406277b16f | |||
7259c63648 | |||
73f524fccd | |||
03e06b239b | |||
c304ea6db3 | |||
9e3c0dfd85 | |||
d1bc15b8bf | |||
1f028fee0f | |||
2c1400c41e | |||
8fd0b82c38 | |||
7579adc3a3 | |||
d567c6be6e | |||
0c07e9d27a | |||
23ff6135bb | |||
409d12a81e | |||
70ff463cc6 |
@ -25,7 +25,7 @@ Lint/NotNil:
|
||||
|
||||
Lint/SpecFilename:
|
||||
Excluded:
|
||||
- spec/parsers_helper.cr
|
||||
- spec/*_helper.cr
|
||||
|
||||
|
||||
#
|
||||
|
10
.github/dependabot.yml
vendored
Normal file
10
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "docker"
|
||||
directory: "/docker"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: "weekly"
|
@ -50,7 +50,7 @@ jobs:
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker AMD64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile
|
||||
@ -75,7 +75,7 @@ jobs:
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker ARM64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile.arm64
|
||||
|
4
.github/workflows/build-stable-container.yml
vendored
4
.github/workflows/build-stable-container.yml
vendored
@ -43,7 +43,7 @@ jobs:
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker AMD64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile
|
||||
@ -69,7 +69,7 @@ jobs:
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker ARM64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile.arm64
|
||||
|
10
.github/workflows/ci.yml
vendored
10
.github/workflows/ci.yml
vendored
@ -57,12 +57,12 @@ jobs:
|
||||
shell: bash
|
||||
|
||||
- name: Install Crystal
|
||||
uses: crystal-lang/install-crystal@v1.8.0
|
||||
uses: crystal-lang/install-crystal@v1.8.2
|
||||
with:
|
||||
crystal: ${{ matrix.crystal }}
|
||||
|
||||
- name: Cache Shards
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
./lib
|
||||
@ -113,7 +113,7 @@ jobs:
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Build Docker ARM64 image
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile.arm64
|
||||
@ -136,12 +136,12 @@ jobs:
|
||||
|
||||
- name: Install Crystal
|
||||
id: lint_step_install_crystal
|
||||
uses: crystal-lang/install-crystal@v1.8.0
|
||||
uses: crystal-lang/install-crystal@v1.8.2
|
||||
with:
|
||||
crystal: latest
|
||||
|
||||
- name: Cache Shards
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
./lib
|
||||
|
2
.github/workflows/stale.yml
vendored
2
.github/workflows/stale.yml
vendored
@ -10,7 +10,7 @@ jobs:
|
||||
stale:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v8
|
||||
- uses: actions/stale@v9
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
days-before-stale: 730
|
||||
|
@ -2,6 +2,12 @@
|
||||
|
||||
## vX.Y.0 (future)
|
||||
|
||||
## v2.20250504.0
|
||||
|
||||
Small release with quick workaround fix for issue #4251 (Nil assertion failed).
|
||||
|
||||
PR: https://github.com/iv-org/invidious/issues/5263
|
||||
|
||||
## v2.20250314.0
|
||||
|
||||
### Wrap-up
|
||||
|
14
README.md
14
README.md
@ -81,9 +81,9 @@
|
||||
- [Available in many languages](locales/), thanks to [our translators](#contribute)
|
||||
|
||||
**Data import/export**
|
||||
- Import subscriptions from YouTube, NewPipe and Freetube
|
||||
- Import subscriptions from YouTube, NewPipe and FreeTube
|
||||
- Import watch history from YouTube and NewPipe
|
||||
- Export subscriptions to NewPipe and Freetube
|
||||
- Export subscriptions to NewPipe and FreeTube
|
||||
- Import/Export Invidious user data
|
||||
|
||||
**Technical features**
|
||||
@ -95,11 +95,11 @@
|
||||
|
||||
## Quick start
|
||||
|
||||
**Using invidious:**
|
||||
**Using Invidious:**
|
||||
|
||||
- [Select a public instance from the list](https://instances.invidious.io) and start watching videos right now!
|
||||
|
||||
**Hosting invidious:**
|
||||
**Hosting Invidious:**
|
||||
|
||||
- [Follow the installation instructions](https://docs.invidious.io/installation/)
|
||||
|
||||
@ -114,8 +114,8 @@ https://github.com/iv-org/documentation
|
||||
### Extensions
|
||||
|
||||
We highly recommend the use of [Privacy Redirect](https://github.com/SimonBrazell/privacy-redirect#get),
|
||||
a browser extension that automatically redirects Youtube URLs to any Invidious instance and replaces
|
||||
embedded youtube videos on other websites with invidious.
|
||||
a browser extension that automatically redirects YouTube URLs to any Invidious instance and replaces
|
||||
embedded YouTube videos on other websites with Invidious.
|
||||
|
||||
The documentation contains a list of browser extensions that we recommended to use along with Invidious.
|
||||
|
||||
@ -140,7 +140,7 @@ We use [Weblate](https://weblate.org) to manage Invidious translations.
|
||||
You can suggest new translations and/or correction here: https://hosted.weblate.org/engage/invidious/.
|
||||
|
||||
Creating an account is not required, but recommended, especially if you want to contribute regularly.
|
||||
Weblate also allows you to log-in with major SSO providers like Github, Gitlab, BitBucket, Google, ...
|
||||
Weblate also allows you to log-in with major SSO providers like GitHub, GitLab, BitBucket, Google, ...
|
||||
|
||||
|
||||
## Projects using Invidious
|
||||
|
@ -54,6 +54,53 @@ db:
|
||||
##
|
||||
#signature_server:
|
||||
|
||||
##
|
||||
## Invidious companion is an external program
|
||||
## for loading the video streams from YouTube servers.
|
||||
##
|
||||
## When this setting is commented out, Invidious companion is not used.
|
||||
## Otherwise, Invidious will proxy the requests to Invidious companion.
|
||||
##
|
||||
## Note: multiple URL can be configured. In this case, invidious will
|
||||
## randomly pick one every time video data needs to be retrieved. This
|
||||
## URL is then kept in the video metadata cache to allow video playback
|
||||
## to work. Once said cache has expired, requesting that video's data
|
||||
## again will cause a new companion URL to be picked.
|
||||
##
|
||||
## The parameter private_url needs to be configured for the internal
|
||||
## communication between the companion and Invidious.
|
||||
## And public_url is the public URL from which companion is listening
|
||||
## to the requests from the user(s).
|
||||
##
|
||||
## If you are using a reverse proxy then you will probably need to
|
||||
## configure the public_url to be the same as the domain used for Invidious.
|
||||
## Also apply when used from an external IP address (without a domain).
|
||||
## Examples: https://MYINVIDIOUSDOMAIN or http://192.168.1.100:8282
|
||||
##
|
||||
## Both parameter can have identical URL when Invidious is hosted in
|
||||
## an internal network or at home or locally (localhost).
|
||||
##
|
||||
## Accepted values: "http(s)://<IP-HOSTNAME>:<Port>"
|
||||
## Default: <none>
|
||||
##
|
||||
#invidious_companion:
|
||||
# - private_url: "http://localhost:8282"
|
||||
# public_url: "http://localhost:8282"
|
||||
|
||||
##
|
||||
## API key for Invidious companion, used for securing the communication
|
||||
## between Invidious and Invidious companion.
|
||||
## The key needs to be exactly 16 characters long.
|
||||
##
|
||||
## Note: This parameter is mandatory when Invidious companion is enabled
|
||||
## and should be a random string.
|
||||
## Such random string can be generated on linux with the following
|
||||
## command: `pwgen 16 1`
|
||||
##
|
||||
## Accepted values: a string (of length 16)
|
||||
## Default: <none>
|
||||
##
|
||||
#invidious_companion_key: "CHANGE_ME!!"
|
||||
|
||||
#########################################
|
||||
#
|
||||
@ -159,7 +206,7 @@ https_only: false
|
||||
#disable_proxy: false
|
||||
|
||||
##
|
||||
## Size of the HTTP pool used to connect to youtube. Each
|
||||
## Max size of the HTTP pool used to connect to youtube. Each
|
||||
## domain ('youtube.com', 'ytimg.com', ...) has its own pool.
|
||||
##
|
||||
## Accepted values: a positive integer
|
||||
@ -167,6 +214,16 @@ https_only: false
|
||||
##
|
||||
#pool_size: 100
|
||||
|
||||
##
|
||||
## Amount of seconds to wait for a client to be free from the pool
|
||||
## before raising an error
|
||||
##
|
||||
##
|
||||
## Accepted values: a positive integer
|
||||
## Default: 5
|
||||
##
|
||||
#pool_checkout_timeout: 5
|
||||
|
||||
|
||||
##
|
||||
## Additional cookies to be sent when requesting the youtube API.
|
||||
@ -811,9 +868,9 @@ default_user_preferences:
|
||||
## Default video quality.
|
||||
##
|
||||
## Accepted values: dash, hd720, medium, small
|
||||
## Default: hd720
|
||||
## Default: dash
|
||||
##
|
||||
#quality: hd720
|
||||
#quality: dash
|
||||
|
||||
##
|
||||
## Default dash video quality.
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM crystallang/crystal:1.12.2-alpine AS builder
|
||||
FROM crystallang/crystal:1.16.2-alpine AS builder
|
||||
|
||||
RUN apk add --no-cache sqlite-static yaml-static
|
||||
|
||||
@ -32,7 +32,7 @@ RUN --mount=type=cache,target=/root/.cache/crystal if [[ "${release}" == 1 ]] ;
|
||||
--link-flags "-lxml2 -llzma"; \
|
||||
fi
|
||||
|
||||
FROM alpine:3.20
|
||||
FROM alpine:3.21
|
||||
RUN apk add --no-cache rsvg-convert ttf-opensans tini tzdata
|
||||
WORKDIR /invidious
|
||||
RUN addgroup -g 1000 -S invidious && \
|
||||
|
@ -1,5 +1,5 @@
|
||||
FROM alpine:3.20 AS builder
|
||||
RUN apk add --no-cache 'crystal=1.12.2-r0' shards sqlite-static yaml-static yaml-dev libxml2-static \
|
||||
FROM alpine:3.21 AS builder
|
||||
RUN apk add --no-cache 'crystal=1.14.0-r0' shards sqlite-static yaml-static yaml-dev libxml2-static \
|
||||
zlib-static openssl-libs-static openssl-dev musl-dev xz-static
|
||||
|
||||
ARG release
|
||||
@ -33,7 +33,7 @@ RUN --mount=type=cache,target=/root/.cache/crystal if [[ "${release}" == 1 ]] ;
|
||||
--link-flags "-lxml2 -llzma"; \
|
||||
fi
|
||||
|
||||
FROM alpine:3.20
|
||||
FROM alpine:3.21
|
||||
RUN apk add --no-cache rsvg-convert ttf-opensans tini tzdata
|
||||
WORKDIR /invidious
|
||||
RUN addgroup -g 1000 -S invidious && \
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: invidious
|
||||
version: 2.20250314.0
|
||||
version: 2.20250314.0-dev
|
||||
|
||||
authors:
|
||||
- Invidious team <contact@invidious.io>
|
||||
|
112
spec/helpers/networking/connection_pool_spec.cr
Normal file
112
spec/helpers/networking/connection_pool_spec.cr
Normal file
@ -0,0 +1,112 @@
|
||||
# Due to the way that specs are handled this file cannot be run
|
||||
# together with everything else without causing a compile time error
|
||||
#
|
||||
# TODO: Allow running different isolated spec through make
|
||||
#
|
||||
# For now run this with `crystal spec -p spec/helpers/networking/connection_pool_spec.cr -Drunning_by_self`
|
||||
{% skip_file unless flag?(:running_by_self) %}
|
||||
|
||||
# Based on https://github.com/jgaskins/http_client/blob/958cf56064c0d31264a117467022b90397eb65d7/spec/http_client_spec.cr
|
||||
require "wait_group"
|
||||
require "uri"
|
||||
require "http"
|
||||
require "http/server"
|
||||
require "http_proxy"
|
||||
|
||||
require "db"
|
||||
require "pg"
|
||||
require "spectator"
|
||||
|
||||
require "../../load_config_helper"
|
||||
require "../../../src/invidious/helpers/crystal_class_overrides"
|
||||
require "../../../src/invidious/connection/*"
|
||||
|
||||
TEST_SERVER_URL = URI.parse("http://localhost:12345")
|
||||
|
||||
server = HTTP::Server.new do |context|
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
case {request.method, request.path}
|
||||
when {"GET", "/get"}
|
||||
response << "get"
|
||||
when {"POST", "/post"}
|
||||
response.status = :created
|
||||
response << "post"
|
||||
when {"GET", "/sleep"}
|
||||
duration = request.query_params["duration_sec"].to_i.seconds
|
||||
sleep duration
|
||||
end
|
||||
end
|
||||
|
||||
spawn server.listen 12345
|
||||
|
||||
Fiber.yield
|
||||
|
||||
Spectator.describe Invidious::ConnectionPool do
|
||||
describe "Pool" do
|
||||
it "Can make a requests through standard HTTP methods" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
|
||||
expect(pool.get("/get").body).to eq("get")
|
||||
expect(pool.post("/post").body).to eq("post")
|
||||
end
|
||||
|
||||
it "Can make streaming requests" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
|
||||
expect(pool.get("/get", &.body_io.gets_to_end)).to eq("get")
|
||||
expect(pool.get("/post", &.body)).to eq("")
|
||||
expect(pool.post("/post", &.body_io.gets_to_end)).to eq("post")
|
||||
end
|
||||
|
||||
it "Allows more than one clients to be checked out (if applicable)" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
|
||||
pool.checkout do |_|
|
||||
expect(pool.post("/post").body).to eq("post")
|
||||
end
|
||||
end
|
||||
|
||||
it "Can make multiple requests with the same client" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
|
||||
pool.checkout do |client|
|
||||
expect(client.get("/get").body).to eq("get")
|
||||
expect(client.post("/post").body).to eq("post")
|
||||
expect(client.get("/get").body).to eq("get")
|
||||
end
|
||||
end
|
||||
|
||||
it "Allows concurrent requests" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
responses = [] of HTTP::Client::Response
|
||||
|
||||
WaitGroup.wait do |wg|
|
||||
100.times do
|
||||
wg.spawn { responses << pool.get("/get") }
|
||||
end
|
||||
end
|
||||
|
||||
expect(responses.map(&.body)).to eq(["get"] * 100)
|
||||
end
|
||||
|
||||
it "Raises on checkout timeout" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 2, timeout: 0.01) { next make_client(TEST_SERVER_URL) }
|
||||
|
||||
# Long running requests
|
||||
2.times do
|
||||
spawn { pool.get("/sleep?duration_sec=2") }
|
||||
end
|
||||
|
||||
Fiber.yield
|
||||
|
||||
expect { pool.get("/get") }.to raise_error(Invidious::ConnectionPool::Error)
|
||||
end
|
||||
|
||||
it "Raises when an error is encountered" do
|
||||
pool = Invidious::ConnectionPool::Pool.new(max_capacity: 100) { next make_client(TEST_SERVER_URL) }
|
||||
expect { pool.get("/get") { raise IO::Error.new } }.to raise_error(Invidious::ConnectionPool::Error)
|
||||
end
|
||||
end
|
||||
end
|
15
spec/load_config_helper.cr
Normal file
15
spec/load_config_helper.cr
Normal file
@ -0,0 +1,15 @@
|
||||
require "yaml"
|
||||
require "log"
|
||||
|
||||
abstract class Kemal::BaseLogHandler
|
||||
end
|
||||
|
||||
require "../src/invidious/config"
|
||||
require "../src/invidious/jobs/base_job"
|
||||
require "../src/invidious/jobs.cr"
|
||||
require "../src/invidious/user/preferences.cr"
|
||||
require "../src/invidious/helpers/logger"
|
||||
require "../src/invidious/helpers/utils"
|
||||
|
||||
CONFIG = Config.from_yaml(File.open("config/config.example.yml"))
|
||||
HMAC_KEY = CONFIG.hmac_key
|
@ -35,6 +35,7 @@ require "protodec/utils"
|
||||
|
||||
require "./invidious/database/*"
|
||||
require "./invidious/database/migrations/*"
|
||||
require "./invidious/connection/*"
|
||||
require "./invidious/http_server/*"
|
||||
require "./invidious/helpers/*"
|
||||
require "./invidious/yt_backend/*"
|
||||
@ -91,11 +92,31 @@ SOFTWARE = {
|
||||
"branch" => "#{CURRENT_BRANCH}",
|
||||
}
|
||||
|
||||
YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
|
||||
YT_POOL = Invidious::ConnectionPool::Pool.new(
|
||||
max_capacity: CONFIG.pool_size,
|
||||
timeout: CONFIG.pool_checkout_timeout
|
||||
) do
|
||||
next make_client(YT_URL, force_resolve: true)
|
||||
end
|
||||
|
||||
# Image request pool
|
||||
|
||||
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
|
||||
GGPHT_URL = URI.parse("https://yt3.ggpht.com")
|
||||
|
||||
GGPHT_POOL = Invidious::ConnectionPool::Pool.new(
|
||||
max_capacity: CONFIG.pool_size,
|
||||
timeout: CONFIG.pool_checkout_timeout
|
||||
) do
|
||||
next make_client(GGPHT_URL, force_resolve: true)
|
||||
end
|
||||
|
||||
COMPANION_POOL = Invidious::ConnectionPool::Pool.new(
|
||||
max_capacity: CONFIG.pool_size,
|
||||
reinitialize_proxy: false
|
||||
) do
|
||||
companion = CONFIG.invidious_companion.sample
|
||||
next make_client(companion.private_url, use_http_proxy: false)
|
||||
end
|
||||
|
||||
# CLI
|
||||
Kemal.config.extra_options do |parser|
|
||||
@ -167,16 +188,9 @@ DECRYPT_FUNCTION =
|
||||
if sig_helper_address = CONFIG.signature_server.presence
|
||||
IV::DecryptFunction.new(sig_helper_address)
|
||||
else
|
||||
LOGGER.warn("WARNING: inv-sig-helper is required for video playback. For more information see https://docs.invidious.io/installation")
|
||||
nil
|
||||
end
|
||||
|
||||
{% for field in %w(po_token visitor_data) %}
|
||||
if !CONFIG.{{field.id}}
|
||||
LOGGER.warn("WARNING: {{field.id}} is required to view and playback videos. For more information see https://docs.invidious.io/installation")
|
||||
end
|
||||
{% end %}
|
||||
|
||||
# Start jobs
|
||||
|
||||
if CONFIG.channel_threads > 0
|
||||
|
@ -166,7 +166,7 @@ def fetch_channel(ucid, pull_all_videos : Bool)
|
||||
}
|
||||
|
||||
LOGGER.trace("fetch_channel: #{ucid} : Downloading RSS feed")
|
||||
rss = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}").body
|
||||
rss = YT_POOL.get("/feeds/videos.xml?channel_id=#{ucid}").body
|
||||
LOGGER.trace("fetch_channel: #{ucid} : Parsing RSS feed")
|
||||
rss = XML.parse(rss)
|
||||
|
||||
|
@ -35,7 +35,7 @@ struct ConfigPreferences
|
||||
property max_results : Int32 = 40
|
||||
property notifications_only : Bool = false
|
||||
property player_style : String = "invidious"
|
||||
property quality : String = "hd720"
|
||||
property quality : String = "dash"
|
||||
property quality_dash : String = "auto"
|
||||
property default_home : String? = "Popular"
|
||||
property feed_menu : Array(String) = ["Popular", "Trending", "Subscriptions", "Playlists"]
|
||||
@ -74,6 +74,16 @@ end
|
||||
class Config
|
||||
include YAML::Serializable
|
||||
|
||||
class CompanionConfig
|
||||
include YAML::Serializable
|
||||
|
||||
@[YAML::Field(converter: Preferences::URIConverter)]
|
||||
property private_url : URI = URI.parse("")
|
||||
|
||||
@[YAML::Field(converter: Preferences::URIConverter)]
|
||||
property public_url : URI = URI.parse("")
|
||||
end
|
||||
|
||||
# Number of threads to use for crawling videos from channels (for updating subscriptions)
|
||||
property channel_threads : Int32 = 1
|
||||
# Time interval between two executions of the job that crawls channel videos (subscriptions update).
|
||||
@ -147,8 +157,13 @@ class Config
|
||||
property host_binding : String = "0.0.0.0"
|
||||
# Path and permissions to make Invidious listen on a UNIX socket instead of a TCP port
|
||||
property socket_binding : SocketBindingConfig? = nil
|
||||
# Pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool of `pool_size`)
|
||||
|
||||
# Max pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool)
|
||||
property pool_size : Int32 = 100
|
||||
|
||||
# Amount of seconds to wait for a client to be free from the pool before rasing an error
|
||||
property pool_checkout_timeout : Float64 = 5
|
||||
|
||||
# HTTP Proxy configuration
|
||||
property http_proxy : HTTPProxyConfig? = nil
|
||||
|
||||
@ -160,6 +175,12 @@ class Config
|
||||
# poToken for passing bot attestation
|
||||
property po_token : String? = nil
|
||||
|
||||
# Invidious companion
|
||||
property invidious_companion : Array(CompanionConfig) = [] of CompanionConfig
|
||||
|
||||
# Invidious companion API key
|
||||
property invidious_companion_key : String = ""
|
||||
|
||||
# Saved cookies in "name1=value1; name2=value2..." format
|
||||
@[YAML::Field(converter: Preferences::StringToCookies)]
|
||||
property cookies : HTTP::Cookies = HTTP::Cookies.new
|
||||
@ -240,6 +261,27 @@ class Config
|
||||
end
|
||||
{% end %}
|
||||
|
||||
if config.invidious_companion.present?
|
||||
# invidious_companion and signature_server can't work together
|
||||
if config.signature_server
|
||||
puts "Config: You can not run inv_sig_helper and invidious_companion at the same time."
|
||||
exit(1)
|
||||
elsif config.invidious_companion_key.empty?
|
||||
puts "Config: Please configure a key if you are using invidious companion."
|
||||
exit(1)
|
||||
elsif config.invidious_companion_key == "CHANGE_ME!!"
|
||||
puts "Config: The value of 'invidious_companion_key' needs to be changed!!"
|
||||
exit(1)
|
||||
elsif config.invidious_companion_key.size != 16
|
||||
puts "Config: The value of 'invidious_companion_key' needs to be a size of 16 characters."
|
||||
exit(1)
|
||||
end
|
||||
elsif config.signature_server
|
||||
puts("WARNING: inv-sig-helper is deprecated. Please switch to Invidious companion: https://docs.invidious.io/companion-installation/")
|
||||
else
|
||||
puts("WARNING: Invidious companion is required to view and playback videos. For more information see https://docs.invidious.io/companion-installation/")
|
||||
end
|
||||
|
||||
# HMAC_key is mandatory
|
||||
# See: https://github.com/iv-org/invidious/issues/3854
|
||||
if config.hmac_key.empty?
|
||||
|
@ -1,51 +1,3 @@
|
||||
# Mapping of subdomain => YoutubeConnectionPool
|
||||
# This is needed as we may need to access arbitrary subdomains of ytimg
|
||||
private YTIMG_POOLS = {} of String => YoutubeConnectionPool
|
||||
|
||||
struct YoutubeConnectionPool
|
||||
property! url : URI
|
||||
property! capacity : Int32
|
||||
property! timeout : Float64
|
||||
property pool : DB::Pool(HTTP::Client)
|
||||
|
||||
def initialize(url : URI, @capacity = 5, @timeout = 5.0)
|
||||
@url = url
|
||||
@pool = build_pool()
|
||||
end
|
||||
|
||||
def client(&)
|
||||
conn = pool.checkout
|
||||
# Proxy needs to be reinstated every time we get a client from the pool
|
||||
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||
|
||||
begin
|
||||
response = yield conn
|
||||
rescue ex
|
||||
conn.close
|
||||
conn = make_client(url, force_resolve: true)
|
||||
|
||||
response = yield conn
|
||||
ensure
|
||||
pool.release(conn)
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private def build_pool
|
||||
options = DB::Pool::Options.new(
|
||||
initial_pool_size: 0,
|
||||
max_pool_size: capacity,
|
||||
max_idle_pool_size: capacity,
|
||||
checkout_timeout: timeout
|
||||
)
|
||||
|
||||
DB::Pool(HTTP::Client).new(options) do
|
||||
next make_client(url, force_resolve: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_yt_headers(request)
|
||||
request.headers.delete("User-Agent") if request.headers["User-Agent"] == "Crystal"
|
||||
request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
|
||||
@ -61,9 +13,9 @@ def add_yt_headers(request)
|
||||
end
|
||||
end
|
||||
|
||||
def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_headers : Bool = false)
|
||||
def make_client(url : URI, region = nil, force_resolve : Bool = false, force_youtube_headers : Bool = false, use_http_proxy : Bool = true)
|
||||
client = HTTP::Client.new(url)
|
||||
client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||
client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy && use_http_proxy
|
||||
|
||||
# Force the usage of a specific configured IP Family
|
||||
if force_resolve
|
||||
@ -78,8 +30,8 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false, force_you
|
||||
return client
|
||||
end
|
||||
|
||||
def make_client(url : URI, region = nil, force_resolve : Bool = false, &)
|
||||
client = make_client(url, region, force_resolve: force_resolve)
|
||||
def make_client(url : URI, region = nil, force_resolve : Bool = false, use_http_proxy : Bool = true, &)
|
||||
client = make_client(url, region, force_resolve: force_resolve, use_http_proxy: use_http_proxy)
|
||||
begin
|
||||
yield client
|
||||
ensure
|
||||
@ -99,18 +51,3 @@ def make_configured_http_proxy_client
|
||||
password: config_proxy.password,
|
||||
)
|
||||
end
|
||||
|
||||
# Fetches a HTTP pool for the specified subdomain of ytimg.com
|
||||
#
|
||||
# Creates a new one when the specified pool for the subdomain does not exist
|
||||
def get_ytimg_pool(subdomain)
|
||||
if pool = YTIMG_POOLS[subdomain]?
|
||||
return pool
|
||||
else
|
||||
LOGGER.info("ytimg_pool: Creating a new HTTP pool for \"https://#{subdomain}.ytimg.com\"")
|
||||
pool = YoutubeConnectionPool.new(URI.parse("https://#{subdomain}.ytimg.com"), capacity: CONFIG.pool_size)
|
||||
YTIMG_POOLS[subdomain] = pool
|
||||
|
||||
return pool
|
||||
end
|
||||
end
|
116
src/invidious/connection/pool.cr
Normal file
116
src/invidious/connection/pool.cr
Normal file
@ -0,0 +1,116 @@
|
||||
module Invidious::ConnectionPool
|
||||
# A connection pool to reuse `HTTP::Client` connections
|
||||
struct Pool
|
||||
getter pool : DB::Pool(HTTP::Client)
|
||||
|
||||
# Creates a connection pool with the provided options, and client factory block.
|
||||
def initialize(
|
||||
*,
|
||||
max_capacity : Int32 = 5,
|
||||
timeout : Float64 = 5.0,
|
||||
@reinitialize_proxy : Bool = true, # Whether or not http-proxy should be reinitialized on checkout
|
||||
&client_factory : -> HTTP::Client
|
||||
)
|
||||
pool_options = DB::Pool::Options.new(
|
||||
initial_pool_size: 0,
|
||||
max_pool_size: max_capacity,
|
||||
max_idle_pool_size: max_capacity,
|
||||
checkout_timeout: timeout
|
||||
)
|
||||
|
||||
@pool = DB::Pool(HTTP::Client).new(pool_options, &client_factory)
|
||||
end
|
||||
|
||||
{% for method in %w[get post put patch delete head options] %}
|
||||
# Streaming API for {{method.id.upcase}} request.
|
||||
# The response will have its body as an `IO` accessed via `HTTP::Client::Response#body_io`.
|
||||
def {{method.id}}(*args, **kwargs, &)
|
||||
self.checkout do | client |
|
||||
client.{{method.id}}(*args, **kwargs) do | response |
|
||||
result = yield response
|
||||
return result
|
||||
ensure
|
||||
response.body_io?.try &.skip_to_end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Executes a {{method.id.upcase}} request.
|
||||
# The response will have its body as a `String`, accessed via `HTTP::Client::Response#body`.
|
||||
def {{method.id}}(*args, **kwargs)
|
||||
self.checkout do | client |
|
||||
return client.{{method.id}}(*args, **kwargs)
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
|
||||
# Checks out a client in the pool
|
||||
def checkout(&)
|
||||
# If a client has been deleted from the pool
|
||||
# we won't try to release it
|
||||
client_exists_in_pool = true
|
||||
|
||||
http_client = pool.checkout
|
||||
|
||||
# When the HTTP::Client connection is closed, the automatic reconnection
|
||||
# feature will create a new IO to connect to the server with
|
||||
#
|
||||
# This new TCP IO will be a direct connection to the server and will not go
|
||||
# through the proxy. As such we'll need to reinitialize the proxy connection
|
||||
|
||||
http_client.proxy = make_configured_http_proxy_client() if @reinitialize_proxy && CONFIG.http_proxy
|
||||
|
||||
response = yield http_client
|
||||
rescue ex : DB::PoolTimeout
|
||||
# Failed to checkout a client
|
||||
raise ConnectionPool::PoolCheckoutError.new(ex.message)
|
||||
rescue ex
|
||||
# An error occurred with the client itself.
|
||||
# Delete the client from the pool and close the connection
|
||||
if http_client
|
||||
client_exists_in_pool = false
|
||||
@pool.delete(http_client)
|
||||
http_client.close
|
||||
end
|
||||
|
||||
# Raise exception for outer methods to handle
|
||||
raise ConnectionPool::Error.new(ex.message, cause: ex)
|
||||
ensure
|
||||
pool.release(http_client) if http_client && client_exists_in_pool
|
||||
end
|
||||
end
|
||||
|
||||
class Error < Exception
|
||||
end
|
||||
|
||||
# Raised when the pool failed to get a client in time
|
||||
class PoolCheckoutError < Error
|
||||
end
|
||||
|
||||
# Mapping of subdomain => Invidious::ConnectionPool::Pool
|
||||
# This is needed as we may need to access arbitrary subdomains of ytimg
|
||||
private YTIMG_POOLS = {} of String => ConnectionPool::Pool
|
||||
|
||||
# Fetches a HTTP pool for the specified subdomain of ytimg.com
|
||||
#
|
||||
# Creates a new one when the specified pool for the subdomain does not exist
|
||||
def self.get_ytimg_pool(subdomain)
|
||||
if pool = YTIMG_POOLS[subdomain]?
|
||||
return pool
|
||||
else
|
||||
LOGGER.info("ytimg_pool: Creating a new HTTP pool for \"https://#{subdomain}.ytimg.com\"")
|
||||
url = URI.parse("https://#{subdomain}.ytimg.com")
|
||||
|
||||
pool = ConnectionPool::Pool.new(
|
||||
max_capacity: CONFIG.pool_size,
|
||||
timeout: CONFIG.pool_checkout_timeout
|
||||
) do
|
||||
next make_client(url, force_resolve: true)
|
||||
end
|
||||
|
||||
YTIMG_POOLS[subdomain] = pool
|
||||
|
||||
return pool
|
||||
end
|
||||
end
|
||||
end
|
@ -23,10 +23,16 @@ module Invidious::Frontend::WatchPage
|
||||
return "<p id=\"download\">#{translate(locale, "Download is disabled")}</p>"
|
||||
end
|
||||
|
||||
url = "/download"
|
||||
if (CONFIG.invidious_companion.present?)
|
||||
invidious_companion = CONFIG.invidious_companion.sample
|
||||
url = "#{invidious_companion.public_url}/download?check=#{invidious_companion_encrypt(video.id)}"
|
||||
end
|
||||
|
||||
return String.build(4000) do |str|
|
||||
str << "<form"
|
||||
str << " class=\"pure-form pure-form-stacked\""
|
||||
str << " action='/download'"
|
||||
str << " action='#{url}'"
|
||||
str << " method='post'"
|
||||
str << " rel='noopener'"
|
||||
str << " target='_blank'>"
|
||||
|
@ -383,3 +383,22 @@ def parse_link_endpoint(endpoint : JSON::Any, text : String, video_id : String)
|
||||
end
|
||||
return text
|
||||
end
|
||||
|
||||
def encrypt_ecb_without_salt(data, key)
|
||||
cipher = OpenSSL::Cipher.new("aes-128-ecb")
|
||||
cipher.encrypt
|
||||
cipher.key = key
|
||||
|
||||
io = IO::Memory.new
|
||||
io.write(cipher.update(data))
|
||||
io.write(cipher.final)
|
||||
io.rewind
|
||||
|
||||
return io
|
||||
end
|
||||
|
||||
def invidious_companion_encrypt(data)
|
||||
timestamp = Time.utc.to_unix
|
||||
encrypted_data = encrypt_ecb_without_salt("#{timestamp}|#{data}", CONFIG.invidious_companion_key)
|
||||
return Base64.urlsafe_encode(encrypted_data)
|
||||
end
|
||||
|
@ -26,7 +26,7 @@ def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
|
||||
end
|
||||
|
||||
video_id = "CvFH_6DNRCY" if rdid.starts_with? "OLAK5uy_"
|
||||
response = YT_POOL.client &.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en", headers)
|
||||
response = YT_POOL.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en", headers)
|
||||
initial_data = extract_initial_data(response.body)
|
||||
|
||||
if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
|
||||
|
@ -8,6 +8,11 @@ module Invidious::Routes::API::Manifest
|
||||
id = env.params.url["id"]
|
||||
region = env.params.query["region"]?
|
||||
|
||||
if CONFIG.invidious_companion.present?
|
||||
invidious_companion = CONFIG.invidious_companion.sample
|
||||
return env.redirect "#{invidious_companion.public_url}/api/manifest/dash/id/#{id}?#{env.params.query}"
|
||||
end
|
||||
|
||||
# Since some implementations create playlists based on resolution regardless of different codecs,
|
||||
# we can opt to only add a source to a representation if it has a unique height within that representation
|
||||
unique_res = env.params.query["unique_res"]?.try { |q| (q == "true" || q == "1").to_unsafe }
|
||||
@ -21,7 +26,7 @@ module Invidious::Routes::API::Manifest
|
||||
end
|
||||
|
||||
if dashmpd = video.dash_manifest_url
|
||||
response = YT_POOL.client &.get(URI.parse(dashmpd).request_target)
|
||||
response = YT_POOL.get(URI.parse(dashmpd).request_target)
|
||||
|
||||
if response.status_code != 200
|
||||
haltf env, status_code: response.status_code
|
||||
@ -162,7 +167,7 @@ module Invidious::Routes::API::Manifest
|
||||
|
||||
# /api/manifest/hls_playlist/*
|
||||
def self.get_hls_playlist(env)
|
||||
response = YT_POOL.client &.get(env.request.path)
|
||||
response = YT_POOL.get(env.request.path)
|
||||
|
||||
if response.status_code != 200
|
||||
haltf env, status_code: response.status_code
|
||||
@ -218,7 +223,7 @@ module Invidious::Routes::API::Manifest
|
||||
|
||||
# /api/manifest/hls_variant/*
|
||||
def self.get_hls_variant(env)
|
||||
response = YT_POOL.client &.get(env.request.path)
|
||||
response = YT_POOL.get(env.request.path)
|
||||
|
||||
if response.status_code != 200
|
||||
haltf env, status_code: response.status_code
|
||||
|
@ -106,7 +106,7 @@ module Invidious::Routes::API::V1::Videos
|
||||
# Auto-generated captions often have cues that aren't aligned properly with the video,
|
||||
# as well as some other markup that makes it cumbersome, so we try to fix that here
|
||||
if caption.name.includes? "auto-generated"
|
||||
caption_xml = YT_POOL.client &.get(url).body
|
||||
caption_xml = YT_POOL.get(url).body
|
||||
|
||||
settings_field = {
|
||||
"Kind" => "captions",
|
||||
@ -147,7 +147,7 @@ module Invidious::Routes::API::V1::Videos
|
||||
query_params = uri.query_params
|
||||
query_params["fmt"] = "vtt"
|
||||
uri.query_params = query_params
|
||||
webvtt = YT_POOL.client &.get(uri.request_target).body
|
||||
webvtt = YT_POOL.get(uri.request_target).body
|
||||
|
||||
if webvtt.starts_with?("<?xml")
|
||||
webvtt = caption.timedtext_to_vtt(webvtt)
|
||||
@ -300,7 +300,7 @@ module Invidious::Routes::API::V1::Videos
|
||||
cache_annotation(id, annotations)
|
||||
end
|
||||
else # "youtube"
|
||||
response = YT_POOL.client &.get("/annotations_invideo?video_id=#{id}")
|
||||
response = YT_POOL.get("/annotations_invideo?video_id=#{id}")
|
||||
|
||||
if response.status_code != 200
|
||||
haltf env, response.status_code
|
||||
|
@ -392,7 +392,7 @@ module Invidious::Routes::Channels
|
||||
value = env.request.resource.split("/")[2]
|
||||
body = ""
|
||||
{"channel", "user", "c"}.each do |type|
|
||||
response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
|
||||
response = YT_POOL.get("/#{type}/#{value}/live?disable_polymer=1")
|
||||
if response.status_code == 200
|
||||
body = response.body
|
||||
end
|
||||
|
@ -92,7 +92,7 @@ module Invidious::Routes::Embed
|
||||
|
||||
return env.redirect url
|
||||
when "live_stream"
|
||||
response = YT_POOL.client &.get("/embed/live_stream?channel=#{env.params.query["channel"]? || ""}")
|
||||
response = YT_POOL.get("/embed/live_stream?channel=#{env.params.query["channel"]? || ""}")
|
||||
video_id = response.body.match(/"video_id":"(?<video_id>[a-zA-Z0-9_-]{11})"/).try &.["video_id"]
|
||||
|
||||
env.params.query.delete_all("channel")
|
||||
@ -203,6 +203,14 @@ module Invidious::Routes::Embed
|
||||
return env.redirect url
|
||||
end
|
||||
|
||||
if CONFIG.invidious_companion.present?
|
||||
invidious_companion = CONFIG.invidious_companion.sample
|
||||
env.response.headers["Content-Security-Policy"] =
|
||||
env.response.headers["Content-Security-Policy"]
|
||||
.gsub("media-src", "media-src #{invidious_companion.public_url}")
|
||||
.gsub("connect-src", "connect-src #{invidious_companion.public_url}")
|
||||
end
|
||||
|
||||
rendered "embed"
|
||||
end
|
||||
end
|
||||
|
@ -9,10 +9,10 @@ module Invidious::Routes::ErrorRoutes
|
||||
item = md["id"]
|
||||
|
||||
# Check if item is branding URL e.g. https://youtube.com/gaming
|
||||
response = YT_POOL.client &.get("/#{item}")
|
||||
response = YT_POOL.get("/#{item}")
|
||||
|
||||
if response.status_code == 301
|
||||
response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target)
|
||||
response = YT_POOL.get(URI.parse(response.headers["Location"]).request_target)
|
||||
end
|
||||
|
||||
if response.body.empty?
|
||||
@ -40,7 +40,7 @@ module Invidious::Routes::ErrorRoutes
|
||||
end
|
||||
|
||||
# Check if item is video ID
|
||||
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404
|
||||
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.head("/watch?v=#{item}").status_code != 404
|
||||
env.response.headers["Location"] = url
|
||||
haltf env, status_code: 302
|
||||
end
|
||||
|
@ -160,8 +160,9 @@ module Invidious::Routes::Feeds
|
||||
"default" => "http://www.w3.org/2005/Atom",
|
||||
}
|
||||
|
||||
response = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}")
|
||||
response = YT_POOL.get("/feeds/videos.xml?channel_id=#{ucid}")
|
||||
return error_atom(404, NotFoundException.new("Channel does not exist.")) if response.status_code == 404
|
||||
|
||||
rss = XML.parse(response.body)
|
||||
|
||||
videos = rss.xpath_nodes("//default:feed/default:entry", namespaces).map do |entry|
|
||||
@ -304,7 +305,7 @@ module Invidious::Routes::Feeds
|
||||
end
|
||||
end
|
||||
|
||||
response = YT_POOL.client &.get("/feeds/videos.xml?playlist_id=#{plid}")
|
||||
response = YT_POOL.get("/feeds/videos.xml?playlist_id=#{plid}")
|
||||
return error_atom(404, NotFoundException.new("Playlist does not exist.")) if response.status_code == 404
|
||||
|
||||
document = XML.parse(response.body)
|
||||
|
@ -12,7 +12,7 @@ module Invidious::Routes::Images
|
||||
end
|
||||
|
||||
begin
|
||||
GGPHT_POOL.client &.get(url, headers) do |resp|
|
||||
GGPHT_POOL.get(url, headers) do |resp|
|
||||
return self.proxy_image(env, resp)
|
||||
end
|
||||
rescue ex
|
||||
@ -42,7 +42,7 @@ module Invidious::Routes::Images
|
||||
end
|
||||
|
||||
begin
|
||||
get_ytimg_pool(authority).client &.get(url, headers) do |resp|
|
||||
ConnectionPool.get_ytimg_pool(authority).get(url, headers) do |resp|
|
||||
env.response.headers["Connection"] = "close"
|
||||
return self.proxy_image(env, resp)
|
||||
end
|
||||
@ -65,7 +65,7 @@ module Invidious::Routes::Images
|
||||
end
|
||||
|
||||
begin
|
||||
get_ytimg_pool("i9").client &.get(url, headers) do |resp|
|
||||
ConnectionPool.get_ytimg_pool("i9").get(url, headers) do |resp|
|
||||
return self.proxy_image(env, resp)
|
||||
end
|
||||
rescue ex
|
||||
@ -81,7 +81,7 @@ module Invidious::Routes::Images
|
||||
end
|
||||
|
||||
begin
|
||||
YT_POOL.client &.get(env.request.resource, headers) do |response|
|
||||
YT_POOL.get(env.request.resource, headers) do |response|
|
||||
env.response.status_code = response.status_code
|
||||
response.headers.each do |key, value|
|
||||
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
|
||||
@ -111,7 +111,7 @@ module Invidious::Routes::Images
|
||||
if name == "maxres.jpg"
|
||||
build_thumbnails(id).each do |thumb|
|
||||
thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg"
|
||||
if get_ytimg_pool("i").client &.head(thumbnail_resource_path, headers).status_code == 200
|
||||
if ConnectionPool.get_ytimg_pool("i").head(thumbnail_resource_path, headers).status_code == 200
|
||||
name = thumb[:url] + ".jpg"
|
||||
break
|
||||
end
|
||||
@ -127,7 +127,7 @@ module Invidious::Routes::Images
|
||||
end
|
||||
|
||||
begin
|
||||
get_ytimg_pool("i").client &.get(url, headers) do |resp|
|
||||
ConnectionPool.get_ytimg_pool("i").get(url, headers) do |resp|
|
||||
return self.proxy_image(env, resp)
|
||||
end
|
||||
rescue ex
|
||||
|
@ -464,7 +464,7 @@ module Invidious::Routes::Playlists
|
||||
|
||||
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
|
||||
def self.watch_videos(env)
|
||||
response = YT_POOL.client &.get(env.request.resource)
|
||||
response = YT_POOL.get(env.request.resource)
|
||||
if url = response.headers["Location"]?
|
||||
url = URI.parse(url).request_target
|
||||
return env.redirect url
|
||||
|
@ -21,7 +21,7 @@ module Invidious::Routes::VideoPlayback
|
||||
end
|
||||
|
||||
# Sanity check, to avoid being used as an open proxy
|
||||
if !host.matches?(/[\w-]+.googlevideo.com/)
|
||||
if !host.matches?(/[\w-]+\.(?:googlevideo|c\.youtube)\.com/)
|
||||
return error_template(400, "Invalid \"host\" parameter.")
|
||||
end
|
||||
|
||||
@ -37,7 +37,8 @@ module Invidious::Routes::VideoPlayback
|
||||
|
||||
# See: https://github.com/iv-org/invidious/issues/3302
|
||||
range_header = env.request.headers["Range"]?
|
||||
if range_header.nil?
|
||||
sq = query_params["sq"]?
|
||||
if range_header.nil? && sq.nil?
|
||||
range_for_head = query_params["range"]? || "0-640"
|
||||
headers["Range"] = "bytes=#{range_for_head}"
|
||||
end
|
||||
@ -256,6 +257,11 @@ module Invidious::Routes::VideoPlayback
|
||||
# YouTube /videoplayback links expire after 6 hours,
|
||||
# so we have a mechanism here to redirect to the latest version
|
||||
def self.latest_version(env)
|
||||
if CONFIG.invidious_companion.present?
|
||||
invidious_companion = CONFIG.invidious_companion.sample
|
||||
return env.redirect "#{invidious_companion.public_url}/latest_version?#{env.params.query}"
|
||||
end
|
||||
|
||||
id = env.params.query["id"]?
|
||||
itag = env.params.query["itag"]?.try &.to_i?
|
||||
|
||||
|
@ -192,6 +192,14 @@ module Invidious::Routes::Watch
|
||||
captions: video.captions
|
||||
)
|
||||
|
||||
if CONFIG.invidious_companion.present?
|
||||
invidious_companion = CONFIG.invidious_companion.sample
|
||||
env.response.headers["Content-Security-Policy"] =
|
||||
env.response.headers["Content-Security-Policy"]
|
||||
.gsub("media-src", "media-src #{invidious_companion.public_url}")
|
||||
.gsub("connect-src", "connect-src #{invidious_companion.public_url}")
|
||||
end
|
||||
|
||||
templated "watch"
|
||||
end
|
||||
|
||||
@ -285,6 +293,9 @@ module Invidious::Routes::Watch
|
||||
if CONFIG.disabled?("downloads")
|
||||
return error_template(403, "Administrator has disabled this endpoint.")
|
||||
end
|
||||
if CONFIG.invidious_companion.present?
|
||||
return error_template(403, "Downloads should be routed through Companion when present")
|
||||
end
|
||||
|
||||
title = env.params.body["title"]? || ""
|
||||
video_id = env.params.body["id"]? || ""
|
||||
@ -314,10 +325,9 @@ module Invidious::Routes::Watch
|
||||
env.params.query["label"] = URI.decode_www_form(label.as_s)
|
||||
|
||||
return Invidious::Routes::API::V1::Videos.captions(env)
|
||||
elsif itag = download_widget["itag"]?.try &.as_i
|
||||
elsif itag = download_widget["itag"]?.try &.as_i.to_s
|
||||
# URL params specific to /latest_version
|
||||
env.params.query["id"] = video_id
|
||||
env.params.query["itag"] = itag.to_s
|
||||
env.params.query["title"] = filename
|
||||
env.params.query["local"] = "true"
|
||||
|
||||
|
@ -16,11 +16,11 @@ module Invidious::Search
|
||||
# Search a youtube channel
|
||||
# TODO: clean code, and rely more on YoutubeAPI
|
||||
def channel(query : Query) : Array(SearchItem)
|
||||
response = YT_POOL.client &.get("/channel/#{query.channel}")
|
||||
response = YT_POOL.get("/channel/#{query.channel}")
|
||||
|
||||
if response.status_code == 404
|
||||
response = YT_POOL.client &.get("/user/#{query.channel}")
|
||||
response = YT_POOL.client &.get("/c/#{query.channel}") if response.status_code == 404
|
||||
response = YT_POOL.get("/user/#{query.channel}")
|
||||
response = YT_POOL.get("/c/#{query.channel}") if response.status_code == 404
|
||||
initial_data = extract_initial_data(response.body)
|
||||
ucid = initial_data.dig?("header", "c4TabbedHeaderRenderer", "channelId").try(&.as_s?)
|
||||
raise ChannelSearchException.new(query.channel) if !ucid
|
||||
|
@ -15,7 +15,7 @@ struct Video
|
||||
# NOTE: don't forget to bump this number if any change is made to
|
||||
# the `params` structure in videos/parser.cr!!!
|
||||
#
|
||||
SCHEMA_VERSION = 2
|
||||
SCHEMA_VERSION = 3
|
||||
|
||||
property id : String
|
||||
|
||||
|
@ -108,27 +108,22 @@ def extract_video_info(video_id : String)
|
||||
params = parse_video_info(video_id, player_response)
|
||||
params["reason"] = JSON::Any.new(reason) if reason
|
||||
|
||||
new_player_response = nil
|
||||
|
||||
# Don't use Android test suite client if po_token is passed because po_token doesn't
|
||||
# work for Android test suite client.
|
||||
if reason.nil? && CONFIG.po_token.nil?
|
||||
# Fetch the video streams using an Android client in order to get the
|
||||
# decrypted URLs and maybe fix throttling issues (#2194). See the
|
||||
# following issue for an explanation about decrypted URLs:
|
||||
# https://github.com/TeamNewPipe/NewPipeExtractor/issues/562
|
||||
client_config.client_type = YoutubeAPI::ClientType::AndroidTestSuite
|
||||
new_player_response = try_fetch_streaming_data(video_id, client_config)
|
||||
end
|
||||
|
||||
# Replace player response and reset reason
|
||||
if !new_player_response.nil?
|
||||
# Preserve captions & storyboard data before replacement
|
||||
new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]?
|
||||
new_player_response["captions"] = player_response["captions"] if player_response["captions"]?
|
||||
|
||||
player_response = new_player_response
|
||||
params.delete("reason")
|
||||
if !CONFIG.invidious_companion.present?
|
||||
if player_response["streamingData"]? && player_response.dig?("streamingData", "adaptiveFormats", 0, "url").nil?
|
||||
LOGGER.warn("Missing URLs for adaptive formats, falling back to other YT clients.")
|
||||
players_fallback = [YoutubeAPI::ClientType::TvHtml5, YoutubeAPI::ClientType::WebMobile]
|
||||
players_fallback.each do |player_fallback|
|
||||
client_config.client_type = player_fallback
|
||||
player_fallback_response = try_fetch_streaming_data(video_id, client_config)
|
||||
if player_fallback_response && player_fallback_response["streamingData"]? &&
|
||||
player_fallback_response.dig?("streamingData", "adaptiveFormats", 0, "url")
|
||||
streaming_data = player_response["streamingData"].as_h
|
||||
streaming_data["adaptiveFormats"] = player_fallback_response["streamingData"]["adaptiveFormats"]
|
||||
player_response["streamingData"] = JSON::Any.new(streaming_data)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
{"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f|
|
||||
|
@ -22,6 +22,8 @@
|
||||
audio_streams.each_with_index do |fmt, i|
|
||||
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
|
||||
src_url += "&local=true" if params.local
|
||||
src_url = invidious_companion.public_url.to_s + src_url +
|
||||
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
|
||||
|
||||
bitrate = fmt["bitrate"]
|
||||
mimetype = HTML.escape(fmt["mimeType"].as_s)
|
||||
@ -34,8 +36,12 @@
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<% if params.quality == "dash" %>
|
||||
<source src="/api/manifest/dash/id/<%= video.id %>?local=true&unique_res=1" type='application/dash+xml' label="dash">
|
||||
<% if params.quality == "dash"
|
||||
src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1"
|
||||
src_url = invidious_companion.public_url.to_s + src_url +
|
||||
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
|
||||
%>
|
||||
<source src="<%= src_url %>" type='application/dash+xml' label="dash">
|
||||
<% end %>
|
||||
|
||||
<%
|
||||
@ -44,6 +50,8 @@
|
||||
fmt_stream.each_with_index do |fmt, i|
|
||||
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
|
||||
src_url += "&local=true" if params.local
|
||||
src_url = invidious_companion.public_url.to_s + src_url +
|
||||
"&check=#{invidious_companion_encrypt(video.id)}" if (invidious_companion)
|
||||
|
||||
quality = fmt["quality"]
|
||||
mimetype = HTML.escape(fmt["mimeType"].as_s)
|
||||
|
@ -500,7 +500,11 @@ module YoutubeAPI
|
||||
data["params"] = params
|
||||
end
|
||||
|
||||
return self._post_json("/youtubei/v1/player", data, client_config)
|
||||
if CONFIG.invidious_companion.present?
|
||||
return self._post_invidious_companion("/youtubei/v1/player", data)
|
||||
else
|
||||
return self._post_json("/youtubei/v1/player", data, client_config)
|
||||
end
|
||||
end
|
||||
|
||||
####################################################################
|
||||
@ -635,15 +639,13 @@ module YoutubeAPI
|
||||
LOGGER.trace("YoutubeAPI: POST data: #{data}")
|
||||
|
||||
# Send the POST request
|
||||
body = YT_POOL.client() do |client|
|
||||
client.post(url, headers: headers, body: data.to_json) do |response|
|
||||
if response.status_code != 200
|
||||
raise InfoException.new("Error: non 200 status code. Youtube API returned \
|
||||
status code #{response.status_code}. See <a href=\"https://docs.invidious.io/youtube-errors-explained/\"> \
|
||||
https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting.")
|
||||
end
|
||||
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
|
||||
body = YT_POOL.post(url, headers: headers, body: data.to_json) do |response|
|
||||
if response.status_code != 200
|
||||
raise InfoException.new("Error: non 200 status code. Youtube API returned \
|
||||
status code #{response.status_code}. See <a href=\"https://docs.invidious.io/youtube-errors-explained/\"> \
|
||||
https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting.")
|
||||
end
|
||||
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
|
||||
end
|
||||
|
||||
# Convert result to Hash
|
||||
@ -666,6 +668,49 @@ module YoutubeAPI
|
||||
return initial_data
|
||||
end
|
||||
|
||||
####################################################################
|
||||
# _post_invidious_companion(endpoint, data)
|
||||
#
|
||||
# Internal function that does the actual request to Invidious companion
|
||||
# and handles errors.
|
||||
#
|
||||
# The requested data is an endpoint (URL without the domain part)
|
||||
# and the data as a Hash object.
|
||||
#
|
||||
def _post_invidious_companion(
|
||||
endpoint : String,
|
||||
data : Hash,
|
||||
) : Hash(String, JSON::Any)
|
||||
headers = HTTP::Headers{
|
||||
"Content-Type" => "application/json; charset=UTF-8",
|
||||
"Authorization" => "Bearer #{CONFIG.invidious_companion_key}",
|
||||
}
|
||||
|
||||
# Logging
|
||||
LOGGER.debug("Invidious companion: Using endpoint: \"#{endpoint}\"")
|
||||
LOGGER.trace("Invidious companion: POST data: #{data}")
|
||||
|
||||
# Send the POST request
|
||||
|
||||
begin
|
||||
response = COMPANION_POOL.post(endpoint, headers: headers, body: data.to_json)
|
||||
body = response.body
|
||||
if (response.status_code != 200)
|
||||
raise Exception.new(
|
||||
"Error while communicating with Invidious companion: \
|
||||
status code: #{response.status_code} and body: #{body.dump}"
|
||||
)
|
||||
end
|
||||
rescue ex
|
||||
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
|
||||
end
|
||||
|
||||
# Convert result to Hash
|
||||
initial_data = JSON.parse(body).as_h
|
||||
|
||||
return initial_data
|
||||
end
|
||||
|
||||
####################################################################
|
||||
# _decompress(body_io, headers)
|
||||
#
|
||||
|
Reference in New Issue
Block a user