Skip to content

Instantly share code, notes, and snippets.

@vimagick
Last active March 1, 2024 12:17
Show Gist options
  • Save vimagick/e14ed4873e1b0b582f0e55e6fe6d5ed9 to your computer and use it in GitHub Desktop.
Save vimagick/e14ed4873e1b0b582f0e55e6fe6d5ed9 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# shadowsocks connectivity check
#
INPUT_FILE=${1:?input file is empty}
OUTPUT_FILE=${2:?output file is empty}
LISTEN_ADDR=127.0.0.1
LISTEN_PORT=1080
PID_FILE=/tmp/ss-local.pid
WAIT_TIMEOUT=5
TEST_URL=https://ipinfo.io
echo "host,port,method,password" > "$OUTPUT_FILE"
grep -oP '^ss://\S+@\S+:\d+' "$INPUT_FILE" |
while read uri; do
echo "### $uri ###"
if [[ $uri =~ ^ss://(.*)@(.*):([0-9]+)$ ]]; then
account=($(echo ${BASH_REMATCH[1]} | base64 -d 2>/dev/null | tr ':' ' '))
if ((${#account[@]}!=2)); then
continue
fi
method=${account[0]}
password=${account[1]}
host=${BASH_REMATCH[2]}
port=${BASH_REMATCH[3]}
if lsof -nP -iTCP:$LISTEN_PORT -s TCP:listen; then
echo "Failed to listen on $LISTEN_ADDR:$LISTEN_PORT"
exit 1
fi
echo ">>> ss-local -f $PID_FILE -s $host -p $port -b $LISTEN_ADDR -l $LISTEN_PORT -m $method -k $password"
if ss-local -f $PID_FILE -s $host -p $port -b $LISTEN_ADDR -l $LISTEN_PORT -m $method -k $password >/dev/null 2>&1; then
sleep $WAIT_TIMEOUT
echo ">>> curl -s -f -m $WAIT_TIMEOUT -w '\n' -x socks5h://$LISTEN_ADDR:$LISTEN_PORT $TEST_URL"
if curl -s -m $WAIT_TIMEOUT -w '\n' -x socks5h://$LISTEN_ADDR:$LISTEN_PORT $TEST_URL; then
echo "$host,$port,$method,$password" >> "$OUTPUT_FILE"
fi
kill $(cat $PID_FILE)
fi
fi
done
# csvlook "$OUTPUT_FILE"
@vimagick
Copy link
Author

$ cat input.txt
ss://[email protected]:443
ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpjZmExN2EwZC01ZTdkLTQ4NzEtOTc2ZS1mMTE2ZTA4MTgxMjM=@service.ouluyun9803.com:21003

$ ./ss-test.sh input.txt output.csv
### ss://[email protected]:443 ###
>>> ss-local -f /tmp/ss-local.pid -s 54.202.3.34 -p 443 -b 127.0.0.1 -l 1080 -m aes-256-cfb -k amazonskr05
>>> curl -s -m 5 -w '\n' -x socks5h://127.0.0.1:1080 https://ipinfo.io
{
  "ip": "54.202.3.34",
  "hostname": "ec2-54-202-3-34.us-west-2.compute.amazonaws.com",
  "city": "Boardman",
  "region": "Oregon",
  "country": "US",
  "loc": "45.8399,-119.7006",
  "org": "AS16509 Amazon.com, Inc.",
  "postal": "97818",
  "timezone": "America/Los_Angeles",
  "readme": "https://ipinfo.io/missingauth"
}
### ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpjZmExN2EwZC01ZTdkLTQ4NzEtOTc2ZS1mMTE2ZTA4MTgxMjM=@service.ouluyun9803.com:21003 ###
>>> ss-local -f /tmp/ss-local.pid -s service.ouluyun9803.com -p 21003 -b 127.0.0.1 -l 1080 -m chacha20-ietf-poly1305 -k cfa17a0d-5e7d-4871-976e-f116e0818123
>>> curl -s -m 5 -w '\n' -x socks5h://127.0.0.1:1080 https://ipinfo.io
{
  "ip": "152.67.16.27",
  "city": "Airoli",
  "region": "Maharashtra",
  "country": "IN",
  "loc": "19.1167,72.9833",
  "org": "AS31898 Oracle Corporation",
  "postal": "400701",
  "timezone": "Asia/Kolkata",
  "readme": "https://ipinfo.io/missingauth"
}

$ csvlook -I output.csv
| host                    | port  | method                 | password                             |
| ----------------------- | ----- | ---------------------- | ------------------------------------ |
| 54.202.3.34             | 443   | aes-256-cfb            | amazonskr05                          |
| service.ouluyun9803.com | 21003 | chacha20-ietf-poly1305 | cfa17a0d-5e7d-4871-976e-f116e0818123 |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment