Skip to content

Commit

Permalink
chore: optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
yichya committed Feb 4, 2024
1 parent 66ef401 commit 9430511
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 2 additions & 4 deletions core/root/etc/init.d/xray_core
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ STOP=15
USE_PROCD=1
NAME=xray_core

FIREWALL_INCLUDE="/usr/share/xray/firewall_include.ut"

setup_firewall() {
ip rule add fwmark 251 lookup 251
ip route add local default dev lo table 251
ip -6 rule add fwmark 251 lookup 251
ip -6 route add local default dev lo table 251

logger -st xray[$$] -p4 "Generating firewall4 rules..."
/usr/bin/utpl ${FIREWALL_INCLUDE} > /var/etc/xray/firewall_include.nft
/usr/bin/utpl /usr/share/xray/firewall_include.ut > /var/etc/xray/01_firewall_include.nft

logger -st xray[$$] -p4 "Triggering firewall4 restart..."
/etc/init.d/firewall restart
Expand All @@ -27,7 +25,7 @@ flush_firewall() {
ip -6 route del local default dev lo table 251

logger -st xray[$$] -p4 "Flushing firewall4 rules..."
rm -f /var/etc/xray/firewall_include.nft
rm -f /var/etc/xray/*.nft

logger -st xray[$$] -p4 "Triggering firewall4 restart..."
/etc/init.d/firewall restart
Expand Down
20 changes: 11 additions & 9 deletions core/root/usr/share/xray/default_gateway.uc
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ function gen_tp_spec_dv6_dg(pd) {
return "";
}
function generate_include(dg, pd) {
const handle = open("/var/etc/xray/gateway_include.nft", "w");
handle.write(gen_tp_spec_dv4_dg(dg));
handle.write(gen_tp_spec_dv6_dg(pd));
function generate_include(rule_dg, rule_pd, file_path) {
const handle = open(file_path, "w");
handle.write(rule_dg);
handle.write(rule_pd);
handle.flush();
handle.close();
}
function update_nft(dg, pd) {
function update_nft(rule_dg, rule_pd) {
const handle = popen("nft -f -", "w");
handle.write(`table inet fw4 {
${gen_tp_spec_dv4_dg(dg)}
${gen_tp_spec_dv6_dg(pd)}
${rule_dg}
${rule_pd}
}`);
handle.flush();
handle.close();
Expand All @@ -102,7 +102,9 @@ if (log == "") {
print("default gateway not available, please wait for interface ready");
} else {
print(`default gateway available at ${log}\n`);
update_nft(dg, pd);
generate_include(dg, pd);
const rule_dg = gen_tp_spec_dv4_dg(dg);
const rule_pd = gen_tp_spec_dv6_dg(pd);
update_nft(rule_dg, rule_pd);
generate_include(rule_dg, rule_pd, "/var/etc/xray/02_default_gateway_include.nft");
}
restart_dnsmasq_if_necessary();

0 comments on commit 9430511

Please sign in to comment.