Guest User

Untitled

a guest
Mar 8th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class Object
  2. def pose_as(klass)
  3. namespace = klass.to_s.split("::")
  4. klass = namespace.pop
  5.  
  6. if namespace.any?
  7. eval <<-EOT
  8. module ::#{namespace.join("::")}
  9. send(:remove_const, :"#{klass}")
  10. #{klass} = #{self}
  11. end
  12. EOT
  13. else
  14. eval <<-EOT
  15. Object.send(:remove_const, :"#{klass}")
  16. #{klass} = #{self}
  17. EOT
  18. end
  19. end
  20. end
  21.  
  22. require 'net/http'
  23. class ProxiedHttp < Net::HTTP
  24. def self.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
  25.  
  26. if env_proxy = (ENV["HTTP_PROXY"] || ENV["http_proxy"])
  27. uri = URI.parse(env_proxy)
  28. p_addr = uri.host
  29. p_port = uri.port
  30. if p_user = (ENV["HTTP_PROXY_USER"] || ENV["http_proxy_user"])
  31. p_pass = (ENV["HTTP_PROXY_PASSWORD"] || ENV["http_proxy_password"])
  32. end
  33. end
  34.  
  35. super(p_addr, p_port, p_user, p_pass)
  36. end
  37. end
  38.  
  39. ProxiedHttp.pose_as(Net::HTTP)
  40.  
  41. from now on Net::HTTP transparently honors ENV["HTTP_PROXY"]
Add Comment
Please, Sign In to add comment