Skip to content

Instantly share code, notes, and snippets.

@JorianWoltjer
Last active September 23, 2024 16:26
Show Gist options
  • Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
My dotfiles setup for WSL with way too many shortcuts
# Environment Variables
export SCREENDIR=$HOME/.screen
export DISCORD_WEBHOOK="https://discord.com/api/webhooks/[...]/[...]"
export DISCORD_TOKEN="[...]"
export MANPAGER="sh -c 'col -bx | bat -l man -p'" # highlight manpages with https://github.com/sharkdp/bat
export PROMPT_DIRTRIM=1 # truncate cwd in prompt to '...'
wt_duplicate() { # Keep working directory when splitting panes in Windows Terminal
echo -en '\e]9;9;"'
wslpath -w "$PWD" | tr -d '\n'
echo -en '"\x07'
}
export PROMPT_COMMAND="wt_duplicate 2>/dev/null"
# Tools
alias ysoserial="/tool/ysoserial-fix/ysoserial-fix.py" # https://gist.github.com/JorianWoltjer/5210e99c13189446ece5ffe3e9fe3d90
alias ysoserial.net="/tool/ysoserial.net/Release/ysoserial.exe"
alias d="/tool/self/drag.py" # https://gist.github.com/JorianWoltjer/cc4ed7415b665d35e2d010cd2c04c8a6
alias ips="powershell.exe Get-IP"
alias bashrc="nano +157 ~/.bashrc && source ~/.bashrc" # start further down in file and automatically apply after saving
alias update="sudo bash -c 'apt update && apt list --upgradable && apt upgrade -y'"
alias dc="docker compose"
alias docker-reload="docker-compose rm -f && docker-compose up --build"
alias b="cd .." # short for 'back'
alias c="code ."
alias objdump="objdump -M intel"
alias strace="strace -ff"
alias python=python3
alias py=python3
alias psh=powershell.exe
alias me="chown $USER:$(id -g)" # useful for `chown $(me) ...`
alias untar="tar -xvf"
alias gitc="git init && git add . && git commit -m 'Initial commit'"
alias sizes="du -had1 . | sort -h"
alias discord='sed '"'"'s/\x1b\[9\([0-7]\)m/\x1b\[3\1m/g'"'"' | sed '"'"'s/\x1b\[10\([0-7]\)m/\x1b\[4\1m/g'"'"' | (echo '"'"'```ansi'"'"'; cat; echo '"'"'```'"'"') | clip.exe'
# ^^ crazy bash syntax for copying command output (piped to it) as a discord ansi code block
alias dubuntu="docker run -it --rm -v $(pwd):/pwd -w /pwd ubuntu bash" # start sandbox docker container with current directory
alias tmp='cd $(mktemp -d)'
# Functions
waitport() { # Wait until one of many ports is open
echo -n "Waiting"
while true; do
for port in "${@:2}"; do
nc -w 1 -z $1 $port
if [[ $? == 0 ]]; then
echo -e "\a\nPort $2 is open on $1"
return
fi
done
sleep 1
echo -n "."
done
}
cdw() {
if [ $# -eq 0 ]; then # Go to windows home directory
cd /mnt/c/
path=$(cmd.exe /C "echo %USERPROFILE%" | tr -d '\r')
else # Go to specified directory
path="$1"
fi
path=$(wslpath "$path")
cd "$path"
}
ham() { # short for 'hamburger', shows top and bottom of file in hex
lines=${2:-10}
size=$(stat --format="%s" "$1")
if [ $size -lt $((32*$lines)) ]; then
hexyl "$1"
return
fi
offset=$((16 - $size % 16))
head "$1" -c $(($lines*16)) | hexyl | sed '$d'
echo "..."
tail "$1" -c $(($lines*16-$offset)) | hexyl -o $(($size+$offset - $lines*16)) | sed '1d'
}
vps_forward() { # Expose a local TCP port remotely on a VPS
echo "Forwarding TCP localhost:$1 to vps.jorianwoltjer.com:$1"
ssh -R 0.0.0.0:$1:localhost:$1 ubuntu@vps.jorianwoltjer.com "read"
}
vps_forward_udp() { # Expose a local UDP port remotely on a VPS (over TCP)
echo "Forwarding UDP localhost:$1 to vps.jorianwoltjer.com:$1"
echo "Make sure VPS firewall also allows $1"
remote_port=$(shuf -i 2000-65000 -n 1)
ssh -R $remote_port:localhost:$1 ubuntu@vps.jorianwoltjer.com "socat udp-listen:$1,reuseaddr,fork udp:localhost:$remote_port" && ssh ubuntu@vps.jorianwoltjer.com "pkill socat"
}
forward() { # Forward one TCP port to another
if [[ $# -ne 2 ]]; then
echo "Usage: forward <listen_port> <forward_port>"
return 1
fi
if (( $1 < 1024 )); then
sudo socat tcp-listen:$1,reuseaddr,fork tcp:0.0.0.0:$2
else
socat tcp-listen:$1,reuseaddr,fork tcp:0.0.0.0:$2
fi
}
xp() { # Open current or specific directory in Windows Explorer
p=${1:-"."}
explorer.exe $(wslpath -w "$p")
}
man() { # Show manpage or help menu if not found
/usr/bin/man $@ || $1 -h
}
len() { # Get length of a string in the first argument or STDIN
if [ $# -gt 1 ]; then
echo "Error: Only one argument is allowed (len 'something with spaces')"
return 1
elif [ -z "$1" ]; then
read -r input
echo ${#input}
else
echo ${#1}
fi
}
cmd() {
cmd.exe /c "$*"
}
clone() {
git clone "$@" && cd "$(basename "$1" .git)"
}
pyrust() { # Setup Rust library for use in Python
python -m venv .env
source .env/bin/activate
pip install maturin
maturin init --bindings pyo3
maturin develop
}
csv() { # Read CSV for piping
column -t -s, -n "$@" | less -F -S -X -K
}
urldecode() { # URL decode STDIN
while read; do echo -e ${REPLY//%/\\x}; done
}
mkdirs() { # Make directory and cd into it
mkdir -p "$1" && cd "$1"
}
# Place this file in %USERPROFILE%\Documents\WindowsPowerShell
Function Get-IP {
[CmdletBinding()]
param (
)
$Table = @()
# Private
$Table += Get-NetIPAddress | Where-Object InterfaceAlias -eq "Ethernet" | Select @{ Name="Interface";Expression={"Ethernet"} },@{ Name="IP";Expression={$_.IPAddress} }
$Table += Get-NetIPAddress | Where-Object InterfaceAlias -eq "Wi-Fi" | Select @{ Name="Interface";Expression={"Wi-Fi"} },@{ Name="IP";Expression={$_.IPAddress} }
$Table += Get-NetIPAddress | Where-Object InterfaceAlias -match "WSL" | Select @{ Name="Interface";Expression={"WSL (Windows)"} },@{ Name="IP";Expression={$_.IPAddress} }
$Table += "" | Select @{ Name="Interface";Expression={"WSL (Linux)"} },@{ Name="IP";Expression={wsl bash -c "ip addr show eth0 | grep 'inet ' | awk '{print \`$2}' | cut -d / -f1"} }
$OpenVPN = Get-NetIPAddress | Where-Object InterfaceAlias -eq "OpenVPN TAP-Windows6"
$Table += $OpenVPN | Select @{ Name="Interface";Expression={"OpenVPN ("+[char]::ConvertFromUtf32(0x1f4cb)+")"} },@{ Name="IP";Expression={$_.IPAddress} }
# Public
# $Table += "" | Select Interface,IP # Empty row
# $Table += "" | Select @{ Name="Interface";Expression={"Public"} },@{ Name="IP";Expression={Invoke-RestMethod api.ipify.org} }
Write-Output $Table
# Copy OpenVPN IP to clipboard
$OpenVPN | foreach { $_.IPAddress } | Set-Clipboard
}
function Get-Port {
param (
$Port
)
Get-Process -Id (Get-NetTCPConnection -LocalPort $Port).OwningProcess
Get-Process -Id (Get-NetUDPEndpoint -LocalPort $Port).OwningProcess
echo "Tip: If nothing is running and the port is still blocked, try:"
echo "cmd net stop hns && net start hns"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment