laylink/scripts/verify-socks5.sh
2026-05-28 20:19:28 +08:00

80 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -u
SOCKS5_HOST="${SOCKS5_HOST:-127.0.0.1}"
SOCKS5_PORT="${SOCKS5_PORT:-1080}"
SOCKS5_USER="${SOCKS5_USER:-}"
SOCKS5_PASSWORD="${SOCKS5_PASSWORD:-}"
CONNECT_TIMEOUT="${CONNECT_TIMEOUT:-10}"
MAX_TIME="${MAX_TIME:-30}"
proxy="socks5h://${SOCKS5_HOST}:${SOCKS5_PORT}"
auth=()
if [[ -n "${SOCKS5_USER}" || -n "${SOCKS5_PASSWORD}" ]]; then
auth=(--proxy-user "${SOCKS5_USER}:${SOCKS5_PASSWORD}")
fi
if ! command -v curl >/dev/null 2>&1; then
echo "ERR curl_not_found"
exit 127
fi
echo "Using SOCKS5 proxy: ${proxy}"
echo
echo "[1/2] HTTPS connectivity: https://bing.com/"
bing_code="$(
curl \
--silent \
--show-error \
--location \
--output /dev/null \
--write-out '%{http_code}' \
--proxy "${proxy}" \
"${auth[@]}" \
--connect-timeout "${CONNECT_TIMEOUT}" \
--max-time "${MAX_TIME}" \
https://bing.com/
)"
curl_status=$?
if [[ ${curl_status} -ne 0 ]]; then
echo "ERR bing_request_failed status=${curl_status}"
exit "${curl_status}"
fi
echo "bing_http_code=${bing_code}"
if [[ ! "${bing_code}" =~ ^(200|301|302|307|308)$ ]]; then
echo "ERR bing_unexpected_status"
exit 1
fi
echo
echo "[2/2] Egress IP: https://ip.sb/"
egress_ip="$(
curl \
--silent \
--show-error \
--location \
--proxy "${proxy}" \
"${auth[@]}" \
--connect-timeout "${CONNECT_TIMEOUT}" \
--max-time "${MAX_TIME}" \
https://ip.sb/
)"
curl_status=$?
if [[ ${curl_status} -ne 0 ]]; then
echo "ERR ip_sb_request_failed status=${curl_status}"
exit "${curl_status}"
fi
egress_ip="$(printf '%s' "${egress_ip}" | tr -d '[:space:]')"
echo "egress_ip=${egress_ip}"
if [[ ! "${egress_ip}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ && ! "${egress_ip}" =~ : ]]; then
echo "ERR ip_sb_unexpected_response"
exit 1
fi
echo
echo "OK socks5_https_verified"