This commit is contained in:
bytedream 2024-02-08 15:12:58 +01:00
parent 6dd7e0084b
commit aecd070642
17 changed files with 194 additions and 490 deletions

View File

@ -1,14 +1,8 @@
# scripts # 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 - DNS
- [clear-cache.sh](dns/clear-cache.sh) - Clear the systemd DNS cache
- [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)

156
android/mitmproxy.sh Executable file
View File

@ -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 $@

33
dns/clear-cache.sh Executable file
View File

@ -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 $@

View File

@ -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
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 747 KiB

View File

@ -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

View File

@ -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] <script name>"
echo " 'script name' must be the name of a sub-directory in this directory"
exit 0
;;
l)
link=true
;;
r)
remove=true
;;
*)
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ $# -ge 1 ]; then
if [ -d $1 ]; then
executablePath="/usr/bin/$1"
copyDir="/usr/share/$1"
if ([ -f $executablePath ] || [ -d $copyDir ]) || [ ! -z $remove ] ; then
if [ ! -z $remove ]; then
if [ -d $copyDir ]; then
rm -r $copyDir
fi
rm $executablePath
check_and_exit "Unglobalized '$1'" "Failed to unglobalize '$1'"
else
echo "The script is already globalized"
fi
elif [ ! -z $link ]; then
validate_script $1
ln -s $1.* $executablePath
chmod +x $executablePath
check_and_exit "Globalized '$1'" "Failed to globalize '$1'"
else
validate_script $1
cp -r $1 /usr/share
ln -s /usr/share/$1/$1.* $executablePath
chmod +x $executablePath
check_and_exit "Globalized '$1'" "Failed to globalize '$1'"
fi
else
echo "This script '$1' does not exist"
fi
else
echo "No script to globalize were given"
fi
}
main $@

View File

@ -1,19 +0,0 @@
## Hashify Filename
This little script renames given files to its [MD5](https://en.wikipedia.org/wiki/MD5) hashsum. File extension won't be changed.
This allows duplicate files in the same directory to be easily detected and merged / overwritten.
This script is just a fork of [randomize-filename](../randomize-filename).
#### Arguments
The can take one extra argument
`-r` - recursive: If any directory where given while calling this command, all files in it will be renamed recursively.
#### Usage
```
$ hashify-filename.sh -r .
```

View File

@ -1,63 +0,0 @@
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
recursive=false
while getopts ":r?" opt; do
case $opt in
r)
recursive=true
;;
*)
echo $usage
exit 1
esac
done
shift $((OPTIND-1))
function rename() {
local base="$1"
shift 1
for file in $*; do
file=${file//[\\]/}
filepath="$base/$file"
ext="${file##*.}"
if [ "$ext" == "$file" ]; then
ext=""
else
ext=".$ext"
fi
if [ -d $filepath ]; then
if $recursive && [ ! -L $filepath ]; then
rename "$filepath" $(ls -b $filepath)
fi
continue
fi
filename="$(md5sum $filepath | awk '{print $1}')$ext"
if [ $file != $filename ]; then
mv -f "$filepath" "$base/$filename"
fi
done
}
if [ "$#" -eq 0 ]; then
echo "At least one file must be given"
exit 1
fi
declare -a files=()
for file in $@; do
if [[ $file != /* ]]; then
file="$(pwd)/$file"
fi
files+=("$file")
done
rename "/" "${files[@]}"
IFS=$SAVEIFS

View File

@ -1,40 +0,0 @@
## Java beautifier
Makes java code look like it should.
#### Usage
```
./java-beautifier.py <a java file>
```
#### Example
1. Create a `Main.java` with the following content:
```java
public class Main {
public static void main(String[] args) {
System.out.println("I'm so beautiful");
}
public static class UnnecessarySubClass {
public static void unnecessaryVoid() {
System.out.println("Hello, i'm unnecessary");
}
}
}
```
2. Beautify the file: `./java-beautifier.py Main.java`
3. Look at the beautiful output:
```java
public class Main {
public static void main(String[] args) {
System.out.println("I'm so beautiful"); }
public static class UnnecessarySubClass {
public static void unnecessaryVoid() {
System.out.println("Hello, i'm unnecessary"); }}}
```

View File

@ -1,30 +0,0 @@
#!/usr/bin/python3
import sys
if __name__ == '__main__':
file = sys.argv[-1]
lenght = 0
content = []
new_content = []
for line in open(file, 'r'):
line = line.strip()
if line:
if len(line) > lenght:
lenght = len(line)
content.append(line)
lenght = lenght + 2
for i, line in enumerate(content):
if line and line[-1] in ('{', '}'):
if len(line.strip()) == 1 and i != 0:
new_content[-1] += ' ' * (lenght - len(new_content[-1]) - 1) + line[-1]
else:
new_content.append(line[:-1] + (' ' * (lenght - len(line))) + line[-1])
else:
new_content.append(line)
sys.stdout.write('\n'.join(new_content))

View File

@ -1,11 +0,0 @@
## Merge readme
Just a simple command line tool to merge multiple pdf files
#### Requirements
- `ghostscript`
#### Usage
`./merge-pdf.sh -o <output file> [files to merge]`

View File

@ -1,22 +0,0 @@
#!/bin/bash
while getopts ":o:" opt; do
case $opt in
o)
output=$OPTARG
;;
*)
echo "Unsupported argument was given (only '-o' is allowed)"
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z $output ]; then
echo "An output must be provided ('-o' flag)"
exit 1
fi
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$output "${@}"

View File

@ -1,12 +0,0 @@
## Nama availability check
Checks if a name has free domains and a free github name
#### Usage
```shell
$ ./nameavability.sh <name1> <name2> ...
```
#### Example
```shell
$ ./nameavability.sh bytedream
```

View File

@ -1,39 +0,0 @@
#!/bin/bash
check_commands() {
failure=false
for cmd in "dig" "curl"; do
if ! command -v $cmd &> /dev/null; then
echo "'$cmd' must be installed"
failure=true
fi
done
if $failure; then
exit 1
fi
}
main() {
for name in $@; do
echo "---> $name"
for tld in "com" "org" "net" "io"; do
dst_ip=`dig 1.1.1.1 +short $name.$tld | tail -n 1`
if [ ! $dst_ip ]; then
echo -e "\t$name.$tld: AVAILABLE"
else
echo -e "\t$name.$tld: NOT available"
fi
done
github_availability=`curl --connect-timeout 3 --retry 3 -o /dev/null -s -w "%{http_code}" https://github.com/$name`
if [ "$github_availability" == "200" ]; then
echo -e "\tGitHub: NOT available"
else
echo -e "\tGitHub: AVAILABLE"
fi
done
}
check_commands
main $@

View File

@ -1,20 +0,0 @@
## Randomize Filename
With this little script you can randomize the names of given files. File extension won't be changed.
#### Arguments
The file takes several arguments
`-r` - recursive: If any directory where given while calling this command, all files in it will be renamed recursively.
`-a` - all (the name is a little misleading): Normally directories are not renamed, but with this argument they will.
`-l LENGHT` - lenght: The lenght of the randomized filenames (default is 16). Replace `LENGHT` with the lenght the randomized names should have.
#### Usage
```
$ randomize-filename.sh -r .
```

View File

@ -1,78 +0,0 @@
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
usage="$0 [-r] [-a] [-l LENGHT] files..."
recursive=false
all=false
lenght=16
while getopts ":r?a?l:" opt; do
case $opt in
r)
recursive=true
;;
a)
all=true
;;
l)
lenght=$OPTARG
echo $lenght
if [[ ! $lenght =~ ^[0-9]+$ ]]; then
echo $usage
exit 1
fi
;;
*)
echo $usage
exit 1
esac
done
shift $((OPTIND-1))
function rename() {
local base="$1"
shift 1
for file in $*; do
file=${file//[\\]/}
filepath="$base/$file"
ext="${file##*.}"
if [ "$ext" == "$file" ]; then
ext=""
else
ext=".$ext"
fi
filename="$(tr -dc A-Za-z0-9 </dev/urandom | head -c 16)$ext"
if [ -d $filepath ]; then
if $recursive && [ ! -L $filepath ]; then
rename "$filepath" $(ls -b $filepath)
fi
if ! $all; then
continue
fi
fi
mv "$filepath" "$base/$filename"
done
}
if [ "$#" -eq 0 ]; then
echo "At least one file must be given"
exit 1
fi
declare -a files=()
for file in $@; do
if [[ $file != /* ]]; then
file="$(pwd)/$file"
fi
files+=("$file")
done
rename "/" "${files[@]}"
IFS=$SAVEIFS