What's new

SBS. Script for using sing-box on Asus routers with Merlin firmware.

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Kyjiep

Regular Contributor
I created an sbtun interface using sing-box on my router RT-AX86U with firmware Merlin 3004.388.7. How can I route traffic from a device with the address 192.168.50.31, which is in the router's network, through this sbtun interface?
Sorry for my English.

Upd. I did this and much more. I present to your attention SBS (sing-box-script).
To do this, your router, in addition to the Merlin firmware, needs a USB drive with Entware installed. To download, install, configure and run sing-box, simply run this command in the router's command line:
Code:
wget -O /jffs/scripts/sbs https://raw.githubusercontent.com/Dr4tez/sing-box4asus/main/sbs && chmod 775 /jffs/scripts/sbs && /jffs/scripts/sbs install
The supplemented and revised Readme no longer fits within the character limit of this forum for one message. You can read it by downloading the attached Readme.txt file. The same applies to the Changes.
Page on github.
 

Attachments

  • Changes.txt
    12.3 KB · Views: 6
  • Readme.txt
    11.2 KB · Views: 8
Last edited:
I created an sbtun interface using sing-box on my router RT-AX86U with firmware Merlin 3004.388.7. How can I route traffic from a device with the address 192.168.50.31, which is in the router's network, through this sbtun interface?
Sorry for my English.
Routes are destination ip based, not source ip based. So if the ips you would like to access via sbtun interface are one or more unique confined ranges you could just add a route in the main route table. All lan could then access these ip/ranges via sbtun interface.

If you plan on accessing I.e internet (all ips) via this interface only from a specific lan ip you will need to setup a new routing table and create ip rules that your lan ip will use only. It gets a tad more messy.
 
Routes are destination ip based, not source ip based. So if the ips you would like to access via sbtun interface are one or more unique confined ranges you could just add a route in the main route table. All lan could then access these ip/ranges via sbtun interface.

If you plan on accessing I.e internet (all ips) via this interface only from a specific lan ip you will need to setup a new routing table and create ip rules that your lan ip will use only. It gets a tad more messy.
Thank you. I already figured this out using ChatGPT, and with its help I even made the corresponding script. It works as I wanted. There are some minor flaws that I couldn't eliminate, but I'm happy with it anyway. Later I will translate the readme into English and upload it here along with the script.
 
My script for running sing-box on Asus routers with Merlin firmware. I have not found other methods on the Internet. It is primitive, but it works. I am not a programmer, and I made this script using ChatGPT. I would be glad to see any improvements from those who know how to make scripts.
You can download the archive with everything you need at https://github.com/Dr4tez/my_domains/raw/main/sing-box-script+readme.zip. I understand that the archive will cause mistrust in many. But I initially did everything in the archive, because in this form everything is simpler and faster, and I have to write fewer instructions. The archive contains readme.txt and a nested archive, which contains a script, sing-box and a sample of my config.json. First, read readme.txt, then do everything according to it.

Upd. The download link for the archive no longer works. Installation and configuration via commands in the router console are already available.
Code:
wget -O /jffs/scripts/sb-script-install https://raw.githubusercontent.com/Dr4tez/sing-box4asus/main/sb-script-install && chmod 775 /jffs/scripts/sb-script-install && /jffs/scripts/sb-script-install
This command no longer works because the files for this version of the script have been removed.
 
Last edited:
My script for running sing-box on Asus routers with Merlin firmware. I have not found other methods on the Internet. It is primitive, but it works. I am not a programmer, and I made this script using ChatGPT. I would be glad to see any improvements from those who know how to make scripts.
You can download the archive with everything you need at https://github.com/Dr4tez/my_domains/raw/main/sing-box-script+readme.zip. I understand that the archive will cause mistrust in many. But I initially did everything in the archive, because in this form everything is simpler and faster, and I have to write fewer instructions. The archive contains readme.txt and a nested archive, which contains a script, sing-box and a sample of my config.json. First, read readme.txt, then do everything according to it.
I checked your script quickly and from what I understand it creates a new routing table with only a default route in it. Nothing else. Then points one IP to use this table.

The problem I see is any local routes (I.e. to guest wifi or other stuff) will not be present in this table and even if it does not create problems for you, right now, it probably will for others. I would propose to use PRIO to set ip rule priority and use a "from all lookup main suppress_prefixlength 0" with higher priority.

This is how Wireguards userspace tool does it for ubuntu:

$ip rule
Code:
0:      from all lookup local
32764:  from all lookup main suppress_prefixlength 0
32765:  not from all fwmark 0xca6c lookup 51820
32766:  from all lookup main
32767:  from all lookup default

The usage of suppress_prefixlength 0 is available on our routers and would give a routing improvement for your script I believe.

Edit: Oh, and another thing... your router may flush all firewall rules and rebuild them on various events. In order to make sure the firewall rules continue to work over time they would need to be re-applied in firewall-start hook script https://github.com/RMerl/asuswrt-merlin.ng/wiki/User-scripts#firewall-start
 
Last edited:
I checked your script quickly and from what I understand it creates a new routing table with only a default route in it. Nothing else. Then points one IP to use this table.

The problem I see is any local routes (I.e. to guest wifi or other stuff) will not be present in this table and even if it does not create problems for you, right now, it probably will for others. I would propose to use PRIO to set ip rule priority and use a "from all lookup main suppress_prefixlength 0" with higher priority.

This is how Wireguards userspace tool does it for ubuntu:

$ip rule
Code:
0:      from all lookup local
32764:  from all lookup main suppress_prefixlength 0
32765:  not from all fwmark 0xca6c lookup 51820
32766:  from all lookup main
32767:  from all lookup default

The usage of suppress_prefixlength 0 is available on our routers and would give a routing improvement for your script I believe.

Edit: Oh, and another thing... your router may flush all firewall rules and rebuild them on various events. In order to make sure the firewall rules continue to work over time they would need to be re-applied in firewall-start hook script https://github.com/RMerl/asuswrt-merlin.ng/wiki/User-scripts#firewall-start
Now I’m reworking the script so that it automatically finds the desired directory on the Entware flash drive, downloads the sing-box archive from the developer’s GitHub, unzips it, and creates the necessary subdirectories and files.
By the way, initially you can specify several IPs in the script, not just one.
I don’t plan to solve the problem of access from guest wi-fi yet, maybe in the future, this is not such a priority issue, I don’t even know.
Now I still didn’t understand anything about PRIO.
But I will solve the problem with the firewall rules as quickly as possible.
Thanks for the advices.
There is one strange thing: in the list of processes it is indicated that the sing-box process uses 1227m of RAM, but in the web interface of the router it shows that more than 500 MB of 1024 are free and swap is not used. This does not seem to cause problems, but it confuses me.
As I already said, I'm not a programmer and don't know much about scripts. So this is somewhat difficult for me, even using chatGPT, which also gets things wrong sometimes.
 
Last edited:
I don’t plan to solve the problem of access from guest wi-fi yet, maybe in the future, this is not such a priority issue, I don’t even know.
Now I still didn’t understand anything about PRIO.
I understand, but it's a quite easy fix, just add it after adding the other rules so it gets added last:
Like:
Code:
for IP in $DEVICE_IPS; do
        echo "Adding routing rule for $IP..."
        ip rule add from $IP table $ROUTE_TABLE
    done
ip rule add from all lookup main suppress_prefixlength 0

And also remove it before the others:
Code:
ip rule del from all lookup main suppress_prefixlength 0
for IP in $DEVICE_IPS; do
        echo "Removing routing rule for $IP..."
        ip rule del from $IP table $ROUTE_TABLE
    done

You can list your rules by
Code:
ip rule
You see the priority numbers and rules are processed in order from low number to high number. You can specify the priority number when adding a rule, like:
Code:
ip rule add from all lookup main suppress_prefixlength 0 prio 32764

and then you can easily remove it by:
Code:
ip rule del prio 32764


Now I’m reworking the script so that it automatically finds the desired directory on the Entware flash drive, downloads the sing-box archive from the developer’s GitHub, unzips it, and creates the necessary subdirectories and files.
That's great! Perhaps it's time to move this thread to the Addons section?
 
That's great! Perhaps it's time to move this thread to the Addons section?
Maybe. How to do it? Although it is far from an addon, just a small script for now. But maybe there will be more specific help there. I do not have enough free time to study scripting, support the project, and especially take into account the wishes of other people. I will be making the necessary changes for quite a long time.
There is another serious problem. When I run sing-box from the command line, it works until I close the command line. I found out that after closing it, the output of the command
Code:
ip route show table 222
disappears, for which the command
Code:
ip route add default dev sbtun table 222
is responsible. The rest of the changes made by the script are saved after closing the command line. But the script stops working correctly. How can this be solved?
 
Last edited:
I would assume the route dissappears because the interface sbtun does not exist anymore? Could you confirm this using i.e "ifconfig sbtun"?

Perhaps some more information relevant to merlin fw here https://www.snbforums.com/threads/struggling-to-write-a-non-killable-process.62455/post-556998
The rest of the changes made by the script are saved after closing the command line.
The sbtun interface exists after closing the command line. There are also rules like
Code:
from 192.168.50.31 lookup 222
and created firewall rules. And the sing-box process continues its work.
 
The sbtun interface exists after closing the command line
Hmm, ok. weird...

Now that I think of it, both fw and other vpn managers (like Wireguard Session Manager) Does not use default route in the policy tables. Instead they use 0.0.0.0/1 & 128.0.0.0/1. I wonder if that is the reason something like that happens??

What if you replace
Code:
ip route add default dev $TUN_INTERFACE table $ROUTE_TABLE
With
Code:
ip route add 0.0.0.0/1 dev $TUN_INTERFACE table $ROUTE_TABLE
ip route add 128.0.0.0/1 dev $TUN_INTERFACE table $ROUTE_TABLE

You would also need to make the same change for removing the rules
Change
Code:
ip route del default dev $TUN_INTERFACE table $ROUTE_TABLE
To
Code:
ip route del 0.0.0.0/1 dev $TUN_INTERFACE table $ROUTE_TABLE
ip route del 128.0.0.0/1 dev $TUN_INTERFACE table $ROUTE_TABLE

Would that make any difference?
 
This doesn't change anything. I suspect that when I close the command line the 222 routing table itself disappears.
I wouldn't think so. You could test this by adding some route manually, I.e
Code:
ip route add 10.11.12.13 dev br0 table 222
Then log out and back in and check that it's still there. It is for me.

More likely your sbtun interface goes down and up again, even so shortly. When an interface goes down it's associated routes are also removed.

Starting the interface outside of the shell session might solve this, like in firewall-start or a cron job.

Another solution would be to re-add the routes periodically in a cron job. But since it's not running continuously you could expect some minutes of outages.
 
I have made some improvements to the script.
1. Added a command to restart the script. (See readme.txt in the archive)
2. Added automatic creation of system scripts firewall-start and services-start, as well as checks for their existence, automatic addition and deletion of records and rules in them, as well as checks for their existence, to prevent their duplication. This is necessary to prevent sudden incorrect termination of the script during some events in the system, as well as for the correct autostart of the script when the router is rebooted.
3. Added a check for the existence of the sing-box process to prevent duplication of some functions. Now, when you try to run the script twice in a row, it will not allow it.
4. Fixed the found errors.
You can download the updated archive using the same link as before https://github.com/Dr4tez/my_domains/raw/main/sing-box-script+readme.zip
This link no longer works because the files for this version of the script have been removed.
 
Last edited:
I wouldn't think so. You could test this by adding some route manually, I.e
Yes, you are right, rules can still be added...
Here's what I noticed: if after starting the script from the console, close this console window, then open it again and execute the command
Code:
ip route add default dev sbtun table 222
, then the script starts working correctly again. And it works correctly even after closing this console window.
 
Last edited:
That's great! Perhaps it's time to move this thread to the Addons section?
Maybe. How to do it? Although it is far from an addon, just a small script for now. But maybe there will be more specific help there.
You would just ping a moderator, like @thiggins and ask him to move the thread to the asuswrt-merlin addons section. I think this fits better there.

I do not have enough free time to study scripting, support the project, and especially take into account the wishes of other people. I will be making the necessary changes for quite a long time.
It could be whatever you want it to be. If you choose to just leave it as it is, then that's great too!
 
The download link for the archive no longer works. Installation and configuration via commands in the router console will be available soon.
 
I did it. To download, install, configure and run sing-box, simply run this command in the router console:
Code:
wget -O /jffs/scripts/sb-script-install https://raw.githubusercontent.com/Dr4tez/sing-box4asus/main/sb-script-install && chmod 775 /jffs/scripts/sb-script-install && /jffs/scripts/sb-script-install
This command no longer works because the files for this version of the script have been removed.
Please read the attached readme file first.
 
Last edited:

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top