Class: PublishToWeb::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/publish_to_web/tunnel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_host:, proxy_user:, proxy_port:, identity:, bind_host:, remote_port:, forward_port:, logger:) ⇒ Tunnel

Returns a new instance of Tunnel.



5
6
7
8
9
10
11
12
13
14
# File 'lib/publish_to_web/tunnel.rb', line 5

def initialize(proxy_host:, proxy_user:, proxy_port:, identity:, bind_host:, remote_port:, forward_port:, logger:)
  @proxy_host   = proxy_host
  @proxy_user   = proxy_user
  @proxy_port   = proxy_port
  @identity     = identity
  @bind_host    = bind_host
  @remote_port  = remote_port
  @forward_port = forward_port
  @logger       = logger
end

Instance Attribute Details

#bind_hostObject (readonly)

Returns the value of attribute bind_host.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def bind_host
  @bind_host
end

#forward_portObject (readonly)

Returns the value of attribute forward_port.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def forward_port
  @forward_port
end

#identityObject (readonly)

Returns the value of attribute identity.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def identity
  @identity
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def logger
  @logger
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def proxy_host
  @proxy_host
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def proxy_port
  @proxy_port
end

#proxy_userObject (readonly)

Returns the value of attribute proxy_user.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def proxy_user
  @proxy_user
end

#remote_portObject (readonly)

Returns the value of attribute remote_port.



3
4
5
# File 'lib/publish_to_web/tunnel.rb', line 3

def remote_port
  @remote_port
end

Instance Method Details

#local_portObject



34
35
36
37
38
39
40
41
# File 'lib/publish_to_web/tunnel.rb', line 34

def local_port
  @local_port ||= begin
    server = TCPServer.new('127.0.0.1', 0)
    local_port = server.addr[1]
    server.close
    local_port
  end
end

#ssh_optionsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/publish_to_web/tunnel.rb', line 16

def ssh_options
  @ssh_options ||= {
    keepalive_interval: 5,
    paranoid: false,
    # ExitOnForwardFailure ??
    use_agent: false,
    user_known_hosts_file: "/dev/null",
    port: proxy_port,
    key_data: [identity],
    # We need to make another logger here because verbose: :warn
    # will change the log level on the logger - we want to keep
    # the info messages from the client in general but avoid the
    # low-level noise from net/ssh
    logger: PublishToWeb.create_logger,
    verbose: :warn
  }
end

#startObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/publish_to_web/tunnel.rb', line 43

def start
  Net::SSH.start proxy_host, proxy_user, ssh_options do |ssh|
    ssh.forward.remote forward_port, bind_host, remote_port do |real_remote_port|
      # Indicate to our caller that we have established the connection successfully
      yield if block_given?
      logger.info "Established remote forwarding at port #{real_remote_port}"
    end

    ssh.forward.local(local_port, bind_host, 8765).tap do |real_local_port|
      logger.info "Established local forwarding at port #{real_local_port}"
    end
    logger.info "Entering keepalive loop"

    ssh.loop { true }
  end
end