Homing So

Homing So

🎉 哈喽啊~各位
github

How to elegantly use sing-box in Windows

What is sing-box#

sing-box is a next-generation universal proxy platform, benchmarking Xray-core and Clash, and it supports multiple protocols (as the name suggests) and has very powerful performance.

sing-box client#

As of now, the official graphical interface clients supported by sing-box are:

While the Windows desktop client is still under development, there is support for third-party graphical clients, such as:

However, the availability of these clients is poor, and many links provided by airports cannot be used directly, and there is an additional learning curve. Although the official Windows graphical client is still under development, NON-GUI is fully supported. For friends with CLI usage experience, this article can help you elegantly use sing-box cli on Windows.

Installing sing-box#

Here are two methods to install sing-box cli:

The first method does not require much explanation. The only thing to note is that you may have noticed that there are two versions for all AMD64 architectures (darwin, linux, and windows), one labeled amd64 and the other amd64v3. How do you choose?

This is the Architecture Level introduced by GO 1.18, which differs in the instruction set that the compiler can use:

  • GOAMD64=v1 (default): Baseline. All 64-bit x86 processors can use it.
  • GOAMD64=v2: All instruction sets of v1, plus CMPXCHG16B, LAHF, SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3.
  • GOAMD64=v3: All instruction sets of v2, plus AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, OSXSAVE.
  • GOAMD64=v4: All instruction sets of v3, plus AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL.

In simple terms, the higher the Level, the newer the available instructions, and the compiled code may have some performance improvements, but compatibility will be worse. If you don't know what instruction set your CPU supports, then using amd64 is never wrong.

The second method is to use package managers on the Windows platform for installation, such as Scoop, Chocolatey, etc. Check the official documentation for package manager installation methods.

# If you use Scoop
scoop install sing-box

# If you use Chocolatey
choco install sing-box

The advantage of this method is that you don't have to worry about the updates of sing-box and other trivial matters. The package manager will take care of these things for you.

Configuring sing-box service#

If you have to open the terminal every time you use sing-box, it would be too annoying. So it is best to configure it as a Windows service, so that we don't have to manually run it every time we restart, and let Windows handle all these tasks! To achieve this, we need to use WinSW, which is a tool that can configure any executable file as a Windows service.

First, create a directory, here my directory is called sing-box, then download WinSW-x64.exe, put it in this directory and rename it to: winsw.exe. Then we create a winsw.xml file to configure sing-box through this file. Note! The names of these two files must be the same, otherwise WinSW will not be able to read the configuration file.

Then edit winsw.xml and write the following content:

<service>
  <id>sing-box</id>
  <name>sing-box</name>
  <description>This service runs sing-box continuous integration system.</description>
  <download from="https://your-subscription-link" to="%BASE%\config.json" auth="sspi" />
  <executable>C:\Users\homin\scoop\shims\sing-box.exe</executable>
  <arguments>run</arguments>
  <log mode="reset" />
  <onfailure action="restart" />
</service>

Here are some explanations for a few parameters:

  • <download>: Before running the specified <executable>, WinSW will retrieve the resource from the specified URL and place it as a file locally. %BASE% points to the directory of WinSW, so here the content of the subscription link is renamed as config.json and placed in the current directory.
  • <executable>: Specifies the executable file to be launched, here it is specified as the path of sing-box installed by Scoop, this path can be obtained after installing sing-box: whereis sing-box.
  • <arguments>: Specifies the arguments to be passed to the executable file, here it is run. Because the execution is in the current path, there is no need to specify the path of config.json.

After completion, you can use ./winsw.exe -h to view all the instructions of WinSW:

./winsw.exe -h

Now use: ./winsw.exe install to install the service. After the installation is complete, the sing-box will automatically run every time the system restarts. But now let's start the service with ./winsw.exe start. You will see the winsw.wrapper.log file in the current directory, which contains the logs when the service starts. If everything is normal, the log should look like this:

2024-02-07 11:34:16,941 DEBUG - Starting WinSW in console mode
2024-02-07 11:34:19,748 DEBUG - Starting WinSW in console mode
2024-02-07 11:34:19,802 INFO  - Starting service 'sing-box (sing-box)'...
2024-02-07 11:34:20,512 DEBUG - Starting WinSW in service mode
2024-02-07 11:34:20,559 INFO  - Service 'sing-box (sing-box)' started successfully.
2024-02-07 11:34:20,629 INFO  - Downloading: https://your-subscription-link to C:\Users\homin\workspace\sing-box\config.json. failOnError=False
2024-02-07 11:34:23,004 INFO  - Starting C:\Users\homin\scoop\shims\sing-box.exe run
2024-02-07 11:34:23,043 INFO  - Started process 27484
2024-02-07 11:34:23,063 DEBUG - Forwarding logs of the process System.Diagnostics.Process (sing-box) to WinSW.ResetLogAppender

The winsw.out.log in the directory contains the logs generated during the operation of sing-box.

At this point, we have achieved the seamless use of sing-box on Windows, and implemented automatic subscription updates.

Reference#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.