From bb7f24d2793fa2442b37839c9b57bb90fb5f395b Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 28 Aug 2021 01:17:32 -0500 Subject: [PATCH] Add HTTP proxy support. Add support for using a proxy for HTTP requests. Only used for external requests, such as downloading files or talking to source sites such as Pixiv or Twitter, not for internal requests, such as talking to IQDB or Reportbooru. --- app/logical/danbooru/http.rb | 8 ++++++++ app/logical/sources/strategies/base.rb | 2 +- app/logical/sources/strategies/pixiv.rb | 2 +- app/logical/twitter_api_client.rb | 11 +++++------ config/danbooru_default_config.rb | 12 ++++++++++++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/logical/danbooru/http.rb b/app/logical/danbooru/http.rb index 2437ed885..a5f62d90a 100644 --- a/app/logical/danbooru/http.rb +++ b/app/logical/danbooru/http.rb @@ -117,6 +117,14 @@ module Danbooru use(cache: { expires_in: expires_in }) end + def proxy(host: Danbooru.config.http_proxy_host, port: Danbooru.config.http_proxy_port.to_i, username: Danbooru.config.http_proxy_username, password: Danbooru.config.http_proxy_password) + return self if host.blank? + + dup.tap do |o| + o.http = o.http.via(host, port, username, password) + end + end + # allow requests only to public IPs, not to local or private networks. def public_only dup.tap do |o| diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index c8a3f2bc9..4b0e50690 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -231,7 +231,7 @@ module Sources # A http client for API requests. def http - Danbooru::Http.new.public_only + Danbooru::Http.new.proxy.public_only end memoize :http diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 3f89ec70f..fb80f8e6b 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -269,7 +269,7 @@ module Sources end def api_client - PixivAjaxClient.new(Danbooru.config.pixiv_phpsessid) + PixivAjaxClient.new(Danbooru.config.pixiv_phpsessid, http: http) end def api_illust diff --git a/app/logical/twitter_api_client.rb b/app/logical/twitter_api_client.rb index 48e4833eb..c75bcc861 100644 --- a/app/logical/twitter_api_client.rb +++ b/app/logical/twitter_api_client.rb @@ -3,13 +3,13 @@ class TwitterApiClient extend Memoist - attr_reader :api_key, :api_secret + attr_reader :api_key, :api_secret, :http # Create a Twitter API client # @param api_key [String] the Twitter API key # @param api_secret [String] the Twitter API secret - def initialize(api_key, api_secret) - @api_key, @api_secret = api_key, api_secret + def initialize(api_key, api_secret, http: Danbooru::Http.new) + @api_key, @api_secret, @http = api_key, api_secret, http end # Authenticate to Twitter with an API key and secret and receive a bearer token in response. @@ -17,14 +17,13 @@ class TwitterApiClient # @return [String] the Twitter bearer token # @see https://developer.twitter.com/en/docs/authentication/api-reference/token def bearer_token(token_expiry = 24.hours) - http = Danbooru::Http.basic_auth(user: api_key, pass: api_secret) - response = http.cache(token_expiry).post("https://api.twitter.com/oauth2/token", form: { grant_type: :client_credentials }) + response = http.basic_auth(user: api_key, pass: api_secret).cache(token_expiry).post("https://api.twitter.com/oauth2/token", form: { grant_type: :client_credentials }) response.parse["access_token"] end # @return [Danbooru::Http] the HTTP client to connect to Twitter with def client - Danbooru::Http.auth("Bearer #{bearer_token}") + http.auth("Bearer #{bearer_token}") end # Fetch a tweet by id. diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index f52a3bada..4ea40fabd 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -494,6 +494,18 @@ module Danbooru false end + def http_proxy_host + end + + def http_proxy_port + end + + def http_proxy_username + end + + def http_proxy_password + end + # The URL for the Reportbooru server (https://github.com/evazion/reportbooru). # Optional. Used for tracking post views, popular searches, and missed searches. # Set to http://localhost/mock/reportbooru to enable a fake reportbooru