Class: CapProxy::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind_address, target, log = nil) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cap_proxy/server.rb', line 11

def initialize(bind_address, target, log = nil)

  proxy_host, proxy_port = bind_address.split(":", 2)
  target_host, target_port = target.split(":", 2)

  @log = log
  @proxy_port = proxy_port
  @proxy_host = proxy_host
  @target_host = target_host
  @target_port = target_port
  reset_filters!
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def filters
  @filters
end

#logObject (readonly)

Returns the value of attribute log.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def log
  @log
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def proxy_host
  @proxy_host
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def proxy_port
  @proxy_port
end

#target_hostObject (readonly)

Returns the value of attribute target_host.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def target_host
  @target_host
end

#target_portObject (readonly)

Returns the value of attribute target_port.



9
10
11
# File 'lib/cap_proxy/server.rb', line 9

def target_port
  @target_port
end

Instance Method Details

#capture(filter_param, &handler) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cap_proxy/server.rb', line 28

def capture(filter_param, &handler)
  filter =
    case filter_param
    when Hash
      Filter.from_hash(filter_param)
    when Filter
      filter_param
    else
      raise RuntimeError("#capture requires a hash or a Filter object")
    end

  @filters.push filter: filter, handler: handler
  filter
end

#reset_filters!Object



24
25
26
# File 'lib/cap_proxy/server.rb', line 24

def reset_filters!
  @filters = ThreadSafe::Array.new
end

#run!Object



43
44
45
46
# File 'lib/cap_proxy/server.rb', line 43

def run!
  log.info "CapProxy: Listening on #{proxy_host}:#{proxy_port}" if log
  EM.start_server proxy_host, proxy_port, Client, self
end