etunnel 1.2.4 → 1.3.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -0
  3. data/lib/etunnel.rb +111 -12
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d2b59b0ae0c429dff6d0a59946f17f1e40e943f38f11014c7d7d30c0e4fcbd8
4
- data.tar.gz: 131fcb38b337bb4342c3753e7aeea91d21d8338b865d2394101c5b27f4ea5a5d
3
+ metadata.gz: e5248f40a678a4d640ecbf9015f888748398a3cb5b7f3da9f1e4ae141c937d03
4
+ data.tar.gz: 20b0b1c6464de3d20eb9891211d8c27fcceee95be037b7e74d76e45b417f985e
5
5
  SHA512:
6
- metadata.gz: f50525db8e148e80cbab58dd60824e0b550306a72b6daf38a6c34d3f2cb24212239c983114dcc0b766ebbae7312782d8cf4b379a7b063d6d310d61d37d13e783
7
- data.tar.gz: 34075602b078ebd0d9dec4955eaa72a59bd49787b4496dd805a2d9ecb3bf35f33bfa0c816c05552d52630b06b02bc42cd51f5279cf5b5945fb8143228633d433
6
+ metadata.gz: 526489529773883b78ef9dcdd1b468f885ba6265cbb73f14c6548e0efb03696336537480d2006a4a3c26b9bc3da2f0ab937af349267a5eba71919f8840b91ddf
7
+ data.tar.gz: a122be80fda9a97895f9a746aa855d3ff3043d4e7e191d8201c64cd5e2cab775a01667858423a9d542a3df737ecf5827f52277c9dc601179d078585400fb1af5
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
@@ -123,6 +126,50 @@ module Etunnel
123
126
  tunnel
124
127
  end
125
128
 
129
+ # Themed 502 page shown when the local app is down (same HTML as the other SDKs).
130
+ def self.local_error_page(error)
131
+ esc = error.to_s.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
132
+ <<~HTML
133
+ <!doctype html><html><head><meta charset="utf-8">
134
+ <meta name="viewport" content="width=device-width, initial-scale=1">
135
+ <title>502 - local app down</title>
136
+ <style>
137
+ :root{--bg:#0b0e14;--panel:#0d1117;--line:#21262d;--fg:#e6edf3;--dim:#8b949e;--green:#3fb950}
138
+ *{box-sizing:border-box;margin:0;padding:0}
139
+ body{font-family:ui-monospace,'SF Mono',Menlo,Consolas,monospace;background:var(--bg);color:var(--fg);
140
+ display:flex;min-height:100vh;align-items:center;justify-content:center;line-height:1.6}
141
+ main{max-width:600px;padding:2.5rem 1.5rem;width:100%}
142
+ .badge{display:inline-block;border:1px solid rgba(248,81,73,.4);color:#f85149;border-radius:99px;
143
+ padding:.2rem .8rem;font-size:.72rem;letter-spacing:.08em;text-transform:uppercase;margin-bottom:1.1rem}
144
+ h1{font-size:1.6rem;letter-spacing:-.01em;margin-bottom:.4rem}
145
+ h1 .g{color:var(--green)}
146
+ p{color:var(--dim);font-size:.9rem;margin-bottom:1.4rem}
147
+ .panel{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:1.1rem 1.3rem;margin-bottom:1.1rem}
148
+ .panel .k{color:var(--dim);font-size:.72rem;text-transform:uppercase;letter-spacing:.08em;margin-bottom:.3rem}
149
+ .panel .v{font-size:.9rem;word-break:break-all}
150
+ .err{color:#f85149}
151
+ .tip{border-left:3px solid var(--green);padding:.8rem 1rem;margin-top:1.2rem;font-size:.82rem;color:var(--dim)}
152
+ .tip b{color:var(--fg)}
153
+ footer{margin-top:2rem;font-size:.75rem;color:var(--dim)}
154
+ footer a{color:#58a6ff;text-decoration:none}
155
+ .pulse{display:inline-block;width:.55em;height:.55em;border-radius:50%;background:var(--green);
156
+ margin-right:.5em;animation:p 1.2s ease-in-out infinite}
157
+ @keyframes p{0%,100%{opacity:1}50%{opacity:.25}}
158
+ @media(prefers-reduced-motion:reduce){.pulse{animation:none}}
159
+ </style></head><body><main>
160
+ <span class="badge">502 local app down</span>
161
+ <h1>tunnel up, <span class="g">local app</span> is not</h1>
162
+ <p>The tunnel connection is alive, but nothing answered when we knocked.</p>
163
+ <div class="panel">
164
+ <div class="k">error</div><div class="v err">#{esc}</div>
165
+ </div>
166
+ <div class="tip"><span class="pulse"></span><b>Fix:</b> start your local service and reload -
167
+ the tunnel stays connected and keeps retrying for you.</div>
168
+ <footer>powered by <a href="https://github.com/topcheer/etunnel">etunnel</a></footer>
169
+ </main></body></html>
170
+ HTML
171
+ end
172
+
126
173
  def self.serve(ws, req, body, sid, local)
127
174
  host, port = local.split(':')
128
175
  method = req['method'] || 'GET'
@@ -139,7 +186,7 @@ module Etunnel
139
186
  resp = http.request(r)
140
187
  [resp.code.to_i, resp.each_header.reject { |k, _| HOP.include?(k) }.to_h, resp.body || '']
141
188
  rescue StandardError => e
142
- [502, { 'Content-Type' => 'text/plain' }, "etunnel: local service error: #{e}\n"]
189
+ [502, { 'Content-Type' => 'text/html; charset=utf-8' }, local_error_page(e)]
143
190
  end
144
191
  end.value
145
192
 
@@ -159,16 +206,68 @@ end
159
206
  def Etunnel.main(argv = ARGV)
160
207
  server = ENV['ETUNNEL_SERVER'] || 'wss://t.etun.uk/tunnel'
161
208
  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
209
+ token = ENV['ETUNNEL_TOKEN']
210
+ subdomain = ENV['ETUNNEL_SUBDOMAIN']
211
+ reconnect = ENV['ETUNNEL_RECONNECT'] != '0'
212
+
169
213
  $stdout.sync = true
170
- puts "TUNNEL_URL: #{t.url}"
171
- trap(:INT) { t.close; exit 0 }
172
- sleep 1 while true
214
+ stopped = false
215
+ trap(:INT) { stopped = true }
216
+ trap(:TERM) { stopped = true }
217
+
218
+ # 500ms doubling, capped at 30s, +-20% jitter.
219
+ backoff = ->(attempt) { [0.5 * (2**attempt), 30.0].min * (0.8 + (rand * 0.4)) }
220
+ nap = lambda do |seconds|
221
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
222
+ sleep 0.25 while !stopped && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
223
+ end
224
+ shutdown = lambda do |tunnel|
225
+ tunnel.close
226
+ rescue StandardError
227
+ nil
228
+ end
229
+
230
+ attempt = 0
231
+ resume = ''
232
+ until stopped
233
+ # The SDK exposes no close callback, but the reader thread it starts dies
234
+ # with the websocket: watch it for the disconnect signal.
235
+ before = Thread.list
236
+ begin
237
+ tunnel = Etunnel.connect(server, local, token: token, subdomain: subdomain, resume: resume)
238
+ resume = tunnel.resume_token if tunnel.respond_to?(:resume_token) && !tunnel.resume_token.empty?
239
+ rescue StandardError => e
240
+ warn(e.message.start_with?('etunnel:') ? e.message : "etunnel: #{e}")
241
+ exit 1 unless reconnect
242
+
243
+ delay = backoff.call(attempt)
244
+ attempt += 1
245
+ warn format('etunnel: reconnecting in %.1fs', delay)
246
+ nap.call(delay)
247
+ next
248
+ end
249
+
250
+ workers = Thread.list - before
251
+ puts "TUNNEL_URL: #{tunnel.url}"
252
+ attempt = 0
253
+
254
+ unless reconnect
255
+ sleep 0.25 until stopped
256
+ shutdown.call(tunnel)
257
+ break
258
+ end
259
+
260
+ sleep 0.25 while !stopped && workers.any?(&:alive?)
261
+ if stopped
262
+ shutdown.call(tunnel)
263
+ break
264
+ end
265
+
266
+ delay = backoff.call(attempt)
267
+ attempt += 1
268
+ warn 'etunnel: connection lost'
269
+ warn format('etunnel: reconnecting in %.1fs', delay)
270
+ nap.call(delay)
271
+ end
173
272
  end
174
273
  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.4
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - etunnel authors