From 369b91843c77e769f9b2446ecf7a9a9f4a673148 Mon Sep 17 00:00:00 2001 From: darkclip Date: Sat, 22 Jul 2023 00:20:09 +0800 Subject: [PATCH] update --- .github/workflows/build-sha.yml | 27 ++++++++++++++++++++---- cf-watch.sh | 37 +++++++++++++++++++++++++++++++++ netgen.py | 10 +++++++-- raydata.py | 10 +++++++-- 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100755 cf-watch.sh diff --git a/.github/workflows/build-sha.yml b/.github/workflows/build-sha.yml index 06349a79..10da0d7d 100644 --- a/.github/workflows/build-sha.yml +++ b/.github/workflows/build-sha.yml @@ -40,10 +40,29 @@ jobs: run: rm target/* - name: Rebuild target branch run: | - ./netgen.py ref/china.txt -o target/china.sh -p 'ip route add ' -s $' dev eth0 via 10.11.0.1 || true\n' - ./netgen.py ref/china6.txt -o target/china6.sh -p 'ip route add ' -s $' dev eth0 via fe80::6a77:24ff:fed6:809d || true\n' - ./netgen.py ref/china.txt -o target/rm-china.sh -p 'ip route del ' -s $' dev eth0 via 10.11.0.1 || true\n' - ./netgen.py ref/china6.txt -o target/rm-china6.sh -p 'ip route del ' -s $' dev eth0 via fe80::6a77:24ff:fed6:809d || true\n' + echo 'iptables -t nat -A POSTROUTING -o wgcf -j MASQUERADE || true' >> target/wgcf-up.sh + echo 'ip6tables -t nat -A POSTROUTING -o wgcf -j MASQUERADE || true' >> target/wgcf-up.sh + echo 'ip route add 188.114.96.0/22 dev eth0 || true' >> target/wgcf-up.sh + echo 'ip route add 2606:4700:d0::/47 dev eth0 || true' >> target/wgcf-up.sh + echo 'ip route add 1.1.1.1/32 dev wgcf || true' >> target/wgcf-up.sh + echo 'ip route add 0.0.0.0/1 dev wgcf || true' >> target/wgcf-up.sh + echo 'ip route add 128.0.0.0/1 dev wgcf || true' >> target/wgcf-up.sh + echo 'ip route add ::/1 dev wgcf || true' >> target/wgcf-up.sh + echo 'ip route add 8000::/1 dev wgcf || true' >> target/wgcf-up.sh + ./netgen.py ref/china.txt -o target/wgcf-up.sh -p 'ip route add ' -s $' dev eth0 via 10.11.0.1 || true\n' + ./netgen.py ref/china6.txt -o target/wgcf-up.sh -p 'ip route add ' -s $' dev eth0 via fe80::6a77:24ff:fed6:809d || true\n' + echo 'iptables -t nat -D POSTROUTING -o wgcf -j MASQUERADE || true' >> target/wgcf-down.sh + echo 'ip6tables -t nat -D POSTROUTING -o wgcf -j MASQUERADE || true' >> target/wgcf-down.sh + echo 'ip route del 188.114.96.0/22 dev eth0 || true' >> target/wgcf-down.sh + echo 'ip route del 2606:4700:d0::/47 dev eth0 || true' >> target/wgcf-down.sh + echo 'ip route del 1.1.1.1/32 dev wgcf || true' >> target/wgcf-down.sh + echo 'ip route del 0.0.0.0/1 dev wgcf || true' >> target/wgcf-down.sh + echo 'ip route del 128.0.0.0/1 dev wgcf || true' >> target/wgcf-down.sh + echo 'ip route del ::/1 dev wgcf || true' >> target/wgcf-down.sh + echo 'ip route del 8000::/1 dev wgcf || true' >> target/wgcf-down.sh + ./netgen.py ref/china.txt -o target/wgcf-down.sh -p 'ip route del ' -s $' dev eth0 via 10.11.0.1 || true\n' + ./netgen.py ref/china6.txt -o target/wgcf-down.sh -p 'ip route del ' -s $' dev eth0 via fe80::6a77:24ff:fed6:809d || true\n' + cp cf-watch.sh target/ cd target chmod +x *.sh tar -cf sha.tar *.sh diff --git a/cf-watch.sh b/cf-watch.sh new file mode 100755 index 00000000..e55d1006 --- /dev/null +++ b/cf-watch.sh @@ -0,0 +1,37 @@ +dev=$1; + +get_rnd(){ + echo $(od -An -N2 -i /dev/random); + return $?; +}; + +get_ping_loss(){ + loss_percent=$(ping -c 5 -q $1 | awk '/packet loss/{print $7}'); + echo ${loss_percent%?}; + return $?; +}; + +cf_v4(){ + echo 188.114.$((($(get_rnd) % 4) + 96)).$((($(get_rnd) % 253) + 1)); + return $?; +}; + +cf_v6(){ + hex=$(head /dev/urandom | md5sum | head -c 16); + hex1=$(echo $hex | awk '{print substr($0,1,4)}'); + hex2=$(echo $hex | awk '{print substr($0,5,4)}'); + hex3=$(echo $hex | awk '{print substr($0,9,4)}'); + hex4=$(echo $hex | awk '{print substr($0,13,4)}'); + echo 2606:4700:d$(($(get_rnd) % 2))::$hex1:$hex2:$hex3:$hex4; + return $?; +}; + +watch="1.1.1.1"; +loss=$(get_ping_loss $watch); +while [ $loss -gt 50 ]; do + cfip=$(cf_v4); + echo "changing $dev endpoint to $cfip"; + wg set $dev peer "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=" endpoint "$cfip:4500"; + sleep 5; + loss=$(get_ping_loss $watch); +done; diff --git a/netgen.py b/netgen.py index ab1332f4..71417df2 100755 --- a/netgen.py +++ b/netgen.py @@ -104,8 +104,14 @@ def main(args): output_file = output.resolve() if not output_file.parent.exists(): os.makedirs(output_file.parent) - with output_file.open('w', encoding='utf-8') as wfp: - wfp.writelines(output_lines) + if output_file.is_dir(): + print('Can not write to directory.') + if output_file.exists(): + with output_file.open('a', encoding='utf-8') as wfp: + wfp.writelines(output_lines) + else: + with output_file.open('w', encoding='utf-8') as wfp: + wfp.writelines(output_lines) print(f'Generated Networks: {len(output_lines)}') diff --git a/raydata.py b/raydata.py index 87051aea..759553fe 100755 --- a/raydata.py +++ b/raydata.py @@ -125,8 +125,14 @@ def main(args): output_file = output.resolve() if not output_file.parent.exists(): os.makedirs(output_file.parent) - with output_file.open('w', encoding='utf-8') as wfp: - wfp.writelines(output_lines) + if output_file.is_dir(): + print('Can not write to directory.') + if output_file.exists(): + with output_file.open('a', encoding='utf-8') as wfp: + wfp.writelines(output_lines) + else: + with output_file.open('w', encoding='utf-8') as wfp: + wfp.writelines(output_lines) print(f'Output: {len(output_lines)}')