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 })
|
use(cache: { expires_in: expires_in })
|
||||||
end
|
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.
|
# allow requests only to public IPs, not to local or private networks.
|
||||||
def public_only
|
def public_only
|
||||||
dup.tap do |o|
|
dup.tap do |o|
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ module Sources
|
|||||||
|
|
||||||
# A http client for API requests.
|
# A http client for API requests.
|
||||||
def http
|
def http
|
||||||
Danbooru::Http.new.public_only
|
Danbooru::Http.new.proxy.public_only
|
||||||
end
|
end
|
||||||
memoize :http
|
memoize :http
|
||||||
|
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
def api_client
|
def api_client
|
||||||
PixivAjaxClient.new(Danbooru.config.pixiv_phpsessid)
|
PixivAjaxClient.new(Danbooru.config.pixiv_phpsessid, http: http)
|
||||||
end
|
end
|
||||||
|
|
||||||
def api_illust
|
def api_illust
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
class TwitterApiClient
|
class TwitterApiClient
|
||||||
extend Memoist
|
extend Memoist
|
||||||
|
|
||||||
attr_reader :api_key, :api_secret
|
attr_reader :api_key, :api_secret, :http
|
||||||
|
|
||||||
# Create a Twitter API client
|
# Create a Twitter API client
|
||||||
# @param api_key [String] the Twitter API key
|
# @param api_key [String] the Twitter API key
|
||||||
# @param api_secret [String] the Twitter API secret
|
# @param api_secret [String] the Twitter API secret
|
||||||
def initialize(api_key, api_secret)
|
def initialize(api_key, api_secret, http: Danbooru::Http.new)
|
||||||
@api_key, @api_secret = api_key, api_secret
|
@api_key, @api_secret, @http = api_key, api_secret, http
|
||||||
end
|
end
|
||||||
|
|
||||||
# Authenticate to Twitter with an API key and secret and receive a bearer token in response.
|
# 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
|
# @return [String] the Twitter bearer token
|
||||||
# @see https://developer.twitter.com/en/docs/authentication/api-reference/token
|
# @see https://developer.twitter.com/en/docs/authentication/api-reference/token
|
||||||
def bearer_token(token_expiry = 24.hours)
|
def bearer_token(token_expiry = 24.hours)
|
||||||
http = Danbooru::Http.basic_auth(user: api_key, pass: api_secret)
|
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 = http.cache(token_expiry).post("https://api.twitter.com/oauth2/token", form: { grant_type: :client_credentials })
|
|
||||||
response.parse["access_token"]
|
response.parse["access_token"]
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Danbooru::Http] the HTTP client to connect to Twitter with
|
# @return [Danbooru::Http] the HTTP client to connect to Twitter with
|
||||||
def client
|
def client
|
||||||
Danbooru::Http.auth("Bearer #{bearer_token}")
|
http.auth("Bearer #{bearer_token}")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetch a tweet by id.
|
# Fetch a tweet by id.
|
||||||
|
|||||||
@@ -494,6 +494,18 @@ module Danbooru
|
|||||||
false
|
false
|
||||||
end
|
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).
|
# The URL for the Reportbooru server (https://github.com/evazion/reportbooru).
|
||||||
# Optional. Used for tracking post views, popular searches, and missed searches.
|
# Optional. Used for tracking post views, popular searches, and missed searches.
|
||||||
# Set to http://localhost/mock/reportbooru to enable a fake reportbooru
|
# Set to http://localhost/mock/reportbooru to enable a fake reportbooru
|
||||||
|
|||||||
Reference in New Issue
Block a user