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.
This commit is contained in:
@@ -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|
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user