Just fyi, when YazFi restarts it waits 12sec as previously stated then it restarts firewall without nat-start event. So only the filter chain gets wiped but thats enough to bork a server peer.Something still bothers me. Even if the firewall did restart, which almost always calls nat-start as well, the nat-start script would have restarted the wg script.
You can list all rules in a particular chain and table by:Is there a way to check for a specific rule that was set by your script from the command line? This way, I can try to narrow down where things are being overwritten, etc.
iptables -nvL CHAIN -t table
iptables -nvL POSTROUTING -t nat
iptables -nvL FORWARD -t filter
iptables -nvL POSTROUTING -t nat | grep wg
logger -t $(basename $0) "Wireguard Peer restarted!"
Thanks. How do you do a lock check in the script?Just fyi, when YazFi restarts it waits 12sec as previously stated then it restarts firewall without nat-start event. So only the filter chain gets wiped but thats enough to bork a server peer.
This made the author of wg_manager to move from restarting peers in nat-start to firewall-start instead.
But there have been reported events (altough only 1) where nat-start events happens on reboot of isp equipment (like a power outage) which did not generate a firewall-start.
According to Merlin, all custom firewall rules should go in nat-start except for filter rules which should go in firewall-start. You are populating rules in both so peers should ideally be reset on both events.
And if, then it could be a good idea to build in a lock-file detection so you dont end up executing your restart script multiple times simultaneously.
The following script was created by @eibgrad and works good:Thanks. How do you do a lock check in the script?
#!/bin/sh
# required for serialization when reentry is possible
LOCK="/tmp/$(basename $0).lock"
acquire_lock() { while ! mkdir $LOCK &>/dev/null; do sleep 2; done; }
release_lock() { rmdir $LOCK &>/dev/null; }
# exit (any concurrent instance(s) may now run)
exit_0() { release_lock; exit 0; }
# one instance at a time
acquire_lock
logger -t $(basename $0) "Started [$@]"
## existing scripts ##
logger -t $(basename $0) "Completed [$@]"
exit_0
Thanks @ZebMcKayhan for answering. Got busy with another job there today. I saw the post, but could not step away.You can list all rules in a particular chain and table by:
Code:iptables -nvL CHAIN -t table
i.e:
Code:iptables -nvL POSTROUTING -t nat iptables -nvL FORWARD -t filter
But you will need to find which rule you are interested in. You could further filter the result using i.e grep, like:
This would only output the lines containing wg.Code:iptables -nvL POSTROUTING -t nat | grep wg
You could also output info to your syslog from your script:
Then you could track in syslog what is happening after. Handy sometimes.Code:logger -t $(basename $0) "Wireguard Peer restarted!"
Just fyi, when YazFi restarts it waits 12sec as previously stated then it restarts firewall without nat-start event. So only the filter chain gets wiped but thats enough to bork a server peer.
This made the author of wg_manager to move from restarting peers in nat-start to firewall-start instead.
But there have been reported events (altough only 1) where nat-start events happens on reboot of isp equipment (like a power outage) which did not generate a firewall-start.
According to Merlin, all custom firewall rules should go in nat-start except for filter rules which should go in firewall-start. You are populating rules in both so peers should ideally be reset on both events.
And if, then it could be a good idea to build in a lock-file detection so you dont end up executing your restart script multiple times simultaneously.
The following script was created by @eibgrad and works good:
Code:#!/bin/sh # required for serialization when reentry is possible LOCK="/tmp/$(basename $0).lock" acquire_lock() { while ! mkdir $LOCK &>/dev/null; do sleep 2; done; } release_lock() { rmdir $LOCK &>/dev/null; } # exit (any concurrent instance(s) may now run) exit_0() { release_lock; exit 0; } # one instance at a time acquire_lock logger -t $(basename $0) "Started [$@]" ## existing scripts ## logger -t $(basename $0) "Completed [$@]" exit_0
you will need to wrap this around your existing script and it will make sure multple executions are executed consecutively.
/jffs/scripts/nat-start
into /jffs/scripts/firewall-start
logger
statements in the script, or call the script from wan-event, nat-start, and firewall-start as /jffs/addons/wireguard/start_wg1.sh 2>&1 | tee /tmp/wiregaurd-server-start-log.txt
You wouldnt need to tear down the peers and back up. If you sectionize the script you could call it from nat-start with an argument i.eThis is by far not an elegant way to handle this
start_wg1.sh nat
and the script only re-applies nat and mangle rules.start_wg1.sh firewall
would only reapply filter rules.start
and stop
and ofcource checking if peer is up before reapplying any rules. Well, you know what they say.... "great minds think alike"You wouldnt need to tear down the peers and back up. If you sectionize the script you could call it from nat-start with an argument i.estart_wg1.sh nat
and the script only re-applies nat and mangle rules.
Similar with argstart_wg1.sh firewall
would only reapply filter rules.
Perhaps would be neater?
Continuing adding arguments likestart
andstop
and ofcource checking if peer is up before reapplying any rules.
Before you know it you will have a full-fledged script ready for the addon-section ;-)!
Are you sure? A while ago when I was setting up a custom VLAN on my AC86U, I discovered that whenever the firewall was restarted all the my custom VLAN interfaces were nuked. I had to add my VLAN script into the firewall-start script to rebuild the VLAN interfaces.You wouldnt need to tear down the peers and back up.
Not in the sense as I have actually tested it. But Wireguard is a virtual interface setup by the kernel modules. I hardly think the firmware would remove it.Are you sure?
What are you using for allowed IPs?That’s awesome about the script. Can’t wait to try it.
regarding the dns server, I’m using a windows client and tried setting dns to the router address. I couldn’t access anything on the internet. I’m not sure why.
Welcome To SNBForums
SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.
If you'd like to post a question, simply register and have at it!
While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!