Class: TailscaleMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/tailscale_middleware.rb,
lib/tailscale_middleware/tailnet.rb,
lib/tailscale_middleware/version.rb

Defined Under Namespace

Classes: Tailnet

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}, &block) ⇒ TailscaleMiddleware

Returns a new instance of TailscaleMiddleware.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tailscale_middleware.rb', line 8

def initialize(app, opts = {}, &block)
  @app = app
  @debug_mode = !!opts[:debug]
  @logger = @logger_proc = nil

  @tsclient = Tsclient.default_client
  logger = opts[:logger]
  if logger
    if logger.respond_to?(:call)
      @logger_proc = opts[:logger]
    else
      @logger = logger
    end
  end

  return unless block_given?

  if block.arity == 1
    yield(self)
  else
    instance_eval(&block)
  end
end

Instance Method Details

#allow(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/tailscale_middleware.rb', line 36

def allow(&block)
  approved_tailnets << (tailnet = Tailnet.new)

  if block.arity == 1
    yield(tailnet)
  else
    tailnet.instance_eval(&block)
  end
end

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tailscale_middleware.rb', line 46

def call(env)
  path = evaluate_path(env)

  debug(env) do
    [
      "Path-Info: #{path}",
    ].join("\n")
  end

  expected_tailnet = path_for_tailnet(path)

  # no matching path -> tailnet, keep request as is
  # because route isn't protected
  return @app.call(env) if expected_tailnet.nil?

  # tsclient is not running, can't verify integrity: fail
  return [404, {}, ["Not Found"]] unless tsclient_running?

  # we're connected to the right tailnet, keep request as is
  return @app.call(env) if current_tailnet(env) == expected_tailnet

  # we're connected to the wrong tailnet: fail
  [404, {}, ["Not Found"]]
end

#debug?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/tailscale_middleware.rb', line 32

def debug?
  @debug_mode
end