shadowsocks-rust icon indicating copy to clipboard operation
shadowsocks-rust copied to clipboard

How to count traffic usage of different users

Open JuTemp opened this issue 4 months ago • 5 comments

JuTemp avatar Aug 24 '25 01:08 JuTemp

Shadowsocks-rust Config Here is my Shadowsocks-rust config file (desensitized)

{
    "servers": [
        {
            "server": "0.0.0.0",
            "server_port": 23000,
            "password": "tCQVWysYldR8D4AkOf4AfGgmy1x9q2yoQJP/qYhjko8=",
            "method": "2022-blake3-chacha20-poly1305"
        },
        {
            "server": "0.0.0.0",
            "server_port": 23001,
            "password": "BoJ+dzSxqQntCCpsgRGhPHlh4JvzV+m6H4VLf+dU/Ks=",
            "method": "2022-blake3-chacha20-poly1305"
        }
    ]
}

It seems that Shadowsocks does not support configuring different users on the same port, and no relevant configuration was found (however, this seems to be a requirement of TCP, one port corresponds to one service)

Shadowsocks Manager Config I don't know why shadowsocks sends stats periodically instead of preparing data and waiting for client requests. This is counterintuitive. I wasted a lot of time because of this.

Here is my Shadowsocks-rust entrypoint file

#!/bin/sh

apk add jq

STATS_FILE='/var/log/shadowsocks/stats'
mkdir -p /var/log/shadowsocks
echo user-stats-file: /var/log/shadowsocks/stats

echo '{}' > ${STATS_FILE}

ssserver --manager-addr 127.0.0.1:8080 &

while :; do
    response=`nc -u -w 10 -l -p 8080 | head -n 1 | grep -v timeout `
    obj=`echo ${response} | cut -d ':' -f 2- `
    jq -c --argjson obj "${obj}" ". + $obj" < ${STATS_FILE} > ${STATS_FILE}.update && mv ${STATS_FILE}.update ${STATS_FILE}
done

When you need to request data:

docker compose exec -it shadowsocks cat /var/log/shadowsocks/stats

Because the method of creating a new file is used, it cannot be exposed using --volume (folders can be exposed)

Another strange thing is that I don't know why the data returned every 10 seconds is a certain port, not all. I can't get the latest data in time by writing it this way. I can only merge the data myself.

There is another very strange thing, I don't know why I have to restart the udp server over and over again. If I use the same udp server, I can only get the first data, like this

nc -u -l -p 8080 | xargs -I{} echo {}

JuTemp avatar Aug 24 '25 01:08 JuTemp

It seems that Shadowsocks does not support configuring different users on the same port

Use 2022-blake3-aes-128-gcm or 2022-blake3-aes-256-gcm instead.

https://github.com/shadowsocks/shadowsocks-rust/issues/1626#issuecomment-2310011435 The REST API should work much better for you.

database64128 avatar Aug 24 '25 06:08 database64128

Related: https://github.com/shadowsocks/shadowsocks-rust/issues/1626 .

zonyitoo avatar Aug 25 '25 02:08 zonyitoo

@database64128 Thanks for your reply. I also want to ask a question. My judgment is that low-performance devices (mobile phones) use chacha20, and high-performance devices (computers) use aes. Is this true?

JuTemp avatar Aug 26 '25 07:08 JuTemp

@JuTemp Prefer AES-based ciphers unless the CPU does not have AES instructions. Modern ARM64 processors have AES instructions, so you'll get higher throughput with lower energy use with AES-GCM compared to ChaCha20-Poly1305.

database64128 avatar Aug 26 '25 08:08 database64128