invidious.cr 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # "Invidious" (which is an alternative front-end to YouTube)
  2. # Copyright (C) 2019 Omar Roth
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published
  6. # by the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. require "digest/md5"
  17. require "file_utils"
  18. # Require kemal, kilt, then our own overrides
  19. require "kemal"
  20. require "kilt"
  21. require "./ext/kemal_content_for.cr"
  22. require "./ext/kemal_static_file_handler.cr"
  23. require "athena-negotiation"
  24. require "openssl/hmac"
  25. require "option_parser"
  26. require "sqlite3"
  27. require "xml"
  28. require "yaml"
  29. require "compress/zip"
  30. require "protodec/utils"
  31. require "./invidious/database/*"
  32. require "./invidious/database/migrations/*"
  33. require "./invidious/http_server/*"
  34. require "./invidious/helpers/*"
  35. require "./invidious/yt_backend/*"
  36. require "./invidious/frontend/*"
  37. require "./invidious/videos/*"
  38. require "./invidious/jsonify/**"
  39. require "./invidious/*"
  40. require "./invidious/comments/*"
  41. require "./invidious/channels/*"
  42. require "./invidious/user/*"
  43. require "./invidious/search/*"
  44. require "./invidious/routes/**"
  45. require "./invidious/jobs/**"
  46. # Declare the base namespace for invidious
  47. module Invidious
  48. end
  49. # Simple alias to make code easier to read
  50. alias IV = Invidious
  51. CONFIG = Config.load
  52. HMAC_KEY = CONFIG.hmac_key
  53. PG_DB = DB.open CONFIG.database_url
  54. ARCHIVE_URL = URI.parse("https://archive.org")
  55. PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
  56. REDDIT_URL = URI.parse("https://www.reddit.com")
  57. YT_URL = URI.parse("https://www.youtube.com")
  58. HOST_URL = make_host_url(Kemal.config)
  59. CHARS_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
  60. TEST_IDS = {"AgbeGFYluEA", "BaW_jenozKc", "a9LDPn-MO4I", "ddFvjfvPnqk", "iqKdEhx-dD4"}
  61. MAX_ITEMS_PER_PAGE = 1500
  62. REQUEST_HEADERS_WHITELIST = {"accept", "accept-encoding", "cache-control", "content-length", "if-none-match", "range"}
  63. RESPONSE_HEADERS_BLACKLIST = {"access-control-allow-origin", "alt-svc", "server"}
  64. HTTP_CHUNK_SIZE = 10485760 # ~10MB
  65. CURRENT_BRANCH = {{ "#{`git branch | sed -n '/* /s///p'`.strip}" }}
  66. CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
  67. CURRENT_VERSION = {{ "#{`git log -1 --format=%ci | awk '{print $1}' | sed s/-/./g`.strip}" }}
  68. # This is used to determine the `?v=` on the end of file URLs (for cache busting). We
  69. # only need to expire modified assets, so we can use this to find the last commit that changes
  70. # any assets
  71. ASSET_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit -- assets`.strip}" }}
  72. SOFTWARE = {
  73. "name" => "invidious",
  74. "version" => "#{CURRENT_VERSION}-#{CURRENT_COMMIT}",
  75. "branch" => "#{CURRENT_BRANCH}",
  76. }
  77. YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
  78. # CLI
  79. Kemal.config.extra_options do |parser|
  80. parser.banner = "Usage: invidious [arguments]"
  81. parser.on("-c THREADS", "--channel-threads=THREADS", "Number of threads for refreshing channels (default: #{CONFIG.channel_threads})") do |number|
  82. begin
  83. CONFIG.channel_threads = number.to_i
  84. rescue ex
  85. puts "THREADS must be integer"
  86. exit
  87. end
  88. end
  89. parser.on("-f THREADS", "--feed-threads=THREADS", "Number of threads for refreshing feeds (default: #{CONFIG.feed_threads})") do |number|
  90. begin
  91. CONFIG.feed_threads = number.to_i
  92. rescue ex
  93. puts "THREADS must be integer"
  94. exit
  95. end
  96. end
  97. parser.on("-o OUTPUT", "--output=OUTPUT", "Redirect output (default: #{CONFIG.output})") do |output|
  98. CONFIG.output = output
  99. end
  100. parser.on("-l LEVEL", "--log-level=LEVEL", "Log level, one of #{LogLevel.values} (default: #{CONFIG.log_level})") do |log_level|
  101. CONFIG.log_level = LogLevel.parse(log_level)
  102. end
  103. parser.on("-v", "--version", "Print version") do
  104. puts SOFTWARE.to_pretty_json
  105. exit
  106. end
  107. parser.on("--migrate", "Run any migrations (beta, use at your own risk!!") do
  108. Invidious::Database::Migrator.new(PG_DB).migrate
  109. exit
  110. end
  111. end
  112. Kemal::CLI.new ARGV
  113. if CONFIG.output.upcase != "STDOUT"
  114. FileUtils.mkdir_p(File.dirname(CONFIG.output))
  115. end
  116. OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mode: "a")
  117. LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)
  118. # Check table integrity
  119. Invidious::Database.check_integrity(CONFIG)
  120. {% if !flag?(:skip_videojs_download) %}
  121. # Resolve player dependencies. This is done at compile time.
  122. #
  123. # Running the script by itself would show some colorful feedback while this doesn't.
  124. # Perhaps we should just move the script to runtime in order to get that feedback?
  125. {% puts "\nChecking player dependencies, this may take more than 20 minutes... If it is stuck, check your internet connection.\n" %}
  126. {% if flag?(:minified_player_dependencies) %}
  127. {% puts run("../scripts/fetch-player-dependencies.cr", "--minified").stringify %}
  128. {% else %}
  129. {% puts run("../scripts/fetch-player-dependencies.cr").stringify %}
  130. {% end %}
  131. {% puts "\nDone checking player dependencies, now compiling Invidious...\n" %}
  132. {% end %}
  133. # Start jobs
  134. if CONFIG.channel_threads > 0
  135. Invidious::Jobs.register Invidious::Jobs::RefreshChannelsJob.new(PG_DB)
  136. end
  137. if CONFIG.feed_threads > 0
  138. Invidious::Jobs.register Invidious::Jobs::RefreshFeedsJob.new(PG_DB)
  139. end
  140. DECRYPT_FUNCTION = DecryptFunction.new(CONFIG.decrypt_polling)
  141. if CONFIG.decrypt_polling
  142. Invidious::Jobs.register Invidious::Jobs::UpdateDecryptFunctionJob.new
  143. end
  144. if CONFIG.statistics_enabled
  145. Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
  146. end
  147. if (CONFIG.use_pubsub_feeds.is_a?(Bool) && CONFIG.use_pubsub_feeds.as(Bool)) || (CONFIG.use_pubsub_feeds.is_a?(Int32) && CONFIG.use_pubsub_feeds.as(Int32) > 0)
  148. Invidious::Jobs.register Invidious::Jobs::SubscribeToFeedsJob.new(PG_DB, HMAC_KEY)
  149. end
  150. if CONFIG.popular_enabled
  151. Invidious::Jobs.register Invidious::Jobs::PullPopularVideosJob.new(PG_DB)
  152. end
  153. CONNECTION_CHANNEL = ::Channel({Bool, ::Channel(PQ::Notification)}).new(32)
  154. Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(CONNECTION_CHANNEL, CONFIG.database_url)
  155. Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new
  156. Invidious::Jobs.start_all
  157. def popular_videos
  158. Invidious::Jobs::PullPopularVideosJob::POPULAR_VIDEOS.get
  159. end
  160. # Routing
  161. before_all do |env|
  162. Invidious::Routes::BeforeAll.handle(env)
  163. end
  164. Invidious::Routing.register_all
  165. error 404 do |env|
  166. Invidious::Routes::ErrorRoutes.error_404(env)
  167. end
  168. error 500 do |env, ex|
  169. error_template(500, ex)
  170. end
  171. static_headers do |response|
  172. response.headers.add("Cache-Control", "max-age=2629800")
  173. end
  174. # Init Kemal
  175. public_folder "assets"
  176. Kemal.config.powered_by_header = false
  177. add_handler FilteredCompressHandler.new
  178. add_handler APIHandler.new
  179. add_handler AuthHandler.new
  180. add_handler DenyFrame.new
  181. add_context_storage_type(Array(String))
  182. add_context_storage_type(Preferences)
  183. add_context_storage_type(Invidious::User)
  184. Kemal.config.logger = LOGGER
  185. Kemal.config.host_binding = Kemal.config.host_binding != "0.0.0.0" ? Kemal.config.host_binding : CONFIG.host_binding
  186. Kemal.config.port = Kemal.config.port != 3000 ? Kemal.config.port : CONFIG.port
  187. Kemal.config.app_name = "Invidious"
  188. # Use in kemal's production mode.
  189. # Users can also set the KEMAL_ENV environmental variable for this to be set automatically.
  190. {% if flag?(:release) || flag?(:production) %}
  191. Kemal.config.env = "production" if !ENV.has_key?("KEMAL_ENV")
  192. {% end %}
  193. Kemal.run