brt

9.5 KB · 204 lines · bash

1#!/usr/bin/env bash
2# brt — Brew Remote Tools: a Swiss-army CLI for networking, editing, and remote access.
3set -euo pipefail
4
5_brt_resolve_install_dir() {
6 local script="$1" dir target
7 dir=$(cd "$(dirname "$script")" && pwd)
8 if [[ -L "$script" ]]; then
9 target=$(readlink "$script")
10 [[ "$target" != /* ]] && target="${dir}/${target}"
11 dir=$(cd "$(dirname "$target")" && pwd)
12 fi
13 echo "$dir"
14}
15
16BRT_DIR="${BRT_DIR:-$(_brt_resolve_install_dir "${BASH_SOURCE[0]}")}"
17BRT_LIB="${BRT_DIR}/lib"
18
19source "${BRT_LIB}/common.sh"
20brt_load_config
21
22# Version file is the source of truth (updated by install/update)
23_BRT_EMBEDDED_VERSION="1.5.39"
24BRT_VERSION="$(brt_read_version "$BRT_DIR")"
25BRT_VERSION="${BRT_VERSION:-${_BRT_EMBEDDED_VERSION}}"
26
27usage() {
28 cat <<EOF
29brt — Brew Remote Tools v${BRT_VERSION}
30
31Usage: brt -<command> [args...]
32
33Networking
34 -ip [check|local|public|interfaces] Show IP information
35 -dns <host> [type] DNS lookup (A, AAAA, MX, TXT, NS, CNAME)
36 -reverse <ip> Reverse DNS lookup
37 -portscan <host> [ports|start-end] Scan ports (default: common ports)
38 -ping <host> [-c count] Ping host
39 -whois <domain|ip> WHOIS lookup
40 -traceroute <host> Trace route to host
41 -headers <url> Fetch HTTP response headers
42 -ssl <host> [port] Check SSL certificate and expiry
43 -curl <url> [curl flags...] Enhanced curl with timing info
44 -post <url> [curl flags...] Send HTTP POST request
45
46Text & Data
47 -edit <file> Nano-style terminal text editor
48 -json [file|-] Pretty-print JSON
49 -encode <string|file> Base64 encode
50 -decode <string|file> Base64 decode
51 -hash <algo> <input> Hash (md5, sha1, sha256, sha512)
52 -urlencode <string> URL-encode a string
53 -urldecode <string> URL-decode a string
54 -uuid Generate a random UUID
55 -pass [length] Generate a secure password
56 -grep <pattern> [path...] Search files with line numbers
57
58 Media
59 -image <file> [-x cols] [-y rows] Render an image in the terminal
60 -video <file|url> [--no-audio] Play video in terminal via mpv (file, URL, YouTube)
61 -doom [--wad file] [--scaling N] Play Doom in the terminal (WASD + arrows)
62 -browse "query" Google search in the terminal (top 25 results)
63
64 System
65 -run <cmd;wait N;cmd...> Run chained commands (wait in seconds)
66 brt -run 'echo hi;wait 2;echo bye'
67 brt -run echo hi wait 2 echo bye
68 -serve <port> [directory] Start a static file server
69 -watch <cmd> Re-run command every 2 seconds
70 -listen [port] Show listening ports or check one port
71 -killport <port> Kill process using a port
72 -env [pattern] Show environment variables
73 -which-all <cmd> Find all instances of a command
74 -du [path] [depth] Disk usage summary
75 -extract <archive> [dest] Extract zip/tar/tar.gz archives
76 -sysinfo Show system information
77 -publickey [key.pub] Show (and copy) SSH public key
78 -timestamp [now|unix|date] Convert or show timestamps
79 -timer <seconds> [message] Countdown timer with alert
80 -calc <expression> Calculator (uses bc)
81
82Remote Access
83 -access serve [-p port] Start relay + live console (stop/disconnect at prompt)
84 -access connect <host:port> <token> Connect to a remote brt terminal relay
85 -access leave Disconnect your own client session
86 -access clients List connected sessions (6-char ids)
87 -access disconnect [<id>] Disconnect yourself, or a session by id
88 -access logs Live audit console (stop · disconnect · clear · quit)
89 -access clear-logs Clear serve audit and process logs
90 -access status Show running access server info
91 -access stop [-f] Stop the access server
92 -access force-stop [-p port] Kill all access servers + free the port
93
94Other
95 -config [edit|show|path] Edit ~/.brt/.env (API keys, preferences)
96 -speed Quick download speed test
97 -weather [location] Current weather (via wttr.in)
98 -qr <text> Display QR code in terminal (requires qrencode)
99 -version Show version
100 -update Check for updates and install from site
101 -update-check Check for updates (runs automatically on terminal startup)
102 -uninstall Remove brt from this system
103 -help Show this help
104
105Examples:
106 brt -ip public
107 brt -ssl github.com
108 brt -headers https://example.com
109 brt -post https://httpbin.org/post -d 'hello=world'
110 brt -killport 8080
111 brt -run 'echo "hello";wait 2;echo "bye"'
112 brt -weather "San Francisco"
113 brt -image photo.jpg -x 120 -y 30
114 brt -video clip.mp4
115 brt -video https://example.com/video.mp4
116 brt -video 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
117 brt -browse "python terminal hyperlinks"
118 brt -doom
119 brt -config
120 brt -access serve
121
122$(brt_print_install_hints)
123EOF
124}
125
126main() {
127 if [[ $# -gt 0 && "$1" != "-update-check" ]]; then
128 brt_maybe_check_updates_async
129 fi
130
131 if [[ $# -eq 0 ]]; then
132 brt_show_pending_update_notice
133 usage
134 exit 0
135 fi
136
137 case "$1" in
138 -update-check|-update|-version|-v|-help|-h) ;;
139 *) brt_show_pending_update_notice ;;
140 esac
141
142 local flag="$1"
143 shift
144
145 case "$flag" in
146 -ip) source "${BRT_LIB}/ip.sh"; brt_ip "$@" ;;
147 -dns|-nslookup) source "${BRT_LIB}/dns.sh"; brt_dns "$@" ;;
148 -portscan|-scan) source "${BRT_LIB}/portscan.sh"; brt_portscan "$@" ;;
149 -ping) source "${BRT_LIB}/ping.sh"; brt_ping "$@" ;;
150 -whois) source "${BRT_LIB}/whois.sh"; brt_whois "$@" ;;
151 -traceroute|-trace) source "${BRT_LIB}/trace.sh"; brt_trace "$@" ;;
152 -headers) source "${BRT_LIB}/headers.sh"; brt_headers "$@" ;;
153 -ssl) source "${BRT_LIB}/ssl.sh"; brt_ssl "$@" ;;
154 -reverse) source "${BRT_LIB}/reverse.sh"; brt_reverse "$@" ;;
155 -curl) source "${BRT_LIB}/curl.sh"; brt_curl "$@" ;;
156 -post) source "${BRT_LIB}/post.sh"; brt_post "$@" ;;
157 -edit) source "${BRT_LIB}/edit.sh"; brt_edit "$@" ;;
158 -json) source "${BRT_LIB}/json.sh"; brt_json "$@" ;;
159 -encode) source "${BRT_LIB}/encode.sh"; brt_encode "$@" ;;
160 -decode) source "${BRT_LIB}/encode.sh"; brt_decode "$@" ;;
161 -hash) source "${BRT_LIB}/hash.sh"; brt_hash "$@" ;;
162 -urlencode) source "${BRT_LIB}/url.sh"; brt_urlencode "$@" ;;
163 -urldecode) source "${BRT_LIB}/url.sh"; brt_urldecode "$@" ;;
164 -grep) source "${BRT_LIB}/grep.sh"; brt_grep "$@" ;;
165 -image|-img) source "${BRT_LIB}/image.sh"; brt_image "$@" ;;
166 -video|-play) source "${BRT_LIB}/video.sh"; brt_video "$@" ;;
167 -doom) source "${BRT_LIB}/doom.sh"; brt_doom "$@" ;;
168 -browse|-search) source "${BRT_LIB}/browse.sh"; brt_browse "$@" ;;
169 -uuid) source "${BRT_LIB}/uuid.sh"; brt_uuid "$@" ;;
170 -pass|-password) source "${BRT_LIB}/pass.sh"; brt_pass "$@" ;;
171 -run) source "${BRT_LIB}/run.sh"; brt_run "$@" ;;
172 -serve) source "${BRT_LIB}/serve.sh"; brt_serve "$@" ;;
173 -watch) source "${BRT_LIB}/watch.sh"; brt_watch "$@" ;;
174 -listen) source "${BRT_LIB}/listen.sh"; brt_listen "$@" ;;
175 -killport) source "${BRT_LIB}/killport.sh"; brt_killport "$@" ;;
176 -du) source "${BRT_LIB}/du.sh"; brt_du "$@" ;;
177 -extract) source "${BRT_LIB}/extract.sh"; brt_extract "$@" ;;
178 -sysinfo) source "${BRT_LIB}/sysinfo.sh"; brt_sysinfo "$@" ;;
179 -publickey) source "${BRT_LIB}/publickey.sh"; brt_publickey "$@" ;;
180 -timestamp) source "${BRT_LIB}/timestamp.sh"; brt_timestamp "$@" ;;
181 -timer) source "${BRT_LIB}/timer.sh"; brt_timer "$@" ;;
182 -calc) source "${BRT_LIB}/calc.sh"; brt_calc "$@" ;;
183 -env) source "${BRT_LIB}/env.sh"; brt_env "$@" ;;
184 -which-all) source "${BRT_LIB}/which.sh"; brt_which_all "$@" ;;
185 -access) source "${BRT_LIB}/access.sh"; brt_access "$@" ;;
186 -config) brt_config "$@" ;;
187 -speed) source "${BRT_LIB}/speed.sh"; brt_speed "$@" ;;
188 -weather) source "${BRT_LIB}/weather.sh"; brt_weather "$@" ;;
189 -qr) source "${BRT_LIB}/qr.sh"; brt_qr "$@" ;;
190 -update) source "${BRT_LIB}/update.sh"; brt_update "$@" ;;
191 -update-check) source "${BRT_LIB}/update.sh"; brt_update_check "$@" ;;
192 -uninstall) source "${BRT_LIB}/uninstall.sh"; brt_uninstall "$@" ;;
193 -version|-v) brt_version_cmd "$@" ;;
194 -help|-h) usage ;;
195 *)
196 brt_error "Unknown command: ${flag}"
197 echo "Run 'brt -help' for usage."
198 exit 1
199 ;;
200 esac
201}
202
203main "$@"
204