diff --git a/README.md b/README.md index f584368..3ec8202 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,8 @@ # scripts -This repository contains some (linux) scripts I am using to simplify my daily work (call it what you want), modify applications to make them look more estatic or just because I can and I have too much time. +This repository contains some Linux scripts I am using to simplify some of my workflows. +- Android + - [mitmproxy.sh](android/mitmproxy.sh) - Start an [AVD](https://developer.android.com/studio/run/managing-avds) with [mitmproxy](https://mitmproxy.org/) enabled for it -### Overview - -- [Change firefox icons](/firefox-icon-fix) -- [Merge pdf files together](/merge-pdf) -- [Rename files random](/randomize-filename) -- [Rename files based on their MD5 hashsum](/hashify-filename) -- [Beautify java source code](/java-beautifier) -- [Name availability check (websites & github)](/nameavability) - -If you want to use any of the scripts globally, execute [globalize.sh](globalize.sh) +- DNS + - [clear-cache.sh](dns/clear-cache.sh) - Clear the systemd DNS cache diff --git a/android/mitmproxy.sh b/android/mitmproxy.sh new file mode 100755 index 0000000..d05e640 --- /dev/null +++ b/android/mitmproxy.sh @@ -0,0 +1,156 @@ +#!/usr/bin/env sh + +res= + +choose_emulator() { + emulators=($(emulator -list-avds | tr ' ' '\n')) + + if [ ${#emulators[@]} -eq 0 ]; then + echo "No avds found. Please create (at least) one" + exit 1 + fi + + echo "Choose one of the following ${#emulators[@]} avds:" + i=0 + for emulator in ${emulators[@]}; do + i=$((i+1)) + echo " $i) $emulator" + done + printf "AVD (number): " + read avd + avd_index=$((avd-1)) + + if [ ! -v 'emulators[avd_index]' ]; then + echo "'$avd' is not a valid number" + exit 1 + fi + + res=${emulators[$avd_index]} +} + +ca_certname() { + certname=$(openssl x509 -inform PEM -subject_hash_old -in ~/.mitmproxy/mitmproxy-ca-cert.cer | head -1) + res=$certname.0 +} + +ensure_ca_cert() { + ca_certname + certname=$res + cp ~/.mitmproxy/mitmproxy-ca-cert.cer ~/.mitmproxy/$certname +} + +proxy_address() { + proxy=$(ip route get 1.1.1.1 | sed -n 's/.*src \([0-9.]\+\).*/\1/p'):8080 + if [[ -v MITMPROXY_ADDRESS ]]; then + proxy=$MITMPROXY_ADDRESS + echo "Using proxy address set via 'MITMPROXY_ADDRESS' env variable ($proxy)" + else + echo "Using default proxy settings ($proxy). Set the 'MITMPROXY_ADDRESS' env variable to overwrite the proxy address. Always use your network address instead of 127.0.0.1" + fi + res=$proxy +} + +verify_commands() { + commands=("adb" "emulator" "mitmproxy") + for command in "${commands[@]}"; do + which $command &> /dev/null + if [ $? -ne 0 ]; then + # if the command cannot be found it maybe wasn't added to PATH but still exists on the system + case $command in + adb) + PATH=$PATH:$HOME/Android/Sdk/platform-tools + ;; + emulator) + PATH=$PATH:$HOME/Android/Sdk/emulator + ;; + esac + which $command &> /dev/null + if [ $? -ne 0 ]; then + echo "command '$command' not found" + exit 1 + fi + echo "Warn: command '$command' was found ($(which $command)) but isn't in PATH" + fi + done +} + +verify_certs() { + if [ ! -f ~/.mitmproxy/mitmproxy-ca-cert.cer ]; then + echo "mitmproxy ca cert doesn't exist (~/.mitmproxy/mitmproxy-ca-cert.cer). Please run mitmproxy at least once to generate it" + exit 1 + fi +} + +up() { + verify_certs + choose_emulator + avd=$res + + # shut down existing avds + down &> /dev/null + + adb kill-server &> /dev/null + adb start-server > /dev/null + emulator -avd $avd -writable-system > /dev/null & + adb wait-for-device + + if [ "$(adb shell getprop ro.build.fingerprint)" = "*/release-keys" ]; then + echo "Doesn't work on release android version. Please use an emulator without Google Play." + exit 1 + fi + + ensure_ca_cert + ca_certname + ca_cert=$res + + adb root + # without sleeping adb wouldn't find the emulator + sleep 5 + + api_version=$(adb shell getprop ro.build.version.sdk) + if [[ "$api_version" -gt "28" ]]; then + adb shell avbctl disable-verification + adb reboot + adb wait-for-device + adb root + sleep 5 + fi + + adb remount + + adb push ~/.mitmproxy/$ca_cert /system/etc/security/cacerts + adb shell chmod 644 /system/etc/security/cacerts/$ca_cert + + adb reboot + # wait until device has actually booted. this is required b/c it needs to be booted in order to set the proxy + while [ "`adb shell getprop sys.boot_completed 2> /dev/null | tr -d '\r' `" != "1" ]; do sleep 1; done + + proxy_address + proxy_addr=$res + adb shell settings put global http_proxy $proxy_addr +} + +down() { + adb shell settings delete global http_proxy + adb shell reboot -p +} + +main() { + case "$1" in + up) + verify_commands + up + ;; + down) + verify_commands + down + ;; + *) + echo "$0 [up|down]" + exit 1 + ;; + esac +} + +main $@ + diff --git a/dns/clear-cache.sh b/dns/clear-cache.sh new file mode 100755 index 0000000..4fb0784 --- /dev/null +++ b/dns/clear-cache.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env sh + +verify_commands() { + commands=("systemd-resolve" "resolvectl") + for command in "${commands[@]}"; do + which $command &> /dev/null + if [ $? -ne 0 ]; then + echo "command '$command' not found" + exit 1 + fi + done +} + +main() { + verify_commands + + # elevate root rights if not present + if [ "$UID" -ne 0 ]; then + sudo "$0" "$@" + exit $? + else + systemd-resolve --flush-caches + resolvectl flush-caches + systemd-resolve --statistics + + if [ $? -ne 0 ]; then + echo "Failed to flush cache. Maybe the systemd-resolved service is not started/enabled?" + exit 1 + fi + fi +} + +main $@ diff --git a/firefox-icon-fix/README.md b/firefox-icon-fix/README.md deleted file mode 100644 index 131e591..0000000 --- a/firefox-icon-fix/README.md +++ /dev/null @@ -1,30 +0,0 @@ -## Firefox Icon Fix - -This changes the firefox icon to whatever image you want. -The [default image](firefox-icon-fix.png) is Senko from the anime 'The Helpful Fox Senko-san'. -If you want to change the image, just replace 'firefox-icon-fix.png' with the image you want. Your image **has to be** named 'firefox-icon-fix.png' too. - -Since the script uses softlinks to reduce space, make sure you never delete or change this directory if it is on your disk and you have fixed the icons with the script. - -**NOTE:** -For better usage make the script executable first: -```console -$ chmod +x firefox-icon-fix.sh -``` - -#### Usage - -Fix icons: -```console -$ ./firefox-icon-fix.sh -``` - -Unfix icons (restore the real ones): -```console -$ ./firefox-icon-fix.sh unfix -``` - -Re-fix icons after firefox update (the icons are sadly getting resetted after every firefox update): -```console -$ ./firefox-icon-fix.sh --overwrite -``` diff --git a/firefox-icon-fix/firefox-icon-fix.png b/firefox-icon-fix/firefox-icon-fix.png deleted file mode 100644 index 1645e38..0000000 Binary files a/firefox-icon-fix/firefox-icon-fix.png and /dev/null differ diff --git a/firefox-icon-fix/firefox-icon-fix.sh b/firefox-icon-fix/firefox-icon-fix.sh deleted file mode 100644 index 8f02fd8..0000000 --- a/firefox-icon-fix/firefox-icon-fix.sh +++ /dev/null @@ -1,39 +0,0 @@ -install_path=/usr/lib/firefox* - -skipping=false -fixed_file=$(readlink -f firefox.png) - -for file in $(find $install_path -type f,l -name "default*.png" && find /usr/share/icons -type f,l -name "firefox*.png") -do - if [ "$#" -ge 1 ] && [ $1 == "unfix" ]; then - if [ -f "$file.bak" ]; then - rm $file - mv "$file.bak" $file - echo "Unfixed file $file" - else - echo "Skipped file $file since no backup of it exists" - fi - elif [ -f "$file.bak" ]; then - if [ "$#" -ge 1 ] && [ $1 == "--overwrite" ]; then - mv $file "$file.bak" - ln -s -r firefox-icon-fix.png $file - if [ -f "$file.bak" ]; then - echo "Fixed file $file" - fi - else - skipping=true - echo "Skipped file $file" - fi - elif [ $file != $fixed_file ]; then - mv $file "$file.bak" - ln -s -r firefox-icon-fix.png $file - if [ -f "$file.bak" ]; then - echo "Fixed file $file" - fi - fi -done - -if $skipping; then - echo "" - echo "If your firefox version has updated and you want to fix the icons (the icon changes are getting resetted by ever update) use this command with the'--overwrite' flag. Example: $0 --overwrite" -fi diff --git a/globalize.sh b/globalize.sh deleted file mode 100755 index 7ed891f..0000000 --- a/globalize.sh +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -check_and_exit() { - if [ $? -eq 0 ]; then - echo $1 - exit 0 - else - echo $2 - exit 1 - fi -} - -validate_script() { - if [ ! -f "$1/$1.sh" ]; then - echo "'$1' cannot be globalized" - exit 1 - fi -} - -main() { - while getopts "?h?l?r?" opt; do - case $opt in - h) - echo "Usage: $0 [-l|-r]