Module: ZimbraInterceptingProxy::Server

Defined in:
lib/zimbra_intercepting_proxy/server.rb

Class Method Summary collapse

Class Method Details

.runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zimbra_intercepting_proxy/server.rb', line 6

def run
  debug = ZimbraInterceptingProxy::Debug
  host = ZimbraInterceptingProxy::Config.bind_address
  port = ZimbraInterceptingProxy::Config.bind_port
  new_mbx_local_ip_regex = ZimbraInterceptingProxy::Config.new_mbx_local_ip_regex

  Proxy.start(:host => host, :port => port) do |conn|
    
    debug.logger "Starting server on #{host}:#{port}"
    
    @backend = {host: ZimbraInterceptingProxy::Config.old_backend, port: 80}
    connection = ZimbraInterceptingProxy::Connection.new

    @parser = Http::Parser.new
    @parser.on_message_begin = proc{ connection.started = true }
    @parser.on_headers_complete = proc { |e| connection.headers = e }
    @parser.on_body = proc { |chunk| connection.body << chunk }
    @parser.on_message_complete = proc do |p|
      
      request = ZimbraInterceptingProxy::Request.new(connection, @parser)
              
      if request.auth_request? || request.route_request?
        user = User.new(request.user_token)
        @backend[:host] = user.backend if user.migrated?
        @backend[:port] = request.port
      end
      
      conn.server @backend[:host], :host => @backend[:host], :port => @backend[:port]
      conn.relay_to_servers connection.buffer
      
      connection.buffer.clear
      
    end
    
    conn.on_connect do |data,b|
      debug.logger [:on_connect, data, b]
    end

    conn.on_data do |data|
      debug.logger [:on_data, data]
      connection.buffer << data
      @parser << data

      data
    end

    conn.on_response do |backend, resp|
      if backend = ZimbraInterceptingProxy::Config.new_backend
        regex = Regexp.new "Auth-Server: #{new_mbx_local_ip_regex}*"
        new_resp = resp.gsub(regex, "Auth-Server: #{ZimbraInterceptingProxy::Config.new_backend}")
      end
      debug.logger [:on_response, backend, new_resp]
      new_resp
    end

    conn.on_finish do |backend, name|
      debug.logger [:on_finish, name].inspect
    end
    
  end
  
end