diff --git a/config/config.example.yml b/config/config.example.yml index ff146764..c7a7e1e8 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -1181,6 +1181,17 @@ video_cache: ## #check_backends_interval: true +## +## The endpoint to check if a backend is alive or not. +## If using a the nadeko.net Invidious companion patches you can set it to +## "/status" to check if the Invidious companion instance that is being checked +## is blocked by youtube. +## +## Accepted values: a string +## Default: "/healthz" +## +#check_backends_path: "/healthz" + ## ## Forces the video proxy. ## diff --git a/src/invidious/config.cr b/src/invidious/config.cr index f2d52b2d..75c842b5 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -238,6 +238,8 @@ class Config property check_backends_interval : Int32 = 30 + property check_backends_path : String = "/healthz" + property force_local : Bool = true property disable_livestreams : Bool = true diff --git a/src/invidious/helpers/backend_info.cr b/src/invidious/helpers/backend_info.cr index 2520173f..a610b431 100644 --- a/src/invidious/helpers/backend_info.cr +++ b/src/invidious/helpers/backend_info.cr @@ -3,7 +3,15 @@ module BackendInfo enum Status Dead = 0 - Working = 1 + Blocked = 1 + Working = 2 + end + + struct CompanionData + include JSON::Serializable + property blocked : Bool = false + @[JSON::Field(key: "blockedCount")] + property blocked_count : Int64 = 0 end @@status : Array(Int32) = Array.new(CONFIG.invidious_companion.size, Status::Dead.to_i) @@ -31,11 +39,27 @@ module BackendInfo begin client = HTTP::Client.new(companion.private_url) client.connect_timeout = 10.seconds - response = client.get("/healthz") + response = client.get(CONFIG.check_backends_path) if response.status_code == 200 - @@check_mutex.synchronize do - updated_status[index] = Status::Working.to_i - updated_ends.push(index) + if response.content_type == "application/json" + body = response.body + status_json = CompanionData.from_json(body) + if status_json.blocked + @@check_mutex.synchronize do + updated_status[index] = Status::Blocked.to_i + updated_ends.push(index) + end + else + @@check_mutex.synchronize do + updated_status[index] = Status::Working.to_i + updated_ends.push(index) + end + end + else + @@check_mutex.synchronize do + updated_status[index] = Status::Working.to_i + updated_ends.push(index) + end end generate_csp([companion.public_url, companion.i2p_public_url], index) else diff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecr index 319f9268..faea05d9 100644 --- a/src/invidious/views/template.ecr +++ b/src/invidious/views/template.ecr @@ -125,6 +125,7 @@ <%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %> @@ -139,6 +140,7 @@ <%= HTML.escape(CONFIG.backend_name_prefix + (index + 1).to_s) %> <%= HTML.escape(companion.note) %>