Sha256: 344790ba1ad5477e3a15af40e92049c6e582b81245ee3c257a99f6021c484d0d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'socket'

module Fulmar
  module Infrastructure
    module Service
      class TunnelService
        attr_reader :host, :remote_port, :local_port

        def initialize(host, port, remote_host = 'localhost')
          @host = host
          @remote_port = port
          @remote_host = remote_host
          @local_port = 0
          @tunnel_pid = 0
        end

        def open
          @local_port = free_port
          @tunnel_pid = Process.spawn "ssh #{@host} -L #{@local_port}:#{@remote_host}:#{@remote_port} -N"
          sleep 1
        end

        def close
          Process.kill 'TERM', @tunnel_pid if @tunnel_pid > 0
          @local_port = 0
          @tunnel_pid = 0
        end

        def open?
          @tunnel_pid > 0
        end

        def free_port
          port = 60000
          begin
            1000.times do
              socket = TCPSocket.new('localhost', port)
              socket.close
              port += 1
            end
          rescue Errno::ECONNREFUSED
            return port
          end
          fail 'Cannot find an open local port'
          0
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fulmar-1.0.0 lib/fulmar/infrastructure/service/tunnel_service.rb