Latency Testing Methods for Clash Nodes in the Fanqiang Repository
The bannedbook/fanqiang repository implements two distinct latency testing methods for Clash nodes: a built-in HTTP HEAD request mechanism via the UrlTest function in fqnews2/libcore/box.go, and an external shell script at v2ss/server-cfg/fqbench.sh that parses output from speedtest-cli.
The bannedbook/fanqiang repository provides comprehensive tools for circumventing network restrictions using Clash and other proxy technologies. Understanding the available latency testing methods for Clash nodes is essential for optimizing connection performance and automating node selection based on real-time response metrics.
Built-in URL-Test Latency Measurement
Clash provides native latency testing capabilities that integrate directly with its policy routing engine. This method operates continuously within the Clash process, providing real-time metrics for automatic node selection.
How the Internal Latency Test Works
The built-in mechanism sends periodic HTTP HEAD requests to a configurable target URL and measures the round-trip time (RTT). When you configure a url-test or fallback proxy group, Clash executes these requests against each node in the group at specified intervals. The resulting latency value in milliseconds determines which node receives traffic, with lower latency nodes prioritized when they fall within the configured tolerance threshold.
Configuring the URL-Test Group
To enable automatic latency-based switching, define a url-test proxy group in your configuration file. Specify a lightweight, reliable target URL such as https://www.gstatic.com/generate_204, set the testing interval in seconds, and define a tolerance value in milliseconds to prevent excessive switching between nodes with similar performance.
# config.yaml
proxies:
- name: "US-Node"
type: vmess
server: us.example.com
port: 443
uuid: <your-uuid>
alterId: 0
cipher: auto
proxy-groups:
- name: "Auto-Test"
type: url-test
url: https://www.gstatic.com/generate_204
interval: 300
tolerance: 50
proxies:
- US-Node
Source Code Implementation
The core latency testing logic resides in fqnews2/libcore/box.go within the UrlTest function. According to the source code, this function implements the following signature:
func UrlTest(i *BoxInstance, link string, timeout int32) (latency int32, err error)
The function accepts a BoxInstance pointer, a target URL string, and a timeout value in seconds. It returns the measured latency as an int32 representing milliseconds, or an error if the request fails or exceeds the timeout threshold.
External Speed-Test Script Method
For comprehensive benchmarking that includes bandwidth measurements alongside latency, the repository provides a shell script wrapper around the speedtest-cli utility.
How the Benchmark Script Works
The v2ss/server-cfg/fqbench.sh script executes the external speedtest-cli binary to perform full network diagnostics. Unlike the lightweight HEAD request method, this approach measures actual upload and download speeds while extracting latency values from the tool's detailed output log. This method suits batch testing scenarios or CI/CD pipelines where comprehensive metrics are required.
Running the Latency Benchmark
Execute the script after making it executable. The script automatically invokes speedtest-cli, captures its output to ./speedtest-cli/speedtest.log, and parses the results for display.
# Make the script executable and run it
chmod +x v2ss/server-cfg/fqbench.sh
./v2ss/server-cfg/fqbench.sh
# Example output format
Node Name Upload Speed Download Speed Latency
---------------------------------------------------------------
US-Node 85.3Mbps 125.7Mbps 23 ms
Script Implementation Details
The latency extraction logic uses awk pattern matching to isolate values from the speedtest-cli log:
awk '/Latency/{print $2" "$3}' ./speedtest-cli/speedtest.log
This command searches for lines containing "Latency" in the log file and prints the second and third fields, typically containing the numerical value and unit (e.g., "23 ms").
Comparing Latency Testing Approaches
Both methods serve the same fundamental purpose—providing numeric latency data for node evaluation—but differ significantly in execution context and overhead:
- Built-in
UrlTestruns within the Clash process, provides sub-second measurement of TCP/HTTP handshake times, and updates the UI and policy engine in real-time. - External
fqbench.shinvokes separate processes for comprehensive speed testing, consumes more system resources, and is designed for manual benchmarking rather than continuous monitoring.
Summary
- The built-in URL-test method in
fqnews2/libcore/box.goprovides lightweight, periodic latency checks using HTTP HEAD requests to configurable endpoints. - The
UrlTestfunction returns latency as anint32value representing milliseconds, accepting timeout parameters to handle unresponsive nodes. - The external benchmark script at
v2ss/server-cfg/fqbench.shoffers detailed bandwidth and latency testing via speedtest-cli integration. - Configuration of
url-testgroups requires settinginterval(seconds),tolerance(milliseconds), and a lightweight target URL in your Clash configuration file.
Frequently Asked Questions
What is the difference between Clash's built-in latency test and the fqbench.sh script?
The built-in method performs lightweight HTTP HEAD requests continuously within the Clash process to measure TCP connection latency, while fqbench.sh executes external speedtest-cli binaries to measure actual bandwidth and latency in batch testing scenarios. The built-in approach is designed for real-time policy routing, whereas the script provides comprehensive diagnostics for manual evaluation.
How do I configure automatic node selection based on latency in Clash?
Create a url-test proxy group in your configuration file, set the url parameter to a fast, reliable endpoint like https://www.gstatic.com/generate_204, and define the interval parameter to control testing frequency. Set the tolerance value in milliseconds to prevent unnecessary switching between nodes with statistically similar performance.
Where is the latency testing logic implemented in the bannedbook/fanqiang repository?
The internal latency measurement logic is implemented in the UrlTest function within fqnews2/libcore/box.go, which handles HTTP-based round-trip time calculations. The external benchmarking wrapper resides in v2ss/server-cfg/fqbench.sh, which parses speedtest-cli output using awk pattern matching to extract latency values from test logs.
What does the UrlTest function return?
According to the source code in fqnews2/libcore/box.go, the UrlTest function returns an int32 value representing the measured latency in milliseconds, along with an error interface. If the request succeeds within the specified timeout period, the latency value indicates the round-trip time for the HTTP HEAD request; otherwise, the function returns an error indicating timeout or connection failure.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →