etunnel 1.6.16 → 1.7.2

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 +21 -0
  3. data/lib/etunnel.rb +60 -4
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf878d7f65f152849691be3f7be57f7203b4ae64e2cd5a8ffbf284684d8f1658
4
- data.tar.gz: 75b272c9f230f51a2a2257987d17fcb7c687c0c6a8725a35f0f542bdc1659020
3
+ metadata.gz: 7c50419f75046b318db7afa204717c54c0302876e2bdbcbfa4f977975d235033
4
+ data.tar.gz: bc7ea186daaed88d0bb633409864b22f3c42d7ffcbe25e218fe75858bd476524
5
5
  SHA512:
6
- metadata.gz: a7988a3aabf7c050bbe7d5aef3c592e1f2765fffc8cbd71ce5e614574c13fa5ddde9f3e1dd62f9078ea2eade6345e1cd54fbfa333d769c13577f980008539da0
7
- data.tar.gz: 8f4512e5990d051290d5bc7cb69d6f94b544f1387aca1cb384b1cbe496f5fbcb322f59a88cd541be6b67aa5afa747c1e1f582cef2658ca51b36b571d682d7718
6
+ metadata.gz: a5fffe7ae05a91097bffa6dd769aaf0b43745f1038194760164bb46aa7066daeb18ad2e2f0b86df7783ea14e4c1f294f422ef4cd20cf6c5bada0666685cacd80
7
+ data.tar.gz: a779c1f0c0bcb09fec11eba120bdf3626b704f138529e6415edb9fc1b8089a0fe6422ccd43987d9c52ca28ea810e4c06794123db458d7fb7b668e57c617e7286
data/README.md CHANGED
@@ -62,3 +62,24 @@ use the [Go SDK](../../sdk) for streaming and WebSocket pass-through).
62
62
  Use of the etunnel relay service (default `wss://t.etun.uk/tunnel`) is governed
63
63
  by its [Terms of Service](https://t.etun.uk/terms) and
64
64
  [Privacy Policy](https://t.etun.uk/privacy).
65
+
66
+ ## Multi-target routing
67
+
68
+ One public hostname, several local services - e.g. `/` to a built frontend
69
+ and `/api` to a backend - without CORS or a second tunnel. Routing is
70
+ client-side; the protocol is unchanged (PROTOCOL.md 2).
71
+
72
+ ```ruby
73
+ t = Etunnel.connect("wss://t.example.com/tunnel", nil, routes: [
74
+ Etunnel::Route.new("/api", "127.0.0.1:8080", true),
75
+ Etunnel::Route.new("/", "127.0.0.1:3000"),
76
+ ])
77
+ ```
78
+
79
+ Semantics (identical in every SDK, pinned by tests): longest prefix
80
+ wins; matching is on segment boundaries (`/api` never matches `/apiv2`);
81
+ `strip` removes only the path part - the query string is carried over
82
+ byte-for-byte, an exact hit bottoms out at `/`; no matching route (only
83
+ possible without a `/` fallback) answers `404` at the SDK edge.
84
+
85
+ > Redirect note: with `strip`, a backend 302 to an absolute path (`/users/next`) lands on the fallback route on the next hop - backends should return relative redirects or keep the prefix (same as nginx `proxy_pass` with trailing slash; see #155).
data/lib/etunnel.rb CHANGED
@@ -35,8 +35,46 @@ 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, resume: nil)
39
- local = "127.0.0.1:#{local}" unless local.to_s.include?(':')
38
+ # --- multi-target local routing (PROTOCOL.md §2) ---------------------------
39
+ Route = Struct.new(:prefix, :addr, :strip)
40
+
41
+ # Validate + compile a route table: trims trailing slashes ("/" stays),
42
+ # rejects duplicates and malformed prefixes, sorts longest-first.
43
+ def self.normalize_routes(routes)
44
+ out = []
45
+ seen = {}
46
+ routes.each_with_index do |r, i|
47
+ pfx = r.prefix.to_s
48
+ raise ArgumentError, "etunnel: route[#{i}]: prefix is required (use \"/\" for the fallback route)" if pfx.empty?
49
+ raise ArgumentError, "etunnel: route[#{i}]: addr is required" if r.addr.to_s.empty?
50
+ raise ArgumentError, "etunnel: route[#{i}]: prefix \"#{pfx}\" must start with '/'" unless pfx.start_with?('/')
51
+ pfx = pfx[0..-2] while pfx.length > 1 && pfx.end_with?('/')
52
+ raise ArgumentError, "etunnel: route[#{i}]: duplicate prefix \"#{pfx}\"" if seen.key?(pfx)
53
+ seen[pfx] = true
54
+ out << Route.new(pfx, r.addr, r.strip ? true : false)
55
+ end
56
+ out.sort_by { |r| -r.prefix.length }
57
+ end
58
+
59
+ # Resolve "path?query": [addr, path] on hit, nil when nothing matches.
60
+ def self.route_for(routes, path_and_query)
61
+ qi = path_and_query.index('?')
62
+ path = qi ? path_and_query[0...qi] : path_and_query
63
+ query = qi ? path_and_query[qi..] : ''
64
+ routes.each do |r|
65
+ next unless r.prefix == '/' || path == r.prefix || path.start_with?("#{r.prefix}/")
66
+
67
+ p = path
68
+ p = (path == r.prefix ? '/' : path[r.prefix.length..] || '') if r.strip && r.prefix != '/'
69
+ return [r.addr, p + query]
70
+ end
71
+ nil
72
+ end
73
+
74
+ def self.connect(server, local, token: nil, subdomain: nil, resume: nil, routes: nil)
75
+ routes = normalize_routes(routes) if routes
76
+ raise ArgumentError, 'etunnel: routes must not be empty' if routes && routes.empty?
77
+ local = "127.0.0.1:#{local}" unless local.nil? || local.to_s.include?(':')
40
78
  ws = Faye::WebSocket::Client.new(server, ['etunnel-v1'])
41
79
 
42
80
  queue = Queue.new
@@ -113,7 +151,7 @@ module Etunnel
113
151
  # local app must not block this reader loop, or server pings go
114
152
  # unanswered while a request is in flight and the tunnel gets kicked.
115
153
  Thread.new do
116
- serve(ws, st[:req], st[:body], sid, local)
154
+ serve(ws, st[:req], st[:body], sid, local, routes)
117
155
  rescue StandardError => e
118
156
  warn "etunnel: request worker failed: #{e}"
119
157
  end
@@ -177,7 +215,25 @@ module Etunnel
177
215
  HTML
178
216
  end
179
217
 
180
- def self.serve(ws, req, body, sid, local)
218
+ def self.serve(ws, req, body, sid, local, routes = nil)
219
+ if routes
220
+ hit = route_for(routes, req['path'] || '/')
221
+ unless hit
222
+ ws.send(JSON.generate({ type: 'res', streamId: sid, status: 404,
223
+ headers: { 'Content-Type' => ['text/plain; charset=utf-8'] } }))
224
+ ws.send(frame(0x01, sid, "404 page not found (no route matches this path)\n"))
225
+ ws.send(frame(0x02, sid, ''))
226
+ return
227
+ end
228
+ addr, = hit
229
+ local = if addr =~ /\A\d+\z/
230
+ "127.0.0.1:#{addr}"
231
+ elsif (m = addr.match(/\A(\[[^\]]+\]|[^:]+):(\d+)\z/))
232
+ "#{m[1]}:#{m[2]}"
233
+ else
234
+ "#{addr}:80"
235
+ end
236
+ end
181
237
  host, port = local.split(':')
182
238
  method = req['method'] || 'GET'
183
239
  path = req['path'] || '/'
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.6.16
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - etunnel authors