logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

request.rb (3492B)


  1. # frozen_string_literal: true
  2. require 'ipaddr'
  3. require 'socket'
  4. class Request
  5. REQUEST_TARGET = '(request-target)'
  6. include RoutingHelper
  7. def initialize(verb, url, **options)
  8. @verb = verb
  9. @url = Addressable::URI.parse(url).normalize
  10. @options = options.merge(socket_class: Socket)
  11. @headers = {}
  12. set_common_headers!
  13. set_digest! if options.key?(:body)
  14. end
  15. def on_behalf_of(account, key_id_format = :acct)
  16. raise ArgumentError unless account.local?
  17. @account = account
  18. @key_id_format = key_id_format
  19. self
  20. end
  21. def add_headers(new_headers)
  22. @headers.merge!(new_headers)
  23. self
  24. end
  25. def perform
  26. begin
  27. response = http_client.headers(headers).public_send(@verb, @url.to_s, @options)
  28. rescue => e
  29. raise e.class, "#{e.message} on #{@url}", e.backtrace[0]
  30. end
  31. begin
  32. yield response.extend(ClientLimit)
  33. ensure
  34. http_client.close
  35. end
  36. end
  37. def headers
  38. (@account ? @headers.merge('Signature' => signature) : @headers).without(REQUEST_TARGET)
  39. end
  40. private
  41. def set_common_headers!
  42. @headers[REQUEST_TARGET] = "#{@verb} #{@url.path}"
  43. @headers['User-Agent'] = user_agent
  44. @headers['Host'] = @url.host
  45. @headers['Date'] = Time.now.utc.httpdate
  46. end
  47. def set_digest!
  48. @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
  49. end
  50. def signature
  51. algorithm = 'rsa-sha256'
  52. signature = Base64.strict_encode64(@account.keypair.sign(OpenSSL::Digest::SHA256.new, signed_string))
  53. "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers}\",signature=\"#{signature}\""
  54. end
  55. def signed_string
  56. @headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
  57. end
  58. def signed_headers
  59. @headers.keys.join(' ').downcase
  60. end
  61. def user_agent
  62. @user_agent ||= "#{HTTP::Request::USER_AGENT} (Mastodon/#{Mastodon::Version}; +#{root_url})"
  63. end
  64. def key_id
  65. case @key_id_format
  66. when :acct
  67. @account.to_webfinger_s
  68. when :uri
  69. [ActivityPub::TagManager.instance.uri_for(@account), '#main-key'].join
  70. end
  71. end
  72. def timeout
  73. { write: 10, connect: 10, read: 10 }
  74. end
  75. def http_client
  76. @http_client ||= HTTP.timeout(:per_operation, timeout).follow(max_hops: 2)
  77. end
  78. module ClientLimit
  79. def body_with_limit(limit = 1.megabyte)
  80. raise Mastodon::LengthValidationError if content_length.present? && content_length > limit
  81. if charset.nil?
  82. encoding = Encoding::BINARY
  83. else
  84. begin
  85. encoding = Encoding.find(charset)
  86. rescue ArgumentError
  87. encoding = Encoding::BINARY
  88. end
  89. end
  90. contents = String.new(encoding: encoding)
  91. while (chunk = readpartial)
  92. contents << chunk
  93. chunk.clear
  94. raise Mastodon::LengthValidationError if contents.bytesize > limit
  95. end
  96. contents
  97. end
  98. end
  99. class Socket < TCPSocket
  100. class << self
  101. def open(host, *args)
  102. outer_e = nil
  103. Addrinfo.foreach(host, nil, nil, :SOCK_STREAM) do |address|
  104. begin
  105. raise Mastodon::HostValidationError if PrivateAddressCheck.private_address? IPAddr.new(address.ip_address)
  106. return super address.ip_address, *args
  107. rescue => e
  108. outer_e = e
  109. end
  110. end
  111. raise outer_e if outer_e
  112. end
  113. alias new open
  114. end
  115. end
  116. private_constant :ClientLimit, :Socket
  117. end