This commit is contained in:
darkclip committed 2024-01-02 05:13:35 +08:00
1 parent bbfcb1eb7f
commit 2ec6200388
1 file changed
+64 -32
+64 -32
View File
@@ -95,8 +95,8 @@ generate_ips(){
while true; do while true; do
ip=$($prog); ip=$($prog);
unique=true; unique=true;
for index in $@; do for item in $@; do
if [ "$index" = "$ip" ]; then if [ "$item" = "$ip" ]; then
unique=false; unique=false;
fi; fi;
done; done;
@@ -134,22 +134,43 @@ speedtest(){
get_ip_cadidates(){ get_ip_cadidates(){
limit=100 limit=100
ip_file='/var/tmp/ip.txt' ip_file='/var/tmp/ip.txt'
for index in $(generate_ips $limit $1); do for item in $(generate_ips $limit $1); do
echo "$index" >> "$ip_file"; echo "$item" >> "$ip_file";
done; done;
if [ $4 = 'true' ]; then if [ $4 = 'true' ]; then
ip_list=$(speedtest $(archAffix) "$ip_file" $3); result_list=$(speedtest $(archAffix) "$ip_file" $3);
else else
ip_list=$(head -$3 "$ip_file" | awk '{print $1":"'$2'}'); result_list=$(head -$3 "$ip_file" | awk '{print $1":"'$2'}');
fi; fi;
result=$?; ret_code=$?;
rm -f "$ip_file"; rm -f "$ip_file";
echo $ip_list; echo $result_list;
if [ $result -ne 0 ]; then if [ $ret_code -ne 0 ]; then
exit 1; exit 1;
fi; fi;
}; };
resolve_ip_addresses(){
addresses=$(cat "$1");
port=$2;
set --;
for addr in $addresses; do
ips=$(getent ahosts $addr | awk '{print $1}');
for ip in $ips; do
unique=true;
for item in $@; do
if [ "$item" = "$ip:$port" ]; then
unique=false;
fi;
done;
if [ $unique = 'true' ]; then
set -- $@ "$ip:$port";
fi;
done;
done;
echo $@;
};
main(){ main(){
num_cadidates=10; num_cadidates=10;
dl_retry_times=3; dl_retry_times=3;
@@ -162,23 +183,27 @@ main(){
echo "Running on $arch" echo "Running on $arch"
loss=$(get_ping_loss $WATCH_ADD); loss=$(get_ping_loss $WATCH_ADD);
while [ $loss -ge $LOSS_THR ]; do while [ $loss -ge $LOSS_THR ]; do
if [ $retry -lt $dl_retry_times ]; then if [ ! "$ENDPOINTS" ]; then
echo "Try to run speed test" if [ $retry -lt $dl_retry_times ]; then
cadidates=$(get_ip_cadidates $FAMILY $DEFAULT_PORT $num_cadidates true); echo "Try to run speed test"
cadidates=$(get_ip_cadidates $FAMILY $DEFAULT_PORT $num_cadidates true);
else
echo "Use random ip on port $DEFAULT_PORT"
cadidates=$(get_ip_cadidates $FAMILY $DEFAULT_PORT $num_cadidates false);
fi;
if [ $? -ne 0 ]; then
retry=$(($retry + 1));
continue;
fi
else else
echo "Use random ip on port $DEFAULT_PORT" cadidates=$(resolve_ip_addresses "$ENDPOINTS" $DEFAULT_PORT);
cadidates=$(get_ip_cadidates $FAMILY $DEFAULT_PORT $num_cadidates false);
fi; fi;
if [ $? -ne 0 ]; then for ip_port in $cadidates; do
retry=$(($retry + 1));
continue;
fi
for cfip in $cadidates; do
loss=$(get_ping_loss $WATCH_ADD); loss=$(get_ping_loss $WATCH_ADD);
if [ $loss -lt $LOSS_THR ]; then if [ $loss -lt $LOSS_THR ]; then
break; break;
fi; fi;
echo "set $INTERFACE endpoint to $cfip"; echo "set $INTERFACE endpoint to $ip_port";
if [ $TEST_RUN = 'true' ]; then if [ $TEST_RUN = 'true' ]; then
continue; continue;
fi; fi;
@@ -188,7 +213,7 @@ main(){
if [ ! $INTERFACE ]; then if [ ! $INTERFACE ]; then
continue; continue;
fi; fi;
wg set $INTERFACE peer 'bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=' endpoint $cfip; wg set $INTERFACE peer 'bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=' endpoint $ip_port;
sleep $wait_in_between; sleep $wait_in_between;
done done
if [ $TEST_RUN = 'true' ]; then if [ $TEST_RUN = 'true' ]; then
@@ -223,17 +248,19 @@ fi;
usage(){ usage(){
echo "Usage: $0 [-4] [-6] [-i <INTERFACE>] [-c <CONFIG>] [-a <ADDRESS>] [-l <LOSS>] [-p <PORT>] [-t] [-d]"; echo "Usage: $0 [OPTIONS]";
echo "Options:" echo "Options:"
echo " -4 IPv4" echo " -4 IPv4 (default)"
echo " -6 IPv6" echo " -6 IPv6"
echo " -i INTERFACE Interface" echo " -i <INTERFACE> Interface"
echo " -c CONFIG Config" echo " -c <CONFIG> Config"
echo " -a ADDRESS Watch address" echo " -a <ADDRESS> Watch address (default: $WATCH_ADD)"
echo " -l LOSS Loss threshold" echo " -l <LOSS> Loss threshold (default: $LOSS_THR)"
echo " -p PORT Default port" echo " -p <PORT> endpoint's default port (default: $DEFAULT_PORT)"
echo " -t Speed test only" echo " -e <ENDPOINTS_FILE> File of endpoint's addresses"
echo " -d Dry run" echo " -t Speed test only"
echo " -d Dry run"
echo "Note: If -e is not set, generate endpoints for warp, else resolve addresses and use default port."
} }
FAMILY=4; FAMILY=4;
@@ -242,9 +269,10 @@ CONFIG='';
WATCH_ADD='1.1.1.1'; WATCH_ADD='1.1.1.1';
LOSS_THR=30; LOSS_THR=30;
DEFAULT_PORT=4500; DEFAULT_PORT=4500;
ENDPOINTS='';
TEST_RUN=false; TEST_RUN=false;
DRY_RUN=false; DRY_RUN=false;
while getopts ":h46i:c:a:l:p:td" opt; do while getopts ":h46i:c:a:l:p:e:td" opt; do
case $opt in case $opt in
h) h)
usage; usage;
@@ -270,6 +298,9 @@ while getopts ":h46i:c:a:l:p:td" opt; do
p) p)
DEFAULT_PORT=$OPTARG; DEFAULT_PORT=$OPTARG;
;; ;;
e)
ENDPOINTS=$OPTARG;
;;
t) t)
TEST_RUN=true; TEST_RUN=true;
;; ;;
@@ -286,3 +317,4 @@ while getopts ":h46i:c:a:l:p:td" opt; do
done done
main main