about summary refs log tree commit diff
path: root/app/lib/request.rb
diff options
context:
space:
mode:
authorMIYAGI Hikaru <hcmiya@users.noreply.github.com>2018-04-25 09:14:49 +0900
committerEugen Rochko <eugen@zeonfederated.com>2018-04-25 02:14:49 +0200
commitf58dcbc9814b5ba2fd4f7d7af643aa25dcf40594 (patch)
tree5ab7dd9b27f6efa1c84c0ed579d8a8691b348ef7 /app/lib/request.rb
parent9d4710ed0059b2f789e6b32b9f81d4ce90b98907 (diff)
HTTP proxy support for outgoing request, manage access to hidden service (#7134)
* Add support for HTTP client proxy

* Add access control for darknet

Supress error when access to darknet via transparent proxy

* Fix the codes pointed out

* Lint

* Fix an omission + lint

* any? -> include?

* Change detection method to regexp to avoid test fail
Diffstat (limited to 'app/lib/request.rb')
-rw-r--r--app/lib/request.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/app/lib/request.rb b/app/lib/request.rb
index dca93a6e9..0acd654da 100644
--- a/app/lib/request.rb
+++ b/app/lib/request.rb
@@ -11,9 +11,10 @@ class Request
   def initialize(verb, url, **options)
     @verb    = verb
     @url     = Addressable::URI.parse(url).normalize
-    @options = options.merge(socket_class: Socket)
+    @options = options.merge(use_proxy? ? Rails.configuration.x.http_client_proxy : { socket_class: Socket })
     @headers = {}
 
+    raise Mastodon::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service?
     set_common_headers!
     set_digest! if options.key?(:body)
   end
@@ -99,6 +100,14 @@ class Request
     @http_client ||= HTTP.timeout(:per_operation, timeout).follow(max_hops: 2)
   end
 
+  def use_proxy?
+    Rails.configuration.x.http_client_proxy.present?
+  end
+
+  def block_hidden_service?
+    !Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match(@url.host)
+  end
+
   module ClientLimit
     def body_with_limit(limit = 1.megabyte)
       raise Mastodon::LengthValidationError if content_length.present? && content_length > limit
@@ -129,6 +138,7 @@ class Request
   class Socket < TCPSocket
     class << self
       def open(host, *args)
+        return super host, *args if thru_hidden_service? host
         outer_e = nil
         Addrinfo.foreach(host, nil, nil, :SOCK_STREAM) do |address|
           begin
@@ -142,6 +152,10 @@ class Request
       end
 
       alias new open
+
+      def thru_hidden_service?(host)
+        Rails.configuration.x.hidden_service_via_transparent_proxy && /\.(onion|i2p)$/.match(host)
+      end
     end
   end