Class: LXC::Extra::ProxyClientSide

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc/extra/proxy_client_side.rb

Overview

Proxy server that listens for connections on the client side and sends the data to the corresponding ProxyServerSide through an Channel.

Usage

# Forward 127.0.0.1:80 in the container to google.com from the host
channel = LXC::Extra::Channel.new
pid = container.attach do
  server = TCPServer.new('127.0.0.1', 5559)
  proxy = LXC::Extra::ProxyClientSide.new(channel, server)
  proxy.start
end

# Here is the proxy server
proxy = LXC::Extra::ProxyServerSide.new(channel) do
  TCPSocket.new('127.0.0.1', 9995)
end
proxy.start

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, listen_server) ⇒ ProxyClientSide

Create a new ProxyClientSide.

Arguments

channel

Channel to communicate over

listen_server

TCPServer to listen to



33
34
35
36
37
# File 'lib/lxc/extra/proxy_client_side.rb', line 33

def initialize(channel, listen_server)
  @channel = channel
  @listen_server = listen_server
  @sockets = {}
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



39
40
41
# File 'lib/lxc/extra/proxy_client_side.rb', line 39

def channel
  @channel
end

#listen_serverObject (readonly)

Returns the value of attribute listen_server.



40
41
42
# File 'lib/lxc/extra/proxy_client_side.rb', line 40

def listen_server
  @listen_server
end

Instance Method Details

#startObject

Start forwarding connections and data. This call will not return until stop is called.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lxc/extra/proxy_client_side.rb', line 46

def start
  begin
    begin
      @selector = Selector.new
      @selector.on_select(@listen_server, &method(:on_server_accept))
      @selector.on_select(@channel.read_fd, &method(:on_channel_response))
      @selector.main_loop
    ensure
      @sockets.values.each { |socket| socket.close }
      @sockets = {}
      @selector = nil
    end
  rescue
    STDERR.puts("Proxy server error on client side: #{$!}\n#{$!.backtrace.join("\n")}")
    @channel.send_message(:server_error, $!)
    raise
  end
end

#stopObject

Stop forwarding connections and data. Existing connections will be closed and a stop message will be sent to the other side.



69
70
71
72
73
74
# File 'lib/lxc/extra/proxy_client_side.rb', line 69

def stop
  @sockets.values.each { |socket| socket.close }
  @sockets = {}
  @selector = nil
  @channel.send_message(:stop)
end