Sha256: c8090d3ddd9f8c266456c0624692e360e4090c0b92b28b8d4586877a8a01fbaa

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH << File.expand_path(File.join(__dir__, '..', 'lib'))

require 'ssh_tunnels'

require 'optparse'
require 'io/console'
require 'yaml'

options = {}
USAGE = 'Usage: ssh_tunnels [options]'

OptionParser.new do |opts|
  opts.banner = USAGE

  opts.on('-c', '--config PATH', 'Configuration file path') do |config|
    options[:config_path] = config
  end
end.parse!

config_path = options.fetch(:config_path, File.expand_path('~/.ssh_tunnels.yml'))

unless File.exist?(config_path)
  warn("Unable to locate configuration file: #{config_path}")
  exit 1
end

config = YAML.safe_load(File.read(config_path))

begin
  default_gateway = config.fetch('default_gateway', nil)
  if !config.key?('tunnels')
    warn('Configuration file must provide `tunnels` section. Exiting.')
    exit 1
  end

  gateways = config.fetch('gateways', {})

  tunnels = config.fetch('tunnels')
  error = false
  tunnels.each do |key, tunnel|
    if tunnel.key?('gateway') && !gateways.key?(tunnel.fetch('gateway'))
      error = true
      warn("Tunnel `#{key}` references unknown gateway `#{tunnel.fetch('gateway')}`")
    elsif !tunnel.key?('gateway') && default_gateway.nil?
      error = true
      warn("Tunnel `#{key}` must provide `gateway` key or define a top-level `default_gateway` configuration.")
    end
  end

  if error
    warn("Configuration errors detected. Exiting.")
    exit 1
  end
end

user = ENV.fetch('USER')
print 'Enter SSH key passphrase (leave blank if not required): '
passphrase = STDIN.noecho(&:gets).chomp
puts

begin
  tunnels = tunnels.map do |name, tunnel_config|
    gateway = if tunnel_config.key?('gateway')
                gateways.fetch(tunnel_config.fetch('gateway'))
              else
                default_gateway
              end
    SshTunnels::Tunnel.new(name, user, tunnel_config, gateway, passphrase)
  end
  ui = SshTunnels::UI.new(tunnels)
  ui.run
rescue Interrupt
  puts 'Interrupt detected.'
  ui.shutdown
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ssh_tunnels-0.2.1 bin/ssh_tunnels
ssh_tunnels-0.2.0 bin/ssh_tunnels