etunnel 1.2.3 → 1.3.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -0
  3. data/lib/etunnel.rb +66 -11
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f854a20b6bf76e2e3120ecc9069813feb0acb67dd23e9dc911cfb57063f781ee
4
- data.tar.gz: 02aa0460087fc620a7a45efddacab9a787e57e1415769344ae8d725f0ca472e2
3
+ metadata.gz: dc396672a75eb91b4ff1a7628b32e70cbcb3eb7add21f763fd7fa11ec64dbbe6
4
+ data.tar.gz: e4a0f32a36f852add06221fbb0b243bd243748a55a80c41b79ce3422bd9a5c39
5
5
  SHA512:
6
- metadata.gz: 3d0a58326f0ff7eadfd375e2767bf828b5fca5e54294dcd63b2e1c6149ef5c65e9dc7814735623019b21f7006ed9354fcad8eb937ae21fe9dbdea1247b00dc1b
7
- data.tar.gz: 4f091ed587e2c0602e2e9fe4f8115ed9b11abe988dc7e60efde63e0bff6c1fa01ef254c9aba4fb41e3157a291dab1bc9e89fb9cf19f262c18b0be4da99ed7e76
6
+ metadata.gz: 2e0158894e2c469f4fbe5fde7670195b470b6feb3814770fc47f4fa132b013c3a94c4e51d7a2eb94a13e0b42c45eb3675aede73025b91c6e2a70f8348d40c439
7
+ data.tar.gz: b14bcf6918412f03e72968f34b506f5d41154204c33cb7b5315aec56390f2bcd086bfff14bf2db691bd9c0614fcda131f031ce67657ac79728abe3c5cca29c31
data/README.md CHANGED
@@ -25,6 +25,14 @@ etunnel 3000
25
25
  Prints `TUNNEL_URL: https://<subdomain>.t.etun.uk` — that URL now routes to
26
26
  your local service. Ctrl-C closes the tunnel.
27
27
 
28
+ **Reconnects automatically.** If the connection drops (including a server
29
+ restart) the CLI re-dials with exponential backoff (500ms → 30s, ±20%
30
+ jitter, reset on success) and prints `TUNNEL_URL:` again. The subdomain is
31
+ **reclaimed**, not re-randomised: the client replays the server's signed
32
+ `resumeToken` in `hello.resume`, so links already shared keep working (the
33
+ server needs a stable `SESSION_SECRET`; see PROTOCOL.md §1). Set
34
+ `ETUNNEL_RECONNECT=0` to restore the old one-shot behaviour.
35
+
28
36
  ## Embed
29
37
 
30
38
  ```ruby
@@ -40,6 +48,7 @@ puts t.url
40
48
  | `ETUNNEL_SERVER` | tunnel server WebSocket URL | `wss://t.etun.uk/tunnel` |
41
49
  | `ETUNNEL_LOCAL` | local port or `host:port` (or 1st CLI arg) | `3000` |
42
50
  | `ETUNNEL_TOKEN` | paid-plan API key (optional) | - |
51
+ | `ETUNNEL_RECONNECT` | `0` disables auto-reconnect (one-shot CLI) | enabled |
43
52
  | `ETUNNEL_SUBDOMAIN` | custom subdomain (paid plans) | random |
44
53
 
45
54
  ## License
data/lib/etunnel.rb CHANGED
@@ -35,7 +35,7 @@ module Etunnel
35
35
  [kind].pack('C') + [sid].pack('N') + [payload.bytesize].pack('N') + payload
36
36
  end
37
37
 
38
- def self.connect(server, local, token: nil, subdomain: nil)
38
+ def self.connect(server, local, token: nil, subdomain: nil, resume: nil)
39
39
  local = "127.0.0.1:#{local}" unless local.to_s.include?(':')
40
40
  ws = Faye::WebSocket::Client.new(server, ['etunnel-v1'])
41
41
 
@@ -57,6 +57,7 @@ module Etunnel
57
57
  hello = { type: 'hello', proto: 1, client: 'etunnel-ruby/1.0' }
58
58
  hello[:token] = token if token
59
59
  hello[:subdomain] = subdomain if subdomain
60
+ hello[:resume] = resume if resume && !resume.empty?
60
61
  ws.send(JSON.generate(hello))
61
62
 
62
63
  url = nil
@@ -69,6 +70,7 @@ module Etunnel
69
70
  m = JSON.parse(payload)
70
71
  if m['type'] == 'welcome'
71
72
  url = m['url']
73
+ resume_token = m['resumeToken'].to_s
72
74
  break
73
75
  elsif m['type'] == 'error'
74
76
  raise "etunnel: #{m['code']}: #{m['message']}"
@@ -116,6 +118,7 @@ module Etunnel
116
118
 
117
119
  tunnel = Object.new
118
120
  tunnel.define_singleton_method(:url) { url }
121
+ tunnel.define_singleton_method(:resume_token) { resume_token }
119
122
  tunnel.define_singleton_method(:close) do
120
123
  ws.send(JSON.generate({ type: 'bye', reason: 'client closed' }))
121
124
  ws.close
@@ -159,16 +162,68 @@ end
159
162
  def Etunnel.main(argv = ARGV)
160
163
  server = ENV['ETUNNEL_SERVER'] || 'wss://t.etun.uk/tunnel'
161
164
  local = argv[0] || ENV['ETUNNEL_LOCAL'] || '3000'
162
- begin
163
- t = Etunnel.connect(server, local,
164
- token: ENV['ETUNNEL_TOKEN'], subdomain: ENV['ETUNNEL_SUBDOMAIN'])
165
- rescue StandardError => e
166
- warn "etunnel: #{e}"
167
- exit 1
168
- end
165
+ token = ENV['ETUNNEL_TOKEN']
166
+ subdomain = ENV['ETUNNEL_SUBDOMAIN']
167
+ reconnect = ENV['ETUNNEL_RECONNECT'] != '0'
168
+
169
169
  $stdout.sync = true
170
- puts "TUNNEL_URL: #{t.url}"
171
- trap(:INT) { t.close; exit 0 }
172
- sleep 1 while true
170
+ stopped = false
171
+ trap(:INT) { stopped = true }
172
+ trap(:TERM) { stopped = true }
173
+
174
+ # 500ms doubling, capped at 30s, +-20% jitter.
175
+ backoff = ->(attempt) { [0.5 * (2**attempt), 30.0].min * (0.8 + (rand * 0.4)) }
176
+ nap = lambda do |seconds|
177
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
178
+ sleep 0.25 while !stopped && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
179
+ end
180
+ shutdown = lambda do |tunnel|
181
+ tunnel.close
182
+ rescue StandardError
183
+ nil
184
+ end
185
+
186
+ attempt = 0
187
+ resume = ''
188
+ until stopped
189
+ # The SDK exposes no close callback, but the reader thread it starts dies
190
+ # with the websocket: watch it for the disconnect signal.
191
+ before = Thread.list
192
+ begin
193
+ tunnel = Etunnel.connect(server, local, token: token, subdomain: subdomain, resume: resume)
194
+ resume = tunnel.resume_token if tunnel.respond_to?(:resume_token) && !tunnel.resume_token.empty?
195
+ rescue StandardError => e
196
+ warn(e.message.start_with?('etunnel:') ? e.message : "etunnel: #{e}")
197
+ exit 1 unless reconnect
198
+
199
+ delay = backoff.call(attempt)
200
+ attempt += 1
201
+ warn format('etunnel: reconnecting in %.1fs', delay)
202
+ nap.call(delay)
203
+ next
204
+ end
205
+
206
+ workers = Thread.list - before
207
+ puts "TUNNEL_URL: #{tunnel.url}"
208
+ attempt = 0
209
+
210
+ unless reconnect
211
+ sleep 0.25 until stopped
212
+ shutdown.call(tunnel)
213
+ break
214
+ end
215
+
216
+ sleep 0.25 while !stopped && workers.any?(&:alive?)
217
+ if stopped
218
+ shutdown.call(tunnel)
219
+ break
220
+ end
221
+
222
+ delay = backoff.call(attempt)
223
+ attempt += 1
224
+ warn 'etunnel: connection lost'
225
+ warn format('etunnel: reconnecting in %.1fs', delay)
226
+ nap.call(delay)
227
+ end
173
228
  end
174
229
  Etunnel.main if $PROGRAM_NAME == __FILE__
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etunnel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - etunnel authors