Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pirania/files/etc/config/pirania
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
config base_config 'base_config'
option enabled '0'
option prune_expired_for_days '30'
option portal_domain 'thisnode.info'
option portal_domain 'gateway.info'
option url_auth '/portal/auth.html'
option url_authenticated '/portal/authenticated.html'
option url_info '/portal/info.html'
Expand Down
2 changes: 1 addition & 1 deletion packages/pirania/files/etc/init.d/pirania-dnsmasq
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ start_service() {
--no-resolv \
--server=/$(uci get dhcp.@dnsmasq[0].domain)/$(uci get network.lm_net_br_lan_anygw_if.ipaddr) \
--addn-hosts=/var/hosts/shared-state-dnsmasq_hosts \
--address=/thisnode.info/$(uci get network.lm_net_br_lan_anygw_if.ipaddr) \
--address=/gateway.info/$(uci get network.lm_net_br_lan_anygw_if.ipaddr) \
--address=/\#/1.2.3.4

# respawn automatically if something died, be careful if you have an alternative process supervisor
Expand Down
71 changes: 71 additions & 0 deletions packages/pirania/files/etc/init.d/pirania-mesh-sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/sh /etc/rc.common

START=99

start() {
(
TIMEOUT=60
INTERVAL=2
ELAPSED=0
ntpd -q -p pool.ntp.org
while [ $ELAPSED -lt $TIMEOUT ]; do
WAN_IF=$(ifstatus wan | jsonfilter -e '@.l3_device')
DEFAULT_ROUTE=$(ip route | awk '/^default/ {print $3" "$5; exit}')
BR_LAN_IP=$(ip -4 -o addr show dev br-lan | awk '{print $4}' | cut -d/ -f1)
GATEWAY=$(echo "$DEFAULT_ROUTE" | awk '{print $1}')
DEV=$(echo "$DEFAULT_ROUTE" | awk '{print $2}')
WLAN_MESH_MAC=$(ip link show "$DEV" | awk '/link\/ether/ {print $2}')
echo "DEBUG: WAN_IF=$WAN_IF"
echo "DEBUG: DEFAULT_ROUTE=$DEFAULT_ROUTE"
echo "DEBUG: BR_LAN_IP=$BR_LAN_IP"
echo "DEBUG: GATEWAY=$GATEWAY"
echo "DEBUG: DEV=$DEV"
echo "DEBUG: WLAN_MESH_MAC=$WLAN_MESH_MAC"
if [ -n "$DEV" ] && [ -n "$BR_LAN_IP" ] && [ -n "$WLAN_MESH_MAC" ] && [ -n "$GATEWAY" ] && [ "$DEV" != "$WAN_IF" ]; then
echo "DEBUG: SECONDARY"
if ! /usr/bin/voucher show_authorized_macs | grep -iq "$WLAN_MESH_MAC" || ! /usr/bin/voucher show_authorized_ips | grep -iq "$BR_LAN_IP"; then
CODE=$(/usr/bin/voucher add '# whitelist-secondary-node' | awk '{print $4}')
/usr/bin/voucher activate "$CODE" "$WLAN_MESH_MAC" "$BR_LAN_IP"
fi
for f in /etc/pirania/hooks/db_change/*; do
[ -x "$f" ] && "$f"
done
uci set dhcp.pirania="hostrecord"
uci set dhcp.pirania.name="gateway.info"
uci set dhcp.pirania.ip="$GATEWAY"
uci commit dhcp
/etc/init.d/dnsmasq reload
exit 0
elif [ -n "$DEV" ] && [ -n "$BR_LAN_IP" ] && [ "$DEV" = "$WAN_IF" ]; then
echo "DEBUG: PRIMARY"
if [ "$1" != "skip" ]; then
sleep 300
fi
for f in /etc/pirania/vouchers/*.json; do
if grep -q '"name"[[:space:]]*:[[:space:]]*"# whitelist-secondary-node"' "$f"; then
MAC=$(grep -o '"mac"[[:space:]]*:[[:space:]]*"[^"]*"' "$f" | awk -F'"' '{print $4}')
IP=$(grep -o '"ip"[[:space:]]*:[[:space:]]*"[^"]*"' "$f" | awk -F'"' '{print $4}')
if [ -n "$MAC" ] && [ -n "$IP" ]; then
lua -e "require('read_for_access.read_for_access').authorize_mac('$MAC', '$IP')"
fi
fi
done
uci set dhcp.pirania="hostrecord"
uci set dhcp.pirania.name="gateway.info"
uci set dhcp.pirania.ip="$BR_LAN_IP"
uci commit dhcp
/etc/init.d/dnsmasq reload
exit 0
fi
sleep $INTERVAL
ELAPSED=$((ELAPSED + INTERVAL))
done
echo "DEBUG: VARIABLES NOT FOUND"
uci set dhcp.pirania="hostrecord"
uci set dhcp.pirania.name="gateway.info"
uci set dhcp.pirania.ip="$BR_LAN_IP"
uci commit dhcp
/etc/init.d/dnsmasq reload
exit 1
) &
}
2 changes: 1 addition & 1 deletion packages/pirania/files/etc/pirania/portal.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Pirania",
"main_text": "In order to have access to Interet you must enter a valid voucher. In the meantime you can continue using the local network services.",
"main_text": "In order to have access to Internet you must enter a valid voucher. In the meantime you can continue using the local network services.",
"logo": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAABrCAMAAACi04sXAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTExIDc5LjE1ODMyNSwgMjAxNS8wOS8xMC0wMToxMDoyMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZi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Pzp3I/x0WGKVIbCfhj2fHjqZM/7MTm/7OrpDpE0e6Pw3551ckod2ve9tAVfTqrO/5G7CaYrWxZN2czXO6uvTqNwd/DAt+Z1rGTJ/WoYdBImEBWi6n9KuW+UdgkXrFmiAS5EoSupl+vPHqZ6g/A4uEuzT+hCvjq0ozgnrXoP4cLNqCkpqzw4F++bq/+VlYvUJLTleSMn7jR2X/CTAA8COGKzk+uG4AAAAASUVORK5CYII=",
"background_color": "#f0e5de",
"link_title": "",
Expand Down
12 changes: 12 additions & 0 deletions packages/pirania/files/etc/uci-defaults/91-mesh-sync-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
/etc/init.d/pirania-mesh-sync enable
/etc/init.d/pirania-mesh-sync start

unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

unique_append \
'*/10 * * * * /etc/init.d/pirania-mesh-sync restart &> /dev/null' \
/etc/crontabs/root
107 changes: 55 additions & 52 deletions packages/pirania/files/usr/bin/captive-portal
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,72 @@ clean_tables () {
}

set_nftables () {
echo "Apply captive-portal rules"
# Detect wheter add or insert rules
#append_nft_rules=$(uci get pirania.base_config.append_nft_rules 2> /dev/null)
#if [ "$append_nft_rules" = "1" ] ; then
# op="add rule"
#else
# op="insert rule"
#fi

# Create pirania tables
nft create table inet pirania
# Create default tables and chains
nft add table inet pirania
nft add chain inet pirania prerouting { type nat hook prerouting priority 0 \; }
nft add chain inet pirania input { type filter hook input priority 0 \; }
nft add chain inet pirania forward { type filter hook forward priority 0 \; }

# Add mac-adress set
nft add set inet pirania pirania-auth-macs { type ether_addr\; }

# Create ipv4 set on pirania table
nft add set inet pirania pirania-allowlist-ipv4 { type ipv4_addr \; flags interval \; comment \"allow ipv4 list\" \; }
# Create ipv6 set on pirania table
nft add set inet pirania pirania-allowlist-ipv6 { type ipv6_addr \; flags interval \; comment \"allow ipv6 list\" \; }

# Only accept packets from interfaces defined in catch_bridged_interfaces
catch_interfaces=$(uci get pirania.base_config.catch_bridged_interfaces | sed 's/ /,/g')

# stop processing the chain for authorized macs and allowed ips (so they are accepted)
nft add rule inet pirania prerouting ether saddr @pirania-auth-macs ct state new,established,related counter log prefix "ValidSMAC" accept
nft add rule inet pirania prerouting ip daddr @pirania-allowlist-ipv4 ct state new,established,related counter log prefix "ACCEPT-ipv4" accept
nft add rule inet pirania prerouting ip6 daddr @pirania-allowlist-ipv6 ct state new,established,related counter log prefix "ACCEPT-ipv6" accept

# send DNS requests, that are not from valid ips or macs, to our own captive portal DNS at 59053
nft add rule inet pirania prerouting meta l4proto udp udp dport 53 ether saddr != @pirania-auth-macs ct state new,established,related counter log prefix "SMACDNS" redirect to :59053
# redirect packets with dest port 80 to port 59080 of this host (the captive portal page).
nft add rule inet pirania prerouting meta l4proto tcp tcp dport 80 ether saddr != @pirania-auth-macs ct state new,established,related counter log prefix "SMACHTTP" redirect to :59080

#nft add rule inet pirania prerouting meta l4proto tcp tcp dport 80 ip saddr @pirania-allowlist-ipv4 ct state new,established,related counter log prefix "IPv4HTTP" redirect to :59080
#nft add rule inet pirania prerouting meta l4proto tcp tcp dport 80 ip6 saddr @pirania-allowlist-ipv6 ct state new,established,related counter log prefix "IPV6HTTP" redirect to :59080

#nft add rule inet pirania prerouting meta l4proto udp udp dport 53 ip saddr @pirania-allowlist-ipv4 ct state new,established,related counter redirect to :59053
#nft add rule inet pirania prerouting meta l4proto udp udp dport 53 ip6 saddr @pirania-allowlist-ipv6 ct state new,established,related counter redirect to :59053


# reject

#nft add rule inet pirania prerouting drop
#nft add rule inet pirania forward meta mark 0x11/0x11 counter reject with tcp reset
#nft add rule inet pirania forward meta mark 0x11/0x11 counter reject
echo "Apply captive-portal rules"

# Previous cleanup
nft delete table inet pirania 2>/dev/null

# Create the main table
nft add table inet pirania

# Chains
nft add chain inet pirania prerouting \{ type nat hook prerouting priority -100\; \}
nft add chain inet pirania forward \{ type filter hook forward priority 0\; policy accept\; \}

# Sets
nft add set inet pirania pirania-auth-macs \{ type ether_addr\; \}
nft add set inet pirania pirania-auth-ips \{ type ipv4_addr\; \}
nft add set inet pirania pirania-allowlist-ipv4 \{ type ipv4_addr\; flags interval\; comment \"allow ipv4 list\"\; \}
nft add set inet pirania pirania-allowlist-ipv6 \{ type ipv6_addr\; flags interval\; comment \"allow ipv6 list\"\; \}

# Allowlist
nft add rule inet pirania prerouting ip daddr @pirania-allowlist-ipv4 accept
nft add rule inet pirania prerouting ip6 daddr @pirania-allowlist-ipv6 accept

# --- Forward rules ---
# Allow traffic only if both IP and MAC are authorized
nft add rule inet pirania forward ether saddr @pirania-auth-macs ip saddr @pirania-auth-ips accept

# DNS always allowed for redirect
nft add rule inet pirania forward udp dport 53 accept

# --- NAT redirect for unauthorized traffic ---
# HTTP redirect
nft add rule inet pirania prerouting tcp dport 80 ip saddr != @pirania-auth-ips redirect to :59080
nft add rule inet pirania prerouting tcp dport 80 ether saddr != @pirania-auth-macs redirect to :59080

# DNS redirect
nft add rule inet pirania prerouting udp dport 53 ip saddr != @pirania-auth-ips redirect to :59053
nft add rule inet pirania prerouting udp dport 53 ether saddr != @pirania-auth-macs redirect to :59053

# HTTPS block
nft add rule inet pirania forward tcp dport 443 ip saddr != @pirania-auth-ips log prefix "BLOCK_HTTPS" drop
nft add rule inet pirania forward tcp dport 443 ether saddr != @pirania-auth-macs log prefix "BLOCK_HTTPS" drop



echo "Captive-portal rules applied successfully"
}

update_ipsets () {

# Create tables and sets
echo "Updating captive-portal rules"

# Flush macs and ips auth lists to exclude invalidated addresses
nft flush set inet pirania pirania-auth-macs
nft flush set inet pirania pirania-auth-ips

# Add authorized MAC addresses
for mac in $(pirania_authorized_macs) ; do
nft add element inet pirania pirania-auth-macs {$mac}
echo "Adicionando enderecos:" $mac
done
# Add authorized IP addresses
for ip in $(pirania_authorized_ips) ; do
nft add element inet pirania pirania-auth-ips {$ip}
echo "Adicionando enderecos:" $ip
done

# Update pirania-allowlist sets for ipv4 and ipv6
nft flush set inet pirania pirania-allowlist-ipv4
Expand Down Expand Up @@ -109,7 +112,7 @@ elif [ "$1" = "clean" ] || [ "$1" = "stop" ] ; then
elif [ "$enabled" = "1" ]; then
echo "Captive-portal already enabled, reloading rules"
clean_tables
# set_nftables
set_nftables # required for "/etc/init.d/pirania start|restart|reload" if "uci get pirania.base_config.enabled" is "1"
update_ipsets
exit
elif [ "$1" = "enabled" ]; then
Expand All @@ -120,4 +123,4 @@ elif [ "$1" = "enabled" ]; then
else
echo "Pirania captive-portal is disabled. Try running captive-portal start"
exit
fi
fi
7 changes: 7 additions & 0 deletions packages/pirania/files/usr/bin/pirania_authorized_ips
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/lua

local portal = require('portal.portal')

for _, ip in pairs(portal.get_authorized_ips()) do
print(ip)
end
13 changes: 11 additions & 2 deletions packages/pirania/files/usr/bin/voucher
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ captive_portal = {}

vouchera.init()

captive_portal.activate = function(code, mac)
local res = vouchera.activate(code, mac)
captive_portal.activate = function(code, mac, ip)
local res = vouchera.activate(code, mac, ip)
if res then
print('Voucher activated!')
return true
Expand Down Expand Up @@ -97,6 +97,15 @@ captive_portal.show_authorized_macs = function()
return true
end

captive_portal.show_authorized_ips = function()
for _, voucher in pairs(vouchera.vouchers) do
if voucher.is_active() then
print(voucher.ip)
end
end
return true
end

--! if is main
if debug.getinfo(2).name == nil then
local arguments = { ... }
Expand Down
16 changes: 15 additions & 1 deletion packages/pirania/files/usr/lib/lua/portal/portal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,25 @@ function portal.get_authorized_macs()
return auth_macs
end

function portal.get_authorized_ips()
local auth_ips = {}
local with_vouchers = portal.get_config().with_vouchers
if with_vouchers then
local vouchera = require("voucher.vouchera")
vouchera.init()
auth_ips = vouchera.get_authorized_ips()
else
auth_ips = read_for_access.get_authorized_ips()
end
return auth_ips
end

function portal.update_captive_portal(daemonized)
if daemonized then
utils.execute_daemonized('captive-portal update')
else
os.execute('captive-portal update')
-- redirects stdout and stderr to /dev/null to not trigger 502 Bad Gateway after voucher portal auth
os.execute('captive-portal update > /dev/null 2>&1')
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function handlers.authorize_mac()
return uci:get("pirania", "base_config", "url_auth")
end
local client_data = utils.getIpv4AndMac(os.getenv('REMOTE_ADDR'))
read_for_access.authorize_mac(client_data.mac)
read_for_access.authorize_mac(client_data.mac, client_data.ip)
local params = utils.urldecode_params(os.getenv("QUERY_STRING"))
local url_prev = utils.urldecode(params['prev'])
local url_authenticated = uci:get("pirania", "base_config", "url_authenticated")
Expand Down
Loading