mirror of
https://github.com/iv-org/invidious.git
synced 2025-05-31 14:11:54 +05:30
Compare commits
11 Commits
refactor-c
...
fix-build-
Author | SHA1 | Date | |
---|---|---|---|
cc643f209a | |||
381074fce1 | |||
033a44fab5 | |||
a3375e512e | |||
1d664c759f | |||
94f0a7a9d2 | |||
1d2f4b6813 | |||
cef0097a30 | |||
bef2d7b6b5 | |||
03f89be929 | |||
d4eb2a9741 |
@ -25,7 +25,7 @@ Lint/NotNil:
|
|||||||
|
|
||||||
Lint/SpecFilename:
|
Lint/SpecFilename:
|
||||||
Excluded:
|
Excluded:
|
||||||
- spec/*_helper.cr
|
- spec/parsers_helper.cr
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
55
.github/workflows/build-nightly-container.yml
vendored
55
.github/workflows/build-nightly-container.yml
vendored
@ -17,17 +17,27 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
platform: linux/amd64
|
||||||
|
name: "AMD64"
|
||||||
|
dockerfile: "docker/Dockerfile"
|
||||||
|
tag_suffix: ""
|
||||||
|
# GitHub doesn't have a ubuntu-latest-arm runner
|
||||||
|
- os: ubuntu-24.04-arm
|
||||||
|
platform: linux/arm64/v8
|
||||||
|
name: "ARM64"
|
||||||
|
dockerfile: "docker/Dockerfile.arm64"
|
||||||
|
tag_suffix: "-arm64"
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
with:
|
|
||||||
platforms: arm64
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
@ -43,45 +53,22 @@ jobs:
|
|||||||
uses: docker/metadata-action@v5
|
uses: docker/metadata-action@v5
|
||||||
with:
|
with:
|
||||||
images: quay.io/invidious/invidious
|
images: quay.io/invidious/invidious
|
||||||
|
flavor: |
|
||||||
|
suffix=${{ matrix.tag_suffix }}
|
||||||
tags: |
|
tags: |
|
||||||
type=sha,format=short,prefix={{date 'YYYY.MM.DD'}}-,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
type=sha,format=short,prefix={{date 'YYYY.MM.DD'}}-,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||||
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||||
labels: |
|
labels: |
|
||||||
quay.expires-after=12w
|
quay.expires-after=12w
|
||||||
|
|
||||||
- name: Build and push Docker AMD64 image for Push Event
|
- name: Build and push Docker ${{ matrix.name }} image for Push Event
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: docker/Dockerfile
|
file: ${{ matrix.dockerfile }}
|
||||||
platforms: linux/amd64
|
platforms: ${{ matrix.platform }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
build-args: |
|
build-args: |
|
||||||
"release=1"
|
"release=1"
|
||||||
|
|
||||||
- name: Docker meta
|
|
||||||
id: meta-arm64
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: quay.io/invidious/invidious
|
|
||||||
flavor: |
|
|
||||||
suffix=-arm64
|
|
||||||
tags: |
|
|
||||||
type=sha,format=short,prefix={{date 'YYYY.MM.DD'}}-,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
|
||||||
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
|
||||||
labels: |
|
|
||||||
quay.expires-after=12w
|
|
||||||
|
|
||||||
- name: Build and push Docker ARM64 image for Push Event
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: docker/Dockerfile.arm64
|
|
||||||
platforms: linux/arm64/v8
|
|
||||||
labels: ${{ steps.meta-arm64.outputs.labels }}
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-arm64.outputs.tags }}
|
|
||||||
build-args: |
|
|
||||||
"release=1"
|
|
||||||
|
55
.github/workflows/build-stable-container.yml
vendored
55
.github/workflows/build-stable-container.yml
vendored
@ -8,17 +8,27 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
platform: linux/amd64
|
||||||
|
name: "AMD64"
|
||||||
|
dockerfile: "docker/Dockerfile"
|
||||||
|
tag_suffix: ""
|
||||||
|
# GitHub doesn't have a ubuntu-latest-arm runner
|
||||||
|
- os: ubuntu-24.04-arm
|
||||||
|
platform: linux/arm64/v8
|
||||||
|
name: "ARM64"
|
||||||
|
dockerfile: "docker/Dockerfile.arm64"
|
||||||
|
tag_suffix: "-arm64"
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
with:
|
|
||||||
platforms: arm64
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
@ -36,46 +46,21 @@ jobs:
|
|||||||
images: quay.io/invidious/invidious
|
images: quay.io/invidious/invidious
|
||||||
flavor: |
|
flavor: |
|
||||||
latest=false
|
latest=false
|
||||||
|
suffix=${{ matrix.tag_suffix }}
|
||||||
tags: |
|
tags: |
|
||||||
type=semver,pattern={{version}}
|
type=semver,pattern={{version}}
|
||||||
type=raw,value=latest
|
type=raw,value=latest
|
||||||
labels: |
|
labels: |
|
||||||
quay.expires-after=12w
|
quay.expires-after=12w
|
||||||
|
|
||||||
- name: Build and push Docker AMD64 image for Push Event
|
- name: Build and push Docker ${{ matrix.name }} image for Push Event
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: docker/Dockerfile
|
file: ${{ matrix.dockerfile }}
|
||||||
platforms: linux/amd64
|
platforms: ${{ matrix.platform }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
build-args: |
|
build-args: |
|
||||||
"release=1"
|
"release=1"
|
||||||
|
|
||||||
- name: Docker meta
|
|
||||||
id: meta-arm64
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: quay.io/invidious/invidious
|
|
||||||
flavor: |
|
|
||||||
latest=false
|
|
||||||
suffix=-arm64
|
|
||||||
tags: |
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=raw,value=latest
|
|
||||||
labels: |
|
|
||||||
quay.expires-after=12w
|
|
||||||
|
|
||||||
- name: Build and push Docker ARM64 image for Push Event
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: docker/Dockerfile.arm64
|
|
||||||
platforms: linux/arm64/v8
|
|
||||||
labels: ${{ steps.meta-arm64.outputs.labels }}
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-arm64.outputs.tags }}
|
|
||||||
build-args: |
|
|
||||||
"release=1"
|
|
||||||
|
62
.github/workflows/ci.yml
vendored
62
.github/workflows/ci.yml
vendored
@ -38,10 +38,11 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
stable: [true]
|
stable: [true]
|
||||||
crystal:
|
crystal:
|
||||||
- 1.12.1
|
- 1.12.2
|
||||||
- 1.13.2
|
- 1.13.3
|
||||||
- 1.14.0
|
- 1.14.1
|
||||||
- 1.15.0
|
- 1.15.1
|
||||||
|
- 1.16.3
|
||||||
include:
|
include:
|
||||||
- crystal: nightly
|
- crystal: nightly
|
||||||
stable: false
|
stable: false
|
||||||
@ -82,46 +83,43 @@ jobs:
|
|||||||
run: crystal build --warnings all --error-on-warnings --error-trace src/invidious.cr
|
run: crystal build --warnings all --error-on-warnings --error-trace src/invidious.cr
|
||||||
|
|
||||||
build-docker:
|
build-docker:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
name: "AMD64"
|
||||||
|
# GitHub doesn't have a ubuntu-latest-arm runner
|
||||||
|
- os: ubuntu-24.04-arm
|
||||||
|
name: "ARM64"
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
name: Test ${{ matrix.name }} Docker build
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Use ARM64 Dockerfile if ARM64
|
||||||
|
if: ${{ matrix.name }} == "ARM64"
|
||||||
|
run: sed -i 's/Dockerfile/Dockerfile.arm64/' docker-compose.yml
|
||||||
|
|
||||||
- name: Build Docker
|
- name: Build Docker
|
||||||
run: docker compose build --build-arg release=0
|
run: docker compose build
|
||||||
|
|
||||||
|
- name: Change hmac_key on docker-compose.yml
|
||||||
|
run: sed -i '/hmac_key/s/CHANGE_ME!!/docker-build-hmac-key/' docker-compose.yml
|
||||||
|
|
||||||
- name: Run Docker
|
- name: Run Docker
|
||||||
run: docker compose up -d
|
run: docker compose up -d
|
||||||
|
|
||||||
- name: Test Docker
|
- name: Test Docker
|
||||||
run: while curl -Isf http://localhost:3000; do sleep 1; done
|
id: test
|
||||||
|
run: curl -If http://localhost:3000 --retry 5 --retry-delay 1 --retry-all-errors
|
||||||
|
|
||||||
build-docker-arm64:
|
- name: Print Invidious container logs
|
||||||
|
# Tells Github Actions to always run this step regardless of whether the previous step has failed
|
||||||
runs-on: ubuntu-latest
|
# Without this expression this step would simply be skipped when the previous step fails.
|
||||||
|
if: success() || steps.test.conclusion == 'failure'
|
||||||
steps:
|
run: docker compose logs
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
with:
|
|
||||||
platforms: arm64
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Build Docker ARM64 image
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: docker/Dockerfile.arm64
|
|
||||||
platforms: linux/arm64/v8
|
|
||||||
build-args: release=0
|
|
||||||
|
|
||||||
- name: Test Docker
|
|
||||||
run: while curl -Isf http://localhost:3000; do sleep 1; done
|
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ https_only: false
|
|||||||
#disable_proxy: false
|
#disable_proxy: false
|
||||||
|
|
||||||
##
|
##
|
||||||
## Max size of the HTTP pool used to connect to youtube. Each
|
## Size of the HTTP pool used to connect to youtube. Each
|
||||||
## domain ('youtube.com', 'ytimg.com', ...) has its own pool.
|
## domain ('youtube.com', 'ytimg.com', ...) has its own pool.
|
||||||
##
|
##
|
||||||
## Accepted values: a positive integer
|
## Accepted values: a positive integer
|
||||||
@ -214,16 +214,6 @@ https_only: false
|
|||||||
##
|
##
|
||||||
#pool_size: 100
|
#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.
|
## Additional cookies to be sent when requesting the youtube API.
|
||||||
|
@ -14,6 +14,10 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:3000:3000"
|
- "127.0.0.1:3000:3000"
|
||||||
|
depends_on:
|
||||||
|
invidious-db:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: true
|
||||||
environment:
|
environment:
|
||||||
# Please read the following file for a comprehensive list of all available
|
# Please read the following file for a comprehensive list of all available
|
||||||
# configuration options and their associated syntax:
|
# configuration options and their associated syntax:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM crystallang/crystal:1.16.2-alpine AS builder
|
FROM crystallang/crystal:1.16.3-alpine AS builder
|
||||||
|
|
||||||
RUN apk add --no-cache sqlite-static yaml-static
|
RUN apk add --no-cache sqlite-static yaml-static
|
||||||
|
|
||||||
|
@ -1,112 +0,0 @@
|
|||||||
# 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
|
|
@ -1,15 +0,0 @@
|
|||||||
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,7 +35,6 @@ require "protodec/utils"
|
|||||||
|
|
||||||
require "./invidious/database/*"
|
require "./invidious/database/*"
|
||||||
require "./invidious/database/migrations/*"
|
require "./invidious/database/migrations/*"
|
||||||
require "./invidious/connection/*"
|
|
||||||
require "./invidious/http_server/*"
|
require "./invidious/http_server/*"
|
||||||
require "./invidious/helpers/*"
|
require "./invidious/helpers/*"
|
||||||
require "./invidious/yt_backend/*"
|
require "./invidious/yt_backend/*"
|
||||||
@ -92,31 +91,15 @@ SOFTWARE = {
|
|||||||
"branch" => "#{CURRENT_BRANCH}",
|
"branch" => "#{CURRENT_BRANCH}",
|
||||||
}
|
}
|
||||||
|
|
||||||
YT_POOL = Invidious::ConnectionPool::Pool.new(
|
YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
|
||||||
max_capacity: CONFIG.pool_size,
|
|
||||||
timeout: CONFIG.pool_checkout_timeout
|
|
||||||
) do
|
|
||||||
next make_client(YT_URL, force_resolve: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Image request pool
|
# Image request pool
|
||||||
|
|
||||||
GGPHT_URL = URI.parse("https://yt3.ggpht.com")
|
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
|
||||||
|
|
||||||
GGPHT_POOL = Invidious::ConnectionPool::Pool.new(
|
COMPANION_POOL = CompanionConnectionPool.new(
|
||||||
max_capacity: CONFIG.pool_size,
|
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
|
# CLI
|
||||||
Kemal.config.extra_options do |parser|
|
Kemal.config.extra_options do |parser|
|
||||||
|
@ -166,7 +166,7 @@ def fetch_channel(ucid, pull_all_videos : Bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : Downloading RSS feed")
|
LOGGER.trace("fetch_channel: #{ucid} : Downloading RSS feed")
|
||||||
rss = YT_POOL.get("/feeds/videos.xml?channel_id=#{ucid}").body
|
rss = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}").body
|
||||||
LOGGER.trace("fetch_channel: #{ucid} : Parsing RSS feed")
|
LOGGER.trace("fetch_channel: #{ucid} : Parsing RSS feed")
|
||||||
rss = XML.parse(rss)
|
rss = XML.parse(rss)
|
||||||
|
|
||||||
|
@ -157,13 +157,8 @@ class Config
|
|||||||
property host_binding : String = "0.0.0.0"
|
property host_binding : String = "0.0.0.0"
|
||||||
# Path and permissions to make Invidious listen on a UNIX socket instead of a TCP port
|
# Path and permissions to make Invidious listen on a UNIX socket instead of a TCP port
|
||||||
property socket_binding : SocketBindingConfig? = nil
|
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
|
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
|
# HTTP Proxy configuration
|
||||||
property http_proxy : HTTPProxyConfig? = nil
|
property http_proxy : HTTPProxyConfig? = nil
|
||||||
|
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
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"
|
|
||||||
|
|
||||||
request.headers["Accept-Charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
|
|
||||||
request.headers["Accept"] ||= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
||||||
request.headers["Accept-Language"] ||= "en-us,en;q=0.5"
|
|
||||||
|
|
||||||
# Preserve original cookies and add new YT consent cookie for EU servers
|
|
||||||
request.headers["Cookie"] = "#{request.headers["cookie"]?}; CONSENT=PENDING+#{Random.rand(100..999)}"
|
|
||||||
if !CONFIG.cookies.empty?
|
|
||||||
request.headers["Cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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 && use_http_proxy
|
|
||||||
|
|
||||||
# Force the usage of a specific configured IP Family
|
|
||||||
if force_resolve
|
|
||||||
client.family = CONFIG.force_resolve
|
|
||||||
client.family = Socket::Family::INET if client.family == Socket::Family::UNSPEC
|
|
||||||
end
|
|
||||||
|
|
||||||
client.before_request { |r| add_yt_headers(r) } if url.host.try &.ends_with?("youtube.com") || force_youtube_headers
|
|
||||||
client.read_timeout = 10.seconds
|
|
||||||
client.connect_timeout = 10.seconds
|
|
||||||
|
|
||||||
return client
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
client.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def make_configured_http_proxy_client
|
|
||||||
# This method is only called when configuration for an HTTP proxy are set
|
|
||||||
config_proxy = CONFIG.http_proxy.not_nil!
|
|
||||||
|
|
||||||
return HTTP::Proxy::Client.new(
|
|
||||||
config_proxy.host,
|
|
||||||
config_proxy.port,
|
|
||||||
|
|
||||||
username: config_proxy.user,
|
|
||||||
password: config_proxy.password,
|
|
||||||
)
|
|
||||||
end
|
|
@ -1,116 +0,0 @@
|
|||||||
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
|
|
@ -26,7 +26,7 @@ def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
|
|||||||
end
|
end
|
||||||
|
|
||||||
video_id = "CvFH_6DNRCY" if rdid.starts_with? "OLAK5uy_"
|
video_id = "CvFH_6DNRCY" if rdid.starts_with? "OLAK5uy_"
|
||||||
response = YT_POOL.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en", headers)
|
response = YT_POOL.client &.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en", headers)
|
||||||
initial_data = extract_initial_data(response.body)
|
initial_data = extract_initial_data(response.body)
|
||||||
|
|
||||||
if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
|
if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
|
||||||
|
@ -26,7 +26,7 @@ module Invidious::Routes::API::Manifest
|
|||||||
end
|
end
|
||||||
|
|
||||||
if dashmpd = video.dash_manifest_url
|
if dashmpd = video.dash_manifest_url
|
||||||
response = YT_POOL.get(URI.parse(dashmpd).request_target)
|
response = YT_POOL.client &.get(URI.parse(dashmpd).request_target)
|
||||||
|
|
||||||
if response.status_code != 200
|
if response.status_code != 200
|
||||||
haltf env, status_code: response.status_code
|
haltf env, status_code: response.status_code
|
||||||
@ -167,7 +167,7 @@ module Invidious::Routes::API::Manifest
|
|||||||
|
|
||||||
# /api/manifest/hls_playlist/*
|
# /api/manifest/hls_playlist/*
|
||||||
def self.get_hls_playlist(env)
|
def self.get_hls_playlist(env)
|
||||||
response = YT_POOL.get(env.request.path)
|
response = YT_POOL.client &.get(env.request.path)
|
||||||
|
|
||||||
if response.status_code != 200
|
if response.status_code != 200
|
||||||
haltf env, status_code: response.status_code
|
haltf env, status_code: response.status_code
|
||||||
@ -223,7 +223,7 @@ module Invidious::Routes::API::Manifest
|
|||||||
|
|
||||||
# /api/manifest/hls_variant/*
|
# /api/manifest/hls_variant/*
|
||||||
def self.get_hls_variant(env)
|
def self.get_hls_variant(env)
|
||||||
response = YT_POOL.get(env.request.path)
|
response = YT_POOL.client &.get(env.request.path)
|
||||||
|
|
||||||
if response.status_code != 200
|
if response.status_code != 200
|
||||||
haltf env, status_code: response.status_code
|
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,
|
# 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
|
# as well as some other markup that makes it cumbersome, so we try to fix that here
|
||||||
if caption.name.includes? "auto-generated"
|
if caption.name.includes? "auto-generated"
|
||||||
caption_xml = YT_POOL.get(url).body
|
caption_xml = YT_POOL.client &.get(url).body
|
||||||
|
|
||||||
settings_field = {
|
settings_field = {
|
||||||
"Kind" => "captions",
|
"Kind" => "captions",
|
||||||
@ -147,7 +147,7 @@ module Invidious::Routes::API::V1::Videos
|
|||||||
query_params = uri.query_params
|
query_params = uri.query_params
|
||||||
query_params["fmt"] = "vtt"
|
query_params["fmt"] = "vtt"
|
||||||
uri.query_params = query_params
|
uri.query_params = query_params
|
||||||
webvtt = YT_POOL.get(uri.request_target).body
|
webvtt = YT_POOL.client &.get(uri.request_target).body
|
||||||
|
|
||||||
if webvtt.starts_with?("<?xml")
|
if webvtt.starts_with?("<?xml")
|
||||||
webvtt = caption.timedtext_to_vtt(webvtt)
|
webvtt = caption.timedtext_to_vtt(webvtt)
|
||||||
@ -300,7 +300,7 @@ module Invidious::Routes::API::V1::Videos
|
|||||||
cache_annotation(id, annotations)
|
cache_annotation(id, annotations)
|
||||||
end
|
end
|
||||||
else # "youtube"
|
else # "youtube"
|
||||||
response = YT_POOL.get("/annotations_invideo?video_id=#{id}")
|
response = YT_POOL.client &.get("/annotations_invideo?video_id=#{id}")
|
||||||
|
|
||||||
if response.status_code != 200
|
if response.status_code != 200
|
||||||
haltf env, response.status_code
|
haltf env, response.status_code
|
||||||
|
@ -392,7 +392,7 @@ module Invidious::Routes::Channels
|
|||||||
value = env.request.resource.split("/")[2]
|
value = env.request.resource.split("/")[2]
|
||||||
body = ""
|
body = ""
|
||||||
{"channel", "user", "c"}.each do |type|
|
{"channel", "user", "c"}.each do |type|
|
||||||
response = YT_POOL.get("/#{type}/#{value}/live?disable_polymer=1")
|
response = YT_POOL.client &.get("/#{type}/#{value}/live?disable_polymer=1")
|
||||||
if response.status_code == 200
|
if response.status_code == 200
|
||||||
body = response.body
|
body = response.body
|
||||||
end
|
end
|
||||||
|
@ -92,7 +92,7 @@ module Invidious::Routes::Embed
|
|||||||
|
|
||||||
return env.redirect url
|
return env.redirect url
|
||||||
when "live_stream"
|
when "live_stream"
|
||||||
response = YT_POOL.get("/embed/live_stream?channel=#{env.params.query["channel"]? || ""}")
|
response = YT_POOL.client &.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"]
|
video_id = response.body.match(/"video_id":"(?<video_id>[a-zA-Z0-9_-]{11})"/).try &.["video_id"]
|
||||||
|
|
||||||
env.params.query.delete_all("channel")
|
env.params.query.delete_all("channel")
|
||||||
|
@ -9,10 +9,10 @@ module Invidious::Routes::ErrorRoutes
|
|||||||
item = md["id"]
|
item = md["id"]
|
||||||
|
|
||||||
# Check if item is branding URL e.g. https://youtube.com/gaming
|
# Check if item is branding URL e.g. https://youtube.com/gaming
|
||||||
response = YT_POOL.get("/#{item}")
|
response = YT_POOL.client &.get("/#{item}")
|
||||||
|
|
||||||
if response.status_code == 301
|
if response.status_code == 301
|
||||||
response = YT_POOL.get(URI.parse(response.headers["Location"]).request_target)
|
response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target)
|
||||||
end
|
end
|
||||||
|
|
||||||
if response.body.empty?
|
if response.body.empty?
|
||||||
@ -40,7 +40,7 @@ module Invidious::Routes::ErrorRoutes
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Check if item is video ID
|
# Check if item is video ID
|
||||||
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.head("/watch?v=#{item}").status_code != 404
|
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404
|
||||||
env.response.headers["Location"] = url
|
env.response.headers["Location"] = url
|
||||||
haltf env, status_code: 302
|
haltf env, status_code: 302
|
||||||
end
|
end
|
||||||
|
@ -160,9 +160,8 @@ module Invidious::Routes::Feeds
|
|||||||
"default" => "http://www.w3.org/2005/Atom",
|
"default" => "http://www.w3.org/2005/Atom",
|
||||||
}
|
}
|
||||||
|
|
||||||
response = YT_POOL.get("/feeds/videos.xml?channel_id=#{ucid}")
|
response = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{ucid}")
|
||||||
return error_atom(404, NotFoundException.new("Channel does not exist.")) if response.status_code == 404
|
return error_atom(404, NotFoundException.new("Channel does not exist.")) if response.status_code == 404
|
||||||
|
|
||||||
rss = XML.parse(response.body)
|
rss = XML.parse(response.body)
|
||||||
|
|
||||||
videos = rss.xpath_nodes("//default:feed/default:entry", namespaces).map do |entry|
|
videos = rss.xpath_nodes("//default:feed/default:entry", namespaces).map do |entry|
|
||||||
@ -305,7 +304,7 @@ module Invidious::Routes::Feeds
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
response = YT_POOL.get("/feeds/videos.xml?playlist_id=#{plid}")
|
response = YT_POOL.client &.get("/feeds/videos.xml?playlist_id=#{plid}")
|
||||||
return error_atom(404, NotFoundException.new("Playlist does not exist.")) if response.status_code == 404
|
return error_atom(404, NotFoundException.new("Playlist does not exist.")) if response.status_code == 404
|
||||||
|
|
||||||
document = XML.parse(response.body)
|
document = XML.parse(response.body)
|
||||||
|
@ -12,7 +12,7 @@ module Invidious::Routes::Images
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
GGPHT_POOL.get(url, headers) do |resp|
|
GGPHT_POOL.client &.get(url, headers) do |resp|
|
||||||
return self.proxy_image(env, resp)
|
return self.proxy_image(env, resp)
|
||||||
end
|
end
|
||||||
rescue ex
|
rescue ex
|
||||||
@ -42,7 +42,7 @@ module Invidious::Routes::Images
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConnectionPool.get_ytimg_pool(authority).get(url, headers) do |resp|
|
get_ytimg_pool(authority).client &.get(url, headers) do |resp|
|
||||||
env.response.headers["Connection"] = "close"
|
env.response.headers["Connection"] = "close"
|
||||||
return self.proxy_image(env, resp)
|
return self.proxy_image(env, resp)
|
||||||
end
|
end
|
||||||
@ -65,7 +65,7 @@ module Invidious::Routes::Images
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConnectionPool.get_ytimg_pool("i9").get(url, headers) do |resp|
|
get_ytimg_pool("i9").client &.get(url, headers) do |resp|
|
||||||
return self.proxy_image(env, resp)
|
return self.proxy_image(env, resp)
|
||||||
end
|
end
|
||||||
rescue ex
|
rescue ex
|
||||||
@ -81,7 +81,7 @@ module Invidious::Routes::Images
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
YT_POOL.get(env.request.resource, headers) do |response|
|
YT_POOL.client &.get(env.request.resource, headers) do |response|
|
||||||
env.response.status_code = response.status_code
|
env.response.status_code = response.status_code
|
||||||
response.headers.each do |key, value|
|
response.headers.each do |key, value|
|
||||||
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
|
||||||
@ -111,7 +111,7 @@ module Invidious::Routes::Images
|
|||||||
if name == "maxres.jpg"
|
if name == "maxres.jpg"
|
||||||
build_thumbnails(id).each do |thumb|
|
build_thumbnails(id).each do |thumb|
|
||||||
thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg"
|
thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg"
|
||||||
if ConnectionPool.get_ytimg_pool("i").head(thumbnail_resource_path, headers).status_code == 200
|
if get_ytimg_pool("i").client &.head(thumbnail_resource_path, headers).status_code == 200
|
||||||
name = thumb[:url] + ".jpg"
|
name = thumb[:url] + ".jpg"
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -127,7 +127,7 @@ module Invidious::Routes::Images
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConnectionPool.get_ytimg_pool("i").get(url, headers) do |resp|
|
get_ytimg_pool("i").client &.get(url, headers) do |resp|
|
||||||
return self.proxy_image(env, resp)
|
return self.proxy_image(env, resp)
|
||||||
end
|
end
|
||||||
rescue ex
|
rescue ex
|
||||||
|
@ -464,7 +464,7 @@ module Invidious::Routes::Playlists
|
|||||||
|
|
||||||
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
|
# Undocumented, creates anonymous playlist with specified 'video_ids', max 50 videos
|
||||||
def self.watch_videos(env)
|
def self.watch_videos(env)
|
||||||
response = YT_POOL.get(env.request.resource)
|
response = YT_POOL.client &.get(env.request.resource)
|
||||||
if url = response.headers["Location"]?
|
if url = response.headers["Location"]?
|
||||||
url = URI.parse(url).request_target
|
url = URI.parse(url).request_target
|
||||||
return env.redirect url
|
return env.redirect url
|
||||||
|
@ -16,11 +16,11 @@ module Invidious::Search
|
|||||||
# Search a youtube channel
|
# Search a youtube channel
|
||||||
# TODO: clean code, and rely more on YoutubeAPI
|
# TODO: clean code, and rely more on YoutubeAPI
|
||||||
def channel(query : Query) : Array(SearchItem)
|
def channel(query : Query) : Array(SearchItem)
|
||||||
response = YT_POOL.get("/channel/#{query.channel}")
|
response = YT_POOL.client &.get("/channel/#{query.channel}")
|
||||||
|
|
||||||
if response.status_code == 404
|
if response.status_code == 404
|
||||||
response = YT_POOL.get("/user/#{query.channel}")
|
response = YT_POOL.client &.get("/user/#{query.channel}")
|
||||||
response = YT_POOL.get("/c/#{query.channel}") if response.status_code == 404
|
response = YT_POOL.client &.get("/c/#{query.channel}") if response.status_code == 404
|
||||||
initial_data = extract_initial_data(response.body)
|
initial_data = extract_initial_data(response.body)
|
||||||
ucid = initial_data.dig?("header", "c4TabbedHeaderRenderer", "channelId").try(&.as_s?)
|
ucid = initial_data.dig?("header", "c4TabbedHeaderRenderer", "channelId").try(&.as_s?)
|
||||||
raise ChannelSearchException.new(query.channel) if !ucid
|
raise ChannelSearchException.new(query.channel) if !ucid
|
||||||
|
153
src/invidious/yt_backend/connection_pool.cr
Normal file
153
src/invidious/yt_backend/connection_pool.cr
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
# 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
|
||||||
|
|
||||||
|
struct CompanionConnectionPool
|
||||||
|
property pool : DB::Pool(HTTP::Client)
|
||||||
|
|
||||||
|
def initialize(capacity = 5, timeout = 5.0)
|
||||||
|
options = DB::Pool::Options.new(
|
||||||
|
initial_pool_size: 0,
|
||||||
|
max_pool_size: capacity,
|
||||||
|
max_idle_pool_size: capacity,
|
||||||
|
checkout_timeout: timeout
|
||||||
|
)
|
||||||
|
|
||||||
|
@pool = DB::Pool(HTTP::Client).new(options) do
|
||||||
|
companion = CONFIG.invidious_companion.sample
|
||||||
|
next make_client(companion.private_url, use_http_proxy: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def client(&)
|
||||||
|
conn = pool.checkout
|
||||||
|
|
||||||
|
begin
|
||||||
|
response = yield conn
|
||||||
|
rescue ex
|
||||||
|
conn.close
|
||||||
|
|
||||||
|
companion = CONFIG.invidious_companion.sample
|
||||||
|
conn = make_client(companion.private_url, use_http_proxy: false)
|
||||||
|
|
||||||
|
response = yield conn
|
||||||
|
ensure
|
||||||
|
pool.release(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
response
|
||||||
|
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"
|
||||||
|
|
||||||
|
request.headers["Accept-Charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
|
||||||
|
request.headers["Accept"] ||= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
||||||
|
request.headers["Accept-Language"] ||= "en-us,en;q=0.5"
|
||||||
|
|
||||||
|
# Preserve original cookies and add new YT consent cookie for EU servers
|
||||||
|
request.headers["Cookie"] = "#{request.headers["cookie"]?}; CONSENT=PENDING+#{Random.rand(100..999)}"
|
||||||
|
if !CONFIG.cookies.empty?
|
||||||
|
request.headers["Cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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 && use_http_proxy
|
||||||
|
|
||||||
|
# Force the usage of a specific configured IP Family
|
||||||
|
if force_resolve
|
||||||
|
client.family = CONFIG.force_resolve
|
||||||
|
client.family = Socket::Family::INET if client.family == Socket::Family::UNSPEC
|
||||||
|
end
|
||||||
|
|
||||||
|
client.before_request { |r| add_yt_headers(r) } if url.host.try &.ends_with?("youtube.com") || force_youtube_headers
|
||||||
|
client.read_timeout = 10.seconds
|
||||||
|
client.connect_timeout = 10.seconds
|
||||||
|
|
||||||
|
return client
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
client.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_configured_http_proxy_client
|
||||||
|
# This method is only called when configuration for an HTTP proxy are set
|
||||||
|
config_proxy = CONFIG.http_proxy.not_nil!
|
||||||
|
|
||||||
|
return HTTP::Proxy::Client.new(
|
||||||
|
config_proxy.host,
|
||||||
|
config_proxy.port,
|
||||||
|
|
||||||
|
username: config_proxy.user,
|
||||||
|
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
|
@ -639,7 +639,8 @@ module YoutubeAPI
|
|||||||
LOGGER.trace("YoutubeAPI: POST data: #{data}")
|
LOGGER.trace("YoutubeAPI: POST data: #{data}")
|
||||||
|
|
||||||
# Send the POST request
|
# Send the POST request
|
||||||
body = YT_POOL.post(url, headers: headers, body: data.to_json) do |response|
|
body = YT_POOL.client() do |client|
|
||||||
|
client.post(url, headers: headers, body: data.to_json) do |response|
|
||||||
if response.status_code != 200
|
if response.status_code != 200
|
||||||
raise InfoException.new("Error: non 200 status code. Youtube API returned \
|
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/\"> \
|
status code #{response.status_code}. See <a href=\"https://docs.invidious.io/youtube-errors-explained/\"> \
|
||||||
@ -647,6 +648,7 @@ module YoutubeAPI
|
|||||||
end
|
end
|
||||||
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
|
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Convert result to Hash
|
# Convert result to Hash
|
||||||
initial_data = JSON.parse(body).as_h
|
initial_data = JSON.parse(body).as_h
|
||||||
@ -693,7 +695,7 @@ module YoutubeAPI
|
|||||||
# Send the POST request
|
# Send the POST request
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = COMPANION_POOL.post(endpoint, headers: headers, body: data.to_json)
|
response = COMPANION_POOL.client &.post(endpoint, headers: headers, body: data.to_json)
|
||||||
body = response.body
|
body = response.body
|
||||||
if (response.status_code != 200)
|
if (response.status_code != 200)
|
||||||
raise Exception.new(
|
raise Exception.new(
|
||||||
|
Reference in New Issue
Block a user