aboutsummaryrefslogtreecommitdiffstats
path: root/modules/host/egress.lua
diff options
context:
space:
mode:
Diffstat (limited to 'modules/host/egress.lua')
-rw-r--r--modules/host/egress.lua34
1 files changed, 28 insertions, 6 deletions
diff --git a/modules/host/egress.lua b/modules/host/egress.lua
index 172805a..d276e05 100644
--- a/modules/host/egress.lua
+++ b/modules/host/egress.lua
@@ -1,7 +1,8 @@
-- The guests' egress policy, for nginx (OpenResty), see default.nix.
--
--- Per VM: which tap (and so whose) a guest is, egress_taps.lua says by its
--- address (which the bridge makes sure of, see default.nix). VMS/TAP has
+-- Per VM: whose a guest is, egress_guests.lua says by its address (which
+-- the bridge makes sure of, see default.nix), and which tap it's on,
+-- ADDR/ADDRESS and ADDR/TAP, as long as they agree. VMS/TAP has
-- the name of the VM's list, RULES/USER/LIST its rules, one per line, both
-- read on every connection and request:
-- https://example.com example.com and its subdomains, passed
@@ -21,10 +22,31 @@ local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/LIST
local VMS = os.getenv("FC_EGRESS_VMS") -- VMS/TAP: LIST
local STATE = os.getenv("FC_EGRESS_STATE") -- ca.key, ca.pem
local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-TAP.sock: the http server
-local guests = require("egress_taps") -- guest address -> { tap, user }
+local ADDR = os.getenv("FC_EGRESS_ADDR") -- ADDR/TAP: ADDRESS, ADDR/ADDRESS: TAP
+-- { addresses = { [ADDRESS] = USER }, taps = { [TAP] = USER } }
+local owners = require("egress_guests")
local by_tap = {}
-for _, g in pairs(guests) do
- by_tap[g.tap] = g
+for tap, user in pairs(owners.taps) do
+ by_tap[tap] = { tap = tap, user = user }
+end
+
+local function first_line(path)
+ local f = io.open(path)
+ local s = f and f:read("*l")
+ if f then
+ f:close()
+ end
+ return s
+end
+
+-- The guest at an address: { tap, user }, or nil.
+local function guest_at(address)
+ local user = owners.addresses[address]
+ local tap = user and first_line(ADDR .. "/" .. address)
+ local guest = tap and by_tap[tap]
+ if guest and guest.user == user and first_line(ADDR .. "/" .. tap) == address then
+ return guest
+ end
end
local function valid_name(s)
@@ -93,7 +115,7 @@ end
-- stream, preread: pass through, decrypt, or close
function _M.preread()
local sni = ngx.var.ssl_preread_server_name
- local guest = guests[ngx.var.remote_addr]
+ local guest = guest_at(ngx.var.remote_addr)
local name, who
if guest then
name, who = list(guest)