dotfiles

Dash Eclipse's dotfiles
git clone git://ezup.dev/dotfiles.git
Log | Files | Refs | README | LICENSE

trt-fzf.sh (8940B)


      1#!/bin/bash
      2
      3: "${progname:="${0##*/}"}"
      4TROJAN_SUBURL=""
      5CSV="/home/user/.local/share/data/trojan/trojan.csv" # use absolute path
      6
      7urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
      8
      9country_zh2code() {
     10	sed -e '
     11	s/,俄羅斯 /,RU /
     12	s/,印度 /,IN /
     13	s/,土耳其 /,TR /
     14	s/,巴西 /,BR /
     15	s/,德國 /,DE /
     16	s/,新加坡 /,SG /
     17	s/,日本 /,JP /
     18	s/,法國 /,FR /
     19	s/,澳大利亞 /,AU /
     20	s/,美國 /,US /
     21	s/,臺灣 /,TW /
     22	s/,英國 /,GB /
     23	s/,菲律賓 /,PH /
     24	s/,韓國 /,KR /
     25	s/,香港 /,HK /
     26	#s/,阿聯酋 /,AE /
     27	'
     28}
     29
     30country_code2name() {
     31	sed -e '
     32	s/AU/Australia/
     33	s/BR/Brazil/
     34	s/DE/Germany/
     35	s/FR/Frace/
     36	s/GB/United Kingdom/
     37	s/HK/Hong Kong/
     38	s/IN/India/
     39	s/JP/Japan/
     40	s/KR/South Korea/
     41	s/PH/Philippines/
     42	s/RU/Russia/
     43	s/SG/Singapore/
     44	s/TR/Turkey/
     45	s/TW/Taiwan/
     46	s/US/United States/
     47	'
     48}
     49
     50query_ipv4() {
     51	all_servers=$1
     52	while true; do
     53		servers=$(printf "$all_servers" | grep '[[:alpha:]]')
     54		if [[ -z "$servers" ]]; then
     55			all_servers=$(echo "$all_servers" | sort | uniq | paste -s -d' ')
     56			break
     57		fi
     58		servers_ip=$(dig +short -t A $servers)
     59		all_servers=$(echo "$all_servers" | sed "/[[:alpha:]]/d")
     60		all_servers+=$(echo -e "\n$servers_ip")
     61	done
     62	echo $all_servers
     63}
     64
     65query_ipv6() {
     66	all_servers=$1
     67	while true; do
     68		servers=$(printf "$all_servers" | grep -v ':' | grep '[[:alpha:]]')
     69		if [[ -z "$servers" ]]; then
     70			all_servers=$(echo "$all_servers" | sort | uniq | paste -s -d' ')
     71			break
     72		fi
     73		servers_ip=$(dig +short -t AAAA $servers)
     74		all_servers=$(echo "$all_servers" | sed "/\./d")
     75		all_servers+=$(echo -e "\n$servers_ip")
     76	done
     77	echo $all_servers
     78}
     79
     80url2csv() {
     81	read -p "Type $(tput bold)Yes$(tput sgr0) to continue... " && [ $REPLY == "Yes" ] || { echo "$(tput bold; tput setaf 1)Canceled$(tput sgr0)"; exit 1; }
     82	: >$CSV
     83	#      1        2           3           4            5            6        7      8               9              10
     84	#echo "password,remote_addr,remote_port,country_code,country_name,provider,number,time_appconnect,speed_download,ipv4" >>$CSV
     85	BASE64_URLS=$(curl --progress-bar $TROJAN_SUBURL)
     86	total=$(echo "$BASE64_URLS" | base64 -d | wc -l)
     87	local i=0
     88	while IFS=, read -r password remote_addr remote_port mark; do
     89		local i=$(($i + 1))
     90		mark=${mark//,}
     91		#mark=${mark/ /,}
     92		#country=${mark%% *}; mark=${mark#*$country}; mark=${mark/ /}
     93		#provider=${mark%% *}; mark=${mark#*$provider}; number=${mark/ /}
     94		IFS=" " read -r country_code provider number <<<$(echo "$mark")
     95		country_name=$(echo "$country_code" | country_code2name)
     96		CONFIG=$HOME/.local/service/trojan-client/client.json switch_proxy
     97		local ipv4=$(query_ipv4 $remote_addr)
     98		sv restart $HOME/.local/service/trojan-client >/dev/null
     99		sleep 0.8
    100		time_appconnect=$(curl --connect-timeout 2 -x socks5h://127.0.0.1:51837 -o/dev/null -sw '%{time_appconnect}' 'https://connectivitycheck.gstatic.com/generate_204')
    101		time_appconnect=$(printf "%.0f ms" "$(bc<<<${time_appconnect}*1000)")
    102		speed_download=$(curl -x socks5h://127.0.0.1:51837 http://speedtest-sgp1.digitalocean.com/10mb.test --progress-bar -o/dev/null --write-out '%{speed_download}' | numfmt --to=iec-i --suffix=B/s)
    103		echo "$i/$total | $country_code $provider $number | $time_appconnect $speed_download | $ipv4"
    104		echo "$password,$remote_addr,$remote_port,$country_code,$country_name,$provider,$number,$time_appconnect,$speed_download,$ipv4" >>$CSV
    105		sleep 2
    106	done < <(urldecode "$(echo "$BASE64_URLS" | base64 -d | sed 's#^trojan://##' | sed 's/?allowInsecure=1&tfo=1//' | sed 's/@/,/; s/:/,/; s/#/,/')" | country_zh2code)
    107}
    108
    109switch_proxy() {
    110	#jo run_type=client local_addr=0.0.0.0 local_port=51837 remote_addr=$remote_addr remote_port=$remote_port password=$(jo -a $password) log_level=0 ssl=$(jo verify=false verify_hostname=true cert= cipher="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:AES128-SHA:AES256-SHA:DES-CBC3-SHA" sni= alpn=$(jo -a h2 "http/1.1") reuse_session=true session_ticket=false curse=) tcp=$(jo no_delay=true keep_alive=true reuse_port=false fast_open=false fast_open_qlen=20) >$CONFIG
    111	#jo run_type=nat local_addr=127.0.0.1 local_port=16280 remote_addr=$remote_addr remote_port=$remote_port password=$(jo -a $password) log_level=0 ssl=$(jo verify=false verify_hostname=true cert= cipher="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:AES128-SHA:AES256-SHA:DES-CBC3-SHA" sni= alpn=$(jo -a h2 "http/1.1") reuse_session=true session_ticket=false curse=) tcp=$(jo no_delay=true keep_alive=true reuse_port=false fast_open=false fast_open_qlen=20) >$CONFIG
    112	jj -v "$remote_addr" -p -i $CONFIG -o $CONFIG 'remote_addr'
    113	jj -v "$remote_port" -p -i $CONFIG -o $CONFIG 'remote_port'
    114	jj -v "$password" -p -i $CONFIG -o $CONFIG 'password.0'
    115}
    116
    117select_srv() {
    118	while true; do
    119		local SORTED_CSV=$(cat $CSV | awk 'BEGIN{FS=OFS=","}{print $9,$0}' | sort -hr)
    120		local NUM=$(while IFS=, read -r speed password remote_addr remote_port country_code country_name provider number time_appconnect speed_download ipv4; do
    121			echo "$speed_download,$time_appconnect,$country_code,$provider,$number,$remote_addr,$remote_port,$country_name"
    122		done < <(echo "$SORTED_CSV") | sed '1iSpeed,Time,Country code,Provider,Number,Address,Port,Country name' | awk 'BEGIN{FS=OFS=","}{print i++","$0}' | column -s, -t | fzf | cut -d' ' -f1)
    123		[ -z "$NUM" ] && exit 0
    124		if [ "$NUM" -ne 0 ]; then
    125			IFS=, read speed password remote_addr remote_port country_code country_name provider number time_appconnect speed_download ipv4 <<<$(echo "$SORTED_CSV" | sed "${NUM}q;d")
    126			break
    127		fi
    128	done
    129}
    130
    131reset_iptables_rules() {
    132	#[ "$EUID" -ne 0 ] && { echo "Please run as root" && exit 1; }
    133	## https://gist.github.com/jarek-przygodzki/29830f868e0c29e1dccb09beafbc4f72
    134	iptables -P INPUT ACCEPT
    135	iptables -P FORWARD ACCEPT
    136	iptables -P OUTPUT ACCEPT
    137	iptables -F INPUT
    138	iptables -F OUTPUT
    139	iptables -F FORWARD
    140	iptables -F
    141	iptables -t nat -F
    142	iptables -t mangle -F
    143	iptables -X
    144	iptables -t nat -X
    145	iptables -t mangle -X
    146	echo "Resetted iptables rules"
    147}
    148add_iptables_rules() {
    149	reset_iptables_rules
    150	## https://github.com/shadowsocks/shadowsocks-libev#transparent-proxy
    151	iptables -t nat -N TROJAN
    152	for ip in $ipv4; do
    153		iptables -t nat -A TROJAN -d $ip -j RETURN
    154	done
    155	iptables -t nat -A TROJAN -d 0.0.0.0/8 -j RETURN
    156	iptables -t nat -A TROJAN -d 10.0.0.0/8 -j RETURN
    157	iptables -t nat -A TROJAN -d 127.0.0.0/8 -j RETURN
    158	iptables -t nat -A TROJAN -d 169.254.0.0/16 -j RETURN
    159	iptables -t nat -A TROJAN -d 172.16.0.0/12 -j RETURN
    160	iptables -t nat -A TROJAN -d 192.168.0.0/16 -j RETURN
    161	iptables -t nat -A TROJAN -d 224.0.0.0/4 -j RETURN
    162	iptables -t nat -A TROJAN -d 240.0.0.0/4 -j RETURN
    163	iptables -t nat -A TROJAN -p tcp -j REDIRECT --to-ports 16280
    164	iptables -t nat -A OUTPUT -j TROJAN
    165	echo "Added iptables rules"
    166}
    167
    168get_info() {
    169	printf 'Clint: '
    170	grep "^$(jq -r '.password[0] + "," + .remote_addr + "," + (.remote_port|tostring)' ~/.local/service/trojan-client/client.json)," $CSV | cut -d',' -f4,6,7 --output-delimiter=' '
    171	printf 'Nat:   '
    172	grep "^$(jq -r '.password[0] + "," + .remote_addr + "," + (.remote_port|tostring)' /etc/trojan/config.json)," $CSV | cut -d',' -f4,6,7 --output-delimiter=' '
    173}
    174
    175usage() {
    176	cat <<_EOF | GREP_COLORS='ms=1' egrep --color "$progname|$"
    177Usage: $progname -f       get csv
    178       $progname s        select node
    179       $progname c        switch node for trojan client
    180       $progname n        switch node for trojan NAT
    181
    182       $progname r        reset iptables rules
    183       $progname a        add iptables rules
    184
    185_EOF
    186	exit 1
    187}
    188
    189
    190case $1 in
    191	-f)	url2csv;;
    192	s)
    193		select_srv
    194		echo "$country_code $provider $number ($remote_addr)"
    195		;;
    196	c)
    197		CONFIG=$HOME/.local/service/trojan-client/client.json
    198		select_srv
    199		echo "Cient: $country_code $provider $number ($remote_addr)"
    200		switch_proxy
    201		sv restart $HOME/.local/service/trojan-client >/dev/null
    202		;;
    203	n)
    204		[ "$EUID" -ne 0 ] && { echo "Please run as root" && exit 1; }
    205		CONFIG=/etc/trojan/config.json
    206		select_srv
    207		echo "NAT: $country_code $provider $number ($remote_addr)"
    208		echo "IPv4: $ipv4"
    209		switch_proxy
    210		sv restart trojan
    211		add_iptables_rules
    212		;;
    213	r)
    214		[ "$EUID" -ne 0 ] && { echo "Please run as root" && exit 1; }
    215		reset_iptables_rules
    216		;;
    217	a)
    218		[ "$EUID" -ne 0 ] && { echo "Please run as root" && exit 1; }
    219		ipv4=$(grep "^$(jq -r '.password[0] + "," + .remote_addr + "," + (.remote_port|tostring)' /etc/trojan/config.json)," $CSV | cut -d',' -f10 --output-delimiter=' ')
    220		add_iptables_rules
    221		;;
    222	i)
    223		get_info
    224		;;
    225	*) usage;;
    226esac
    227exit 0