ssh_tunnels 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae788942bb584a3aeb278a81eff1e25aff8861668835fc4844bf3ce32a58d3c8
4
+ data.tar.gz: b8adc0bf535fa0c219887a69a640dde3d32503f119b2c8b7c8e83c9e4313749c
5
+ SHA512:
6
+ metadata.gz: 63367bcdd582fdbfd353f450b8c702930f30473f735a6d33dc6b12a44aaf6529c512756c7becaa061d2c0a51a0ba7b21ed53a40cdd0cb597f803b2af78a66147
7
+ data.tar.gz: 6a56b3b03faa392aaa30b0e5cecae91eab88633dd447663c980ee0e37e976b97e8676e43073271b6f463e3457d3d35e3204c5bbf66bf8ef3e5b7b244fc5446e3
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.7.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ssh_tunnels (0.1.0)
5
+ curses (~> 1.3)
6
+ net-ssh-gateway (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.1)
12
+ concurrent-ruby (1.1.6)
13
+ curses (1.3.2)
14
+ diff-lcs (1.4.2)
15
+ i18n (1.8.3)
16
+ concurrent-ruby (~> 1.0)
17
+ net-ssh (6.1.0)
18
+ net-ssh-gateway (2.0.0)
19
+ net-ssh (>= 4.0.0)
20
+ paint (2.2.0)
21
+ parallel (1.19.2)
22
+ parser (2.7.1.4)
23
+ ast (~> 2.4.1)
24
+ rainbow (3.0.0)
25
+ regexp_parser (1.7.1)
26
+ rexml (3.2.4)
27
+ rspec (3.9.0)
28
+ rspec-core (~> 3.9.0)
29
+ rspec-expectations (~> 3.9.0)
30
+ rspec-mocks (~> 3.9.0)
31
+ rspec-core (3.9.2)
32
+ rspec-support (~> 3.9.3)
33
+ rspec-expectations (3.9.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-mocks (3.9.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-support (3.9.3)
40
+ rubocop (0.86.0)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.0.1)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.7)
45
+ rexml
46
+ rubocop-ast (>= 0.0.3, < 1.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 2.0)
49
+ rubocop-ast (0.0.3)
50
+ parser (>= 2.7.0.1)
51
+ ruby-progressbar (1.10.1)
52
+ strong_versions (0.4.4)
53
+ i18n (>= 0.5.0)
54
+ paint (~> 2.0)
55
+ unicode-display_width (1.7.0)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ rspec (~> 3.9)
62
+ rubocop (~> 0.86.0)
63
+ ssh_tunnels!
64
+ strong_versions (~> 0.4.4)
65
+
66
+ BUNDLED WITH
67
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Bob Farrell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ .PHONY: test
2
+ test:
3
+ bundle exec rspec
4
+ bundle exec rubocop
5
+ bundle exec strong_versions
@@ -0,0 +1,43 @@
1
+ # SshTunnels
2
+
3
+ Interactive _SSH_ tunnel management.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ gem install ssh_tunnels
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ ssh_tunnels -c config.yml
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ ```yaml
20
+ # config.yml
21
+
22
+ gateway:
23
+ host: gateway.example.com
24
+ port: 22
25
+
26
+ tunnels:
27
+ my_host:
28
+ local: 1234
29
+ host: my.host.example.com
30
+ remote: 4567
31
+ other_host:
32
+ local: 1111
33
+ host: other.host.example.com
34
+ remote: 5555
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ Pull requests are welcome.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ssh_tunnels'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.expand_path(File.join(__dir__, '..', 'lib'))
5
+
6
+ require 'ssh_tunnels'
7
+
8
+ require 'optparse'
9
+ require 'io/console'
10
+ require 'yaml'
11
+
12
+ options = {}
13
+ USAGE = 'Usage: ssh_tunnels [options]'
14
+
15
+ OptionParser.new do |opts|
16
+ opts.banner = USAGE
17
+
18
+ opts.on('-c', '--config PATH', 'Configuration file path') do |config|
19
+ options[:config_path] = config
20
+ end
21
+ end.parse!
22
+
23
+ unless options[:config_path]
24
+ warn('Must provide configuration path. See --help')
25
+ exit 1
26
+ end
27
+
28
+ unless File.exist?(options[:config_path])
29
+ warn("Unable to locate configuration file: #{options[:config_path]}")
30
+ exit 1
31
+ end
32
+
33
+ config = YAML.safe_load(File.read(options[:config_path]))
34
+
35
+ begin
36
+ gateway = config.fetch('gateway')
37
+ tunnels = config.fetch('tunnels')
38
+ rescue KeyError
39
+ warn('Configuration file must provide `gateway` and `tunnels` sections.')
40
+ exit 1
41
+ end
42
+
43
+ user = ENV.fetch('USER')
44
+ print 'Enter SSH key passphrase (leave blank if not required): '
45
+ passphrase = STDIN.noecho(&:gets).chomp
46
+ puts
47
+
48
+ begin
49
+ tunnels = tunnels.map do |name, tunnel_config|
50
+ SshTunnels::Tunnel.new(name, user, tunnel_config, gateway, passphrase)
51
+ end
52
+ ui = SshTunnels::UI.new(tunnels)
53
+ ui.run
54
+ rescue Interrupt
55
+ puts 'Interrupt detected.'
56
+ ui.shutdown
57
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'curses'
4
+ require 'net/ssh/gateway'
5
+
6
+ require 'ssh_tunnels/version'
7
+ require 'ssh_tunnels/tunnel'
8
+ require 'ssh_tunnels/ui'
9
+
10
+ module SshTunnels
11
+ class Error < StandardError; end
12
+ class UserQuit < Error; end
13
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SshTunnels
4
+ # SSH Tunnel
5
+ class Tunnel
6
+ attr_reader :name, :error
7
+
8
+ def initialize(name, user, config, gateway, passphrase)
9
+ @name = name
10
+ @user = user
11
+ @config = config
12
+ @passphrase = passphrase
13
+ @config = config
14
+ @gateway = gateway
15
+ @connection = nil
16
+ end
17
+
18
+ def to_s
19
+ base = "#{local_port}:#{remote_host}:#{remote_port}"
20
+ return base unless @error
21
+
22
+ "#{base} (#{@error})"
23
+ end
24
+
25
+ def toggle
26
+ active? ? shutdown : open
27
+ end
28
+
29
+ def open
30
+ @connection = Net::SSH::Gateway.new(@gateway.fetch('host'), @user, options)
31
+ @connection.open(remote_host, remote_port, local_port)
32
+ rescue Errno::EADDRINUSE => e
33
+ @error = e.to_s
34
+ shutdown
35
+ end
36
+
37
+ def active?
38
+ return false if @connection.nil?
39
+
40
+ @connection.active?
41
+ end
42
+
43
+ def shutdown
44
+ @connection.shutdown!
45
+ end
46
+
47
+ private
48
+
49
+ def remote_host
50
+ @config.fetch('host')
51
+ end
52
+
53
+ def remote_port
54
+ @config.fetch('remote_port')
55
+ end
56
+
57
+ def local_port
58
+ @config.fetch('local_port')
59
+ end
60
+
61
+ def options
62
+ {
63
+ keepalive: true,
64
+ keepalive_interval: 5,
65
+ port: @gateway.fetch('port'),
66
+ passphrase: @passphrase
67
+ }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SshTunnels
4
+ # rubocop:disable Metrics/ClassLength
5
+ # User Interface
6
+ class UI
7
+ def initialize(tunnels)
8
+ @tunnels = tunnels
9
+ end
10
+
11
+ def setup
12
+ Curses.init_screen
13
+ Curses.start_color
14
+ Curses.init_pair(1, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
15
+ Curses.init_pair(2, Curses::COLOR_BLUE, Curses::COLOR_BLACK)
16
+ Curses.init_pair(3, Curses::COLOR_GREEN, Curses::COLOR_BLACK)
17
+ Curses.init_pair(4, Curses::COLOR_CYAN, Curses::COLOR_BLACK)
18
+ Curses.init_pair(5, Curses::COLOR_RED, Curses::COLOR_BLACK)
19
+ Curses.timeout = 1000
20
+ Curses.curs_set(0)
21
+ Curses.noecho
22
+ end
23
+
24
+ def run
25
+ setup
26
+ monitor
27
+ rescue Net::SSH::Disconnect, Errno::ECONNRESET => e
28
+ shutdown("Error encountered: #{e}")
29
+ ensure
30
+ Curses.close_screen
31
+ end
32
+
33
+ def shutdown(error = nil)
34
+ Curses.close_screen
35
+ puts error unless error.nil?
36
+ puts 'Shutting down connections.'
37
+ @tunnels.select(&:active?).each(&:shutdown)
38
+ puts 'Shutdown complete.'
39
+ end
40
+
41
+ private
42
+
43
+ def monitor
44
+ loop do
45
+ @tunnels.each_with_index do |tunnel, index|
46
+ display_tunnel(tunnel, index)
47
+ end
48
+ refresh
49
+ end
50
+ rescue UserQuit
51
+ shutdown
52
+ end
53
+
54
+ def refresh
55
+ display_usage
56
+ window.refresh
57
+ input = window.getch
58
+ process_input(input) unless input.nil?
59
+ clean_status if @status_time && Time.now.utc - @status_time > 2.5
60
+ end
61
+
62
+ def window
63
+ @window ||= Curses.stdscr
64
+ end
65
+
66
+ # rubocop:disable Metrics/AbcSize
67
+ def display_tunnel(tunnel, index)
68
+ window.setpos(index + 2, 2)
69
+ window.attrset(color(:white))
70
+ window.addstr("#{index + 1}. ")
71
+ window.attrset(tunnel_color(tunnel))
72
+ window.addstr("#{tunnel.name} ")
73
+ window.attrset(color(:cyan))
74
+ window.addstr(tunnel.to_s)
75
+ end
76
+ # rubocop:enable Metrics/AbcSize
77
+
78
+ def display_usage
79
+ window.setpos(@tunnels.size + 3, 2)
80
+ window.attrset(color(:cyan))
81
+ message = "[1-#{@tunnels.size}] to connect/disconnect. Press 'q' to quit."
82
+ window.addstr(message)
83
+ end
84
+
85
+ def process_input(input)
86
+ raise UserQuit if input == 'q'
87
+ return status("Unrecognized input: #{input}") unless input.is_a?(String) && input =~ /\A\d+\Z/
88
+
89
+ tunnel = @tunnels[input.to_i - 1]
90
+ return status("Unrecognized tunnel: #{input}") if tunnel.nil?
91
+
92
+ if tunnel.active?
93
+ status("Disconnecting: #{tunnel}")
94
+ else
95
+ status("Connecting: #{tunnel}")
96
+ end
97
+ tunnel.toggle
98
+ end
99
+
100
+ def tunnel_color(tunnel)
101
+ return color(:red) if tunnel.error
102
+
103
+ tunnel.active? ? color(:green) : color(:blue)
104
+ end
105
+
106
+ def status(message)
107
+ clean_status
108
+ @status_time = Time.now.utc
109
+ window.setpos(*status_coordinates)
110
+ window.attrset(color(:white))
111
+ window.addstr(message)
112
+ window.refresh
113
+ end
114
+
115
+ def clean_status
116
+ y, x = status_coordinates
117
+ window.setpos(y, x)
118
+ window.attrset(color(:white))
119
+ window.addstr(' ' * (Curses.cols - x))
120
+ end
121
+
122
+ def status_coordinates
123
+ [@tunnels.size + 5, 2]
124
+ end
125
+
126
+ def color(name)
127
+ Curses.color_pair(
128
+ {
129
+ white: 1,
130
+ blue: 2,
131
+ green: 3,
132
+ cyan: 4,
133
+ red: 5
134
+ }.fetch(name)
135
+ )
136
+ end
137
+ end
138
+ # rubocop:enable Metrics/ClassLength
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SshTunnels
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ssh_tunnels/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ssh_tunnels'
7
+ spec.version = SshTunnels::VERSION
8
+ spec.authors = ['Bob Farrell']
9
+ spec.email = ['git@bob.frl']
10
+
11
+ spec.summary = 'Interactive SSH tunnel management'
12
+ spec.description = 'Conveniently manage numerous SSH tunnels with ncurses'
13
+ spec.homepage = 'https://github.com/bobf/ssh_tunnels'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = 'https://github.com/bobf/ssh_tunnels/blob/master/CHANGELOG.md'
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'bin'
25
+ spec.executables = 'ssh_tunnels'
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_runtime_dependency 'curses', '~> 1.3'
29
+ spec.add_runtime_dependency 'net-ssh-gateway', '~> 2.0'
30
+
31
+ spec.add_development_dependency 'rspec', '~> 3.9'
32
+ spec.add_development_dependency 'rubocop', '~> 0.86.0'
33
+ spec.add_development_dependency 'strong_versions', '~> 0.4.4'
34
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssh_tunnels
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bob Farrell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh-gateway
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.86.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.86.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: strong_versions
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.4
83
+ description: Conveniently manage numerous SSH tunnels with ncurses
84
+ email:
85
+ - git@bob.frl
86
+ executables:
87
+ - ssh_tunnels
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".ruby-version"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - Makefile
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - bin/ssh_tunnels
103
+ - lib/ssh_tunnels.rb
104
+ - lib/ssh_tunnels/tunnel.rb
105
+ - lib/ssh_tunnels/ui.rb
106
+ - lib/ssh_tunnels/version.rb
107
+ - ssh_tunnels.gemspec
108
+ homepage: https://github.com/bobf/ssh_tunnels
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ homepage_uri: https://github.com/bobf/ssh_tunnels
113
+ source_code_uri: https://github.com/bobf/ssh_tunnels
114
+ changelog_uri: https://github.com/bobf/ssh_tunnels/blob/master/CHANGELOG.md
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.3.0
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Interactive SSH tunnel management
134
+ test_files: []