Cygwin下Bash Shell通过netstat命令一键查询代理端口,并查询代理类型(HTTP或SOCKS)

stevehe 2024年02月02日 104次浏览

trojanports函数本体:

trojanports() {
    ## 读取 Trojan-qt5.exe 代理端口配置;
	## See Also:"D:\Extra\Trojan-Qt5-Windows-1.4.0\config.json"
	print_color 40 "通过 netstat 命令获取 trojan-qt5.exe 端口监听信息..."
	local pid=$(ps2 trojan-qt5.exe --nopath|dos2unix -q|awk -F '=' '/ProcessId=/{print $2;exit}')
	[ -z "$pid" ] && print_color 9 "没有发现 trojan-qt5.exe 进程!" && return
	local portsInfo=$(netstat -ano|grep $pid|dos2unix -q|awk -F " " '{if(match($2,/0.0.0.0:/) && $4=="LISTENING") print;}'|tee /dev/tty)
	local portsInfo=$(echo "$portsInfo"|awk -F " " '{sub("0.0.0.0","127.0.0.1",$2);print}')
	print_color 40 "探测各个端口类型..."
	while read line
	do
		#echo "$line--"
		local proxyAddr=$(echo "$line"|cut -d' ' -f2)
		local testCount=0
		while :;
		do 
			[[ "$testAddr" != "" && "$testAddr" =~ ^socks5:// ]] && local testAddr="http://$proxyAddr" || local testAddr="socks5://$proxyAddr"
			curl -s --connect-timeout 3 -x "$testAddr" https://www.baidu.com &>/dev/null
			local curlRet=$?
			#[ $curlRet -eq 0 ] && {
			[ $curlRet -eq 0 -o $curlRet -eq 56 ] && {
				print_color 70 "$proxyAddr 采用以下方式调用:"
				[ $curlRet -eq 56 ] && print_color 40 "【提示】:该端口可能是PAC代理端口..."
				cat <<EOF
export http_proxy=$testAddr
export https_proxy=$testAddr
export all_proxy=$testAddr
---
setallproxy ${testAddr/http:\/\//}

EOF
				break
			}
			let testCount+=1
			[ $testCount -ge 2 ] && break
		done
		unset testAddr
	done <<<"$portsInfo"
}

运行截图:

image.png

Github Gist地址:

https://gist.github.com/hexiyou/fa45fc193b06c61e7d923d633c1fdbdd

可能会有后续更新,Bug修复或功能增强,etc...