Class: Authoxy

Inherits:
Object
  • Object
show all
Defined in:
lib/authoxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, local_port, upstream_proxy, user, password) ⇒ Authoxy

Prepares a new Authoxy:

my_proxy = Authoxy.new 'office', 8080, 'http://my.office.proxy.com:8080'



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/authoxy.rb', line 21

def initialize name, local_port, upstream_proxy, user, password
  @name = name
  proxy_uri          = URI::parse(upstream_proxy) 
  proxy_uri.user     = user
  proxy_uri.password = password
  
  # Set up the proxy itself
  @server = WEBrick::HTTPProxyServer.new(
    :LogFile => false,
    :Port => local_port.to_i,
    :ProxyVia => true,
    :ProxyURI => proxy_uri
  )
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/authoxy.rb', line 15

def name
  @name
end

Class Method Details

.load(path) ⇒ Object

Starts Authoxy loading the configuration from a YAML file like this:

office_proxy:

local_port: 8080
upstream_proxy: http://my.office.proxy:8080
password: very_secret_word
user: elia.schito

other_office_proxy:

local_port: 8081
upstream_proxy: http://other.proxy.com:8080
user: elia
password: other_very_secret_word


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/authoxy.rb', line 64

def self.load path
  proxy_definitions = YAML.load_file(path)

  @proxies ||= []
  proxy_definitions.each do |(name, proxy)|
    @proxies << Authoxy.new( name, proxy['local_port'].to_i, proxy['upstream_proxy'], proxy['user'], proxy['password'] || ask("password (#{name}): ") )
  end
  
  trap('INT') {
    @proxies.each { |proxy| proxy.stop }
    exit
  }
  @proxies.each { |proxy| proxy.start }
  sleep
end

Instance Method Details

#startObject

Starts the proxy



37
38
39
40
41
42
# File 'lib/authoxy.rb', line 37

def start
  @thread = Thread.new do
    puts "Starting #{name} proxy..."
    @server.start
  end
end

#stopObject

Stops the proxy



45
46
47
48
# File 'lib/authoxy.rb', line 45

def stop
  puts "Stopping #{name} proxy..."
  @server.shutdown
end