Skip to content

Commit

Permalink
add ability for invidious companion to check request from invidious
Browse files Browse the repository at this point in the history
  • Loading branch information
unixfox committed Dec 13, 2024
1 parent ab72bba commit bce789b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ db:

##
## API key for Invidious companion
## The size of the key needs to be more or equal to 16.
##
## Needed when invidious_companion is configured
##
Expand Down
3 changes: 3 additions & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class Config
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 or more."
exit(1)
end
end

Expand Down
19 changes: 19 additions & 0 deletions src/invidious/helpers/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
date_today = Time.utc.to_unix
encrypted_data = encrypt_ecb_without_salt("#{date_today}|#{data}", CONFIG.invidious_companion_key)
return Base64.urlsafe_encode(encrypted_data)
end
9 changes: 6 additions & 3 deletions src/invidious/views/components/player.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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 = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?)
bitrate = fmt["bitrate"]
mimetype = HTML.escape(fmt["mimeType"].as_s)
Expand All @@ -37,7 +38,8 @@
<% else %>
<% if params.quality == "dash"
src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1"
src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?)
%>
<source src="<%= src_url %>" type='application/dash+xml' label="dash">
<% end %>
Expand All @@ -48,7 +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 = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url +
"&check=#{invidious_companion_encrypt(video.id)}" if (!CONFIG.invidious_companion.empty?)
quality = fmt["quality"]
mimetype = HTML.escape(fmt["mimeType"].as_s)
Expand Down

0 comments on commit bce789b

Please sign in to comment.