Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix firewall error #248

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
32 changes: 32 additions & 0 deletions root/etc/homeproxy/scripts/firewall_pre_forward.ut
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/utpl -S

{%-
import { cursor } from 'uci';

const cfgname = 'homeproxy';
const uci = cursor();
uci.load(cfgname);

const routing_mode = uci.get(cfgname, 'config', 'routing_mode') || 'bypass_mainland_china',
proxy_mode = uci.get(cfgname, 'config', 'proxy_mode') || 'redirect_tproxy';

let outbound_node, tun_name;
if (match(proxy_mode, /tun/)) {
if (routing_mode === 'custom')
outbound_node = uci.get(cfgname, 'routing', 'default_outbound') || 'nil';
else
outbound_node = uci.get(cfgname, 'config', 'main_node') || 'nil';

if (outbound_node !== 'nil')
tun_name = uci.get(cfgname, 'infra', 'tun_name') || 'singtun0';
}

const server_enabled = uci.get(cfgname, 'server', 'enabled');
let auto_firewall = '0';
if (server_enabled === '1')
auto_firewall = uci.get(cfgname, 'server', 'auto_firewall') || '0';

-%}
{% if (tun_name): %}
oifname {{ tun_name }} counter accept comment "!{{ cfgname }}: accept tun forward"
{% endif %}
31 changes: 11 additions & 20 deletions root/etc/homeproxy/scripts/firewall_pre.ut → ...c/homeproxy/scripts/firewall_pre_input.ut
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,19 @@
auto_firewall = uci.get(cfgname, 'server', 'auto_firewall') || '0';

-%}

{% if (tun_name || auto_firewall === '1'): %}
{% if (tun_name): %}
chain forward {
oifname {{ tun_name }} counter accept comment "!{{ cfgname }}: accept tun forward"
}
iifname {{ tun_name }} counter accept comment "!{{ cfgname }}: accept tun input"
{% endif %}

{% if (tun_name || auto_firewall === '1'): %}
chain input {
{% if (tun_name): %}
iifname {{ tun_name }} counter accept comment "!{{ cfgname }}: accept tun input"
{% endif %}
{%
if (auto_firewall === '1')
uci.foreach(cfgname, 'server', (s) => {
if (s.enabled !== '1')
return;

let proto = s.network || '{ tcp, udp }';
printf(' meta l4proto %s th dport %s counter accept comment "!%s: accept server %s"\n',
proto, s.port, cfgname, s['.name']);
});
if (auto_firewall === '1')
uci.foreach(cfgname, 'server', (s) => {
if (s.enabled !== '1')
return;

let proto = s.network || '{ tcp, udp }';
printf('meta l4proto %s th dport %s counter accept comment "!%s: accept server %s"\n',
proto, s.port, cfgname, s['.name']);
});
%}
}
{% endif %}
6 changes: 4 additions & 2 deletions root/etc/init.d/homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ start_service() {
fi

# Setup firewall
utpl -S "$HP_DIR/scripts/firewall_pre.ut" > "$RUN_DIR/fw4_pre.nft"
utpl -S "$HP_DIR/scripts/firewall_pre_forward.ut" > "$RUN_DIR/fw4_pre_forward.nft"
utpl -S "$HP_DIR/scripts/firewall_pre_input.ut" > "$RUN_DIR/fw4_pre_input.nft"
[ "$outbound_node" = "nil" ] || utpl -S "$HP_DIR/scripts/firewall_post.ut" > "$RUN_DIR/fw4_post.nft"
fw4 reload >"/dev/null" 2>&1

Expand Down Expand Up @@ -353,7 +354,8 @@ stop_service() {
nft flush set inet fw4 "$i"
nft delete set inet fw4 "$i"
done 2>"/dev/null"
echo > "$RUN_DIR/fw4_pre.nft" 2>"/dev/null"
echo > "$RUN_DIR/fw4_pre_forward.nft" 2>"/dev/null"
echo > "$RUN_DIR/fw4_pre_input.nft" 2>"/dev/null"
echo > "$RUN_DIR/fw4_post.nft" 2>"/dev/null"
fw4 reload >"/dev/null" 2>&1

Expand Down
18 changes: 14 additions & 4 deletions root/etc/uci-defaults/luci-homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

uci -q batch <<-EOF >"/dev/null"
delete firewall.homeproxy_pre
set firewall.homeproxy_pre=include
set firewall.homeproxy_pre.type=nftables
set firewall.homeproxy_pre.path="/var/run/homeproxy/fw4_pre.nft"
set firewall.homeproxy_pre.position="table-pre"

delete firewall.homeproxy_pre_input
set firewall.homeproxy_pre_input=include
set firewall.homeproxy_pre_input.type=nftables
set firewall.homeproxy_pre_input.path="/var/run/homeproxy/fw4_pre_input.nft"
set firewall.homeproxy_pre_input.position="chain-pre"
set firewall.homeproxy_pre_input.chain="input"

delete firewall.homeproxy_pre_forward
set firewall.homeproxy_pre_forward=include
set firewall.homeproxy_pre_forward.type=nftables
set firewall.homeproxy_pre_forward.path="/var/run/homeproxy/fw4_pre_forward.nft"
set firewall.homeproxy_pre_forward.position="chain-pre"
set firewall.homeproxy_pre_forward.chain="forward"

delete firewall.homeproxy_post
set firewall.homeproxy_post=include
Expand Down