/
proxy.rb
76 lines (70 loc) · 2.56 KB
/
proxy.rb
1
2
3
4
5
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
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2013 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require "gqtp/server"
module GQTP
class Proxy
attr_accessor :listen_host, :listen_port
attr_accessor :upstream_host, :upstream_port
def initialize(options={})
@options = options.dup
@listen_host = @options[:listen_host] || @options[:listen_address]
@listen_host ||= "0.0.0.0"
@listen_port = @options[:listen_port] || 10043
@upstream_host = @options[:upstream_host] || @options[:upstream_address]
@upstream_host ||= "127.0.0.1"
@upstream_port = @options[:upstream_port] || 10043
@connection = @options[:connection] || :thread
@server = Server.new(:host => @listen_host,
:port => @listen_port,
:connection => @connection)
end
def run
@server.on_connect do |client|
create_connection
end
@server.on_request do |request, client, connection|
connection.write(request.header.pack, request.body) do
connection.read(Header.size) do |header|
response_header = Header.parse(header)
connection.read(response_header.size) do |body|
client.write(header, body) do
end
end
end
end
end
@server.run
end
def shutdown
@server.shutdown
end
private
def create_connection
begin
require "gqtp/connection/#{@connection}"
rescue LoadError
raise "unknown connection: <#{@connection.inspect}>"
end
require "gqtp/connection/#{@connection}"
module_name = @connection.to_s.capitalize
connection_module = GQTP::Connection::const_get(module_name)
connection_module::Client.new(:host => @upstream_host,
:port => @upstream_port)
end
end
end