Class: LXC::Extra::ProxyServerSide

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

Overview

Proxy server that listens for connections froom the corresponsing ProxyClientSide and sends them to the actual server.

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, &server_connector) ⇒ ProxyServerSide

Create a new ProxyServerSide.

Arguments

channel: Channel to send/receive connections from the other side.

&server_connector

block that will be called (with no arguments) when the server opens a new connection, to create a new connection to the server. Must return a socket.



35
36
37
38
# File 'lib/lxc/extra/proxy_server_side.rb', line 35

def initialize(channel, &server_connector)
  @channel = channel
  @server_connector = server_connector
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



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

def channel
  @channel
end

#server_connectorObject (readonly)

Returns the value of attribute server_connector.



41
42
43
# File 'lib/lxc/extra/proxy_server_side.rb', line 41

def server_connector
  @server_connector
end

Instance Method Details

#startObject

Start forwarding connections from the other side to the server. Blocks until stop is called.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lxc/extra/proxy_server_side.rb', line 47

def start
  @filenos = {}
  @sockets = {}
  begin
    @selector = Selector.new
    @selector.on_select(channel.read_fd, &method(:on_channel_message))
    @selector.main_loop
  ensure
    @sockets.values.each { |socket| socket.close }
  end
end

#stopObject

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



63
64
65
# File 'lib/lxc/extra/proxy_server_side.rb', line 63

def stop
  @selector.stop
end