Skip to content

Commit

Permalink
Add second proxy setting
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo authored and atsu1125 committed Apr 17, 2024
1 parent 74d50ee commit 0a5e6de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/lib/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def initialize(verb, url, **options)
@http_client = options.delete(:http_client)
@options = options.merge(socket_class: use_proxy? ? ProxySocket : Socket)
@options = @options.merge(Rails.configuration.x.http_client_proxy) if use_proxy?
@options = @options.merge(Rails.configuration.x.http_client_second_proxy) if use_second_proxy?
@headers = {}

raise Mastodon::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service?
Expand Down Expand Up @@ -181,6 +182,10 @@ def use_proxy?
Rails.configuration.x.http_client_proxy.present?
end

def use_second_proxy?
Rails.configuration.x.http_client_second_proxy.present? && Rails.configuration.x.domain_to_use_second_proxy.include?(Addressable::URI.parse(@url).host)
end

def block_hidden_service?
!Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match?(@url.host)
end
Expand Down
19 changes: 19 additions & 0 deletions config/initializers/http_client_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,24 @@
}.compact
end

if ENV['SECOND_HTTP_PROXY'].present? && ENV['DOMAIN_TO_USE_SECOND_PROXY'].present?
proxy = URI.parse(ENV['SECOND_HTTP_PROXY'])

raise "Unsupported proxy type: #{proxy.scheme}" unless %w(http https).include? proxy.scheme
raise "No proxy host" unless proxy.host

host = proxy.host
host = host[1...-1] if host[0] == '[' # for IPv6 address

config.x.http_client_second_proxy[:proxy] = {
proxy_address: host,
proxy_port: proxy.port,
proxy_username: proxy.user,
proxy_password: proxy.password,
}.compact

config.x.domain_to_use_second_proxy = ENV['DOMAIN_TO_USE_SECOND_PROXY'].split(',')
end

config.x.access_to_hidden_service = ENV['ALLOW_ACCESS_TO_HIDDEN_SERVICE'] == 'true'
end

0 comments on commit 0a5e6de

Please sign in to comment.