ssh_tunnels 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 428ecc2c8e81f6b9d705f634a974fdb9c6a48d36285430154afb54ba842f5f0b
4
- data.tar.gz: aa630aa9387d0e2ad8a2e61a2c4c5d5a137d0b4ec131432105fc357b9ef7fd02
3
+ metadata.gz: 79e3f82e75a37a3fd7ca199dc827a217d33c91be2b5f1fc65c445e583d14d82c
4
+ data.tar.gz: b08d3849be1742e6581dcdf23e9c65f4cfc3c048d396bf877227e1bc2aa9f177
5
5
  SHA512:
6
- metadata.gz: 609d1bdff13df6d9557b80bccf0cac59d103357fc3da9f652985da003a4fdafac26be3617bdde91d76b29652afcef1f3fba479b7e36cc29df57eeb85d91a9653
7
- data.tar.gz: 3e9fa47276e2743552c9e191a33c1c5dd4c7334db574e44f45ffc2d00baa643ca685de9777996bbf8e36712d510838cdc2536d3564f8c141dc5bdc439767ec44
6
+ metadata.gz: 8d03a2c30f5e5d341513f0c24dd4d0efbf482b6cce2339dd41fdf128bdf28f9b53c0674afed04dae6de5899a803def7e28c25f0bd7f24d43cead291eab06348c
7
+ data.tar.gz: 87ed978f47c22b43cc27a9fe182ab86d9b30d070cea090eddf5940f37a59bf1ecbf473579c509b01ea140550d07b3ae325a38525106956476f7ce5665576a912
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ssh_tunnels (0.3.1)
4
+ ssh_tunnels (0.4.1)
5
5
  bcrypt_pbkdf (~> 1.1)
6
6
  curses (~> 1.4)
7
7
  ed25519 (~> 1.3)
data/README.md CHANGED
@@ -103,6 +103,20 @@ tunnels:
103
103
  remote: 8080
104
104
  ```
105
105
 
106
+ ## Live reload
107
+
108
+ The configuration file is checked once a second while the app is running. Saving a change updates the tunnel list in place, so there is no need to quit and reconnect:
109
+
110
+ * New tunnels appear and can be connected straight away.
111
+ * Tunnels that are disconnected take their new settings immediately.
112
+ * Tunnels that are connected keep running on their current settings. They are marked `config changed, reconnect to apply` and pick up the new settings when next disconnected.
113
+ * Tunnels deleted from the file are removed from the list if disconnected. If connected they stay listed, marked `removed from config`, until disconnected.
114
+ * An invalid or half-saved file is reported in the status line and leaves the current tunnels untouched until the next successful reload.
115
+
116
+ ## Error output
117
+
118
+ Anything written to the standard error stream while the app is running (a backtrace from a failed connection, for example) is captured and shown below the tunnel list. Press `Esc` to clear it. Only the most recent output is kept.
119
+
106
120
  ## Contributing
107
121
 
108
122
  Pull requests are welcome.
data/bin/ssh_tunnels CHANGED
@@ -22,39 +22,12 @@ end.parse!
22
22
 
23
23
  config_path = options.fetch(:config_path, File.expand_path('~/.ssh_tunnels.yml'))
24
24
 
25
- unless File.exist?(config_path)
26
- warn("Unable to locate configuration file: #{config_path}")
27
- exit 1
28
- end
29
-
30
- config = YAML.safe_load_file(config_path)
31
-
32
25
  begin
33
- default_gateway = config.fetch('default_gateway', nil)
34
- default_local_ip = config.fetch('default_local_ip', nil)
35
- unless config.key?('tunnels')
36
- warn('Configuration file must provide `tunnels` section. Exiting.')
37
- exit 1
38
- end
39
-
40
- gateways = config.fetch('gateways', {})
41
-
42
- tunnels = config.fetch('tunnels')
43
- error = false
44
- tunnels.each do |key, tunnel|
45
- if tunnel.key?('gateway') && !gateways.key?(tunnel.fetch('gateway'))
46
- error = true
47
- warn("Tunnel `#{key}` references unknown gateway `#{tunnel.fetch('gateway')}`")
48
- elsif !tunnel.key?('gateway') && default_gateway.nil?
49
- error = true
50
- warn("Tunnel `#{key}` must provide `gateway` key or define a top-level `default_gateway` configuration.")
51
- end
52
- end
53
-
54
- if error
55
- warn('Configuration errors detected. Exiting.')
56
- exit 1
57
- end
26
+ config = SshTunnels::Config.load(config_path)
27
+ rescue SshTunnels::ConfigError => e
28
+ warn(e.message)
29
+ warn('Configuration errors detected. Exiting.')
30
+ exit 1
58
31
  end
59
32
 
60
33
  user = ENV.fetch('USER')
@@ -63,18 +36,9 @@ passphrase = $stdin.noecho(&:gets).chomp
63
36
  puts
64
37
 
65
38
  begin
66
- tunnels = tunnels.map do |name, tunnel_config|
67
- gateway = if tunnel_config.key?('gateway')
68
- gateways.fetch(tunnel_config.fetch('gateway'))
69
- else
70
- default_gateway
71
- end
72
- config = { 'local_ip' => default_local_ip }.merge(tunnel_config)
73
- SshTunnels::Tunnel.new(name, user, config, gateway, passphrase)
74
- end
75
- ui = SshTunnels::UI.new(tunnels)
39
+ ui = SshTunnels::UI.new(config, user, passphrase)
76
40
  ui.run
77
41
  rescue Interrupt
78
42
  puts 'Interrupt detected.'
79
- ui.shutdown
43
+ ui&.shutdown
80
44
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SshTunnels
4
+ # Loads and validates a YAML configuration file and builds Tunnel objects
5
+ # from it. Invalid configuration raises ConfigError rather than exiting so
6
+ # that a live reload can report the problem and carry on unchanged.
7
+ class Config
8
+ REQUIRED_TUNNEL_KEYS = %w[host remote_port].freeze
9
+
10
+ attr_reader :path
11
+
12
+ def self.load(path)
13
+ raise ConfigError, "Unable to locate configuration file: #{path}" unless File.exist?(path)
14
+
15
+ new(path, YAML.safe_load_file(path))
16
+ rescue Psych::SyntaxError => e
17
+ raise ConfigError, "Invalid YAML: #{e.problem} (line #{e.line}, column #{e.column})"
18
+ end
19
+
20
+ def initialize(path, data)
21
+ @path = path
22
+ @data = data
23
+ validate
24
+ end
25
+
26
+ def mtime
27
+ File.mtime(@path)
28
+ rescue Errno::ENOENT
29
+ nil
30
+ end
31
+
32
+ def tunnels(user, passphrase)
33
+ tunnel_configs.map do |name, tunnel_config|
34
+ settings = { 'local_ip' => default_local_ip }.merge(tunnel_config)
35
+ Tunnel.new(name, user, settings, gateway_for(tunnel_config), passphrase)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def validate
42
+ validate_structure
43
+ errors = tunnel_configs.flat_map { |name, tunnel_config| tunnel_errors(name, tunnel_config) }
44
+ raise ConfigError, errors.join("\n") unless errors.empty?
45
+ end
46
+
47
+ def validate_structure
48
+ raise ConfigError, 'Configuration file must contain a YAML map.' unless @data.is_a?(Hash)
49
+ raise ConfigError, 'Configuration file must provide `tunnels` section.' unless @data.key?('tunnels')
50
+ raise ConfigError, '`tunnels` section must be a map.' unless tunnel_configs.is_a?(Hash)
51
+ raise ConfigError, '`gateways` section must be a map.' unless gateways.is_a?(Hash)
52
+ end
53
+
54
+ def tunnel_errors(name, tunnel_config)
55
+ return ["Tunnel `#{name}` must be a map."] unless tunnel_config.is_a?(Hash)
56
+
57
+ missing = REQUIRED_TUNNEL_KEYS.reject { |key| tunnel_config.key?(key) }
58
+ missing.map { |key| "Tunnel `#{name}` must provide `#{key}`." } + gateway_errors(name, tunnel_config)
59
+ end
60
+
61
+ def gateway_errors(name, tunnel_config)
62
+ if tunnel_config.key?('gateway')
63
+ gateway = tunnel_config.fetch('gateway')
64
+ return [] if gateways.key?(gateway)
65
+
66
+ ["Tunnel `#{name}` references unknown gateway `#{gateway}`."]
67
+ elsif default_gateway.nil?
68
+ ["Tunnel `#{name}` must provide `gateway` key or define a top-level `default_gateway` configuration."]
69
+ else
70
+ []
71
+ end
72
+ end
73
+
74
+ def gateway_for(tunnel_config)
75
+ return default_gateway unless tunnel_config.key?('gateway')
76
+
77
+ gateways.fetch(tunnel_config.fetch('gateway'))
78
+ end
79
+
80
+ def tunnel_configs
81
+ @data.fetch('tunnels')
82
+ end
83
+
84
+ def gateways
85
+ @data.fetch('gateways', {})
86
+ end
87
+
88
+ def default_gateway
89
+ @data.fetch('default_gateway', nil)
90
+ end
91
+
92
+ def default_local_ip
93
+ @data.fetch('default_local_ip', nil)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SshTunnels
4
+ # Captures everything written to the standard error stream while the curses
5
+ # screen is active. Output written straight to the terminal in that mode
6
+ # appears without line breaks and corrupts the display, so the stream is
7
+ # redirected into a pipe and the text is kept for the UI to draw.
8
+ #
9
+ # Only one block of output is retained: output arriving after a quiet period
10
+ # replaces whatever was captured before.
11
+ class StderrCapture
12
+ QUIET_PERIOD = 2
13
+ LIMIT = 64 * 1024
14
+ TAB = ' ' * 4
15
+
16
+ def initialize(stream = $stderr)
17
+ @stream = stream
18
+ @mutex = Mutex.new
19
+ @text = +''
20
+ @updated_at = nil
21
+ @original = nil
22
+ end
23
+
24
+ # Redirection happens at the file descriptor level so that output from
25
+ # loggers and background threads holding a reference to the stream is
26
+ # captured too.
27
+ def start
28
+ return unless @original.nil?
29
+
30
+ @original = @stream.dup
31
+ @reader, writer = IO.pipe
32
+ @stream.reopen(writer)
33
+ @stream.sync = true
34
+ writer.close
35
+ @thread = Thread.new { drain }
36
+ end
37
+
38
+ def stop
39
+ return if @original.nil?
40
+
41
+ @stream.reopen(@original)
42
+ @stream.sync = true
43
+ @original.close
44
+ @original = nil
45
+ @thread.join
46
+ @reader.close
47
+ end
48
+
49
+ def empty?
50
+ @mutex.synchronize { @text.empty? }
51
+ end
52
+
53
+ def clear
54
+ @mutex.synchronize { @text = +'' }
55
+ end
56
+
57
+ # Captured text as display lines no wider than +width+.
58
+ def lines(width)
59
+ text = @mutex.synchronize { @text.dup }
60
+ text.force_encoding(Encoding::UTF_8).scrub('?').split(/\r?\n/).flat_map do |line|
61
+ line = line.gsub("\t", TAB).gsub(/[^[:print:]]/, '')
62
+ line.empty? ? [line] : line.scan(/.{1,#{width}}/)
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def drain
69
+ loop { append(@reader.readpartial(4096)) }
70
+ rescue IOError
71
+ nil
72
+ end
73
+
74
+ def append(chunk)
75
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
76
+ @mutex.synchronize do
77
+ @text = +'' if @updated_at && now - @updated_at > QUIET_PERIOD
78
+ @updated_at = now
79
+ @text << chunk.byteslice(0, LIMIT - @text.bytesize).to_s
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,9 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SshTunnels
4
+ # rubocop:disable Metrics/ClassLength
4
5
  # SSH Tunnel
5
6
  class Tunnel
6
- attr_reader :name, :error
7
+ attr_reader :name, :error, :config, :gateway
8
+
9
+ # Merges freshly-loaded tunnels into the current list. Tunnels that share a
10
+ # name keep their existing object (and therefore any live connection) and
11
+ # receive the new settings via #update. Tunnels no longer present in the
12
+ # configuration are dropped if inactive, or kept and flagged as removed
13
+ # while still connected so a file edit never interrupts a live session.
14
+ def self.reconcile(current, incoming)
15
+ existing = current.to_h { |tunnel| [tunnel.name, tunnel] }
16
+ merged = incoming.map do |tunnel|
17
+ match = existing.delete(tunnel.name)
18
+ next tunnel if match.nil?
19
+
20
+ match.update(tunnel.config, tunnel.gateway)
21
+ match
22
+ end
23
+ merged + existing.values.select(&:active?).each(&:remove)
24
+ end
7
25
 
8
26
  def initialize(name, user, config, gateway, passphrase)
9
27
  @name = name
@@ -14,6 +32,8 @@ module SshTunnels
14
32
  @session = nil
15
33
  @thread = nil
16
34
  @active = false
35
+ @pending = nil
36
+ @removed = false
17
37
  end
18
38
 
19
39
  def to_s
@@ -27,11 +47,38 @@ module SshTunnels
27
47
  "#{base} (#{@error})"
28
48
  end
29
49
 
50
+ # Called on configuration reload. Inactive tunnels take the new settings
51
+ # immediately; active tunnels keep running on their current settings and
52
+ # apply the new ones when next disconnected.
53
+ def update(config, gateway)
54
+ @removed = false
55
+ if config == @config && gateway == @gateway
56
+ @pending = nil
57
+ elsif active?
58
+ @pending = [config, gateway]
59
+ else
60
+ apply(config, gateway)
61
+ end
62
+ end
63
+
64
+ def remove
65
+ @removed = true
66
+ end
67
+
68
+ def removed?
69
+ @removed
70
+ end
71
+
72
+ def changed?
73
+ !@pending.nil?
74
+ end
75
+
30
76
  def toggle
31
77
  active? ? shutdown : open
32
78
  end
33
79
 
34
80
  def open
81
+ apply_pending
35
82
  @error = nil
36
83
  connect
37
84
  @active = true
@@ -39,8 +86,7 @@ module SshTunnels
39
86
  rescue StandardError => e
40
87
  @error = e
41
88
  @active = false
42
- @session&.close
43
- @session = nil
89
+ close_session
44
90
  raise
45
91
  end
46
92
 
@@ -53,10 +99,22 @@ module SshTunnels
53
99
  @thread&.join
54
100
  @thread = nil
55
101
  @session = nil
102
+ apply_pending
56
103
  end
57
104
 
58
105
  private
59
106
 
107
+ def apply_pending
108
+ apply(*@pending) unless @pending.nil?
109
+ end
110
+
111
+ def apply(config, gateway)
112
+ @config = config
113
+ @gateway = gateway
114
+ @pending = nil
115
+ @error = nil
116
+ end
117
+
60
118
  def connect
61
119
  @session = Net::SSH.start(@gateway.fetch('host'), @gateway.fetch('user', @user), options)
62
120
  forward_local
@@ -64,19 +122,37 @@ module SshTunnels
64
122
 
65
123
  # Runs in the background thread. When the loop ends — whether because the
66
124
  # user disconnected (@active set to false) or the connection dropped (the
67
- # loop raises) — we must close the session here so the local forwarded port
68
- # is released. Otherwise the orphaned listener keeps the port bound, causing
69
- # connecting apps to hang and reconnects to fail with "address in use".
125
+ # loop raises) — the local forwarded port must be released here. Otherwise
126
+ # the orphaned listener keeps the port bound, causing connecting apps to
127
+ # hang and reconnects to fail with "address in use".
70
128
  def run_loop
71
129
  @session.loop(0.001) { @active }
72
130
  rescue StandardError => e
73
131
  @error = e
74
132
  ensure
75
133
  @active = false
134
+ close_session
135
+ end
136
+
137
+ # Net::SSH::Connection::Session#close only closes channels and the
138
+ # transport socket; the TCPServer bound by forward.local is only released
139
+ # by forward.cancel_local, so cancel before closing.
140
+ def close_session
141
+ release_port
76
142
  @session&.close
77
143
  @session = nil
78
144
  end
79
145
 
146
+ def release_port
147
+ return if @session.nil?
148
+
149
+ args = [local_port]
150
+ args << local_host if local_host
151
+ @session.forward.cancel_local(*args)
152
+ rescue StandardError
153
+ nil
154
+ end
155
+
80
156
  def forward_local
81
157
  args = [local_port, remote_host, remote_port]
82
158
  args.unshift(local_host) if local_host
@@ -108,4 +184,5 @@ module SshTunnels
108
184
  }
109
185
  end
110
186
  end
187
+ # rubocop:enable Metrics/ClassLength
111
188
  end
@@ -5,19 +5,30 @@ module SshTunnels
5
5
  # User Interface
6
6
  class UI
7
7
  IDENTIFIERS = ['1'..'9', 'a'..'z', 'A'..'Z'].map(&:to_a).flatten
8
-
9
- def initialize(tunnels)
10
- @tunnels = tunnels
8
+ COLORS = {
9
+ white: Curses::COLOR_WHITE,
10
+ blue: Curses::COLOR_BLUE,
11
+ green: Curses::COLOR_GREEN,
12
+ cyan: Curses::COLOR_CYAN,
13
+ red: Curses::COLOR_RED,
14
+ yellow: Curses::COLOR_YELLOW
15
+ }.freeze
16
+ ESCAPE = [27, "\e"].freeze
17
+
18
+ def initialize(config, user, passphrase)
19
+ @config = config
20
+ @user = user
21
+ @passphrase = passphrase
22
+ @tunnels = config.tunnels(user, passphrase)
23
+ @config_mtime = config.mtime
24
+ @stderr_capture = StderrCapture.new
11
25
  end
12
26
 
13
27
  def setup
28
+ @stderr_capture.start
14
29
  Curses.init_screen
15
30
  Curses.start_color
16
- Curses.init_pair(1, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
17
- Curses.init_pair(2, Curses::COLOR_BLUE, Curses::COLOR_BLACK)
18
- Curses.init_pair(3, Curses::COLOR_GREEN, Curses::COLOR_BLACK)
19
- Curses.init_pair(4, Curses::COLOR_CYAN, Curses::COLOR_BLACK)
20
- Curses.init_pair(5, Curses::COLOR_RED, Curses::COLOR_BLACK)
31
+ COLORS.each_value.with_index(1) { |value, pair| Curses.init_pair(pair, value, Curses::COLOR_BLACK) }
21
32
  Curses.timeout = 1000
22
33
  Curses.curs_set(0)
23
34
  Curses.noecho
@@ -29,11 +40,11 @@ module SshTunnels
29
40
  rescue Net::SSH::Disconnect, Errno::ECONNRESET => e
30
41
  shutdown("Error encountered: #{e}")
31
42
  ensure
32
- Curses.close_screen
43
+ close_screen
33
44
  end
34
45
 
35
46
  def shutdown(error = nil)
36
- Curses.close_screen
47
+ close_screen
37
48
  puts error unless error.nil?
38
49
  puts 'Shutting down connections.'
39
50
  @tunnels.select(&:active?).each(&:shutdown)
@@ -42,11 +53,17 @@ module SshTunnels
42
53
 
43
54
  private
44
55
 
56
+ def close_screen
57
+ Curses.close_screen
58
+ @stderr_capture.stop
59
+ end
60
+
45
61
  def monitor
46
62
  loop do
47
- @tunnels.each_with_index do |tunnel, index|
48
- display_tunnel(tunnel, index)
49
- end
63
+ reload_if_changed
64
+ prune_removed
65
+ @tunnels.each_with_index { |tunnel, index| display_tunnel(tunnel, index) }
66
+ display_error_output
50
67
  refresh
51
68
  end
52
69
  rescue UserQuit
@@ -61,6 +78,37 @@ module SshTunnels
61
78
  clean_status if @status_time && Time.now.utc - @status_time > 2.5
62
79
  end
63
80
 
81
+ # The configuration file is polled once per tick (see Curses.timeout). A
82
+ # change triggers a reload; an unreadable or invalid file leaves the
83
+ # current tunnels untouched and reports the problem until the next
84
+ # successful reload.
85
+ def reload_if_changed
86
+ mtime = @config.mtime
87
+ return if mtime == @config_mtime
88
+
89
+ @config_mtime = mtime
90
+ reload
91
+ end
92
+
93
+ def reload
94
+ @config = Config.load(@config.path)
95
+ @tunnels = Tunnel.reconcile(@tunnels, @config.tunnels(@user, @passphrase))
96
+ window.erase
97
+ status('Configuration reloaded.')
98
+ rescue ConfigError => e
99
+ status("Configuration error: #{e.message.lines.first.chomp}", sticky: true)
100
+ end
101
+
102
+ # Tunnels removed from the configuration stay listed while connected and
103
+ # disappear once they are no longer active.
104
+ def prune_removed
105
+ orphans = @tunnels.select { |tunnel| tunnel.removed? && !tunnel.active? }
106
+ return if orphans.empty?
107
+
108
+ @tunnels -= orphans
109
+ window.erase
110
+ end
111
+
64
112
  def window
65
113
  @window ||= Curses.stdscr
66
114
  end
@@ -74,21 +122,80 @@ module SshTunnels
74
122
  window.addstr("#{tunnel.name} ")
75
123
  window.attrset(color(:cyan))
76
124
  window.addstr(tunnel.to_s)
125
+ display_marker(tunnel)
126
+ window.clrtoeol
77
127
  end
78
128
  # rubocop:enable Metrics/AbcSize
79
129
 
130
+ def display_marker(tunnel)
131
+ marker = tunnel_marker(tunnel)
132
+ return if marker.nil?
133
+
134
+ window.attrset(color(:yellow))
135
+ window.addstr(" [#{marker}]")
136
+ end
137
+
138
+ def tunnel_marker(tunnel)
139
+ return 'removed from config, disconnect to clear' if tunnel.removed?
140
+ return 'config changed, reconnect to apply' if tunnel.changed?
141
+
142
+ nil
143
+ end
144
+
80
145
  def display_usage
81
146
  window.setpos(@tunnels.size + 3, 2)
82
147
  window.attrset(color(:cyan))
83
- message = "[1-#{@tunnels.size}] to connect/disconnect. Press 'q' to quit."
84
- window.addstr(message)
148
+ window.addstr(usage_message)
149
+ window.clrtoeol
150
+ end
151
+
152
+ def usage_message
153
+ return "No tunnels configured. Press 'q' to quit." if @tunnels.empty?
154
+
155
+ "[1-#{IDENTIFIERS[@tunnels.size - 1]}] to connect/disconnect. Press 'q' to quit."
156
+ end
157
+
158
+ # Output captured from the standard error stream (a backtrace, usually) is
159
+ # drawn below the status line, one line per row, until cleared with Esc.
160
+ # Output that does not fit the window is cut short.
161
+ def display_error_output
162
+ top = status_coordinates.first + 2
163
+ clear_rows(top)
164
+ rows = window.maxy - top - 1
165
+ return if @stderr_capture.empty? || rows < 1
166
+
167
+ write_line(top, 'Error output (Esc to clear):', :yellow)
168
+ error_lines(rows).each_with_index { |line, index| write_line(top + 1 + index, line, :red) }
169
+ end
170
+
171
+ def error_lines(rows)
172
+ lines = @stderr_capture.lines([window.maxx - 4, 1].max)
173
+ hidden = lines.size - rows
174
+ return lines unless hidden.positive?
175
+
176
+ lines.first(rows - 1) << "... #{hidden + 1} more lines"
177
+ end
178
+
179
+ def write_line(row, text, color_name)
180
+ window.attrset(color(color_name))
181
+ window.setpos(row, 2)
182
+ window.addstr(text)
183
+ end
184
+
185
+ def clear_rows(top)
186
+ (top...window.maxy).each do |row|
187
+ window.setpos(row, 0)
188
+ window.clrtoeol
189
+ end
85
190
  end
86
191
 
87
192
  def process_input(input)
88
193
  raise UserQuit if input == 'q'
194
+ return @stderr_capture.clear if ESCAPE.include?(input)
89
195
  return status("Unrecognized input: #{input}") unless input.is_a?(String) && input =~ /\A[0-9a-zA-Z]\Z/
90
196
 
91
- tunnel = @tunnels[IDENTIFIERS.index { |value| value == input }]
197
+ index = IDENTIFIERS.index(input)
198
+ tunnel = index && @tunnels[index]
92
199
  return status("Unrecognized tunnel: #{input}") if tunnel.nil?
93
200
 
94
201
  toggle_tunnel(tunnel)
@@ -107,9 +214,11 @@ module SshTunnels
107
214
  tunnel.active? ? color(:green) : color(:blue)
108
215
  end
109
216
 
110
- def status(message)
217
+ # A sticky status stays until replaced by the next status message rather
218
+ # than being cleared after a few seconds.
219
+ def status(message, sticky: false)
111
220
  clean_status
112
- @status_time = Time.now.utc
221
+ @status_time = sticky ? nil : Time.now.utc
113
222
  window.setpos(*status_coordinates)
114
223
  window.attrset(color(:white))
115
224
  window.addstr(message)
@@ -120,7 +229,7 @@ module SshTunnels
120
229
  y, x = status_coordinates
121
230
  window.setpos(y, x)
122
231
  window.attrset(color(:white))
123
- window.addstr(' ' * (Curses.cols - x))
232
+ window.clrtoeol
124
233
  end
125
234
 
126
235
  def status_coordinates
@@ -128,15 +237,7 @@ module SshTunnels
128
237
  end
129
238
 
130
239
  def color(name)
131
- Curses.color_pair(
132
- {
133
- white: 1,
134
- blue: 2,
135
- green: 3,
136
- cyan: 4,
137
- red: 5
138
- }.fetch(name)
139
- )
240
+ Curses.color_pair(COLORS.keys.index(name) + 1)
140
241
  end
141
242
  end
142
243
  # rubocop:enable Metrics/ClassLength
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SshTunnels
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/ssh_tunnels.rb CHANGED
@@ -2,12 +2,16 @@
2
2
 
3
3
  require 'curses'
4
4
  require 'net/ssh'
5
+ require 'yaml'
5
6
 
6
7
  require 'ssh_tunnels/version'
8
+ require 'ssh_tunnels/config'
9
+ require 'ssh_tunnels/stderr_capture'
7
10
  require 'ssh_tunnels/tunnel'
8
11
  require 'ssh_tunnels/ui'
9
12
 
10
13
  module SshTunnels
11
14
  class Error < StandardError; end
12
15
  class UserQuit < Error; end
16
+ class ConfigError < Error; end
13
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssh_tunnels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Farrell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-30 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt_pbkdf
@@ -89,6 +89,8 @@ files:
89
89
  - bin/ssh_tunnels
90
90
  - doc/demo.png
91
91
  - lib/ssh_tunnels.rb
92
+ - lib/ssh_tunnels/config.rb
93
+ - lib/ssh_tunnels/stderr_capture.rb
92
94
  - lib/ssh_tunnels/tunnel.rb
93
95
  - lib/ssh_tunnels/ui.rb
94
96
  - lib/ssh_tunnels/version.rb