-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
229 lines (195 loc) · 5.59 KB
/
.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
# Output
e_ok() { printf " ✔ %s\\n" "$@" ;}
e_info() { printf " ➜ %s\\n" "$@" ;}
e_error() { printf " ✖ %s\\n" "$@" ;}
e_warn() { printf " %s\\n" "$@" ;}
e_abort() { e_error "$1"; return "${2:-1}" ;}
# Check if command exists
is_cmd() { command -v "$@" >/dev/null 2>&1 ;}
# Check if root
is_root() { [[ $EUID == 0 ]] ;}
# OS detection
is_linux() { [[ $(uname) == Linux ]] ;}
is_osx() { [[ $(uname) == Darwin ]] ;}
# Load functions
if [[ -d "${HOME}/.functions.d" ]]; then
# shellcheck disable=1090
for file in "${HOME}"/.functions.d/*.sh; do . "$file"; done
fi
# Create a directory and jump directly into it
mcd() { mkdir -p "$1" && cd "$1" || return ;}
# Inspect a certificate
cert_inspect(){
openssl x509 -in "$1" -text -noout
}
# Start an HTTP server from a directory, optionally specifying the port
server() {
local port="${1:-8000}";
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port";
}
# Extract most know archives with one command
extract () {
if [ -f "$1" ] ; then
case $1 in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar e "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# CHROME
# USAGE: open_chrome -s -r=oga
open_chrome(){
# vars
local var scratch remote tmpdir chrome port proxy
# defaults
scratch=false
tmpdir="$(mktemp -d -t chrome-unsafe_data_dir.XXXXXXXXXX)"
chrome='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
port='8523'
# argumets
for var in "$@"; do
if [[ "$var" == -s ]]; then
scratch=true
elif [[ "$var" =~ -r=.* ]]; then
remote=${var//-r=/}
fi
done
if [ -n "${remote}" ]; then
proxy="--proxy-server=socks5://localhost:${port}"
ssh="ssh -vXNCD $port $remote"
else
proxy='--proxy-server=direct://'
ssh=''
fi
if $scratch; then
chrome="${chrome} --disable-sync --disable-first-run-ui --no-default-browser-check --no-first-run --user-data-dir='${tmpdir}'"
fi
sh -c "$chrome $proxy https://www.whatismyip.com" >/dev/null 2>&1 &
sh -c "$ssh"
}
# SSH Tunnel
ssh_tunnel(){
# shellcheck disable=SC2029
ssh -vXNCD "$1" "$2"
}
# Timezones
now_in(){
for t in US/Central America/Phoenix Europe/Bucharest US/Eastern; do
zdump $t
done
}
# Watch directory for changes and execute command
watch_dir(){
local dir=$1
shift
local cmd=$*
fswatch -0 -o -r "$dir" | while read -r -d ""; do
$cmd
done
}
# Simple bash server
simple_bash_server(){
while true; do
HOST=$(hostname -s)
HTML="
<html>
<head>
<title>Simple BASH Server</title>
<style>body {margin-top: 40px; background-color: #333;}</style>
</head>
<body>
<div style=color:white;text-align:center>
<h1>Running on ${HOST}</h1>
</div>
</body>
</html>
"
echo "$HTML" | nc -q 0 -l -p 80
done
}
# Encode special characters in string (for example in passwords)
# $ urlencode 'test&test^' outputs test%26test%5E
urlencode() {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
# Decode special characters in string
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# Kill all running containers.
docker_killall(){
for c in $(docker ps -q 2>/dev/null); do docker kill "$c"; done
}
# Delete all stopped containers, untagged images, volumes, networks, etc.
docker_clean(){
docker system prune --all --force
}
# Miscellaneous GPG Functions
# Load GPG Agent
gpg_agent_start(){
# Do not load if tty is not installed
if ! is_cmd tty; then return; fi
# Do not load if the agent is not installed
if ! is_cmd gpg-agent; then return; fi
# Do not load if gpg-connect-agent is not installed
if ! is_cmd gpg-connect-agent; then return; fi
# Do not load if gpg-connect-agent is not installed
if ! is_cmd gpgconf; then return; fi
# Use a TTY for GPG
GPG_TTY="$(tty)"; export GPG_TTY
# Start the gpg-agent if not already running
if ! pgrep -x -u "${USER}" gpg-agent >/dev/null 2>&1; then
gpg-connect-agent /bye >/dev/null 2>&1
gpg-connect-agent updatestartuptty /bye >/dev/null
fi
}
# Reload GPG Agent
gpg_agent_reload(){
echo 'Reloading GPG Agent...'
gpgconf --reload gpg-agent
}
# Unload GPG Agent
gpg_agent_kill(){
echo 'Killing GPG Agent...'
gpgconf --kill gpg-agent
}
# Restart GPG Agent
gpg_agent_restart(){
echo 'Stopping GPG Agent...'
gpg_agent_kill
echo 'Starting GPG Agent...'
gpg_agent_load
}
# Test encryption and decryption of phrase
gpg_agent_test(){
echo 'OK' | gpg --encrypt --recipient "$(whoami)" | gpg --decrypt
}