Sha256: dceb894eb8df38e0894a4c6e2e8cba8b5d9499067aa97d6699bf20874c6af205
Contents?: true
Size: 1.43 KB
Versions: 3
Compression:
Stored size: 1.43 KB
Contents
require "socket" require "uri" require "uri/socks" module Proxifier class Proxy class << self def proxify?(host, no_proxy = nil) return true unless no_proxy dont_proxy = no_proxy.split(",") dont_proxy.none? { |h| host =~ /#{h}\Z/ } end end attr_reader :url, :options def initialize(url, options = {}) url = URI.parse(uri) unless url.is_a?(URI::Generic) @url, @options = url, options end def open(host, port, local_host = nil, local_port = nil) return TCPSocket.new(host, port, local_host, local_port) unless proxify?(host) socket = TCPSocket.new(proxy.host, proxy.port, local_host, local_port) begin proxify(socket, host, port) rescue socket.close raise end socket end def proxify?(host) self.class.proxify?(host, options[:no_proxy]) end def proxify(socket, host, port) do_proxify(socket, host, port) end %w(host port user password query version).each do |attr| class_eval "def #{attr}; url.#{attr} end" end def query_options @query_options ||= query ? Hash[query.split("&").map { |q| q.split("=") }] : {} end %w(no_proxy).each do |option| class_eval "def #{option}; options[:#{option}] end" end protected def do_proxify(socket, host, port) raise NotImplementedError, "#{self} must implement do_proxify" end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
proxifier-1.0.2 | lib/proxifier/proxy.rb |
proxifier-1.0.1 | lib/proxifier/proxy.rb |
proxifier-1.0.0 | lib/proxifier/proxy.rb |